
    i5                         d dl mZ d dlmZmZmZ d dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZ esd	gZ G d
 de      Zy)    )Sequence)AnyOptionalUnionN)Tensor)_rmse_sw_compute_rmse_sw_update)Metric)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEz+RootMeanSquaredErrorUsingSlidingWindow.plotc                        e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   eed	<   d
Zee   ed<   eed<   	 ddedeeef   dd
f fdZdededd
fdZdee   fdZ	 ddeeeee   f      dee   defdZ xZS )&RootMeanSquaredErrorUsingSlidingWindowa@  Computes Root Mean Squared Error (RMSE) using sliding window.

    As input to ``forward`` and ``update`` the metric accepts the following input

    - ``preds`` (:class:`~torch.Tensor`): Predictions from model of shape ``(N,C,H,W)``
    - ``target`` (:class:`~torch.Tensor`): Ground truth values of shape ``(N,C,H,W)``

    As output of `forward` and `compute` the metric returns the following output

    - ``rmse_sw`` (:class:`~torch.Tensor`): returns float scalar tensor with average RMSE-SW value over sample

    Args:
        window_size: Sliding window used for rmse calculation
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

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

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

    Fhigher_is_betterTis_differentiablefull_state_update        plot_lower_boundrmse_val_sumNrmse_maptotal_imageswindow_sizekwargsreturnc                 .   t        |   di | t        |t              rt        |t              r|dk  rt	        d      || _        | j                  dt        j                  d      d       | j                  dt        j                  d      d       y )	N   z<Argument `window_size` is expected to be a positive integer.r   r   sum)defaultdist_reduce_fxr    )	super__init__
isinstanceint
ValueErrorr   	add_statetorchtensor)selfr   r   	__class__s      o/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/image/rmse_sw.pyr"   z/RootMeanSquaredErrorUsingSlidingWindow.__init__E   s}    
 	"6"+s+
;0LQ\_`Q`[\\&~u||C/@QVW~u||C/@QVW    predstargetc                 2   | j                   @|j                  dd }t        j                  ||j                  |j
                        | _         t        ||| j                  | j                  | j                   | j                        \  | _        | _         | _	        y)z*Update state with predictions and targets.Nr   )dtypedevice)
r   shaper'   zerosr0   r1   r	   r   r   r   )r)   r-   r.   
_img_shapes       r+   updatez-RootMeanSquaredErrorUsingSlidingWindow.updateR   st    == ab)J!KK
&,,v}}]DM>M64++T->->tO`O`?
;4=$*;r,   c                 ~    | j                   J t        | j                  | j                   | j                        \  }}|S )zWCompute Root Mean Squared Error (using sliding window) and potentially return RMSE map.)r   r   r   r   )r)   rmse_s      r+   computez.RootMeanSquaredErrorUsingSlidingWindow.compute\   s9    }}((("4#4#4dmmTEVEVWar,   valaxc                 &    | j                  ||      S )a  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> import torch
            >>> from torchmetrics.image import RootMeanSquaredErrorUsingSlidingWindow
            >>> metric = RootMeanSquaredErrorUsingSlidingWindow()
            >>> metric.update(torch.rand(4, 3, 16, 16), torch.rand(4, 3, 16, 16))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.image import RootMeanSquaredErrorUsingSlidingWindow
            >>> metric = RootMeanSquaredErrorUsingSlidingWindow()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.rand(4, 3, 16, 16), torch.rand(4, 3, 16, 16)))
            >>> fig_, ax_ = metric.plot(values)

        )_plot)r)   r:   r;   s      r+   plotz+RootMeanSquaredErrorUsingSlidingWindow.plotb   s    P zz#r""r,   )   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r$   dictstrr   r"   r5   r9   r   r   r   r   r>   __classcell__)r*   s   @r+   r   r      s    : #d""t"#t#!e!!%Hhv% XX sCx.X 
	X
F 
F 
t 
&)  _c(#E&(6*:":;<(#IQRZI[(#	(#r,   r   )collections.abcr   typingr   r   r   r'   r   %torchmetrics.functional.image.rmse_swr   r	   torchmetrics.metricr
   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   __doctest_skip__r   r    r,   r+   <module>rQ      s<    % ' '   S & @ @EFl#V l#r,   