
    i                     f    d dl 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  G d de      Zy)	    )AnyOptionalN)Tensortensor)_scc_per_channel_compute)_scc_update)Metricc            	       ~     e Zd ZU dZdZdZdZeed<   eed<   dde	e   d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 )SpatialCorrelationCoefficienta  Compute Spatial Correlation Coefficient (SCC_).

    As input to ``forward`` and ``update`` the metric accepts the following input

    - ``preds`` (:class:`~torch.Tensor`): Predictions from model of shape ``(N,C,H,W)`` or ``(N,H,W)``.
    - ``target`` (:class:`~torch.Tensor`): Ground truth values of shape ``(N,C,H,W)`` or ``(N,H,W)``.

    As output of `forward` and `compute` the metric returns the following output

    - ``scc`` (:class:`~torch.Tensor`): Tensor with scc score

    Args:
        hp_filter: High-pass filter tensor. default: tensor([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]).
        window_size: Local window size integer. default: 8.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torch import randn
        >>> from torchmetrics.image import SpatialCorrelationCoefficient as SCC
        >>> preds = randn([32, 3, 64, 64])
        >>> target = randn([32, 3, 64, 64])
        >>> scc = SCC()
        >>> scc(preds, target)
        tensor(0.0023)

    TF	scc_scoretotalNhigh_pass_filterwindow_sizekwargsreturnc                     t        |   di | |t        g dg dg dg      }|| _        || _        | j                  dt        d      d       | j                  dt        d      d       y )	N)r   r   )r      r   r   g        sum)defaultdist_reduce_fxr    )super__init__r   	hp_filterws	add_state)selfr   r   r   	__class__s       k/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/image/scc.pyr   z&SpatialCorrelationCoefficient.__init__;   se    "6"#%|[,&OP){F3KNwsEJ    predstargetc                 4   t        ||| j                  | j                        \  }}}t        |j	                  d            D cg c]R  }t        |dd|ddddf   j                  d      |dd|ddddf   j                  d      || j                        T }}| xj                  t        j                  t        j                  t        j                  |d      g d            z  c_        | xj                  |j	                  d      z  c_        yc c}w )z*Update state with predictions and targets.   N)dim)r%         r   )r   r   r   rangesize_scc_compute	unsqueezer   torchr   meancatr   )r   r"   r#   r   iscc_per_channels         r    updatez$SpatialCorrelationCoefficient.updateG   s    #.ufdnndgg#V vy 5::a=)
) q!Qz*44Q71a
9K9U9UVW9XZceielelm) 	 
 	%))EJJuyya/PV_$`aa

ejjm#

s   ADc                 4    | j                   | j                  z  S )zICompute the VIF score based on inputs passed in to ``update`` previously.)r   r   )r   s    r    computez%SpatialCorrelationCoefficient.computeQ   s    ~~

**r!   )Nr   )__name__
__module____qualname____doc__is_differentiablehigher_is_betterfull_state_updater   __annotations__r   intr   r   r2   r4   __classcell__)r   s   @r    r   r      sz    6 M
K&)9 
Ks 
Kbe 
Kjn 
K$F $F $t $+ +r!   r   )typingr   r   r-   r   r   !torchmetrics.functional.image.sccr   r+   r   torchmetrics.metricr	   r   r   r!   r    <module>rB      s%    !    V 9 &;+F ;+r!   