§
    ’©ºj)&  ã                   óŒ  — d dl mZ d dlmZmZmZmZ d dl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mZ esd	gZ	 ddej        dej        dej        dej        dej        dej        dej        dej        dedeej        ej        ej        ej        ej        ej        ej        ej        f         fd„Z G d„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnionN)ÚTensor)Ú_pearson_corrcoef_computeÚ_pearson_corrcoef_update)ÚMetric)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzPearsonCorrCoef.plotç»½×Ùß|Û=Úmeans_xÚmeans_yÚ
maxs_abs_xÚ
maxs_abs_yÚvars_xÚvars_yÚcorrs_xyÚnbsÚepsÚreturnc	           	      óp  — t          | ¦  «        dk    r:| d         |d         |d         |d         |d         |d         |d         |d         fS | d         }	|d         }
|d         }|d         }|d         }|d         }|d         }|d         }t          dt          | ¦  «        ¦  «        D �]}| |         }||         }||         }||         }||         }||         }||         }||         }t          j        t          j        ||¦  «        ||z   |¦  «        }||	z  ||z  z   |z  }||
z  ||z  z   |z  }||z  |z  }||	z
  }||
z
  }||z   ||dz  z  z   } ||z   ||dz  z  z   }!||z   ||z  |z  z   }"t          j        ||¦  «        }#t          j        ||¦  «        }$|}	|}
|#}|$}| }|!}|"}|}�Œ|||#|$| |!|"|fS )a¥  Aggregate the statistics from multiple devices.

    Formula taken from here: `Parallel algorithm for calculating variance
    <https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm>`_

    We use `eps` to avoid division by zero when `n1` and `n2` are both zero. Generally, the value of `eps` should not
    matter, as if `n1` and `n2` are both zero, all the states will also be zero.

    é   r   é   )ÚlenÚrangeÚtorchÚwhereÚ
logical_orÚmaximum)%r   r   r   r   r   r   r   r   r   Úmx1Úmy1Úmax1Úmay1Úvx1Úvy1Úcxy1Ún1ÚiÚmx2Úmy2Úmax2Úmay2Úvx2Úvy2Úcxy2Ún2ÚnbÚmean_xÚmean_yÚn12_bÚdelta_xÚdelta_yÚvar_xÚvar_yÚcorr_xyÚmax_abs_dev_xÚmax_abs_dev_ys%                                        úo/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/regression/pearson.pyÚ_final_aggregationr@      s  € õ, ˆ7�|„|�qÒÐØ�qŒz˜7 1œ: z°!¤}°jÀ´mÀVÈAÄYÐPVÐWXÔPYÐ[cÐdeÔ[fÐhkÐlmÔhnÐnÐnØ
�!Œ*€CØ
�!Œ*€CØ�aŒ=€DØ�aŒ=€DØ
�Œ)€CØ
�Œ)€CØ�AŒ;€DØ	ˆQŒ€BÝ�1•c˜'‘l”lÑ#Ô#ð #ñ #ˆØ�aŒjˆØ�aŒjˆØ˜!Œ}ˆØ˜!Œ}ˆØ�QŒiˆØ�QŒiˆØ˜Œ{ˆØ�ŒVˆåŒ[�Ô)¨"¨bÑ1Ô1°2¸±7¸CÑ@Ô@ˆà�s‘(˜R #™XÑ%¨Ñ+ˆà�s‘(˜R #™XÑ%¨Ñ+ˆà�R‘˜"‘ˆØ˜‘)ˆØ˜‘)ˆà�c‘	˜E G¨Q¡JÑ.Ñ.ˆà�c‘	˜E G¨Q¡JÑ.Ñ.ˆà˜‘+ ¨¡°'Ñ 9Ñ9ˆÝœ d¨DÑ1Ô1ˆÝœ d¨DÑ1Ô1ˆàˆØˆØˆØˆØˆØˆØˆØˆ‰Ø�6˜=¨-¸ÀÀwÐPRÐRÐRó    c                   óp  ‡ — e Zd ZU dZdZeed<   dZe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d<   eed<   eed<   eed<   eed<   eed<   eed<   eed<   	 ddededdfˆ fd„Zdede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 )!ÚPearsonCorrCoefa  Compute `Pearson Correlation Coefficient`_.

    .. math::
        P_{corr}(x,y) = \frac{cov(x,y)}{\sigma_x \sigma_y}

    Where :math:`y` is a tensor of target values, and :math:`x` is a tensor of predictions.

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

    - ``preds`` (:class:`~torch.Tensor`): either single output float tensor with shape ``(N,)``
      or multioutput float tensor of shape ``(N,d)``
    - ``target`` (:class:`~torch.Tensor`): either single output tensor with shape ``(N,)``
      or multioutput tensor of shape ``(N,d)``

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

    - ``pearson`` (:class:`~torch.Tensor`): A tensor with the Pearson Correlation Coefficient

    Args:
        num_outputs: Number of outputs in multioutput setting
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example (single output regression):
        >>> from torchmetrics.regression import PearsonCorrCoef
        >>> target = torch.tensor([3, -0.5, 2, 7])
        >>> preds = torch.tensor([2.5, 0.0, 2, 8])
        >>> pearson = PearsonCorrCoef()
        >>> pearson(preds, target)
        tensor(0.9849)

    Example (multi output regression):
        >>> from torchmetrics.regression import PearsonCorrCoef
        >>> target = torch.tensor([[3, -0.5], [2, 7]])
        >>> preds = torch.tensor([[2.5, 0.0], [2, 8]])
        >>> pearson = PearsonCorrCoef(num_outputs=2)
        >>> pearson(preds, target)
        tensor([1., 1.])

    TÚis_differentiableNÚhigher_is_betterÚfull_state_updateg      ð¿Úplot_lower_boundg      ð?Úplot_upper_boundÚpredsÚtargetr5   r6   r=   r>   r:   r;   r<   Ún_totalr   Únum_outputsÚkwargsr   c                 óŒ  •—  t          ¦   «         j        di |¤Ž t          |t          ¦  «        s|dk     rt	          d¦  «        ‚|| _        |                      dt          j        | j        ¦  «        d ¬¦  «         |                      dt          j        | j        ¦  «        d ¬¦  «         |                      dt          j        | j        ¦  «        d ¬¦  «         |                      dt          j        | j        ¦  «        d ¬¦  «         |                      dt          j        | j        ¦  «        d ¬¦  «         |                      d	t          j        | j        ¦  «        d ¬¦  «         |                      d
t          j        | j        ¦  «        d ¬¦  «         |                      dt          j        | j        ¦  «        d ¬¦  «         d S )Nr   zQExpected argument `num_outputs` to be an int larger than 0, but got {num_outputs}r5   )ÚdefaultÚdist_reduce_fxr6   r=   r>   r:   r;   r<   rK   © )	ÚsuperÚ__init__Ú
isinstanceÚintÚ
ValueErrorrL   Ú	add_stater   Úzeros)ÚselfrL   rM   Ú	__class__s      €r?   rS   zPearsonCorrCoef.__init__�   sŠ  ø€ ð
 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"Ý˜+¥sÑ+Ô+ð 	r°¸a²°ÝÐpÑqÔqÐqØ&ˆÔà�Š�x­¬°TÔ5EÑ)FÔ)FÐW[ˆÑ\Ô\Ð\Ø�Š�x­¬°TÔ5EÑ)FÔ)FÐW[ˆÑ\Ô\Ð\Ø�Š�µ´¸DÔ<LÑ0MÔ0MÐ^bˆÑcÔcÐcØ�Š�µ´¸DÔ<LÑ0MÔ0MÐ^bˆÑcÔcÐcØ�Š�w­¬°DÔ4DÑ(EÔ(EÐVZˆÑ[Ô[Ð[Ø�Š�w­¬°DÔ4DÑ(EÔ(EÐVZˆÑ[Ô[Ð[Ø�Š�y­%¬+°dÔ6FÑ*GÔ*GÐX\ˆÑ]Ô]Ð]Ø�Š�y­%¬+°dÔ6FÑ*GÔ*GÐX\ˆÑ]Ô]Ð]Ð]Ð]rA   c                 óö   — t          ||| j        | j        | j        | j        | j        | j        | j        | j        | j	        ¬¦  «        \  | _        | _        | _        | _        | _        | _        | _        | _        dS )z*Update state with predictions and targets.)rI   rJ   r5   r6   r=   r>   r:   r;   r<   Ú	num_priorrL   N)
r
   r5   r6   r=   r>   r:   r;   r<   rK   rL   )rY   rI   rJ   s      r?   ÚupdatezPearsonCorrCoef.update°   s   € õ %ØØØ”;Ø”;ØÔ,ØÔ,Ø”*Ø”*Ø”LØ”lØÔ(ð
ñ 
ô 
ñ		
ØŒKØŒKØÔØÔØŒJØŒJØŒLØŒLˆLˆLrA   c           
      ó–  — | j         dk    r| j                             ¦   «         dk    s| j         dk    rY| j        j        dk    rIt	          | j        | j        | j        | j        | j        | j	        | j
        | j        ¬¦  «        \  }}}}}}}}n*| j        }| j        }| j        }| j	        }| j
        }| j        }t          ||||||¦  «        S )z3Compute pearson correlation coefficient over state.r   )r   r   r   r   r   r   r   r   )rL   r5   ÚnumelÚndimr@   r6   r=   r>   r:   r;   r<   rK   r	   )rY   Ú_r=   r>   r:   r;   r<   rK   s           r?   ÚcomputezPearsonCorrCoef.computeÉ   sä   € àÔ Ò!Ð! d¤k×&7Ò&7Ñ&9Ô&9¸AÒ&=Ð&=À4ÔCSÐVWÒCWÐCWÐ\`Ô\gÔ\lÐopÒ\pÐ\påQcØœØœØÔ-ØÔ-Ø”zØ”zØœØ”Lð	Rñ 	Rô 	RÑNˆAˆq�- °°u¸gÀwÀwð !Ô.ˆMØ Ô.ˆMØ”JˆEØ”JˆEØ”lˆGØ”lˆGÝ(¨¸ÀuÈeÐU\Ð^eÑfÔfÐfrA   ÚvalÚaxc                 ó.   — |                       ||¦  «        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

            >>> from torch import randn
            >>> # Example plotting a single value
            >>> from torchmetrics.regression import PearsonCorrCoef
            >>> metric = PearsonCorrCoef()
            >>> metric.update(randn(10,), randn(10,))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> from torch import randn
            >>> # Example plotting multiple values
            >>> from torchmetrics.regression import PearsonCorrCoef
            >>> metric = PearsonCorrCoef()
            >>> values = []
            >>> for _ in range(10):
            ...     values.append(metric(randn(10,), randn(10,)))
            >>> fig, ax = metric.plot(values)

        )Ú_plot)rY   rc   rd   s      r?   ÚplotzPearsonCorrCoef.plotà   s   € ðP �zŠz˜#˜rÑ"Ô"Ð"rA   )r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rD   ÚboolÚ__annotations__rE   r   rF   rG   ÚfloatrH   r   r   rU   r   rS   r]   rb   r   r   r   r   rg   Ú__classcell__)rZ   s   @r?   rC   rC   d   sÞ  ø€ € € € € € ð&ð &ðP #Ð�tÐ"Ð"Ñ"Ø'+Ð�h˜t”nÐ+Ð+Ñ+Ø"Ð�tÐ"Ð"Ñ"Ø"Ð�eÐ"Ð"Ñ"Ø!Ð�eÐ!Ð!Ñ!Ø�Œ<ÐÐÑØ�ŒLÐÐÑØ€N€N�NØ€N€N�NØÐÐÑØÐÐÑØ€M€M�MØ€M€M�MØ€O€O�OØ€O€O�Oð ð^ð ^àð^ð ð^ð 
ð	^ð ^ð ^ð ^ð ^ð ^ð&
˜Fð 
¨Fð 
°tð 
ð 
ð 
ð 
ð2g˜ð gð gð gð gð0 _cð(#ð (#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð(#ØIQÐRZÔI[ð(#à	ð(#ð (#ð (#ð (#ð (#ð (#ð (#ð (#rA   rC   )r   )Úcollections.abcr   Útypingr   r   r   r   r   r   Ú*torchmetrics.functional.regression.pearsonr	   r
   Útorchmetrics.metricr   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__rn   Útupler@   rC   rQ   rA   r?   ú<module>rx      sÕ  ðð %Ð $Ð $Ð $Ð $Ð $Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à €€€Ø Ð Ð Ð Ð Ð à jÐ jÐ jÐ jÐ jÐ jÐ jÐ jØ &Ð &Ð &Ð &Ð &Ð &Ø @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð 0Ø.Ð/Ðð ðDSð DSØŒ\ðDSàŒ\ðDSð ”ðDSð ”ð	DSð
 ŒLðDSð ŒLðDSð ŒlðDSð 
ŒðDSð 
ðDSð Ø	„L�%”, ¤¨e¬l¸E¼LÈ%Ì,ÐX]ÔXdÐfkÔfrÐrôðDSð DSð DSð DSðNd#ð d#ð d#ð d#ð d#�fñ d#ô d#ð d#ð d#ð d#rA   