o
    .wi                     @   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edede	d	ee d
ee dee de
eeef fddZd	ee d
edede
ee ef fddZ	ddedede	dedeee e
ee ef f f
ddZdS )    )OptionalUnionN)Tensor)_uniform_filter)_check_same_shapepredstargetwindow_sizermse_val_sumrmse_maptotal_imagesreturnc           
      C   s  | j |j krtd| j  d|j  dt| | t| jdkr(td| j dt|d |jd ks>t|d |jd krVtdt|jd |jd  d	t|d  d|d
urb||jd 7 }ntj	|jd |j
d}||  d }t||}t|}t|d }|d
ur|d
d
d
d
|| || f }	||	d 7 }n|d
d
d
d
|| || f d }|d
ur||d7 }n|d}|||fS )a  Calculate the sum of RMSE values and RMSE map for the batch of examples and update intermediate states.

    Args:
        preds: Deformed image
        target: Ground truth image
        window_size: Sliding window used for rmse calculation
        rmse_val_sum: Sum of RMSE over all examples per individual channels
        rmse_map: Sum of RMSE map values over all examples
        total_images: Total number of images

    Return:
        (Optionally) Intermediate state of RMSE (using sliding window) over the accumulated examples.
        (Optionally) Intermediate state of RMSE map
        Updated total number of already processed images

    Raises:
        ValueError: If ``preds`` and ``target`` do not have the same data type.
        ValueError: If ``preds`` and ``target`` do not have ``BxCxWxH`` shape.
        ValueError: If ``round(window_size / 2)`` is greater or equal to width or height of the image.

    zBExpected `preds` and `target` to have the same data type. But got z and .   z=Expected `preds` and `target` to have BxCxHxW shape. But got       zBParameter `round(window_size / 2)` is expected to be smaller than z	 but got Nr   )device)dtype	TypeErrorr   lenshape
ValueErrorroundmintorchtensorr   r   sqrtsummean)
r   r   r	   r
   r   r   error	_rmse_map
crop_slidermse_val r#   b/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/image/rmse_sw.py_rmse_sw_update   s8   
,


$.

r%   c                 C   s,   | dur| | nd}|dur|| }||fS )a\  Compute RMSE from the aggregated RMSE value. Optionally also computes the mean value for RMSE map.

    Args:
        rmse_val_sum: Sum of RMSE over all examples
        rmse_map: Sum of RMSE map values over all examples
        total_images: Total number of images

    Return:
        RMSE using sliding window
        (Optionally) RMSE map

    Nr#   )r
   r   r   rmser#   r#   r$   _rmse_sw_computeZ   s   r'      Freturn_rmse_mapc                 C   s^   t |trt |tr|dk rtdt| ||dddd\}}}t|||\}}|r-||fS |S )a	  Compute Root Mean Squared Error (RMSE) using sliding window.

    Args:
        preds: Deformed image
        target: Ground truth image
        window_size: Sliding window used for rmse calculation
        return_rmse_map: An indication whether the full rmse reduced image should be returned.

    Return:
        RMSE using sliding window
        (Optionally) RMSE map

    Example:
        >>> from torch import rand
        >>> from torchmetrics.functional.image import root_mean_squared_error_using_sliding_window
        >>> preds = rand(4, 3, 16, 16)
        >>> target = rand(4, 3, 16, 16)
        >>> root_mean_squared_error_using_sliding_window(preds, target)
        tensor(0.4158)

    Raises:
        ValueError: If ``window_size`` is not a positive integer.

       z<Argument `window_size` is expected to be a positive integer.N)r
   r   r   )
isinstanceintr   r%   r'   )r   r   r	   r)   r
   r   r   r&   r#   r#   r$   ,root_mean_squared_error_using_sliding_windowp   s   r-   )r(   F)typingr   r   r   r   #torchmetrics.functional.image.utilsr   torchmetrics.utilities.checksr   r,   tupler%   r'   boolr-   r#   r#   r#   r$   <module>   sR   
B
