o
    .wi                     @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ 	ddeded	ed
ed de	eeef f
ddZ
dedeeef dedefddZ		ddeded
ed d	edef
ddZdS )    )UnionN)Tensor)Literal)_mean_squared_error_updatemeanpredstargetnum_outputsnormalization)r   rangestdl2returnc                 C   s   t | ||\}}|dkr|dn|}|dkrtj|dd}n6|dkr4tj|ddjtj|ddj }n!|dkrAtj|ddd}n|d	krNtj|d
dd}nt	d| |||fS )a[  Updates and returns the sum of squared errors and the number of observations for NRMSE computation.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        num_outputs: Number of outputs in multioutput setting
        normalization: type of normalization to be applied. Choose from "mean", "range", "std", "l2"

       r   r   )dimr   r   )
correctionr   r      )pr   zQArgument `normalization` should be either 'mean', 'range', 'std' or 'l2' but got )
r   viewtorchr   maxvaluesminr   norm
ValueError)r   r   r	   r
   sum_squared_errornum_obsdenom r   e/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/regression/nrmse.py*_normalized_root_mean_squared_error_update   s   "
r!   r   r   r   c                 C   s   t | | }|| S )z"Calculates RMSE and normalizes it.)r   sqrt)r   r   r   rmser   r   r    +_normalized_root_mean_squared_error_compute5   s   r$   r   c                 C   s"   t | |||d\}}}t|||S )af  Calculates the `Normalized Root Mean Squared Error`_ (NRMSE) also know as scatter index.

    Args:
        preds: estimated labels
        target: ground truth labels
        normalization: type of normalization to be applied. Choose from "mean", "range", "std", "l2" which corresponds
          to normalizing the RMSE by the mean of the target, the range of the target, the standard deviation of the
          target or the L2 norm of the target.
        num_outputs: Number of outputs in multioutput setting

    Return:
        Tensor with the NRMSE score

    Example:
        >>> import torch
        >>> from torchmetrics.functional.regression import normalized_root_mean_squared_error
        >>> preds = torch.tensor([0., 1, 2, 3])
        >>> target = torch.tensor([0., 1, 2, 2])
        >>> normalized_root_mean_squared_error(preds, target, normalization="mean")
        tensor(0.4000)
        >>> normalized_root_mean_squared_error(preds, target, normalization="range")
        tensor(0.2500)
        >>> normalized_root_mean_squared_error(preds, target, normalization="std")
        tensor(0.6030)
        >>> normalized_root_mean_squared_error(preds, target, normalization="l2")
        tensor(0.1667)

    Example (multioutput):
        >>> import torch
        >>> from torchmetrics.functional.regression import normalized_root_mean_squared_error
        >>> preds = torch.tensor([[0., 1], [2, 3], [4, 5], [6, 7]])
        >>> target = torch.tensor([[0., 1], [3, 3], [4, 5], [8, 9]])
        >>> normalized_root_mean_squared_error(preds, target, normalization="mean", num_outputs=2)
        tensor([0.2981, 0.2222])

    )r	   r
   )r!   r$   )r   r   r
   r	   r   r   r   r   r   r    "normalized_root_mean_squared_error=   s   *r%   )r   )r   r   )typingr   r   r   typing_extensionsr   &torchmetrics.functional.regression.mser   inttupler!   r$   r%   r   r   r   r    <module>   sN   


