
    i                        d dl 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Z	 ddededeeeeedf   f      deeef   fdZ	 	 	 ddededeeeeef   f   d	ed
ed   deeeeedf   f      defdZy)    )OptionalUnionN)Tensortensor)Literal)rank_zero_warnreducesum_squared_errornum_obs
data_rangebase	reduction)elementwise_meansumnoneNreturnc                     dt        j                  |      z  t        j                  | |z        z
  }|dt        j                  t        |            z  z  }t        ||      S )a  Compute peak signal-to-noise ratio.

    Args:
        sum_squared_error: Sum of square of errors over all observations
        num_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, num_obs = _psnr_update(preds, target)
        >>> _psnr_compute(sum_squared_error, num_obs, data_range)
        tensor(2.5527)

       
   )r   )torchlogr   r	   )r
   r   r   r   r   psnr_base_e	psnr_valss          w/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/image/psnr.py_psnr_computer      sT    < eii
++eii8IG8S.TTKrEIIfTl$;;<I)y11    predstargetdim.c                    | j                         s| j                  t        j                        } |j                         s|j                  t        j                        }|Ut        j                  t        j
                  | |z
  d            }t        |j                         |j                        }||fS | |z
  }t        j                  ||z  |      }t        |t              r|gn
t        |      }|s)t        |j                         |j                        }||fS t        |j                         |j                        |   j                         }|j                  |      }||fS )aZ  Update and return 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.

    r   )devicer   )is_floating_pointtor   float32r   powr   numelr!   
isinstanceintlistsizeprod	expand_as)r   r   r   r
   r   diffdim_lists          r   _psnr_updater0   :   s     ""$'##%5==)
{!IIeii&BC> '))6>D		$+37"3,u$s)H>
 g%% v}}=hGLLN##$56g%%r   c                 Z   ||dk7  rt        d| d       t        |t              rQt        j                  | |d   |d         } t        j                  ||d   |d         }t        |d   |d   z
        }nt        t        |            }t        | ||      \  }}t        |||||      S )	a  Compute the peak signal-to-noise ratio.

    Args:
        preds: estimated signal
        target: groun truth signal
        data_range:
            the range of the data. If a tuple is provided then the range is calculated as the difference and
            input is clamped between the values.
        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

    Example:
        >>> from torchmetrics.functional.image 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, data_range=3.0)
        tensor(2.5527)

    .. attention::
        Half precision is only support on GPU for this metric.

    r   zThe `reduction=z.` will not have any effect when `dim` is None.r      )minmaxr"   )r   r   )	r   r(   tupler   clampr   floatr0   r   )	r   r   r   r   r   r   data_range_valr
   r   s	            r   peak_signal_noise_ratior9   _   s    R {y$663abc*e$Ez!}*Q-HVAJqMJ
1
1 =>j 12!-eV!Ew*G^$Zcddr   )      $@r   )N)r:   r   N)typingr   r   r   r   r   typing_extensionsr   torchmetrics.utilitiesr   r	   r7   r   r)   r5   r0   r9    r   r   <module>r?      s;   #    % 9 BT 2 2 2  2 	 2
 >? 2  2L 26"&"&"& 
%U38_,-	."& 66>	"&R BT154e4e4e eU5%<0014e 	4e
 >?4e 
%U38_,-	.4e 4er   