Ë
    ýÿæik  ã            	       óÈ   — d dl mZmZ d dlmZmZmZmZmZ d dl	Z	d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ d dlmZmZ 	 	 dd	e
d
eed   ef   dee   de
fd„Z G d„ dee«      Zy)é    )ÚABCÚabstractmethod)ÚAnyÚCallableÚListÚOptionalÚUnionN)ÚTensorÚtensor)ÚLiteral)ÚMetric)Ú_check_retrieval_inputs)Ú_flexible_bincountÚdim_zero_catÚvaluesÚaggregation©ÚmeanÚmedianÚminÚmaxÚdimÚreturnc                 óš  — |dk(  r$|€| j                  «       S | j                  |¬«      S |dk(  r.|€| j                  «       S | j                  |¬«      j                  S |dk(  r.|€| j                  «       S | j                  |¬«      j                  S |dk(  r.|€| j	                  «       S | j	                  |¬«      j                  S  || |¬«      S )z9Aggregate the final retrieval values into a single value.r   ©r   r   r   r   )r   r   r   r   r   )r   r   r   s      úp/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/retrieval/base.pyÚ_retrieval_aggregater      sÃ   € ð �fÒØ # ˆv�{‰{‹}ÐE°·±À°Ó1EÐEØ�hÒØ"% +ˆv�}‰}‹ÐP°6·=±=ÀS°=Ó3I×3PÑ3PÐPØ�eÒØ"˜{ˆv�z‰z‹|ÐJ°·
±
¸s°
Ó0C×0JÑ0JÐJØ�eÒØ"˜{ˆv�z‰z‹|ÐJ°·
±
¸s°
Ó0C×0JÑ0JÐJÙ�v 3Ô'Ð'ó    c                   óô   ‡ — e Zd ZU dZdZeed<   dZeed<   dZeed<   e	e
   ed<   e	e
   ed<   e	e
   ed	<   	 	 	 ddedee   deed   ef   dedd
f
ˆ fd„Zde
d	e
de
dd
fd„Zde
fd„Zede
d	e
de
fd„«       Zˆ xZS )ÚRetrievalMetricaQ	  Works with binary target data. Accepts float predictions from a model output.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~torch.Tensor`): A float tensor of shape ``(N, ...)``
    - ``target`` (:class:`~torch.Tensor`): A long or bool tensor of shape ``(N, ...)``
    - ``indexes`` (:class:`~torch.Tensor`): A long tensor of shape ``(N, ...)`` which indicate to which query a
      prediction belongs

    .. hint::
        The ``indexes``, ``preds`` and ``target`` must have the same dimension and will be flattened
        to single dimension once provided.

    .. attention::
        Predictions will be first grouped by ``indexes`` and then the real metric, defined by overriding
        the `_metric` method, will be computed as the mean of the scores over each query.

    As output to ``forward`` and ``compute`` the metric returns the following output:

    - ``metric`` (:class:`~torch.Tensor`): A tensor as computed by ``_metric`` if the number of positive targets is
      at least 1, otherwise behave as specified by ``self.empty_target_action``.

    Args:
        empty_target_action:
            Specify what to do with queries that do not have at least a positive
            or negative (depend on metric) target. Choose from:

            - ``'neg'``: those queries count as ``0.0`` (default)
            - ``'pos'``: those queries count as ``1.0``
            - ``'skip'``: skip those queries; if all queries are skipped, ``0.0`` is returned
            - ``'error'``: raise a ``ValueError``

        ignore_index:
            Ignore predictions where the target is equal to this number.
        aggregation:
            Specify how to aggregate over indexes. Can either a custom callable function that takes in a single tensor
            and returns a scalar value or one of the following strings:

            - ``'mean'``: average value is returned
            - ``'median'``: median value is returned
            - ``'max'``: max value is returned
            - ``'min'``: min value is returned

        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        ValueError:
            If ``empty_target_action`` is not one of ``error``, ``skip``, ``neg`` or ``pos``.
        ValueError:
            If ``ignore_index`` is not `None` or an integer.

    FÚis_differentiableTÚhigher_is_betterÚfull_state_updateÚindexesÚpredsÚtargetNÚempty_target_actionÚignore_indexr   r   Úkwargsr   c                 ót  •— t        ‰| �  di |¤Ž d| _        d}||vrt        d|› d�«      ‚|| _        |�t        |t        «      st        d«      ‚|| _        |dv st        |«      st        d|› d�«      ‚|| _	        | j                  d	g d ¬
«       | j                  dg d ¬
«       | j                  dg d ¬
«       y )NF)ÚerrorÚskipÚnegÚposz7Argument `empty_target_action` received a wrong value `z`.z3Argument `ignore_index` must be an integer or None.r   zˆArgument `aggregation` must be one of `mean`, `median`, `min`, `max` or a custom callable functionwhich takes tensor of values, but got Ú.r$   )ÚdefaultÚdist_reduce_fxr%   r&   © )ÚsuperÚ__init__Úallow_non_binary_targetÚ
ValueErrorr'   Ú
isinstanceÚintr(   Úcallabler   Ú	add_state)Úselfr'   r(   r   r)   Úempty_target_action_optionsÚ	__class__s         €r   r4   zRetrievalMetric.__init__i   sÞ   ø€ ô 	‰ÑÑ"˜6Ò"Ø',ˆÔ$à&EÐ#ØÐ&AÑAÜÐVÐWjÐVkÐkmÐnÓoÐoØ#6ˆÔ àÐ#¬J°|ÄSÔ,IÜÐRÓSÐSØ(ˆÔàÐ?Ñ?Ä8ÈKÔCXÜð9Ø9D¸ÀQðHóð ð 'ˆÔà�‰�y¨"¸TˆÔBØ�‰�w¨¸4ˆÔ@Ø�‰�x¨¸DˆÕAr   c                 ó  — |€t        d«      ‚t        |||| j                  | j                  ¬«      \  }}}| j                  j                  |«       | j                  j                  |«       | j                  j                  |«       y)zGCheck shape, check and convert dtypes, flatten and add to accumulators.Nz!Argument `indexes` cannot be None)r5   r(   )r6   r   r5   r(   r$   Úappendr%   r&   )r;   r%   r&   r$   s       r   ÚupdatezRetrievalMetric.update‡   st   € àˆ?ÜÐ@ÓAÐAä!8Ø�U˜F¸D×<XÑ<XÐgk×gxÑgxô"
Ñˆ�˜ð 	�‰×Ñ˜GÔ$Ø�
‰
×Ñ˜%Ô Ø�‰×Ñ˜6Õ"r   c           
      óÀ  — t        | j                  «      }t        | j                  «      }t        | j                  «      }t	        j
                  |«      \  }}||   }||   }t        |«      j                  «       j                  «       j                  «       }g }t        t	        j                  ||d¬«      t	        j                  ||d¬«      «      D ]¥  \  }}|j                  «       so| j                  dk(  rt        d«      ‚| j                  dk(  r|j                  t!        d«      «       ŒZ| j                  dk(  sŒj|j                  t!        d«      «       Œ…|j                  | j#                  ||«      «       Œ§ |rFt%        t	        j&                  |D �	cg c]  }	|	j)                  |«      ‘Œ c}	«      | j*                  «      S t!        d«      j)                  |«      S c c}	w )	a„  First concat state ``indexes``, ``preds`` and ``target`` since they were stored as lists.

        After that, compute list of groups that will help in keeping together predictions about the same query. Finally,
        for each group compute the ``_metric`` if the number of positive targets is at least 1, otherwise behave as
        specified by ``self.empty_target_action``.

        r   r   r+   zC`compute` method was provided with a query with no positive target.r.   g      ð?r-   g        )r   r$   r%   r&   ÚtorchÚsortr   ÚdetachÚcpuÚtolistÚzipÚsplitÚsumr'   r6   r?   r   Ú_metricr   ÚstackÚtor   )
r;   r$   r%   r&   ÚindicesÚsplit_sizesÚresÚ
mini_predsÚmini_targetÚxs
             r   ÚcomputezRetrievalMetric.compute”   s~  € ô ˜tŸ|™|Ó,ˆÜ˜TŸZ™ZÓ(ˆÜ˜dŸk™kÓ*ˆä Ÿ:™: gÓ.Ñˆ�Ø�g‘ˆØ˜‘ˆä(¨Ó1×8Ñ8Ó:×>Ñ>Ó@×GÑGÓIˆàˆÜ'*Ü�K‰K˜˜{°Ô2´E·K±KÀÈÐYZÔ4[ö(
Ñ#ˆJ˜ð —?‘?Ô$Ø×+Ñ+¨wÒ6Ü$Ð%jÓkÐkØ×+Ñ+¨uÒ4Ø—J‘Jœv c›{Õ+Ø×-Ñ-°Ó6Ø—J‘Jœv c›{Õ+ð —
‘
˜4Ÿ<™<¨
°KÓ@ÕAð(
ñ Ü'¬¯©É#Ó4NÉ#ÀQ°Q·T±T¸%µ[È#Ñ4NÓ(OÐQU×QaÑQaÓbÐbÜ�c‹{�~‰~˜eÓ$Ð$ùò 5Os   ÆGc                  ó   — y)z„Compute a metric over a predictions and target of a single group.

        This method should be overridden by subclasses.

        Nr2   )r;   r%   r&   s      r   rJ   zRetrievalMetric._metric¹   s   � r   )r-   Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   ÚboolÚ__annotations__r"   r#   r   r
   Ústrr   r8   r	   r   r   r   r4   r@   rS   r   rJ   Ú__classcell__)r=   s   @r   r    r    +   sû   ø… ñ3ðj $Ð�tÓ#Ø!Ð�dÓ!Ø#Ð�tÓ#à�&‰\ÓØ�‰<ÓØ�‰LÓð $)Ø&*ØPVñ	Bà ðBð ˜s‘mðBð ˜7Ð#AÑBÀHÐLÑMð	Bð
 ðBð 
õBð<#˜Fð #¨Fð #¸Vð #Èó #ð#%˜ó #%ðJ ð˜Vð ¨Vð ¸ò ó ôr   r    )r   N)Úabcr   r   Útypingr   r   r   r   r	   rB   r
   r   Útyping_extensionsr   Útorchmetricsr   Útorchmetrics.utilities.checksr   Útorchmetrics.utilities.datar   r   r8   r   r    r2   r   r   Ú<module>rc      sy   ð÷ $ß 7Õ 7ã ß  Ý %å Ý Aß Hð
 MSØñ(Øð(à�wÐ=Ñ>ÀÐHÑIð(ð 
�#‰ð(ð ó	(ô"T�f˜cõ Tr   