o
    .wi                     @   s`   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 deZdS )	    )AnyOptionalN)Tensortensor)_scc_per_channel_compute)_scc_update)Metricc                	       sz   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dZdededdfddZdefddZ  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                    sl   t  jdi | |d u rtg dg dg dg}|| _|| _| jdtddd | jdtddd d S )	N)r   r   )r   r   r   r
   g        sum)defaultdist_reduce_fxr    )super__init__r   	hp_filterws	add_state)selfr   r   r   	__class__r   S/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/image/scc.pyr   ;   s   z&SpatialCorrelationCoefficient.__init__predstargetc              	      s~   t jj\  fddtdD } jttjtj	|ddg dd7  _ j
d7  _
dS )z*Update state with predictions and targets.c              
      sX   g | ](}t d d |d d d d f dd d |d d d d f d jqS )N   )_scc_compute	unsqueezer   ).0ir   r   r   r    r   r   
<listcomp>J   s    Jz8SpatialCorrelationCoefficient.update.<locals>.<listcomp>r!   )dim)r!         r   N)r   r   r   rangesizer
   torchr   meancatr   )r   r   r    scc_per_channelr   r&   r   updateG   s   ,z$SpatialCorrelationCoefficient.updatec                 C   s   | j | j S )zICompute the VIF score based on inputs passed in to ``update`` previously.)r
   r   )r   r   r   r   computeQ   s   z%SpatialCorrelationCoefficient.compute)Nr   )__name__
__module____qualname____doc__is_differentiablehigher_is_betterfull_state_updater   __annotations__r   intr   r   r1   r2   __classcell__r   r   r   r   r	      s   
 $
r	   )typingr   r   r-   r   r   !torchmetrics.functional.image.sccr   r"   r   torchmetrics.metricr   r	   r   r   r   r   <module>   s   