
    i%                         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sdgZesddgZ G d de      Zy)    )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_TYPEzQualityWithNoReference.plotQualityWithNoReferencec                   8    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Zdedeeef   ddfdZdefdZ	 d deeeee   f      dee   defdZ x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alphabeta
norm_orderwindow_size	reductionelementwise_meansumnonekwargsreturnNc                    t        |   di | t        d       t        |t        t
        f      r|dk  rt        d| d      || _        t        |t        t
        f      r|dk  rt        d| d      || _        t        |t              r|dk  rt        d| d      || _	        t        |t              r|dk  rt        d| d      || _
        d}||vrt        d	| d
|       || _        | j                  dg d       | j                  dg d       | j                  dg d       | j                  dg d       y )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__s	           k/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/image/qnr.pyr2   zQualityWithNoReference.__init__b   sg    	"6"K	

 %#u.%!)]^c]ddefgg
$e-[\`[aabcdd	*c*jAo_`j_kklmnn$+s+{a/?abmannopqq&@..GHZG[[dendopqq"w5AtR>ub?xEB    targetc                    d|vrt        d|j                          d      d|vrt        d|j                          d      |d   }|d   }|j                  d      }t        ||      \  }}t	        ||||      \  }}}}| j
                  j                  |       | j                  j                  |d          | j                  j                  |d          d|v r| j                  j                  |d          yy)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   s         r;   updatezQualityWithNoReference.update   s      vOPVP[P[P]^_`aaPQWQ\Q\Q^P__`abbD\UmH%5eR@	r!A%SRX!Yr3

% vd|$u&vKKvh/0 r<   c           	         t        | j                        }t        | j                        }t        | j                        }t	        | j
                        dkD  rt        | j
                        nd}t        ||| j                  | j                        }t        ||||| j                  | j                  | j                        }d|z
  | j                  z  d|z
  | j                  z  z  S )z.Compute and returns quality with no reference.r   N   )r   r   r   r   lenr   r
   r"   r$   r   r#   r    r!   )r8   r   r   r   r   d_lambdad_ss          r;   computezQualityWithNoReference.compute   s    TZZ($''"488$.1$++.>.Bdkk*5eRRVR`R`a/2sFDOOT5E5Et~~
 H+q3w499.DDDr<   valaxc                 &    | j                  ||      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   rI   rJ   s      r;   plotzQualityWithNoReference.plot   s    d zz#r""r<   )rD   rD   rD      r&   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r5   r   r   r   r4   r	   r   r2   dictstrrB   rH   r   r   r   r   r   rM   __classcell__)r:   s   @r;   r   r   $   sB   0d "d!"t"#t#!e!!e!<V	fL @R"C"C "C 	"C
 "C <="C "C 
"CH1F 1Df,= 1$ 1>
E 
E _c2#E&(6*:":;<2#IQRZI[2#	2#r<   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   r<   r;   <module>rd      sT    % - -  % x q & 1 4 X @5602OP#V #r<   