§
    ’©ºj¢!  ã                   ó†  — d dl Z d dlmZmZ d dl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ed	ed
ededeeeef         f
d„Zdedeeeedf         f         defd„Zdededefd„Zdededefd„Zded	ededeeeef         fd„Zded	ed
ededef
d„Z	 	 	 dded	ed
ee         dedeed                  defd„ZdS )é    N)ÚOptionalÚUnion)ÚTensorÚtensor)Úconv2dÚpad)ÚLiteral)Ú_check_same_shape)ÚreduceÚpredsÚtargetÚ	hp_filterÚwindow_sizeÚreturnc           
      ó>  — | j         |j         k    r|                     | j         ¦  «        }t          | |¦  «         | j        dvr t	          d| j        › d|j        › d�¦  «        ‚t          | j        ¦  «        dk    r*|                      d¦  «        } |                     d¦  «        }|dk    st	          d|› d�¦  «        ‚||                      d	¦  «        k    s||                      d¦  «        k    r?t	          d
|› d|                      d	¦  «        › d|                      d¦  «        › d�¦  «        ‚|                      t          j
        ¦  «        } |                     t          j
        ¦  «        }|dddd…f                              | j         | j        ¬¦  «        }| ||fS )a¤  Update and returns variables required to compute Spatial Correlation Coefficient.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        hp_filter: High-pass filter tensor
        window_size: Local window size integer

    Return:
        Tuple of (preds, target, hp_filter) tensors

    Raises:
        ValueError:
            If ``preds`` and ``target`` have different number of channels
            If ``preds`` and ``target`` have different shapes
            If ``preds`` and ``target`` have invalid shapes
            If ``window_size`` is not a positive integer
            If ``window_size`` is greater than the size of the image

    )é   é   zŠExpected `preds` and `target` to have batch of colored images with BxCxHxW shape  or batch of grayscale images of BxHxW shape. Got preds: z and target: ú.r   é   r   z5Expected `window_size` to be a positive integer. Got é   z[Expected `window_size` to be less than or equal to the size of the image. Got window_size: z and image size: ÚxN)ÚdtypeÚdevice)r   Útor
   ÚndimÚ
ValueErrorÚshapeÚlenÚ	unsqueezeÚsizeÚtorchÚfloat32r   )r   r   r   r   s       úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/image/scc.pyÚ_scc_updater$      sË  € ð* „{�f”lÒ"Ð"Ø—’˜5œ;Ñ'Ô'ˆÝ�e˜VÑ$Ô$Ð$Ø„z˜ÐÐÝðEà œ;ðEð Eà5;´\ðEð Eð Eñ
ô 
ð 	
õ ˆ5Œ;ÑÔ˜1ÒÐØ—’ Ñ"Ô"ˆØ×!Ò! !Ñ$Ô$ˆà˜Š?ˆ?ÝÐ_ÐQ\Ð_Ð_Ð_Ñ`Ô`Ð`à�U—Z’Z ‘]”]Ò"Ð" k°E·J²J¸q±M´MÒ&AÐ&AÝð`Ø!,ð`ð `Ø?D¿zºzÈ!¹}¼}ð`ð `ØOTÏzÊzÐZ[É}Ì}ð`ð `ð `ñ
ô 
ð 	
ð
 �HŠH•U”]Ñ#Ô#€EØ�YŠY•u”}Ñ%Ô%€FØ˜$  a a a˜-Ô(×+Ò+°%´+ÀeÄlÐ+ÑSÔS€IØ�&˜)Ð#Ð#ó    Ú	input_imgr   .c                 ó|  — t          |t          ¦  «        r||||f}t          |¦  «        dk    rt          dt          |¦  «        › �¦  «        ‚| dd…dd…dd…d|d         …f                              dg¬¦  «        }| dd…dd…dd…|d          d…f                              dg¬¦  «        }t          j        || |gd¬¦  «        }|dd…dd…d|d	         …dd…f                              d	g¬¦  «        }|dd…dd…|d          d…dd…f                              d	g¬¦  «        }t          j        |||gd	¬¦  «        S )
zlApplies symmetric padding to the 2D image tensor input using ``reflect`` mode (d c b a | a b c d | d c b a).r   z+Expected padding to have length 4, but got Nr   r   )Údimsr   ©Údimr   )Ú
isinstanceÚintr   r   Úflipr!   Úcat)r&   r   Úleft_padÚ	right_padÚpaddedÚtop_padÚ
bottom_pads          r#   Ú_symmetric_reflect_pad_2dr4   L   sq  € å�#•sÑÔð #Ø�C˜˜cÐ"ˆÝ
ˆ3�x„x�1‚}€}ÝÐQÅsÈ3ÁxÄxÐQÐQÑRÔRÐRà˜˜˜˜A˜A˜A˜q˜q˜q ! c¨!¤f *Ð,Ô-×2Ò2¸¸Ð2Ñ<Ô<€HØ˜!˜!˜!˜Q˜Q˜Q    C¨¤F 7 9 9Ð,Ô-×2Ò2¸¸Ð2Ñ<Ô<€IÝŒY˜ )¨YÐ7¸QÐ?Ñ?Ô?€Fà�Q�Q�Q˜˜˜˜1˜s 1œv˜: q q qÐ(Ô)×.Ò.°Q°CÐ.Ñ8Ô8€GØ˜˜˜˜1˜1˜1˜s 1œv˜g˜i˜i¨¨¨Ð*Ô+×0Ò0°q°cÐ0Ñ:Ô:€JÝŒ9�g˜v zÐ2¸Ð:Ñ:Ô:Ð:r%   Úkernelc                 óè  — t          j        |                     d¦  «        dz
  dz  ¦  «        }t          j        |                     d¦  «        dz
  dz  ¦  «        }t          j        |                     d¦  «        dz
  dz  ¦  «        }t          j        |                     d¦  «        dz
  dz  ¦  «        }t	          | ||||f¬¦  «        }|                     ddg¦  «        }t          ||dd¬¦  «        S )zHApplies 2D signal convolution to the input tensor with the given kernel.r   r   r   )r   r   ©ÚstrideÚpadding)ÚmathÚfloorr    Úceilr4   r-   r   )r&   r5   Úleft_paddingÚright_paddingÚtop_paddingÚbottom_paddingr1   s          r#   Ú_signal_convolve_2drA   \   s×   € å”:˜vŸ{š{¨1™~œ~°Ñ1°QÑ6Ñ7Ô7€LÝ”I˜vŸ{š{¨1™~œ~°Ñ1°QÑ6Ñ7Ô7€MÝ”*˜fŸkšk¨!™nœn¨qÑ0°AÑ5Ñ6Ô6€KÝ”Y §¢¨A¡¤°Ñ 2°aÑ7Ñ8Ô8€Nå& y°|À]ÐT_ÐaoÐ6pÐqÑqÔq€FØ�[Š[˜!˜Q˜Ñ Ô €FÝ�&˜&¨°AÐ6Ñ6Ô6Ð6r%   c                 ó(   — t          | |¦  «        dz  S )zOApplies 2-D Laplace filter to the input tensor with the given high pass filter.g       @)rA   )r&   r5   s     r#   Ú_hp_2d_laplacianrC   h   s   € å˜y¨&Ñ1Ô1°CÑ7Ð7r%   Úwindowc                 ó  — t          j        |                     d¦  «        dz
  dz  ¦  «        }t          j        |                     d¦  «        dz
  dz  ¦  «        }t	          | ||||f¦  «        } t	          |||||f¦  «        }t          | |dd¬¦  «        }t          ||dd¬¦  «        }t          | dz  |dd¬¦  «        |dz  z
  }t          |dz  |dd¬¦  «        |dz  z
  }t          || z  |dd¬¦  «        ||z  z
  }	|||	fS )z<Computes local variance and covariance of the input tensors.r   r   r   r   r7   )r:   r<   r    r;   r   r   )
r   r   rD   r=   r>   Ú
preds_meanÚtarget_meanÚ	preds_varÚ
target_varÚtarget_preds_covs
             r#   Ú_local_variance_covariancerK   m   s   € õ
 ”9˜fŸkšk¨!™nœn¨qÑ0°AÑ5Ñ6Ô6€LÝ”J §¢¨A¡¤°Ñ 2°aÑ7Ñ8Ô8€Må�˜ m°\À=ÐQÑRÔR€EÝ�˜,¨°|À]ÐSÑTÔT€Få˜˜v¨a¸Ð;Ñ;Ô;€JÝ˜ °¸1Ð=Ñ=Ô=€Kå�u˜a‘x °¸1Ð=Ñ=Ô=À
ÈAÁÑM€IÝ˜ ™	 6°!¸QÐ?Ñ?Ô?À+ÈqÁ.ÑP€JÝ˜f u™n¨f¸QÈÐJÑJÔJÈ[Ð[eÑMeÑeÐà�jÐ"2Ð2Ð2r%   c                 óv  — | j         }| j        }t          j        dd||f||¬¦  «        |dz  z  }t	          | |¦  «        }t	          ||¦  «        }t          |||¦  «        \  }	}
}d|	|	dk     <   d|
|
dk     <   t          j        |
¦  «        t          j        |	¦  «        z  }|dk    }d||dk    <   ||z  }d||<   |S )a[  Computes per channel Spatial Correlation Coefficient.

    Args:
        preds: estimated image of Bx1xHxW shape.
        target: ground truth image of Bx1xHxW shape.
        hp_filter: 2D high-pass filter.
        window_size: size of window for local mean calculation.

    Return:
        Tensor with Spatial Correlation Coefficient score

    r   )r    r   r   r   r   )r   r   r!   ÚonesrC   rK   Úsqrt)r   r   r   r   r   r   rD   Úpreds_hpÚ	target_hprH   rI   rJ   ÚdenÚidxÚsccs                  r#   Ú_scc_per_channel_computerT   ‚   sá   € ð ŒK€EØŒ\€Fõ
 ŒZ˜a  K°Ð=ÀUÐSYÐZÑZÔZÐ^iÐklÑ^lÑm€Få  yÑ1Ô1€HÝ  ¨Ñ3Ô3€Iå.HÈÐS\Ð^dÑ.eÔ.eÑ+€IˆzÐ+à €Iˆi˜!ŠmÑØ!"€Jˆz˜AŠ~Ñå
Œ*�ZÑ
 Ô
 ¥5¤:¨iÑ#8Ô#8Ñ
8€CØ
�Š(€CØ€CˆˆqŠ�MØ
˜SÑ
 €CØ€Cˆ�HØ€Jr%   é   ÚmeanÚ	reduction)rV   ÚnoneNc                 ó¾  ‡ ‡‡‡— ‰€t          g d¢g d¢g d¢g¦  «        Š|€d}|dvrt          d|› �¦  «        ‚t          ‰ ‰‰‰¦  «        \  Š ŠŠˆˆ ˆˆfd„t          ‰                      d¦  «        ¦  «        D ¦   «         }|dk    r,t          j        t          j        |d¬	¦  «        g d
¢¬	¦  «        S |dk    r%t          t          j        |d¬	¦  «        d¬¦  «        S dS )a  Compute Spatial Correlation Coefficient (SCC_).

    Args:
        preds: predicted images of shape ``(N,C,H,W)`` or ``(N,H,W)``.
        target: ground truth images of shape ``(N,C,H,W)`` or ``(N,H,W)``.
        hp_filter: High-pass filter tensor. default: tensor([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]])
        window_size: Local window size integer. default: 8,
        reduction: Reduction method for output tensor. If ``None`` or ``"none"``,
                   returns a tensor with the per sample results. default: ``"mean"``.

    Return:
        Tensor with scc score

    Example:
        >>> from torch import randn
        >>> from torchmetrics.functional.image import spatial_correlation_coefficient as scc
        >>> x = randn(5, 3, 16, 16)
        >>> scc(x, x)
        tensor(1.)
        >>> x = randn(5, 16, 16)
        >>> scc(x, x)
        tensor(1.)
        >>> x = randn(5, 3, 16, 16)
        >>> y = randn(5, 3, 16, 16)
        >>> scc(x, y, reduction="none")
        tensor([0.0223, 0.0256, 0.0616, 0.0159, 0.0170])

    N)éÿÿÿÿrZ   rZ   )rZ   rU   rZ   rX   )rV   rX   z3Expected reduction to be 'mean' or 'none', but got c                 óÀ   •— g | ]Z}t          ‰d d …|d d …d d …f                              d¦  «        ‰d d …|d d …d d …f                              d¦  «        ‰‰¦  «        ‘Œ[S )Nr   )rT   r   )Ú.0Úir   r   r   r   s     €€€€r#   ú
<listcomp>z3spatial_correlation_coefficient.<locals>.<listcomp>Ò   s�   ø€ ð ð ð ð õ 	!Ø�!�!�!�Q˜˜˜˜1˜1˜1�*Ô×'Ò'¨Ñ*Ô*¨F°1°1°1°a¸¸¸¸A¸A¸A°:Ô,>×,HÒ,HÈÑ,KÔ,KÈYÐXcñ	
ô 	
ðð ð r%   r   r)   )r   r   r   rV   Úelementwise_mean)rW   )	r   r   r$   Úranger    r!   rV   r.   r   )r   r   r   r   rW   Úper_channels   ````  r#   Úspatial_correlation_coefficientrb   §   s*  øøøø€ ðF ÐÝ˜L˜L˜L¨+¨+¨+°|°|°|ÐDÑEÔEˆ	ØÐØˆ	ØÐ(Ð(Ð(ÝÐZÈyÐZÐZÑ[Ô[Ð[Ý*¨5°&¸)À[ÑQÔQÑ€Eˆ6�9ðð ð ð ð ð ð õ �u—z’z !‘}”}Ñ%Ô%ð	ñ ô €Kð �FÒÐÝŒz�%œ) K°QÐ7Ñ7Ô7¸Y¸Y¸YÐGÑGÔGÐGØ�FÒÐÝ•e”i °Ð3Ñ3Ô3Ð?QÐRÑRÔRÐRØˆ4r%   )NrU   rV   )r:   Útypingr   r   r!   r   r   Útorch.nn.functionalr   r   Útyping_extensionsr	   Útorchmetrics.utilities.checksr
   Ú"torchmetrics.utilities.distributedr   r,   Útupler$   r4   rA   rC   rK   rT   rb   © r%   r#   ú<module>rj      s]  ðð €€€Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  Ø +Ð +Ð +Ð +Ð +Ð +Ð +Ð +Ø %Ð %Ð %Ð %Ð %Ð %à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5ð/$�vð /$ vð /$¸&ð /$Èsð /$ÐW\Ð]cÐekÐmsÐ]sÔWtð /$ð /$ð /$ð /$ðd;¨ð ;°e¸CÀÀsÈCÀxÄÐ<PÔ6Qð ;ÐV\ð ;ð ;ð ;ð ;ð 	7 6ð 	7°6ð 	7¸fð 	7ð 	7ð 	7ð 	7ð8 ð 8°ð 8¸6ð 8ð 8ð 8ð 8ð
3 fð 3°fð 3Àfð 3ÐQVÐW]Ð_eÐgmÐWmÔQnð 3ð 3ð 3ð 3ð*" Fð "°Fð "Àvð "Ð\_ð "Ðdjð "ð "ð "ð "ðP #'ØØ9?ð5ð 5Øð5àð5ð ˜Ôð5ð ð	5ð
 ˜Ð 4Ô5Ô6ð5ð ð5ð 5ð 5ð 5ð 5ð 5r%   