
    i                         d dl mZ d dlZd dlmZ d dlmZ d dlmZ 	 ddededed	ee	   d
e
eeef   f
dZdededed
efdZ	 ddededed	ee	   d
ef
dZy)    )OptionalN)Tensor)_check_same_shape_safe_dividepredstarget	thresholdkeep_sequence_dimreturnc                     t        | |       d}nYdcxk  r| j                  k  sn t        d| j                   d       t        fdt	        | j                        D              }| |k\  j                         }||k\  j                         }yt        j                  ||z        j                         }t        j                  ||z  |z        j                         }t        j                  ||z  |z        j                         }	n~t        j                  ||z  |      j                         }t        j                  ||z  |z  |      j                         }t        j                  ||z  |z  |      j                         }	|||	fS )a%  Update and return variables required to compute Critical Success Index. Checks for same shape of tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        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.

    Nr   z.Expected keep_sequence dim to be in range [0, z
] but got c              3   .   K   | ]  }|k7  s	|  y wN ).0ir   s     {/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/regression/csi.py	<genexpr>z1_critical_success_index_update.<locals>.<genexpr>,   s     P$5q>O9O$5s   
)dim)	r   ndim
ValueErrortuplerangebooltorchsumint)
r   r	   r
   r   sum_dims	preds_bin
target_binhitsmissesfalse_alarmss
      `      r   _critical_success_index_updater$      sb    eV$ #0ejj0I%**U_`q_rsttPE%**$5PP )#))+II%++-J yyZ/0446I
2j@AEEGyy)j"8I!EFJJLyyZ/X>BBDI
2j@hOSSUyy)j"8I!E8TXXZ%%    r!   r"   r#   c                 &    t        | | |z   |z         S )a  Compute critical success index.

    Args:
        hits: Number of true positives after binarization
        misses: Number of false negatives after binarization
        false_alarms: Number of false positives after binarization

    Returns:
        If input tensors are 5-dimensional and ``keep_sequence_dim=True``, the metric returns a ``(S,)`` vector
        with CSI scores for each image in the sequence. Otherwise, it returns a scalar tensor with the CSI score.

    r   )r!   r"   r#   s      r   _critical_success_index_computer'   =   s     dVml:;;r%   c                 @    t        | |||      \  }}}t        |||      S )aY  Compute critical success index.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        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.

    Returns:
        If ``keep_sequence_dim`` is specified, the metric returns a vector of  with CSI scores for each image
        in the sequence. Otherwise, it returns a scalar tensor with the CSI score.

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

    Example:
        >>> import torch
        >>> from torchmetrics.functional.regression import critical_success_index
        >>> 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]]])
        >>> critical_success_index(x, y, 0.5, keep_sequence_dim=0)
        tensor([0.3333, 0.3333])

    )r$   r'   )r   r	   r
   r   r!   r"   r#   s          r   critical_success_indexr)   M   s-    D "@vyZk!lD&,*4FFr%   r   )typingr   r   r   torchmetrics.utilities.checksr   torchmetrics.utilities.computer   floatr   r   r$   r'   r)   r   r%   r   <module>r.      s       ; 7 Y]#&#&!#&.3#&HPQT#&
666!"#&L<& <& <PV <[a <" Y]#G#G!#G.3#GHPQT#G#Gr%   