o
    yi                     @   s   d dl mZmZmZ d dlZd dlmZmZ d dlmZ d dl	m
Z
mZ 		dded	ed
ededed defddZ	ddededeeeeedf f  deeef fddZ				ddeded
ee deded deeeeedf f  defddZdS )    )OptionalTupleUnionN)Tensortensor)Literal)rank_zero_warnreduce      $@elementwise_meansum_squared_errorn_obs
data_rangebase	reduction)r   sumnoneNreturnc                 C   s>   dt | t | |  }|dt t|  }t||dS )a  Computes peak signal-to-noise ratio.

    Args:
        sum_squared_error: Sum of square of errors over all observations
        n_obs: Number of predictions or observations
        data_range: the range of the data. If None, it is determined from the data (max - min).
           ``data_range`` must be given when ``dim`` is not None.
        base: a base of a logarithm to use
        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.tensor([[0.0, 1.0], [2.0, 3.0]])
        >>> target = torch.tensor([[3.0, 2.0], [1.0, 0.0]])
        >>> data_range = target.max() - target.min()
        >>> sum_squared_error, n_obs = _psnr_update(preds, target)
        >>> _psnr_compute(sum_squared_error, n_obs, data_range)
        tensor(2.5527)
       
   )r   )torchlogr   r	   )r   r   r   r   r   psnr_base_e	psnr_vals r   V/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/image/psnr.py_psnr_compute   s   r   predstargetdim.c                 C   s   |du rt t | | d}t| |jd}||fS | | }t j|| |d}t|tr2|g}nt|}|sEt| |jd}||fS t|	 |jd| 
 }||}||fS )a[  Updates and returns variables required to compute peak signal-to-noise ratio.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        dim: Dimensions to reduce PSNR scores over provided as either an integer or a list of integers. Default is
            None meaning scores will be reduced across all dimensions.
    Nr   )devicer   )r   r   powr   numelr    
isinstanceintlistsizeprod	expand_as)r   r   r   r   r   diffdim_listr   r   r   _psnr_update:   s   

r,   c                 C   sz   |du r|dkrt d| d |du r%|durtd| |  }ntt|}t| ||d\}}t|||||dS )a  Computes the peak signal-to-noise ratio.

    Args:
        preds: estimated signal
        target: groun truth signal
        data_range: the range of the data. If None, it is determined from the data (max - min).
            ``data_range`` must be given when ``dim`` is not None.
        base: a base of a logarithm to use
        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

        dim:
            Dimensions to reduce PSNR scores over provided as either an integer or a list of integers. Default is
            None meaning scores will be reduced across all dimensions.

    Return:
        Tensor with PSNR score

    Raises:
        ValueError:
            If ``dim`` is not ``None`` and ``data_range`` is not provided.

    Example:
        >>> from torchmetrics.functional import peak_signal_noise_ratio
        >>> pred = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
        >>> target = torch.tensor([[3.0, 2.0], [1.0, 0.0]])
        >>> peak_signal_noise_ratio(pred, target)
        tensor(2.5527)

    .. note::
        Half precision is only support on GPU for this metric
    Nr   zThe `reduction=z.` will not have any effect when `dim` is None.z6The `data_range` must be given when `dim` is not None.r!   )r   r   )r   
ValueErrormaxminr   floatr,   r   )r   r   r   r   r   r   r   r   r   r   r   peak_signal_noise_ratio]   s   +r1   )r
   r   )N)Nr
   r   N)typingr   r   r   r   r   r   typing_extensionsr   torchmetrics.utilitiesr   r	   r0   r   r%   r,   r1   r   r   r   r   <module>   sb   
&

&