o
    .wiv                     @   s   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sKdgZG dd deZdS )    )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_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deded 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	d deeeee f  dee defddZ  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   elementwise_meanp	reductionr   sumnonekwargsreturnNc                    s   t  jdi | td t|tr|dkrtd| d|| _d}||vr1t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)defaultdist_reduce_fxr    )	super__init__r   
isinstanceint
ValueErrorr   r   	add_state)selfr   r   r!   allowed_reductions	__class__r'   X/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/image/d_lambda.pyr)   L   s   z SpectralDistortionIndex.__init__c                 C   s*   t ||\}}| j| | j| dS )z#Update state with preds and target.N)r
   r   appendr   r.   r   r   r'   r'   r2   update`   s   zSpectralDistortionIndex.updatec                 C   s&   t | j}t | j}t||| j| jS )z.Compute and returns spectral distortion index.)r   r   r   r	   r   r   r4   r'   r'   r2   computef   s   

zSpectralDistortionIndex.computevalaxc                 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
            >>> 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.   r7   r8   r'   r'   r2   plotl   s   ,r   )r   r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r+   r   r   r)   r5   r6   r   r   r   r   r   r:   __classcell__r'   r'   r0   r2   r      s:   
 #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'   r'   r'   r2   <module>   s   