o
    .wi                     @   s   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dZdededed
efddZ	ddededed	ee	 d
ef
ddZdS )    )OptionalN)Tensor)_check_same_shape_safe_dividepredstarget	thresholdkeep_sequence_dimreturnc           
         s  t | |  du rd}n%d   kr| jk s#n td| j d  t fddt| jD }| |k }||k } du rat||@  }t||A |@  }t||A |@  }	n%tj||@ |d }tj||A |@ |d }tj||A |@ |d }	|||	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   s    | ]	}| kr|V  qd S N ).0ir
   r   c/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/regression/csi.py	<genexpr>,   s    z1_critical_success_index_update.<locals>.<genexpr>)dim)	r   ndim
ValueErrortuplerangebooltorchsumint)
r   r   r	   r
   sum_dims	preds_bin
target_binhitsmissesfalse_alarmsr   r   r   _critical_success_index_update   s    

r"   r   r    r!   c                 C   s   t | | | | 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!   r   r   r   _critical_success_index_compute=   s   r#   c                 C   s    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!   r   r   r   critical_success_indexM   s   "r$   r   )typingr   r   r   torchmetrics.utilities.checksr   torchmetrics.utilities.computer   floatr   r   r"   r#   r$   r   r   r   r   <module>   s<   
&