§
    ’©ºjW+  ã                   óB  — d dl mZ d dl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 esdd	gZ	 dd
edededee         deeeeee         f         f
d„Z	 	 	 	 dd
edededee         dededed         defd„Z	 	 	 	 dd
edededee         dededed         defd„ZdS )é    )ÚOptionalN)ÚTensor)ÚLiteral)Úuniversal_image_quality_index)Úreduce)Ú_TORCHVISION_AVAILABLEÚ!_spatial_distortion_index_computeÚspatial_distortion_indexÚpredsÚmsÚpanÚpan_lrÚreturnc                 ó¢  — t          | j        ¦  «        dk    rt          d| j        › d�¦  «        ‚| j        |j        k    r t	          d| j        › d|j        › d�¦  «        ‚| j        |j        k    r t	          d| j        › d|j        › d�¦  «        ‚|�0| j        |j        k    r t	          d	| j        › d
|j        › d�¦  «        ‚t          |j        ¦  «        dk    rt          d|j        › d�¦  «        ‚t          |j        ¦  «        dk    rt          d|j        › d�¦  «        ‚|�0t          |j        ¦  «        dk    rt          d|j        › d�¦  «        ‚| j        dd…         |j        dd…         k    r t          d| j        › d|j        › d�¦  «        ‚| j        dd…         |j        dd…         k    r t          d| j        › d|j        › d�¦  «        ‚|�@| j        dd…         |j        dd…         k    r t          d| j        › d
|j        › d�¦  «        ‚| j        dd…         \  }}|j        dd…         \  }}|j        dd…         \  }}	||k    rt          d|› d|› �¦  «        ‚||	k    rt          d|› d|	› �¦  «        ‚||z  dk    rt          d|› d|› d�¦  «        ‚||z  dk    rt          d|› d|› d�¦  «        ‚||z  dk    rt          d|› d|› d�¦  «        ‚|	|z  dk    rt          d|	› d|› d�¦  «        ‚|�J|j        dd…         \  }
}|
|k    rt          d|› d
|
› d�¦  «        ‚||k    rt          d|› d
|› d�¦  «        ‚| |||fS )a  Update and returns variables required to compute Spatial Distortion Index.

    Args:
        preds: High resolution multispectral image.
        ms: Low resolution multispectral image.
        pan: High resolution panchromatic image.
        pan_lr: Low resolution panchromatic image.

    Return:
        A tuple of Tensors containing ``preds``, ``ms``, ``pan`` and ``pan_lr``.

    Raises:
        TypeError:
            If ``preds``, ``ms``, ``pan`` and ``pan_lr`` don't have the same data type.
        ValueError:
            If ``preds``, ``ms``, ``pan`` and ``pan_lr`` don't have ``BxCxHxW shape``.
        ValueError:
            If ``preds``, ``ms``, ``pan`` and ``pan_lr`` don't have the same batch and channel sizes.
        ValueError:
            If ``preds`` and ``pan`` don't have the same dimension.
        ValueError:
            If ``ms`` and ``pan_lr`` don't have the same dimension.
        ValueError:
            If ``preds`` and ``pan`` don't have dimension which is multiple of that of ``ms``.

    é   z3Expected `preds` to have BxCxHxW shape. Got preds: ú.zAExpected `preds` and `ms` to have the same data type. Got preds: z	 and ms: zBExpected `preds` and `pan` to have the same data type. Got preds: z
 and pan: NzEExpected `preds` and `pan_lr` to have the same data type. Got preds: z and pan_lr: z-Expected `ms` to have BxCxHxW shape. Got ms: z/Expected `pan` to have BxCxHxW shape. Got pan: z5Expected `pan_lr` to have BxCxHxW shape. Got pan_lr: é   zOExpected `preds` and `ms` to have the same batch and channel sizes. Got preds: zPExpected `preds` and `pan` to have the same batch and channel sizes. Got preds: zSExpected `preds` and `pan_lr` to have the same batch and channel sizes. Got preds: éþÿÿÿz?Expected `preds` and `pan` to have the same height. Got preds: z>Expected `preds` and `pan` to have the same width. Got preds: r   zHExpected height of `preds` to be multiple of height of `ms`. Got preds: zFExpected width of `preds` to be multiple of width of `ms`. Got preds: zFExpected height of `pan` to be multiple of height of `ms`. Got preds: zDExpected width of `pan` to be multiple of width of `ms`. Got preds: z<Expected `ms` and `pan_lr` to have the same height. Got ms: z;Expected `ms` and `pan_lr` to have the same width. Got ms: )ÚlenÚshapeÚ
ValueErrorÚdtypeÚ	TypeError)r   r   r   r   Úpreds_hÚpreds_wÚms_hÚms_wÚpan_hÚpan_wÚpan_lr_hÚpan_lr_ws               úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/image/d_s.pyÚ _spatial_distortion_index_updater#      sÍ  € õ: ˆ5Œ;ÑÔ˜1ÒÐÝÐ]ÈuÌ{Ð]Ð]Ð]Ñ^Ô^Ð^Ø„{�b”hÒÐÝØqÐPUÔP[ÐqÐqÐfhÔfnÐqÐqÐqñ
ô 
ð 	
ð „{�c”iÒÐÝØtÐQVÔQ\ÐtÐtÐhkÔhqÐtÐtÐtñ
ô 
ð 	
ð Ð˜eœk¨V¬\Ò9Ð9ÝðEØ œ;ðEð EØ5;´\ðEð Eð Eñ
ô 
ð 	
õ ˆ2Œ8�}„}˜ÒÐÝÐTÈÌÐTÐTÐTÑUÔUÐUÝ
ˆ3Œ9�~„~˜ÒÐÝÐWÈ3Ì9ÐWÐWÐWÑXÔXÐXØÐ�c &¤,Ñ/Ô/°1Ò4Ð4ÝÐ`ÐQWÔQ]Ð`Ð`Ð`ÑaÔaÐaØ„{�2�A�2„˜"œ( 2 A 2œ,Ò&Ð&Ýð=Ø œ;ð=ð =Ø13´ð=ð =ð =ñ
ô 
ð 	
ð „{�2�A�2„˜#œ) B Q Bœ-Ò'Ð'Ýð?Ø œ;ð?ð ?Ø25´)ð?ð ?ð ?ñ
ô 
ð 	
ð Ð˜eœk¨"¨1¨"œo°´¸b¸q¸bÔ1AÒAÐAÝðEØ œ;ðEð EØ5;´\ðEð Eð Eñ
ô 
ð 	
ð
 ”{ 2 3 3Ô'Ñ€GˆWØ”˜"˜#˜#”�J€Dˆ$Ø”9˜R˜S˜S”>�L€Eˆ5Ø�%ÒÐÝÐuÐ[bÐuÐuÐnsÐuÐuÑvÔvÐvØ�%ÒÐÝÐtÐZaÐtÐtÐmrÐtÐtÑuÔuÐuØ��~˜ÒÐÝØpÐW^ÐpÐpÐimÐpÐpÐpñ
ô 
ð 	
ð ��~˜ÒÐÝØnÐU\ÐnÐnÐgkÐnÐnÐnñ
ô 
ð 	
ð ˆt�|�qÒÐÝØlÐUZÐlÐlÐeiÐlÐlÐlñ
ô 
ð 	
ð ˆt�|�qÒÐÝÐwÐ`eÐwÐwÐptÐwÐwÐwÑxÔxÐxàÐØ#œ\¨"¨#¨#Ô.Ñˆ�(Ø�tÒÐÝØmÈtÐmÐmÐbjÐmÐmÐmñô ð ð �tÒÐÝØlÈdÐlÐlÐaiÐlÐlÐlñô ð ð �"�c˜6Ð!Ð!ó    é   é   Úelementwise_meanÚ
norm_orderÚwindow_sizeÚ	reduction)r'   ÚsumÚnonec           	      óÐ  — | j         d         }|j         dd…         \  }}	||k    s||	k    rt          d|› d�¦  «        ‚|€Kt          st          d¦  «        ‚ddlm}
 dd	lm}  |||¬
¦  «        } |
||j         dd…         d¬¦  «        }n|}t          j        || j	        ¬¦  «        }t          j        || j	        ¬¦  «        }t          |¦  «        D ]d}t          |dd…||dz   …f         |dd…||dz   …f         ¦  «        ||<   t          | dd…||dz   …f         |dd…||dz   …f         ¦  «        ||<   Œe||z
                       ¦   «         |z  }t          ||¦  «        d|z  z  S )aœ  Compute Spatial Distortion Index (SpatialDistortionIndex_).

    Args:
        preds: High resolution multispectral image.
        ms: Low resolution multispectral image.
        pan: High resolution panchromatic image.
        pan_lr: Low resolution panchromatic image.
        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

    Return:
        Tensor with SpatialDistortionIndex score

    Raises:
        ValueError
            If ``window_size`` is smaller than dimension of ``ms``.

    Example:
        >>> from torch import rand
        >>> preds = rand([16, 3, 32, 32])
        >>> ms = rand([16, 3, 16, 16])
        >>> pan = rand([16, 3, 32, 32])
        >>> preds, ms, pan, pan_lr = _spatial_distortion_index_update(preds, ms, pan)
        >>> _spatial_distortion_index_compute(preds, ms, pan, pan_lr)
        tensor(0.0090)

    r%   r   NzNExpected `window_size` to be smaller than dimension of `ms`. Got window_size: r   z¿When `pan_lr` is not provided as input to metric Spatial distortion index, torchvision should be installed. Please install with `pip install torchvision` or `pip install torchmetrics[image]`.r   )Úresize)Ú_uniform_filter)r)   F)ÚsizeÚ	antialias)Údevice)r   r   r   Ú!torchvision.transforms.functionalr.   Ú#torchmetrics.functional.image.utilsr/   ÚtorchÚzerosr2   Úranger   Úabsr   )r   r   r   r   r(   r)   r*   Úlengthr   r   r.   r/   Úpan_degradedÚm1Úm2ÚiÚdiffs                    r"   r	   r	   ƒ   sÕ  € ðR Œ[˜Œ^€Fà”˜"˜#˜#”�J€Dˆ$Ø�dÒÐ˜k¨TÒ1Ð1ÝØkÐ]hÐkÐkÐkñ
ô 
ð 	
ð €~Ý%ð 	Ýðqñô ð ð 	=Ð<Ð<Ð<Ð<Ð<àGÐGÐGÐGÐGÐGà&� s¸ÐDÑDÔDˆØ�v˜l°´¸"¸#¸#´È%ÐPÑPÔPˆˆàˆå	Œ�V E¤LÐ	1Ñ	1Ô	1€BÝ	Œ�V E¤LÐ	1Ñ	1Ô	1€Bå�6‰]Œ]ð Vð VˆÝ-¨b°°°°A¸¸A¹°I°Ô.>ÀÈQÈQÈQÐPQÐTUÐXYÑTYÐPYÈ\Ô@ZÑ[Ô[ˆˆ1‰Ý-¨e°A°A°A°q¸1¸q¹5°y°LÔ.AÀ3ÀqÀqÀqÈ!ÈaÐRSÉeÈ)À|ÔCTÑUÔUˆˆ1‰ˆØ�‰G�=Š=‰?Œ?˜jÑ(€DÝ�$˜	Ñ"Ô" q¨:¡~Ñ6Ð6r$   c           	      ó  — t          |t          ¦  «        r|dk    rt          d|› d�¦  «        ‚t          |t          ¦  «        r|dk    rt          d|› d�¦  «        ‚t          | |||¦  «        \  } }}}t	          | ||||||¦  «        S )aœ  Calculate `Spatial Distortion Index`_ (SpatialDistortionIndex_) also known as D_s.

    Metric is used to compare the spatial distortion between two images.

    Args:
        preds: High resolution multispectral image.
        ms: Low resolution multispectral image.
        pan: High resolution panchromatic image.
        pan_lr: Low resolution panchromatic image.
        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

    Return:
        Tensor with SpatialDistortionIndex score

    Raises:
        TypeError:
            If ``preds``, ``ms``, ``pan`` and ``pan_lr`` don't have the same data type.
        ValueError:
            If ``preds``, ``ms``, ``pan`` and ``pan_lr`` don't have ``BxCxHxW shape``.
        ValueError:
            If ``preds``, ``ms``, ``pan`` and ``pan_lr`` don't have the same batch and channel sizes.
        ValueError:
            If ``preds`` and ``pan`` don't have the same dimension.
        ValueError:
            If ``ms`` and ``pan_lr`` don't have the same dimension.
        ValueError:
            If ``preds`` and ``pan`` don't have dimension which is multiple of that of ``ms``.
        ValueError:
            If ``norm_order`` is not a positive integer.
        ValueError:
            If ``window_size`` is not a positive integer.

    Example:
        >>> from torch import rand
        >>> from torchmetrics.functional.image import spatial_distortion_index
        >>> preds = rand([16, 3, 32, 32])
        >>> ms = rand([16, 3, 16, 16])
        >>> pan = rand([16, 3, 32, 32])
        >>> spatial_distortion_index(preds, ms, pan)
        tensor(0.0090)

    r   z@Expected `norm_order` to be a positive integer. Got norm_order: r   zBExpected `window_size` to be a positive integer. Got window_size: )Ú
isinstanceÚintr   r#   r	   )r   r   r   r   r(   r)   r*   s          r"   r
   r
   Í   s«   € õr �j¥#Ñ&Ô&ð k¨*¸ª/¨/ÝÐiÐ\fÐiÐiÐiÑjÔjÐjÝ�k¥3Ñ'Ô'ð n¨;¸!Ò+;Ð+;ÝÐlÐ^iÐlÐlÐlÑmÔmÐmÝ=¸eÀRÈÈfÑUÔUÑ€Eˆ2ˆs�FÝ,¨U°B¸¸VÀZÐQ\Ð^gÑhÔhÐhr$   )N)Nr%   r&   r'   )Útypingr   r5   r   Útyping_extensionsr   Ú!torchmetrics.functional.image.uqir   Ú"torchmetrics.utilities.distributedr   Útorchmetrics.utilities.importsr   Ú__doctest_skip__Útupler#   rA   r	   r
   © r$   r"   ú<module>rJ      s  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à KÐ KÐ KÐ KÐ KÐ KØ 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø AÐ AÐ AÐ AÐ AÐ Aàð YØ;Ð=WÐXÐð HLðc"ð c"Øðc"Øðc"Ø$*ðc"Ø4<¸VÔ4Dðc"à
ˆ6�6˜6 8¨FÔ#3Ð3Ô4ðc"ð c"ð c"ð c"ðT  $ØØØ<NðG7ð G7ØðG7àðG7ð 
ðG7ð �VÔð	G7ð
 ðG7ð ðG7ð Ð8Ô9ðG7ð ðG7ð G7ð G7ð G7ð\  $ØØØ<Nð>ið >iØð>iàð>ið 
ð>ið �VÔð	>ið
 ð>ið ð>ið Ð8Ô9ð>ið ð>ið >ið >ið >ið >ið >ir$   