o
    yi                     @   sx   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
mZ d dlmZ d dlmZ d dlmZ G dd	 d	eZd
S )    )AnyListOptionalSequence)Tensor)Literal)_uqi_compute_uqi_update)Metric)rank_zero_warn)dim_zero_catc                       s   e Zd ZU dZ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e dee ded dee 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  ZS )UniversalImageQualityIndexa  Computes 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

        data_range: Range of the image. If ``None``, it is determined from the image (max - min)
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.


    Return:
        Tensor with UniversalImageQualityIndex score

    Example:
        >>> import torch
        >>> from torchmetrics 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predstarget   r         ?r   elementwise_meanNkernel_sizesigma	reduction)r   sumnoneN
data_rangekwargsreturnc                    sV   t  jdi | td | jdg dd | jdg dd || _|| _|| _|| _d S )NzMetric `UniversalImageQualityIndex` will save all targets and predictions in buffer. For large datasets this may lead to large memory footprint.r   cat)defaultdist_reduce_fxr    )super__init__r   	add_stater   r   r   r   )selfr   r   r   r   r   	__class__r#   J/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/image/uqi.pyr%   G   s   
z#UniversalImageQualityIndex.__init__c                 C   s*   t ||\}}| j| | j| dS )z*Update state with predictions and targets.N)r	   r   appendr   r'   r   r   r#   r#   r*   update]   s   z!UniversalImageQualityIndex.updatec                 C   s.   t | j}t | j}t||| j| j| j| jS )z'Computes explained variance over state.)r   r   r   r   r   r   r   r   r,   r#   r#   r*   computec   s   

z"UniversalImageQualityIndex.compute)r   r   r   N)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r   r   intfloatr   r   r   r%   r-   r.   __classcell__r#   r#   r(   r*   r      s4   
 &r   N)typingr   r   r   r   torchr   typing_extensionsr   !torchmetrics.functional.image.uqir   r	   torchmetrics.metricr
   torchmetrics.utilitiesr   torchmetrics.utilities.datar   r   r#   r#   r#   r*   <module>   s   