
    in                     n    d dl mZmZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ  G d d	e
      Zy)
    )AnyListN)Tensor)Literal)_vif_per_channel)Metric)dim_zero_catc            	            e Zd ZU dZdZdZdZee   e	d<   ee	d<   dde
ded   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 )VisualInformationFidelityu<  Compute Pixel Based Visual Information Fidelity (VIF_).

    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)`` with H,W ≥ 41
    - ``target`` (:class:`~torch.Tensor`): Ground truth values of shape ``(N,C,H,W)`` with H,W ≥ 41

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

    - ``vif-p`` (:class:`~torch.Tensor`):
        - If ``reduction='mean'`` (default), returns a Tensor mean VIF score.
        - If ``reduction='none'``, returns a tensor of shape ``(N,)`` with VIF values per sample.

    Args:
        sigma_n_sq: variance of the visual noise
        reduction: The reduction method for aggregating scores.

            - ``'mean'``: return the average VIF across the batch.
            - ``'none'``: return a VIF score for each sample in the batch.

        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torch import randn
        >>> from torchmetrics.image import VisualInformationFidelity
        >>> preds = randn([32, 3, 41, 41], generator=torch.Generator().manual_seed(42))
        >>> target = randn([32, 3, 41, 41], generator=torch.Generator().manual_seed(43))
        >>> vif_mean = VisualInformationFidelity(reduction='mean')
        >>> vif_mean(preds, target)
        tensor(0.0032)
        >>> vif_none = VisualInformationFidelity(reduction='none')
        >>> vif_none(preds, target)
        tensor([0.0040, 0.0049, 0.0017, 0.0039, 0.0041, 0.0043, 0.0030, 0.0028, 0.0012,
                0.0067, 0.0010, 0.0014, 0.0030, 0.0048, 0.0050, 0.0038, 0.0037, 0.0025,
                0.0041, 0.0019, 0.0007, 0.0034, 0.0037, 0.0016, 0.0026, 0.0021, 0.0038,
                0.0033, 0.0031, 0.0020, 0.0036, 0.0057])

    TF	vif_scoretotal
sigma_n_sq	reductionmeannonekwargsreturnNc                     t        |   di | t        |t        t        f      r|dk  rt        d|       |dvrt        d|       || _        || _        | j                  dg d        y )Nr   zIArgument `sigma_n_sq` is expected to be a positive float or int, but got r   z7Argument `reduction` must be 'mean' or 'none', but got r   )defaultdist_reduce_fx )	super__init__
isinstancefloatint
ValueErrorr   r   	add_state)selfr   r   r   	__class__s       k/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/image/vif.pyr   z"VisualInformationFidelity.__init__H   sz    "6"*ucl3zA~hishtuvv,,VW`Vabcc$"{BtD    predstargetc                 z   |j                  d      }t        |      D cg c]3  }t        |dd|ddddf   |dd|ddddf   | j                        5 }}|dkD  r)t	        j
                  t	        j                  |      d      nt	        j                  |      }| j                  j                  |       yc c}w )z*Update state with predictions and targets.   Nr   )
sizeranger   r   torchr   stackcatr   append)r    r$   r%   channelsivif_per_channels         r"   updatez VisualInformationFidelity.updateU   s    ::a=^cdl^m
^mYZU1aA:.q!Qz0BDOOT^m 	 
 JRTU%**U[[%A1E[`[d[det[uo.	
s   8B8c                 n    t        | j                        }| j                  dk(  r|j                         S |S )zCompute VIF over state.r   )r	   r   r   r   )r    r   s     r"   computez!VisualInformationFidelity.compute^   s/     0	>>V#>>##r#   )g       @r   )__name__
__module____qualname____doc__is_differentiablehigher_is_betterfull_state_updater   r   __annotations__r   r   r   r   r1   r3   __classcell__)r!   s   @r"   r   r      s    %N F|ME5 E7>;R Egj Eos E/F /F /t / r#   r   )typingr   r   r*   r   typing_extensionsr   !torchmetrics.functional.image.vifr   torchmetrics.metricr   torchmetrics.utilities.datar	   r   r   r#   r"   <module>rB      s*       % > & 4J Jr#   