a
    d                     @   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
 eeeeef ddd	Zdeeeeef ed edddZdeeeeef ed ed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rXt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: )Zdtype	TypeErrorr   lenshape
ValueError)r   r	    r   l/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/image/ergas.py_ergas_update   s$    
r   r   elementwise_mean)r   sumnoneN)r   r	   ratio	reductionr
   c                 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:
        >>> gen = torch.manual_seed(42)
        >>> preds = torch.rand([16, 1, 16, 16], generator=gen)
        >>> target = preds * 0.75
        >>> preds, target = _ergas_update(preds, target)
        >>> torch.round(_ergas_compute(preds, target))
        tensor(154.)

       )Zdimd      )r   Zreshapetorchr   sqrtmeanr   )r   r	   r   r   bchwdiffZsum_squared_errorZrmse_per_bandZmean_targetZ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.image import error_relative_global_dimensionless_synthesis
        >>> gen = torch.manual_seed(42)
        >>> preds = torch.rand([16, 1, 16, 16], generator=gen)
        >>> 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_synthesisV   s    *r%   )r   r   )r   r   )typingr   r   r   r   Ztyping_extensionsr   Ztorchmetrics.utilities.checksr   Z"torchmetrics.utilities.distributedr   r   intfloatr$   r%   r   r   r   r   <module>   s0     
+  
