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)_sam_compute_sam_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 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 )SpectralAngleMappera  The metric `Spectral Angle Mapper`_ determines the spectral similarity between image spectra and reference
    spectra by calculating the angle between the spectra, where small angles between indicate high similarity and
    high angles indicate low similarity.

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

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

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

    Args:
        reduction: a method to reduce metric score over labels.

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

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

    Return:
        Tensor with SpectralAngleMapper score

    Example:
        >>> import torch
        >>> from torchmetrics import SpectralAngleMapper
        >>> preds = torch.rand([16, 3, 16, 16], generator=torch.manual_seed(42))
        >>> target = torch.rand([16, 3, 16, 16], generator=torch.manual_seed(123))
        >>> sam = SpectralAngleMapper()
        >>> sam(preds, target)
        tensor(0.5943)
    Fhigher_is_betterTis_differentiablefull_state_updatepredstargetelementwise_mean	reduction)r   sumnonekwargsreturnNc                    sD   t  jdi | td | jdg dd | jdg dd || _d S )NzMetric `SpectralAngleMapper` will save all targets and predictions in the buffer. For large datasets, this may lead to a large memory footprint.r   cat)defaultdist_reduce_fxr    )super__init__r	   	add_stater   )selfr   r   	__class__r   J/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/image/sam.pyr   E   s   
zSpectralAngleMapper.__init__c                 C   s*   t ||\}}| j| | j| dS )z*Update state with predictions and targets.N)r   r   appendr   r   r   r   r   r   r!   updateT   s   zSpectralAngleMapper.updatec                 C   s"   t | j}t | j}t||| jS )zComputes spectra over state.)r
   r   r   r   r   r#   r   r   r!   computeZ   s   

zSpectralAngleMapper.compute)r   )__name__
__module____qualname____doc__r   bool__annotations__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.samr   r   torchmetrics.metricr   torchmetrics.utilitiesr	   torchmetrics.utilities.datar
   r   r   r   r   r!   <module>   s   