a
    d                     @   s   d dl mZ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sd
gZG dd deZdS )    )AnyListOptionalSequenceUnion)Tensor)Literal)"_spectral_distortion_index_compute!_spectral_distortion_index_update)Metric)rank_zero_warn)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPESpectralDistortionIndex.plotc                       s   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eed edd fddZeeddddZedddZdeeeee f  ee edddZ  ZS )SpectralDistortionIndexa  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:
        >>> import torch
        >>> _ = torch.manual_seed(42)
        >>> from torchmetrics.image import SpectralDistortionIndex
        >>> preds = torch.rand([16, 3, 16, 16])
        >>> target = torch.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   elementwise_meanr   sumnoneN)p	reductionkwargsreturnc                    s   t  jf i | td t|tr,|dkr<td| d|| _d}||vrbtd| d| || _| jdg d	d
 | jdg d	d
 d S )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)defaultZdist_reduce_fxr   )	super__init__r   
isinstanceint
ValueErrorr   r    Z	add_state)selfr   r    r!   Zallowed_reductions	__class__ d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/image/d_lambda.pyr'   L   s    z SpectralDistortionIndex.__init__)r   r   r"   c                 C   s*   t ||\}}| j| | j| dS )z#Update state with preds and target.N)r
   r   appendr   r+   r   r   r.   r.   r/   update`   s    zSpectralDistortionIndex.update)r"   c                 C   s&   t | j}t | j}t||| j| jS )z.Compute and returns spectral distortion index.)r   r   r   r	   r   r    r1   r.   r.   r/   computef   s    

zSpectralDistortionIndex.compute)valaxr"   c                 C   s   |  ||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
            >>> import torch
            >>> _ = torch.manual_seed(42)
            >>> from torchmetrics.image import SpectralDistortionIndex
            >>> preds = torch.rand([16, 3, 16, 16])
            >>> target = torch.rand([16, 3, 16, 16])
            >>> metric = SpectralDistortionIndex()
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

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

        )Z_plot)r+   r4   r5   r.   r.   r/   plotl   s    .r   )r   r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r)   r   r   r'   r2   r3   r   r   r   r   r   r6   __classcell__r.   r.   r,   r/   r      s(   
$ 
 r   N)typingr   r   r   r   r   Ztorchr   Ztyping_extensionsr   Z&torchmetrics.functional.image.d_lambdar	   r
   Ztorchmetrics.metricr   Ztorchmetrics.utilitiesr   Ztorchmetrics.utilities.datar   Ztorchmetrics.utilities.importsr   Ztorchmetrics.utilities.plotr   r   Z__doctest_skip__r   r.   r.   r.   r/   <module>   s   