
    iv                         d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZm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mZ esdgZ G d de      Zy)    )Sequence)AnyListOptionalUnion)Tensor)Literal)"_spectral_distortion_index_compute!_spectral_distortion_index_update)Metric)rank_zero_warn)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEzSpectralDistortionIndex.plotc            	            e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   d	Ze
ed
<   ee   ed<   e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	 ddeeeee   f      dee   defdZ xZS )SpectralDistortionIndexag  Compute Spectral Distortion Index (SpectralDistortionIndex_) also now as D_lambda.

    The metric is used to compare the spectral distortion between two images.

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

    - ``preds`` (:class:`~torch.Tensor`): Low resolution multispectral image of shape ``(N,C,H,W)``
    - ``target``(:class:`~torch.Tensor`): High resolution fused image of shape ``(N,C,H,W)``

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

    - ``sdi`` (:class:`~torch.Tensor`): if ``reduction!='none'`` returns float scalar tensor with average SDI value
      over sample else returns tensor of shape ``(N,)`` with SDI values per sample

    Args:
        p: Large spectral differences
        reduction: a method to reduce metric score over labels.

            - ``'elementwise_mean'``: takes the mean (default)
            - ``'sum'``: takes the sum
            - ``'none'``: no reduction will be applied

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

    Example:
        >>> from torch import rand
        >>> from torchmetrics.image import SpectralDistortionIndex
        >>> preds = rand([16, 3, 16, 16])
        >>> target = rand([16, 3, 16, 16])
        >>> sdi = SpectralDistortionIndex()
        >>> sdi(preds, target)
        tensor(0.0234)

    Thigher_is_betteris_differentiableFfull_state_updateg        plot_lower_boundg      ?plot_upper_boundpredstargetp	reductionelementwise_meansumnonekwargsreturnNc                    t        |   di | t        d       t        |t              r|dk  rt        d| d      || _        d}||vrt        d| d|       || _        | j                  dg d	
       | j                  dg d	
       y )NzMetric `SpectralDistortionIndex` will save all targets and predictions in buffer. For large datasets this may lead to large memory footprint.r   z.Expected `p` to be a positive integer. Got p: .r   z(Expected argument `reduction` be one of z	 but got r   cat)defaultdist_reduce_fxr    )	super__init__r   
isinstanceint
ValueErrorr   r   	add_state)selfr   r   r!   allowed_reductions	__class__s        p/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/image/d_lambda.pyr*   z SpectralDistortionIndex.__init__L   s     	"6"*	
 !S!Q!VMaSPQRSS@..GHZG[[dendopqq"w5AxEB    c                     t        ||      \  }}| j                  j                  |       | j                  j                  |       y)z#Update state with preds and target.N)r   r   appendr   r/   r   r   s      r2   updatezSpectralDistortionIndex.update`   s6    9%Hv

% 6"r3   c                     t        | j                        }t        | j                        }t        ||| j                  | j
                        S )z.Compute and returns spectral distortion index.)r   r   r   r
   r   r   r6   s      r2   computezSpectralDistortionIndex.computef   s7    TZZ(dkk*1%XXr3   valaxc                 &    | j                  ||      S )a  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> from torch import rand
            >>> from torchmetrics.image import SpectralDistortionIndex
            >>> preds = rand([16, 3, 16, 16])
            >>> target = rand([16, 3, 16, 16])
            >>> metric = SpectralDistortionIndex()
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> from torch import rand
            >>> from torchmetrics.image import SpectralDistortionIndex
            >>> preds = rand([16, 3, 16, 16])
            >>> target = rand([16, 3, 16, 16])
            >>> metric = SpectralDistortionIndex()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(preds, target))
            >>> fig_, ax_ = metric.plot(values)

        )_plot)r/   r:   r;   s      r2   plotzSpectralDistortionIndex.plotl   s    X zz#r""r3   )   r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r,   r	   r   r*   r7   r9   r   r   r   r   r   r>   __classcell__)r1   s   @r2   r   r      s    !F "d!"t"#t#!e!!e!<L SeCC%,-N%OCpsC	C(#F #F #t #Y Y _c,#E&(6*:":;<,#IQRZI[,#	,#r3   r   N)collections.abcr   typingr   r   r   r   torchr   typing_extensionsr	   &torchmetrics.functional.image.d_lambdar
   r   torchmetrics.metricr   torchmetrics.utilitiesr   torchmetrics.utilities.datar   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   __doctest_skip__r   r(   r3   r2   <module>rS      sB    % - -  % x & 1 4 @ @67y#f y#r3   