§
    ’©ºj¸  ã                   ór   — d dl mZmZmZ d dlZd dlmZ d dlmZmZ d dl	m
Z
 d dlmZ  G d„ de
¦  «        ZdS )	é    )ÚAnyÚListÚOptionalN)ÚTensor)Ú_critical_success_index_computeÚ_critical_success_index_update)ÚMetric)Údim_zero_catc            	       óà   ‡ — e Zd ZU dZdZeed<   dZeed<   eed<   eed<   eed<   e	e         ed	<   e	e         ed
<   e	e         ed<   dde
dee         deddfˆ fd„Zdededdfd„Zdefd„Zˆ xZS )ÚCriticalSuccessIndexa=  Calculate critical success index (CSI).

    Critical success index (also known as the threat score) is a statistic used weather forecasting that measures
    forecast performance over inputs binarized at a specified threshold. It is defined as:

    .. math:: \text{CSI} = \frac{\text{TP}}{\text{TP}+\text{FN}+\text{FP}}

    Where :math:`\text{TP}`, :math:`\text{FN}` and :math:`\text{FP}` represent the number of true positives, false
    negatives and false positives respectively after binarizing the input tensors.

    Args:
        threshold: Values above or equal to threshold are replaced with 1, below by 0
        keep_sequence_dim: Index of the sequence dimension if the inputs are sequences of images. If specified,
            the score will be calculated separately for each image in the sequence. If ``None``, the score will be
            calculated across all dimensions.

    Example:
        >>> import torch
        >>> from torchmetrics.regression import CriticalSuccessIndex
        >>> x = torch.Tensor([[0.2, 0.7], [0.9, 0.3]])
        >>> y = torch.Tensor([[0.4, 0.2], [0.8, 0.6]])
        >>> csi = CriticalSuccessIndex(0.5)
        >>> csi(x, y)
        tensor(0.3333)

    Example:
        >>> import torch
        >>> from torchmetrics.regression import CriticalSuccessIndex
        >>> x = torch.Tensor([[[0.2, 0.7], [0.9, 0.3]], [[0.2, 0.7], [0.9, 0.3]]])
        >>> y = torch.Tensor([[[0.4, 0.2], [0.8, 0.6]], [[0.4, 0.2], [0.8, 0.6]]])
        >>> csi = CriticalSuccessIndex(0.5, keep_sequence_dim=0)
        >>> csi(x, y)
        tensor([0.3333, 0.3333])

    FÚis_differentiableTÚhigher_is_betterÚhitsÚmissesÚfalse_alarmsÚ	hits_listÚmisses_listÚfalse_alarms_listNÚ	thresholdÚkeep_sequence_dimÚkwargsÚreturnc                 ób  •—  t          ¦   «         j        di |¤Ž t          |¦  «        | _        |r-t	          |t
          ¦  «        r|dk     rt          d|› �¦  «        ‚|| _        |€€|                      dt          j
        d¦  «        d¬¦  «         |                      dt          j
        d¦  «        d¬¦  «         |                      dt          j
        d¦  «        d¬¦  «         d S |                      dg d	¬¦  «         |                      d
g d	¬¦  «         |                      dg d	¬¦  «         d S )Nr   z@Expected keep_sequence_dim to be a non-negative integer but got r   Úsum)ÚdefaultÚdist_reduce_fxr   r   r   Úcatr   r   © )ÚsuperÚ__init__Úfloatr   Ú
isinstanceÚintÚ
ValueErrorr   Ú	add_stateÚtorchÚtensor)Úselfr   r   r   Ú	__class__s       €úk/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/regression/csi.pyr    zCriticalSuccessIndex.__init__G   s3  ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý˜yÑ)Ô)ˆŒàð 	u¥jÐ1BÅCÑ&HÔ&Hð 	uÐL]Ð`aÒLaÐLaÝÐsÐ`qÐsÐsÑtÔtÐtØ!2ˆÔàÐ$Ø�NŠN˜6­5¬<¸©?¬?È5ˆNÑQÔQÐQØ�NŠN˜8­U¬\¸!©_¬_ÈUˆNÑSÔSÐSØ�NŠN˜>µ5´<À±?´?ÐSXˆNÑYÔYÐYÐYÐYà�NŠN˜;°À5ˆNÑIÔIÐIØ�NŠN˜=°"ÀUˆNÑKÔKÐKØ�NŠNÐ.¸È5ˆNÑQÔQÐQÐQÐQó    ÚpredsÚtargetc                 óT  — t          ||| j        | j        ¦  «        \  }}}| j        €2| xj        |z  c_        | xj        |z  c_        | xj        |z  c_        dS | j                             |¦  «         | j                             |¦  «         | j	                             |¦  «         dS )z*Update state with predictions and targets.N)
r   r   r   r   r   r   r   Úappendr   r   )r(   r,   r-   r   r   r   s         r*   ÚupdatezCriticalSuccessIndex.updateX   sµ   € å%CØ�6˜4œ>¨4Ô+Añ&
ô &
Ñ"ˆˆf�lð Ô!Ð)ØˆIŒI˜ÑˆIŒIØˆKŒK˜6Ñ!ˆKŒKØÐÔ Ñ-ÐÔÐÐàŒN×!Ò! $Ñ'Ô'Ð'ØÔ×#Ò# FÑ+Ô+Ð+ØÔ"×)Ò)¨,Ñ7Ô7Ð7Ð7Ð7r+   c                 óÖ   — | j         €| j        }| j        }| j        }n<t	          | j        ¦  «        }t	          | j        ¦  «        }t	          | j        ¦  «        }t          |||¦  «        S )z*Compute critical success index over state.)	r   r   r   r   r
   r   r   r   r   )r(   r   r   r   s       r*   ÚcomputezCriticalSuccessIndex.computef   sd   € àÔ!Ð)Ø”9ˆDØ”[ˆFØÔ,ˆLˆLå ¤Ñ/Ô/ˆDÝ! $Ô"2Ñ3Ô3ˆFÝ'¨Ô(>Ñ?Ô?ˆLÝ.¨t°V¸\ÑJÔJÐJr+   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   r!   r   r#   r   r    r0   r2   Ú__classcell__)r)   s   @r*   r   r      s;  ø€ € € € € € ð"ð "ðH $Ð�tÐ#Ð#Ñ#Ø!Ð�dÐ!Ð!Ñ!à
€L€L�LØ€N€N�NØÐÐÑØ�FŒ|ÐÐÑØ�f”ÐÐÑØ˜F”|Ð#Ð#Ñ#ðRð R %ð R¸HÀS¼Mð RÐ\_ð RÐdhð Rð Rð Rð Rð Rð Rð"8˜Fð 8¨Fð 8°tð 8ð 8ð 8ð 8ð
K˜ð 
Kð 
Kð 
Kð 
Kð 
Kð 
Kð 
Kð 
Kr+   r   )Útypingr   r   r   r&   r   Ú&torchmetrics.functional.regression.csir   r   Útorchmetrics.metricr	   Útorchmetrics.utilitiesr
   r   r   r+   r*   ú<module>r>      sÄ   ðð 'Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à €€€Ø Ð Ð Ð Ð Ð à rÐ rÐ rÐ rÐ rÐ rÐ rÐ rØ &Ð &Ð &Ð &Ð &Ð &Ø /Ð /Ð /Ð /Ð /Ð /ðXKð XKð XKð XKð XK˜6ñ XKô XKð XKð XKð XKr+   