o
    .wi%                     @   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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 d dlmZmZ esUdgZes[ddgZG dd deZdS )    )Sequence)AnyListOptionalUnion)Tensor)Literal)"_spectral_distortion_index_compute!_spectral_distortion_index_update)!_spatial_distortion_index_compute _spatial_distortion_index_update)Metric)rank_zero_warn)dim_zero_cat)_MATPLOTLIB_AVAILABLE_TORCHVISION_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEQualityWithNoReference.plotQualityWithNoReferencec                       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 ed< ee ed< 					d&de
de
dededed deddf fddZdedeeef 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 )(r   a  Compute Quality with No Reference (QualityWithNoReference_) also now as QNR.

    The metric is used to compare the joint spectral and spatial distortion between two images.

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

    - ``preds`` (:class:`~torch.Tensor`): High resolution multispectral image of shape ``(N,C,H,W)``.
    - ``target`` (:class:`~Dict`): A dictionary containing the following keys:

      - ``ms`` (:class:`~torch.Tensor`): Low resolution multispectral image of shape ``(N,C,H',W')``.
      - ``pan`` (:class:`~torch.Tensor`): High resolution panchromatic image of shape ``(N,C,H,W)``.
      - ``pan_lr`` (:class:`~torch.Tensor`): (optional) Low resolution panchromatic image of shape ``(N,C,H',W')``.

    where H and W must be multiple of H' and W'.

    When ``pan_lr`` is ``None``, a uniform filter will be applied on ``pan`` to produce a degraded image. The degraded
    image is then resized to match the size of ``ms`` and served as ``pan_lr`` in the calculation.

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

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

    Args:
        alpha: Relevance of spectral distortion.
        beta: Relevance of spatial distortion.
        norm_order: Order of the norm applied on the difference.
        window_size: Window size of the filter applied to degrade the high resolution panchromatic image.
        reduction: a method to reduce metric score over labels.

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

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

    Example:
        >>> from torch import rand
        >>> from torchmetrics.image import QualityWithNoReference
        >>> preds = rand([16, 3, 32, 32])
        >>> target = {
        ...     'ms': rand([16, 3, 16, 16]),
        ...     'pan': rand([16, 3, 32, 32]),
        ... }
        >>> qnr = QualityWithNoReference()
        >>> qnr(preds, target)
        tensor(0.9694)

    Thigher_is_betteris_differentiableFfull_state_updateg        plot_lower_boundg      ?plot_upper_boundpredsmspanpan_lr      elementwise_meanalphabeta
norm_orderwindow_size	reductionr!   sumnonekwargsreturnNc                    s,  t  jdi | td t|ttfr|dk r td| d|| _t|ttfr.|dk r6td| d|| _t|trB|dkrJtd| d|| _	t|trV|dkr^td| d|| _
d}||vrqtd	| d
| || _| jdg dd | jdg dd | jdg dd | jdg dd d S )NzMetric `QualityWithNoReference` will save all targets and predictions in buffer. For large datasets this may lead to large memory footprint.r   z>Expected `alpha` to be a non-negative real number. Got alpha: .z<Expected `beta` to be a non-negative real number. Got beta: z@Expected `norm_order` to be a positive integer. Got norm_order: zBExpected `window_size` to be a positive integer. Got window_size: r'   z(Expected argument `reduction` be one of z	 but got r   cat)defaultdist_reduce_fxr   r   r    )super__init__r   
isinstanceintfloat
ValueErrorr"   r#   r$   r%   r&   	add_state)selfr"   r#   r$   r%   r&   r*   allowed_reductions	__class__r0   S/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/image/qnr.pyr2   b   s0   	zQualityWithNoReference.__init__targetc                 C   s   d|vrt d|  dd|vrt d|  d|d }|d }|d}t||\}}t||||\}}}}| j| | j|d  | j|d  d|v r_| j	|d  dS dS )a  Update state with preds and target.

        Args:
            preds: High resolution multispectral image.
            target: A dictionary containing the following keys:

                - ``'ms'``: low resolution multispectral image.
                - ``'pan'``: high resolution panchromatic image.
                - ``'pan_lr'``: (optional) low resolution panchromatic image.

        Raises:
            ValueError:
                If ``target`` doesn't have ``ms`` and ``pan``.

        r   z0Expected `target` to have key `ms`. Got target: r,   r   z1Expected `target` to have key `pan`. Got target: r   N)
r6   keysgetr
   r   r   appendr   r   r   )r8   r   r=   r   r   r   r0   r0   r<   update   s   
zQualityWithNoReference.updatec                 C   s   t | j}t | j}t | j}t| jdkrt | jnd}t||| j| j}t	||||| j| j
| j}d| | j d| | j  S )z.Compute and returns quality with no reference.r   Nr   )r   r   r   r   lenr   r	   r$   r&   r   r%   r"   r#   )r8   r   r   r   r   d_lambdad_sr0   r0   r<   compute   s   


zQualityWithNoReference.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
            >>> from torch import rand
            >>> from torchmetrics.image import QualityWithNoReference
            >>> preds = rand([16, 3, 32, 32])
            >>> target = {
            ...     'ms': rand([16, 3, 16, 16]),
            ...     'pan': rand([16, 3, 32, 32]),
            ... }
            >>> metric = QualityWithNoReference()
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> from torch import rand
            >>> from torchmetrics.image import QualityWithNoReference
            >>> preds = rand([16, 3, 32, 32])
            >>> target = {
            ...     'ms': rand([16, 3, 16, 16]),
            ...     'pan': rand([16, 3, 32, 32]),
            ... }
            >>> metric = QualityWithNoReference()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(preds, target))
            >>> fig_, ax_ = metric.plot(values)

        )_plot)r8   rF   rG   r0   r0   r<   plot   s   2r   )r   r   r   r    r!   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r5   r   r   r   r4   r   r   r2   dictstrrA   rE   r   r   r   r   r   rI   __classcell__r0   r0   r:   r<   r   $   sR   
 2$N)collections.abcr   typingr   r   r   r   torchr   typing_extensionsr   &torchmetrics.functional.image.d_lambdar	   r
   !torchmetrics.functional.image.d_sr   r   torchmetrics.metricr   torchmetrics.utilitiesr   torchmetrics.utilities.datar   torchmetrics.utilities.importsr   r   torchmetrics.utilities.plotr   r   __doctest_skip__r   r0   r0   r0   r<   <module>   s    