o
    .wiG                     @   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ed def
ddZ		dde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| |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    r   `/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/image/ergas.py_ergas_update   s   
r   r
   elementwise_meanratio	reduction)r   sumnoneNc                 C   s   | j \}}}}| |||| } ||||| }| | }tj|| dd}	t|	||  }
tj|dd}d| ttj|
| d dd|  }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_scorer   r   r   _ergas_compute,   s   (
r)   c                 C   s   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   r   r   r   -error_relative_global_dimensionless_synthesisT   s   $r*   )r
   r   )r   r   typing_extensionsr   torchmetrics.utilities.checksr   "torchmetrics.utilities.distributedr   tupler   floatr)   r*   r   r   r   r   <module>   s@   
+