§
    ’©ºjk  ã            	       óâ   — 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dS )é    )ÚABCÚabstractmethod)ÚAnyÚCallableÚListÚOptionalÚUnionN)ÚTensorÚtensor)ÚLiteral)ÚMetric)Ú_check_retrieval_inputs)Ú_flexible_bincountÚdim_zero_catÚmeanÚvaluesÚaggregation©r   ÚmedianÚminÚmaxÚdimÚreturnc                 óÊ  — |dk    r,|€|                       ¦   «         n|                       |¬¦  «        S |dk    r1|€|                      ¦   «         n|                      |¬¦  «        j        S |dk    r1|€|                      ¦   «         n|                      |¬¦  «        j        S |dk    r1|€|                      ¦   «         n|                      |¬¦  «        j        S  || |¬¦  «        S )z9Aggregate the final retrieval values into a single value.r   N©r   r   r   r   )r   r   r   r   r   )r   r   r   s      úk/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/retrieval/base.pyÚ_retrieval_aggregater      sÞ   € ð �fÒÐØ # ˆv�{Š{‰}Œ}ˆ}°·²À°Ñ1EÔ1EÐEØ�hÒÐØ"% +ˆv�}Š}‰Œˆ°6·=²=ÀS°=Ñ3IÔ3IÔ3PÐPØ�eÒÐØ"˜{ˆv�zŠz‰|Œ|ˆ|°·
²
¸s°
Ñ0CÔ0CÔ0JÐJØ�eÒÐØ"˜{ˆv�zŠz‰|Œ|ˆ|°·
²
¸s°
Ñ0CÔ0CÔ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ÚtargetÚnegNr   Úempty_target_actionÚignore_indexr   r   Úkwargsr   c                 óÌ  •—  t          ¦   «         j        di |¤Ž d| _        d}||vrt          d|› d�¦  «        ‚|| _        |�$t          |t          ¦  «        st          d¦  «        ‚|| _        |dv s"t          |¦  «        st          d|› d�¦  «        ‚|| _	        |  
                    d	g d ¬
¦  «         |  
                    dg d ¬
¦  «         |  
                    dg d ¬
¦  «         d S )NF)ÚerrorÚskipr'   Ú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ÐAÝÐnÐWjÐnÐnÐnÑoÔoÐoØ#6ˆÔ àÐ#­J°|ÅSÑ,IÔ,IÐ#ÝÐRÑSÔSÐSØ(ˆÔàÐ?Ð?Ð?Å8ÈKÑCXÔCXÐ?ÝðHØ9DðHð Hð Hñô ð ð 'ˆÔà�Š�y¨"¸TˆÑBÔBÐBØ�Š�w¨¸4ˆÑ@Ô@Ð@Ø�Š�x¨¸DˆÑAÔAÐAÐAÐAr   c                 ó  — |€t          d¦  «        ‚t          |||| j        | j        ¬¦  «        \  }}}| j                             |¦  «         | j                             |¦  «         | j                             |¦  «         dS )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‡   s�   € àˆ?ÝÐ@ÑAÔAÐAå!8Ø�U˜F¸DÔ<XÐgkÔgxð"
ñ "
ô "
Ñˆ�˜ð 	Œ×Ò˜GÑ$Ô$Ð$ØŒ
×Ò˜%Ñ Ô Ð ØŒ×Ò˜6Ñ"Ô"Ð"Ð"Ð"r   c                 óì  ‡— t          | j        ¦  «        }t          | j        ¦  «        Št          | j        ¦  «        }t	          j        |¦  «        \  }}‰|         Š||         }t          |¦  «                             ¦   «                              ¦   «          	                    ¦   «         }g }t          t	          j        ‰|d¬¦  «        t	          j        ||d¬¦  «        ¦  «        D ]¸\  }}|                     ¦   «         sv| j        dk    rt          d¦  «        ‚| j        dk    r#|                     t!          d¦  «        ¦  «         Œa| j        dk    r"|                     t!          d¦  «        ¦  «         Œ�|                     |                      ||¦  «        ¦  «         Œ¹|r3t%          t	          j        ˆfd	„|D ¦   «         ¦  «        | j        ¦  «        S t!          d¦  «                             ‰¦  «        S )
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        c                 ó:   •— g | ]}|                      ‰¦  «        ‘ŒS r2   )Úto)Ú.0Úxr%   s     €r   ú
<listcomp>z+RetrievalMetric.compute.<locals>.<listcomp>¶   s#   ø€ Ð4NÐ4NÐ4NÀQ°Q·T²T¸%±[´[Ð4NÐ4NÐ4Nr   )r   r$   r%   r&   ÚtorchÚsortr   ÚdetachÚcpuÚtolistÚzipÚsplitÚsumr(   r6   r?   r   Ú_metricr   Ústackr   rC   )	r;   r$   r&   ÚindicesÚsplit_sizesÚresÚ
mini_predsÚmini_targetr%   s	           @r   ÚcomputezRetrievalMetric.compute”   sÑ  ø€ õ ˜tœ|Ñ,Ô,ˆÝ˜TœZÑ(Ô(ˆÝ˜dœkÑ*Ô*ˆå œ: gÑ.Ô.Ñˆ�Ø�g”ˆØ˜”ˆå(¨Ñ1Ô1×8Ò8Ñ:Ô:×>Ò>Ñ@Ô@×GÒGÑIÔIˆàˆÝ'*ÝŒK˜˜{°Ð2Ñ2Ô2µE´KÀÈÐYZÐ4[Ñ4[Ô4[ñ(
ô (
ð 	Bð 	BÑ#ˆJ˜ð —?’?Ñ$Ô$ð 	BØÔ+¨wÒ6Ð6Ý$Ð%jÑkÔkÐkØÔ+¨uÒ4Ð4Ø—J’J�v c™{œ{Ñ+Ô+Ð+Ð+ØÔ-°Ò6Ð6Ø—J’J�v c™{œ{Ñ+Ô+Ð+øð —
’
˜4Ÿ<š<¨
°KÑ@Ô@ÑAÔAÐAÐAàð 	cÝ'­¬Ð4NÐ4NÐ4NÐ4NÈ#Ð4NÑ4NÔ4NÑ(OÔ(OÐQUÔQaÑbÔbÐbÝ�c‰{Œ{�~Š~˜eÑ$Ô$Ð$r   c                 ó   — dS )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   rO   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@   rV   r   rO   Ú__classcell__)r=   s   @r   r    r    +   s�  ø€ € € € € € ð3ð 3ðj $Ð�tÐ#Ð#Ñ#Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#à�&Œ\ÐÐÑØ�Œ<ÐÐÑØ�ŒLÐÐÑð $)Ø&*ØPVð	Bð Bà ðBð ˜s”mðBð ˜7Ð#AÔBÀHÐLÔMð	Bð
 ðBð 
ðBð Bð Bð Bð Bð Bð<#˜Fð #¨Fð #¸Vð #Èð #ð #ð #ð #ð#%˜ð #%ð #%ð #%ð #%ðJ ð˜Vð ¨Vð ¸ð ð ð ñ „^ðð ð ð ð r   r    )r   N)Úabcr   r   Útypingr   r   r   r   r	   rG   r
   r   Útyping_extensionsr   Útorchmetricsr   Útorchmetrics.utilities.checksr   Útorchmetrics.utilities.datar   r   r8   r   r    r2   r   r   ú<module>rf      sS  ðð $Ð #Ð #Ð #Ð #Ð #Ð #Ð #Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  Ø %Ð %Ð %Ð %Ð %Ð %à Ð Ð Ð Ð Ð Ø AÐ AÐ AÐ AÐ AÐ AØ HÐ HÐ HÐ HÐ HÐ HÐ HÐ Hð
 MSØð(ð (Øð(à�wÐ=Ô>ÀÐHÔIð(ð 
�#Œð(ð ð	(ð (ð (ð (ð"Tð Tð Tð Tð T�f˜cñ Tô Tð Tð Tð Tr   