o
    yik                     @   sl   d dl 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 G dd	 d	e	ZdS )
    )AnyListN)Tensor)_spearman_corrcoef_compute_spearman_corrcoef_update)Metric)rank_zero_warn)dim_zero_catc                       s   e Zd ZU dZ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dZde
de
ddfddZde
fddZ  ZS )SpearmanCorrCoefa`  Computes `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 torchmetrics import SpearmanCorrCoef
        >>> target = torch.tensor([3, -0.5, 2, 7])
        >>> preds = torch.tensor([2.5, 0.0, 2, 8])
        >>> spearman = SpearmanCorrCoef()
        >>> spearman(preds, target)
        tensor(1.0000)

    Example (multi output regression):
        >>> from torchmetrics import SpearmanCorrCoef
        >>> target = torch.tensor([[3, -0.5], [2, 7]])
        >>> preds = torch.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_updatepredstarget   num_outputskwargsreturnNc                    s^   t  jdi | td t|ts|dk rtd|| _| jdg dd | jdg dd d S )	NzMetric `SpearmanCorrcoef` will save all targets and predictions in the buffer. For large datasets, this may lead to large memory footprint.r   zQExpected argument `num_outputs` to be an int larger than 0, but got {num_outputs}r   cat)defaultdist_reduce_fxr    )super__init__r   
isinstanceint
ValueErrorr   	add_state)selfr   r   	__class__r   T/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/regression/spearman.pyr   F   s   zSpearmanCorrCoef.__init__c                 C   s0   t ||| jd\}}| j| | j| dS )z*Update state with predictions and targets.)r   N)r   r   r   appendr   r   r   r   r   r   r!   updateW   s   zSpearmanCorrCoef.updatec                 C   s   t | j}t | j}t||S )z,Computes Spearman's correlation coefficient.)r	   r   r   r   r#   r   r   r!   compute]   s   


zSpearmanCorrCoef.compute)r   )__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r   r   r   r   r$   r%   __classcell__r   r   r   r!   r
      s"   
 &r
   )typingr   r   torchr   +torchmetrics.functional.regression.spearmanr   r   torchmetrics.metricr   torchmetrics.utilitiesr   torchmetrics.utilities.datar	   r
   r   r   r   r!   <module>   s   