
    iG                         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ed   def
dZ	 	 dde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      | |fS )u   Update and returns variables required to compute Erreur Relative Globale Adimensionnelle de Synthèse.

    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: )dtype	TypeErrorr   lenshape
ValueError)r   r   s     x/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/image/ergas.py_ergas_updater      s     {{fll" ;;-}V\\N!E
 	
 eV$
5;;1Nu{{m[hioiuiuhvvwx
 	
 &=    ratio	reduction)elementwise_meansumnoneNc                    | j                   \  }}}}| j                  ||||z        } |j                  ||||z        }| |z
  }t        j                  ||z  d      }	t        j                  |	||z  z        }
t        j
                  |d      }d|z  t        j                  t        j                  |
|z  dz  d      |z        z  }t        ||      S )u  Erreur Relative Globale Adimensionnelle de Synthèse.

    Args:
        preds: estimated image
        target: ground truth image
        ratio: ratio of high resolution to low resolution
        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, 1, 16, 16])
        >>> target = preds * 0.75
        >>> preds, target = _ergas_update(preds, target)
        >>> torch.round(_ergas_compute(preds, target))
        tensor(10.)

       )dimd      )r   reshapetorchr   sqrtmeanr   )r   r   r   r   bchwdiffsum_squared_errorrmse_per_bandmean_targetergas_scores                r   _ergas_computer,   ,   s    6 JAq!QMM!QA&E^^Aq!a%(F6>D		$+15JJ0AE:;M**V+K+

599mk6QVW5W]^+_bc+c ddK+y))r   c                 <    t        | |      \  } }t        | |||      S )a  Calculates `Error relative global dimensionless synthesis`_ (ERGAS) metric.

    Args:
        preds: estimated image
        target: ground truth image
        ratio: ratio of high resolution to low resolution
        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 RelativeG 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 error_relative_global_dimensionless_synthesis
        >>> preds = rand([16, 1, 16, 16])
        >>> target = preds * 0.75
        >>> error_relative_global_dimensionless_synthesis(preds, target)
        tensor(9.6193)

    )r   r,   )r   r   r   r   s       r   -error_relative_global_dimensionless_synthesisr.   T   s&    H "%0ME6%	::r   )r   r   )r    r   typing_extensionsr   torchmetrics.utilities.checksr   "torchmetrics.utilities.distributedr   tupler   floatr,   r.    r   r   <module>r5      s      % ; 5  E&&.4I 0 BT	%*%*%* %* >?	%*
 %*V BT	%;%;%; %; >?	%;
 %;r   