o
    .wif                  	   @   s   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 )    N)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 )zUpdate and returns variables required to compute Spectral Angle Mapper.

    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   ^/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/image/sam.py_sam_update   s,   
r   elementwise_mean	reduction)r   sumnoneNc                 C   sL   | | j dd}| jdd}|jdd}t|||  dd }t||S )aW  Compute 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:
        >>> from torch import rand
        >>> preds = rand([16, 3, 16, 16])
        >>> target = rand([16, 3, 16, 16])
        >>> preds, target = _sam_update(preds, target)
        >>> _sam_compute(preds, target)
        tensor(0.5914)

    r   )dim)r   normtorchclampacosr   )r   r   r   dot_product
preds_normtarget_norm	sam_scorer   r   r   _sam_compute1   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 torch import rand
        >>> from torchmetrics.functional.image import spectral_angle_mapper
        >>> preds = rand([16, 3, 16, 16],)
        >>> target = rand([16, 3, 16, 16])
        >>> spectral_angle_mapper(preds, target)
        tensor(0.5914)

    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_mapperQ   s   'r#   )r   )r   r   typing_extensionsr   torchmetrics.utilities.checksr   "torchmetrics.utilities.distributedr   tupler   r"   r#   r   r   r   r   <module>   s4   
#