
    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
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y)    )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        |   di | t        d       t        |t              s|dk  rt        d|       || _        | j                  dg d       | j                  dg d       y )	NzMetric `SpearmanCorrcoef` will save all targets and predictions in the buffer. For large datasets, this may lead to large memory footprint.   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      u/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/regression/spearman.pyr$   zSpearmanCorrCoef.__init__Q   sv    
 	"6"L	
 +s+acdocpqrr&w5AxEB    c                 
   t        ||| j                        \  }}| j                  j                  |j	                  | j
                               | j                  j                  |j	                  | j
                               y)z*Update state with predictions and targets.)r   N)r
   r   r   appendtodtyper   r)   r   r   s      r+   updatezSpearmanCorrCoef.updateb   sX    1%TM]M]^v

%((4::./699TZZ01r,   c                 n    t        | j                        }t        | j                        }t        ||      S )z+Compute Spearman's correlation coefficient.)r   r   r   r	   r1   s      r+   computezSpearmanCorrCoef.computeh   s+    TZZ(dkk*)%88r,   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

            >>> 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 zz#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"2F 2F 2t 29 9 _c(#E&(6*:":;<(#IQRZI[(#	(#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 & 1 4 @ @/0x#v x#r,   