o
    .wi                     @   s   d dl mZ d dlmZmZmZm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 d d
lmZmZ esMdgZG dd deZdS )    )Sequence)AnyListOptionalUnion)Tensortensor)Literal)_uqi_compute_uqi_update)Metric)rank_zero_warn)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEUniversalImageQualityIndex.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< d	Ze
ed
< ee ed< ee ed< eed< eed< 			d#dee dee
 ded de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 )%UniversalImageQualityIndexa  Compute Universal Image Quality Index (UniversalImageQualityIndex_).

    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

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

    Args:
        kernel_size: size of the gaussian kernel
        sigma: Standard deviation of the gaussian kernel
        reduction: a method to reduce metric score over labels.

            - ``'elementwise_mean'``: takes the mean (default)
            - ``'sum'``: takes the sum
            - ``'none'`` or ``None``: no reduction will be applied

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

    Return:
        Tensor with UniversalImageQualityIndex score

    Example:
        >>> import torch
        >>> from torchmetrics.image import UniversalImageQualityIndex
        >>> preds = torch.rand([16, 1, 16, 16])
        >>> target = preds * 0.75
        >>> uqi = UniversalImageQualityIndex()
        >>> uqi(preds, target)
        tensor(0.9216)

    Tis_differentiablehigher_is_betterFfull_state_update        plot_lower_boundg      ?plot_upper_boundpredstargetsum_uqinumel   r         ?r!   elementwise_meankernel_sizesigma	reductionr"   sumnoneNkwargsreturnNc                    s   t  jdi | |dvrtd| d|d u s|dkr2td | jdg dd | jd	g dd n| jd
tddd | jdtddd || _|| _|| _d S )Nr&   zThe `reduction` zI is not valid. Valid options are `elementwise_mean`, `sum`, `none`, None.r(   zMetric `UniversalImageQualityIndex` will save all targets and predictions in the buffer when using`reduction=None` or `reduction='none'. For large datasets, this may lead to a large memory footprint.r   cat)defaultdist_reduce_fxr   r   r   r'   )r-   r   r    )	super__init__
ValueErrorr   	add_stater   r#   r$   r%   )selfr#   r$   r%   r)   	__class__r.   S/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/image/uqi.pyr0   P   s    

z#UniversalImageQualityIndex.__init__c                 C   s   t ||\}}| jdu s| jdkr| j| | j| dS t||| j| jdd}|  j|7  _|j	}|  j
|d |d  |d | jd  d  |d | jd  d  7  _
dS )	z*Update state with predictions and targets.Nr(   r'   )r%   r            )r   r%   r   appendr   r
   r#   r$   r   shaper   )r3   r   r   	uqi_scorepsr.   r.   r6   updatej   s   Jz!UniversalImageQualityIndex.updatec                 C   sZ   | j dks
| j du rt| j}t| j}t||| j| j| j S | j dkr*| j| j S | jS )z&Compute explained variance over state.r(   Nr"   )	r%   r   r   r   r
   r#   r$   r   r   )r3   r   r   r.   r.   r6   computev   s
   

z"UniversalImageQualityIndex.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 UniversalImageQualityIndex
            >>> preds = torch.rand([16, 1, 16, 16])
            >>> target = preds * 0.75
            >>> metric = UniversalImageQualityIndex()
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.image import UniversalImageQualityIndex
            >>> preds = torch.rand([16, 1, 16, 16])
            >>> target = preds * 0.75
            >>> metric = UniversalImageQualityIndex()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(preds, target))
            >>> fig_, ax_ = metric.plot(values)

        )_plot)r3   r@   rA   r.   r.   r6   plot~   s   ,r   )r   r    r"   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   floatr   r   r   r   intr	   r   r0   r>   r?   r   r   r   r   rC   __classcell__r.   r.   r4   r6   r      sF   
 %	r   N)collections.abcr   typingr   r   r   r   torchr   r   typing_extensionsr	   !torchmetrics.functional.image.uqir
   r   torchmetrics.metricr   torchmetrics.utilitiesr   torchmetrics.utilities.datar   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   __doctest_skip__r   r.   r.   r.   r6   <module>   s   