
    if              	           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	Z		 ddeded
ed   defdZ
	 ddeded
ed   defdZy)    N)Tensor)Literal)_check_same_shape)reducepredstargetreturnc                    | j                   |j                   k7  r&t        d| j                    d|j                    d      t        | |       t        | j                        dk7  r&t        d| j                   d|j                   d      | j                  d   dk  s|j                  d   dk  r,t        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   s     v/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/image/sam.py_sam_updater      s     {{fll" ;;-}V\\N!E
 	
 eV$
5;;1Nu{{m[hioiuiuhvvwx
 	
 	A!aA!5 ;;q>*-Q7HK
 	
 &=    	reduction)elementwise_meansumnoneNc                     | |z  j                  d      }| j                  d      }|j                  d      }t        j                  |||z  z  dd      j	                         }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_scores          r   _sam_computer%   1   sm    2 6>&&1&-K"J++!+$KK:+CDb!LQQSI)Y''r   c                 :    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   s      r   spectral_angle_mapperr'   Q   s$    N  v.ME6vy11r   )r   )r   r   typing_extensionsr   torchmetrics.utilities.checksr   "torchmetrics.utilities.distributedr   tupler   r%   r'    r   r   <module>r-      s      % ; 5v v %2G : CU((( >?( 	(F CU(2(2(2 >?(2 	(2r   