Ë
    þÍ:j  ã                   óX   — d dl mZ d dlZd dlmZmZ d dlmZ ddededee   ded	ef
d
„Z	y)é    )ÚOptionalN)ÚTensorÚtensor)Ú"_check_retrieval_functional_inputsÚpredsÚtargetÚtop_kÚ
adaptive_kÚreturnc                 ó*  — t        | |«      \  } }t        |t        «      st        d«      ‚|�|r!|| j                  d   kD  r| j                  d   }t        |t
        «      r|dkD  st        d«      ‚|j                  «       st        d| j                  ¬«      S t        j                  | dkD  |t        j                  |«      «      }|| j                  t        || j                  d   «      d¬«      d      j                  «       j                  «       }||z  S )	a  Compute the precision metric for information retrieval.

    Precision is the fraction of relevant documents among all the retrieved documents.

    ``preds`` and ``target`` should be of the same shape and live on the same device. If no ``target`` is ``True``,
    ``0`` is returned. ``target`` must be either `bool` or `integers` and ``preds`` must be ``float``,
    otherwise an error is raised. If you want to measure Precision@K, ``top_k`` must be a positive integer.

    Args:
        preds: estimated probabilities of each document to be relevant.
        target: ground truth about each document being relevant or not.
        top_k: consider only the top k elements (default: ``None``, which considers them all)
        adaptive_k: adjust `k` to `min(k, number of documents)` for each query

    Returns:
        A single-value tensor with the precision (at ``top_k``) of the predictions ``preds`` w.r.t. the labels
          ``target``.

    Raises:
        ValueError:
            If ``top_k`` is not `None` or an integer larger than 0.
        ValueError:
            If ``adaptive_k`` is not boolean.

    Example:
        >>> preds = tensor([0.2, 0.3, 0.5])
        >>> target = tensor([True, False, True])
        >>> retrieval_precision(preds, target, top_k=2)
        tensor(0.5000)

    z `adaptive_k` has to be a booleanéÿÿÿÿr   z,`top_k` has to be a positive integer or Noneg        )Údevice)Údimé   )r   Ú
isinstanceÚboolÚ
ValueErrorÚshapeÚintÚsumr   r   ÚtorchÚwhereÚ
zeros_likeÚtopkÚminÚfloat)r   r   r	   r
   Útarget_filteredÚrelevants         ú€/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchmetrics/functional/retrieval/precision.pyÚretrieval_precisionr       sí   € ô@ 7°u¸fÓE�M€Eˆ6ä�j¤$Ô'ÜÐ;Ó<Ð<à€}™¨°·±¸B±Ò(?Ø—‘˜B‘ˆä�uœcÔ" u¨q¢yÜÐGÓHÐHà�:‰:Œ<Ü�c %§,¡,Ô/Ð/ä—k‘k %¨!¡)¨V´U×5EÑ5EÀfÓ5MÓN€OØ˜uŸz™z¬#¨e°U·[±[À±_Ó*EÈ2˜zÓNÈqÑQÑR×VÑVÓX×^Ñ^Ó`€Hà�eÑÐó    )NF)
Útypingr   r   r   r   Útorchmetrics.utilities.checksr   r   r   r    © r!   r   ú<module>r%      sA   ðõ ã ß  å Lñ1˜vð 1¨vð 1¸hÀs¹mð 1Ð`dð 1Ðqwô 1r!   