o
    ÐôjQ  ã                   @   sŽ   d dl mZ d dlmZmZmZmZ d dlmZ d dl	m
Z
 d dl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„ deƒZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnion)ÚTensor)Úrelative_average_spectral_error)ÚMetric)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEú!RelativeAverageSpectralError.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 ed	< ee ed
< 	ddedeeef ddf‡ fdd„Zd	ed
eddfdd„Zdefdd„Z	ddeeeee f  dee defdd„Z‡  ZS )ÚRelativeAverageSpectralErrora+  Computes Relative Average Spectral Error (RASE) (RelativeAverageSpectralError_).

    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

    - ``rase`` (:class:`~torch.Tensor`): returns float scalar tensor with average RASE value over sample

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

    Return:
        Relative Average Spectral Error (RASE)

    Example:
        >>> from torch import rand
        >>> preds = rand(4, 3, 16, 16)
        >>> target = rand(4, 3, 16, 16)
        >>> rase = RelativeAverageSpectralError()
        >>> rase(preds, target)
        tensor(5326.40...)

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

    FÚhigher_is_betterTÚis_differentiableÚfull_state_updateg        Úplot_lower_boundÚpredsÚtargeté   Úwindow_sizeÚkwargsÚreturnNc                    sf   t ƒ jdi |¤Ž t|tƒrt|tƒr|dk rtd|› �ƒ‚|| _| jdg dd� | jdg dd� d S )Né   zEArgument `window_size` is expected to be a positive integer, but got r   Úcat)ÚdefaultÚdist_reduce_fxr   © )ÚsuperÚ__init__Ú
isinstanceÚintÚ
ValueErrorr   Ú	add_state)Úselfr   r   ©Ú	__class__r   új/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torchmetrics/image/rase.pyr    F   s   z%RelativeAverageSpectralError.__init__c                 C   s   | j  |¡ | j |¡ dS )z*Update state with predictions and targets.N)r   Úappendr   ©r%   r   r   r   r   r(   ÚupdateT   s   z#RelativeAverageSpectralError.updatec                 C   s"   t | jƒ}t | jƒ}t||| jƒS )z/Compute Relative Average Spectral Error (RASE).)r
   r   r   r   r   r*   r   r   r(   ÚcomputeY   s   

z$RelativeAverageSpectralError.computeÚvalÚaxc                 C   s   |   ||¡S )aX  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 RelativeAverageSpectralError
            >>> metric = RelativeAverageSpectralError()
            >>> 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
            >>> from torch import rand
            >>> from torchmetrics.image import RelativeAverageSpectralError
            >>> metric = RelativeAverageSpectralError()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(rand(4, 3, 16, 16), rand(4, 3, 16, 16)))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)r%   r-   r.   r   r   r(   Úplot_   s   (r   )r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   Úfloatr   r   r"   ÚdictÚstrr   r    r+   r,   r   r   r   r   r   r0   Ú__classcell__r   r   r&   r(   r      s4   
 þþ
ýüÿÿÿþr   N)Úcollections.abcr   Útypingr   r   r   r   Útorchr   Ú"torchmetrics.functional.image.raser   Útorchmetrics.metricr	   Útorchmetrics.utilities.datar
   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r   r   r   r(   Ú<module>   s   