o
    yi                     @   s   d dl mZ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
 deded	eeef fd
dZ		ddededeeef ded d	ef
ddZ		ddededeeef ded d	ef
ddZdS )    )TupleUnionN)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   Updates and returns variables required to compute Erreur Relative Globale Adimensionnelle de Synthèse.
    Checks for same shape and type of the input tensors.

    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   W/home/ubuntu/.local/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:
        >>> preds = torch.rand([16, 1, 16, 16], generator=torch.manual_seed(42))
        >>> target = preds * 0.75
        >>> preds, target = _ergas_update(preds, target)
        >>> torch.round(_ergas_compute(preds, target))
        tensor(154.)
       )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 )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

    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 torchmetrics.functional import error_relative_global_dimensionless_synthesis
        >>> preds = torch.rand([16, 1, 16, 16], generator=torch.manual_seed(42))
        >>> target = preds * 0.75
        >>> ergds = error_relative_global_dimensionless_synthesis(preds, target)
        >>> torch.round(ergds)
        tensor(154.)

    References:
        [1] Qian Du; Nicholas H. Younan; Roger King; Vijay P. Shah, "On the Performance Evaluation of
        Pan-Sharpening Techniques" in IEEE Geoscience and Remote Sensing Letters, vol. 4, no. 4, pp. 518-522,
        15 October 2007, doi: 10.1109/LGRS.2007.896328.
    )r   r+   )r   r	   r   r   r   r   r   -error_relative_global_dimensionless_synthesisU   s   (r,   )r   r   )typingr   r   r   r   typing_extensionsr   torchmetrics.utilities.checksr   "torchmetrics.utilities.distributedr   r   intfloatr+   r,   r   r   r   r   <module>   sB   

)
