a
    d                     @   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	 eeeeef ddd	Z
deeed edddZdeeed ed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rXtd| j d|j d| jd dkst|jd dkrt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: )Zdtype	TypeErrorr   lenshape
ValueError)r   r    r   j/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/image/sam.py_sam_update   s4    
r   elementwise_mean)r   sumnoneN)r   r   	reductionr	   c                 C   sL   | | j dd}| jdd}|jdd}t|||  dd }t||S )a  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:
        >>> gen = torch.manual_seed(42)
        >>> preds = torch.rand([16, 3, 16, 16], generator=gen)
        >>> target = torch.rand([16, 3, 16, 16], generator=gen)
        >>> preds, target = _sam_update(preds, target)
        >>> _sam_compute(preds, target)
        tensor(0.5914)

    r   )Zdim)r   Znormtorchclampacosr   )r   r   r   Zdot_productZ
preds_normZtarget_normZ	sam_scorer   r   r   _sam_compute3   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.image import spectral_angle_mapper
        >>> gen = torch.manual_seed(42)
        >>> preds = torch.rand([16, 3, 16, 16], generator=gen)
        >>> target = torch.rand([16, 3, 16, 16], generator=gen)
        >>> 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_mapperS   s    'r   )r   )r   )typingr   r   r   Ztyping_extensionsr   Ztorchmetrics.utilities.checksr   Z"torchmetrics.utilities.distributedr   r   r   r   r   r   r   r   <module>   s(    # 