o
    yi                   	   @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 deded	eeef fd
dZ
	ddededed d	efddZ	ddededed d	efddZdS )    )TupleN)Tensor)Literal)_check_same_shape)reducepredstargetreturnc                 C   s   | j |j krtd| j  d|j  dt| | t| jdkr,td| j d|j d| jd dks:|jd dkrKtd| jd  d|jd  d| |fS )zUpdates and returns variables required to compute Spectral Angle Mapper. Checks for same shape and type of
    the input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
    zEExpected `preds` and `target` to have the same data type. Got preds: z and target: .   z@Expected `preds` and `target` to have BxCxHxW shape. Got preds:    zSExpected channel dimension of `preds` and `target` to be larger than 1. Got preds: )dtype	TypeErrorr   lenshape
ValueError)r   r    r   U/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/image/sam.py_sam_update   s4   	
r   elementwise_mean	reduction)r   sumnoneNc                 C   sL   | | j dd}| jdd}|jdd}t|||  dd }t||S )a  Computes Spectral Angle Mapper.

    Args:
        preds: estimated image
        target: ground truth image
        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

    Example:
        >>> preds = torch.rand([16, 3, 16, 16], generator=torch.manual_seed(42))
        >>> target = torch.rand([16, 3, 16, 16], generator=torch.manual_seed(123))
        >>> preds, target = _sam_update(preds, target)
        >>> _sam_compute(preds, target)
        tensor(0.5943)
    r   )dim)r   normtorchclampacosr   )r   r   r   dot_product
preds_normtarget_norm	sam_scorer   r   r   _sam_compute4   s
   
r#   c                 C   s   t | |\} }t| ||S )a  Universal Spectral Angle Mapper.

    Args:
        preds: estimated image
        target: ground truth image
        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

    Return:
        Tensor with Spectral Angle Mapper score

    Raises:
        TypeError:
            If ``preds`` and ``target`` don't have the same data type.
        ValueError:
            If ``preds`` and ``target`` don't have ``BxCxHxW shape``.

    Example:
        >>> from torchmetrics.functional import spectral_angle_mapper
        >>> preds = torch.rand([16, 3, 16, 16], generator=torch.manual_seed(42))
        >>> target = torch.rand([16, 3, 16, 16], generator=torch.manual_seed(123))
        >>> spectral_angle_mapper(preds, target)
        tensor(0.5943)

    References:
        [1] Roberta H. Yuhas, Alexander F. H. Goetz and Joe W. Boardman, "Discrimination among semi-arid
        landscape endmembers using the Spectral Angle Mapper (SAM) algorithm" in PL, Summaries of the Third Annual JPL
        Airborne Geoscience Workshop, vol. 1, June 1, 1992.
    )r   r#   )r   r   r   r   r   r   spectral_angle_mapperR   s   %r$   )r   )typingr   r   r   typing_extensionsr   torchmetrics.utilities.checksr   "torchmetrics.utilities.distributedr   r   r#   r$   r   r   r   r   <module>   s6   
!