o
    .wi                     @   s   d dl mZ d dlmZmZmZmZ d dlZd dlmZm	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 d d
lmZmZ esKdgZG dd deZdS )    )Sequence)AnyListOptionalUnionN)Tensortensor)Literal)_total_variation_compute_total_variation_update)Metric)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPETotalVariation.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	< ee ed
< eed< ddeed  deddf fddZ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 )TotalVariationa  Compute Total Variation loss (`TV`_).

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

    - ``img`` (:class:`~torch.Tensor`): A tensor of shape ``(N, C, H, W)`` consisting of images

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

    - ``sdi`` (:class:`~torch.Tensor`): if ``reduction!='none'`` returns float scalar tensor with average TV value
      over sample else returns tensor of shape ``(N,)`` with TV values per sample

    Args:
        reduction: a method to reduce metric score over samples

            - ``'mean'``: takes the mean over samples
            - ``'sum'``: takes the sum over samples
            - ``None`` or ``'none'``: return the score per sample

        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        ValueError:
            If ``reduction`` is not one of ``'sum'``, ``'mean'``, ``'none'`` or ``None``

    Example:
        >>> from torch import rand
        >>> from torchmetrics.image import TotalVariation
        >>> tv = TotalVariation()
        >>> img = torch.rand(5, 3, 28, 28)
        >>> tv(img)
        tensor(7546.8018)

    Ffull_state_updateTis_differentiablehigher_is_betterg        plot_lower_boundnum_elements
score_listscoresum	reduction)meanr   nonekwargsreturnNc                    sx   t  jdi | |d ur|dvrtd|| _| jdg dd | jdtdtjdd	d | jd
tdtjdd	d d S )N)r   r   r   zHExpected argument `reduction` to either be 'sum', 'mean', 'none' or Noner   cat)defaultdist_reduce_fxr   r   )dtyper   r    )	super__init__
ValueErrorr   	add_stater   torchfloatint)selfr   r   	__class__r$   R/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/image/tv.pyr&   K   s   zTotalVariation.__init__imgc                 C   sR   t |\}}| jdu s| jdkr| j| n	|  j| 7  _|  j|7  _dS )z0Update current score with batch of input images.Nr   )r   r   r   appendr   r   r   )r,   r0   r   r   r$   r$   r/   updateU   s
   zTotalVariation.updatec                 C   s4   | j du s
| j dkrt| jn| j}t|| j| j S )zCompute final total variation.Nr   )r   r   r   r   r
   r   )r,   r   r$   r$   r/   compute^   s   $zTotalVariation.computevalaxc                 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 TotalVariation
            >>> metric = TotalVariation()
            >>> metric.update(torch.rand(5, 3, 28, 28))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

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

        )_plot)r,   r4   r5   r$   r$   r/   plotc   s   (r   )r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r*   r   r   r   r	   r   r&   r2   r3   r   r   r   r   r7   __classcell__r$   r$   r-   r/   r      s(   
 "$
	r   )collections.abcr   typingr   r   r   r   r)   r   r   typing_extensionsr	    torchmetrics.functional.image.tvr
   r   torchmetrics.metricr   torchmetrics.utilities.datar   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   __doctest_skip__r   r$   r$   r$   r/   <module>   s   