o
    yi                     @   sp   d dl 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 G dd	 d	eZd
S )    )AnyList)Tensor)Literal)"_spectral_distortion_index_compute!_spectral_distortion_index_update)Metric)rank_zero_warn)dim_zero_catc                	       s   e Zd ZU dZ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  ZS )SpectralDistortionIndexa|  Computes Spectral Distortion Index (SpectralDistortionIndex_) also now as D_lambda 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 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_update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_REDUCTION	__class__r   O/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/image/d_lambda.pyr    C   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   r)   updateW   s   zSpectralDistortionIndex.updatec                 C   s&   t | j}t | j}t||| j| jS )z/Computes and returns spectral distortion index.)r
   r   r   r   r   r   r+   r   r   r)   compute]   s   

zSpectralDistortionIndex.compute)r   r   )__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r   r"   r   r   r    r,   r-   __classcell__r   r   r'   r)   r      s&   
 "r   N)typingr   r   torchr   typing_extensionsr   &torchmetrics.functional.image.d_lambdar   r   torchmetrics.metricr   torchmetrics.utilitiesr	   torchmetrics.utilities.datar
   r   r   r   r   r)   <module>   s   