o
    ¶ôj¸  ã                   @   sd   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„ de
ƒZdS )	é    )ÚAnyÚListÚOptionalN)ÚTensor)Ú_critical_success_index_computeÚ_critical_success_index_update)ÚMetric)Údim_zero_catc                	       s²   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d„Zdededdfdd„Zdefdd„Z‡  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                    sÈ   t ƒ jdi |¤Ž t|ƒ| _|r t|tƒr|dk r td|› �ƒ‚|| _|d u rJ| jdt	 
d¡dd� | jdt	 
d¡dd� | jdt	 
d¡dd� d S | jdg d	d� | jd
g d	d� | jdg d	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__r   ún/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchmetrics/regression/csi.pyr   G   s   
zCriticalSuccessIndex.__init__ÚpredsÚtargetc                 C   sx   t ||| j| jƒ\}}}| jdu r(|  j|7  _|  j|7  _|  j|7  _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   r   r   r(   ÚupdateX   s   
ÿ
zCriticalSuccessIndex.updatec                 C   sH   | j du r| j}| j}| j}nt| jƒ}t| jƒ}t| jƒ}t|||ƒS )z*Compute critical success index over state.N)	r   r   r   r   r	   r   r   r   r   )r%   r   r   r   r   r   r(   Úcomputef   s   



zCriticalSuccessIndex.compute)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   r   r   r    r   r   r,   r-   Ú__classcell__r   r   r&   r(   r
      s   
 $$r
   )Útypingr   r   r   r#   r   Ú&torchmetrics.functional.regression.csir   r   Útorchmetrics.metricr   Útorchmetrics.utilitiesr	   r
   r   r   r   r(   Ú<module>   s   