a
    d-                     @   s   d dl mZmZ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srdgZG d	d
 d
eZdS )    )AnyDictOptionalSequenceUnionN)Tensor)_rmse_sw_compute_rmse_sw_update)Metric)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPE+RootMeanSquaredErrorUsingSlidingWindow.plotc                       s   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eeeef d
d fddZeed
dddZee dddZdeeeee f  ee edddZ  ZS )&RootMeanSquaredErrorUsingSlidingWindowaO  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 torchmetrics.image import RootMeanSquaredErrorUsingSlidingWindow
        >>> g = torch.manual_seed(22)
        >>> preds = torch.rand(4, 3, 16, 16)
        >>> target = torch.rand(4, 3, 16, 16)
        >>> rmse_sw = RootMeanSquaredErrorUsingSlidingWindow()
        >>> rmse_sw(preds, target)
        tensor(0.3999)

    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                    sl   t  jf i | t|tr.t|tr6|dk r6td|| _| jdtddd | jdtddd d S )N   z<Argument `window_size` is expected to be a positive integer.r   r   sum)defaultZdist_reduce_fxr   )	super__init__
isinstanceint
ValueErrorr   Z	add_statetorchZtensor)selfr   r   	__class__ c/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/image/rmse_sw.pyr    D   s    z/RootMeanSquaredErrorUsingSlidingWindow.__init__)predstargetr   c                 C   sX   | j du r.|jdd }tj||j|jd| _ t||| j| j| j | j	\| _| _ | _	dS )z*Update state with predictions and targets.Nr   )dtypedevice)
r   shaper$   zerosr,   r-   r	   r   r   r   )r%   r*   r+   Z
_img_shaper(   r(   r)   updateQ   s    
z-RootMeanSquaredErrorUsingSlidingWindow.update)r   c                 C   s(   | j dusJ t| j| j | j\}}|S )zWCompute Root Mean Squared Error (using sliding window) and potentially return RMSE map.N)r   r   r   r   )r%   Zrmse_r(   r(   r)   compute[   s    z.RootMeanSquaredErrorUsingSlidingWindow.compute)valaxr   c                 C   s   |  ||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)

        )Z_plot)r%   r3   r4   r(   r(   r)   plota   s    (r   )r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r"   r   strr   r    r0   r2   r   r   r   r   r5   __classcell__r(   r(   r&   r)   r      s*   
 

 r   )typingr   r   r   r   r   r$   r   Z%torchmetrics.functional.image.rmse_swr   r	   Ztorchmetrics.metricr
   Ztorchmetrics.utilities.importsr   Ztorchmetrics.utilities.plotr   r   Z__doctest_skip__r   r(   r(   r(   r)   <module>   s   