§
    ’©ºj²  ã                   ó¬   — 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
m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mZ esd
gZ G d„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnion)ÚTensor)Ú_spearman_corrcoef_computeÚ_spearman_corrcoef_update)ÚMetric)Úrank_zero_warn)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzSpearmanCorrCoef.plotc                   ó  ‡ — 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<   	 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 )ÚSpearmanCorrCoefaƒ  Compute `spearmans rank correlation coefficient`_.

    .. math:
        r_s = = \frac{cov(rg_x, rg_y)}{\sigma_{rg_x} * \sigma_{rg_y}}

    where :math:`rg_x` and :math:`rg_y` are the rank associated to the variables :math:`x` and :math:`y`.
    Spearmans correlations coefficient corresponds to the standard pearsons correlation coefficient calculated
    on the rank variables.

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

    - ``preds`` (:class:`~torch.Tensor`): Predictions from model in float tensor with shape ``(N,d)``
    - ``target`` (:class:`~torch.Tensor`): Ground truth values in float tensor with shape ``(N,d)``

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

    - ``spearman`` (:class:`~torch.Tensor`): A tensor with the spearman correlation(s)

    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 torch import tensor
        >>> from torchmetrics.regression import SpearmanCorrCoef
        >>> target = tensor([3, -0.5, 2, 7])
        >>> preds = tensor([2.5, 0.0, 2, 8])
        >>> spearman = SpearmanCorrCoef()
        >>> spearman(preds, target)
        tensor(1.0000)

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

    FÚis_differentiableTÚhigher_is_betterÚfull_state_updateg      ð¿Úplot_lower_boundg      ð?Úplot_upper_boundÚpredsÚtargeté   Únum_outputsÚkwargsÚreturnNc                 ó   •—  t          ¦   «         j        di |¤Ž t          d¦  «         t          |t          ¦  «        s|dk     rt          d|› �¦  «        ‚|| _        |                      dg d¬¦  «         |                      dg d¬¦  «         d S )	Nz‹Metric `SpearmanCorrcoef` will save all targets and predictions in the buffer. For large datasets, this may lead to large memory footprint.r   zDExpected argument `num_outputs` to be an int larger than 0, but got r   Úcat)ÚdefaultÚdist_reduce_fxr   © )ÚsuperÚ__init__r   Ú
isinstanceÚintÚ
ValueErrorr   Ú	add_state)Úselfr   r   Ú	__class__s      €úp/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/regression/spearman.pyr$   zSpearmanCorrCoef.__init__Q   s­   ø€ ð
 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"ÝðLñ	
ô 	
ð 	
õ ˜+¥sÑ+Ô+ð 	s°¸a²°ÝÐqÐdoÐqÐqÑrÔrÐrØ&ˆÔà�Š�w¨¸5ˆÑAÔAÐAØ�Š�x¨¸EˆÑBÔBÐBÐBÐBó    c                 ó  — t          ||| j        ¬¦  «        \  }}| j                             |                     | j        ¦  «        ¦  «         | j                             |                     | j        ¦  «        ¦  «         dS )z*Update state with predictions and targets.)r   N)r
   r   r   ÚappendÚtoÚdtyper   ©r)   r   r   s      r+   ÚupdatezSpearmanCorrCoef.updateb   sk   € å1°%¸ÈTÔM]Ð^Ñ^Ô^‰ˆˆvØŒ
×Ò˜%Ÿ(š( 4¤:Ñ.Ô.Ñ/Ô/Ð/ØŒ×Ò˜6Ÿ9š9 T¤ZÑ0Ô0Ñ1Ô1Ð1Ð1Ð1r,   c                 ór   — t          | j        ¦  «        }t          | j        ¦  «        }t          ||¦  «        S )z+Compute Spearman's correlation coefficient.)r   r   r   r	   r1   s      r+   ÚcomputezSpearmanCorrCoef.computeh   s0   € å˜TœZÑ(Ô(ˆÝ˜dœkÑ*Ô*ˆÝ)¨%°Ñ8Ô8Ð8r,   Ú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 SpearmanCorrCoef
            >>> metric = SpearmanCorrCoef()
            >>> 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 SpearmanCorrCoef
            >>> metric = SpearmanCorrCoef()
            >>> values = []
            >>> for _ in range(10):
            ...     values.append(metric(randn(10,), randn(10,)))
            >>> fig, ax = metric.plot(values)

        )Ú_plot)r)   r5   r6   s      r+   ÚplotzSpearmanCorrCoef.plotn   s   € ðP �zŠz˜#˜rÑ"Ô"Ð"r,   )r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   Úfloatr   r   r   r&   r   r$   r2   r4   r   r   r   r   r   r9   Ú__classcell__)r*   s   @r+   r   r      s‡  ø€ € € € € € ð'ð 'ðR $Ð�tÐ#Ð#Ñ#Ø!Ð�dÐ!Ð!Ñ!Ø#Ð�tÐ#Ð#Ñ#Ø"Ð�eÐ"Ð"Ñ"Ø!Ð�eÐ!Ð!Ñ!à�Œ<ÐÐÑØ�ŒLÐÐÑð ðCð CàðCð ðCð 
ð	Cð Cð Cð Cð Cð Cð"2˜Fð 2¨Fð 2°tð 2ð 2ð 2ð 2ð9˜ð 9ð 9ð 9ð 9ð _cð(#ð (#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð(#ØIQÐRZÔI[ð(#à	ð(#ð (#ð (#ð (#ð (#ð (#ð (#ð (#r,   r   N)Úcollections.abcr   Útypingr   r   r   r   Útorchr   Ú+torchmetrics.functional.regression.spearmanr	   r
   Útorchmetrics.metricr   Útorchmetrics.utilitiesr   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r"   r,   r+   ú<module>rL      s  ðð %Ð $Ð $Ð $Ð $Ð $Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à Ð Ð Ð Ð Ð à mÐ mÐ mÐ mÐ mÐ mÐ mÐ mØ &Ð &Ð &Ð &Ð &Ð &Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð 1Ø/Ð0Ððx#ð x#ð x#ð x#ð x#�vñ x#ô x#ð x#ð x#ð x#r,   