o
    yi                  
   @   s   d dl mZ d dlZd dlmZ d dlmZ d dlmZ dedefdd	Zdedefd
dZ	dedede
deeef fddZddedededefddZdededefddZdS )    )TupleN)Tensor) _check_data_shape_to_num_outputs)_check_same_shapedatareturnc                 C   s   |    }| d }ttjdg|jd|dd |dd kg}|| }tt|tj| gg|jdg	 }|dd |dd  }|dk}|| S )z_find and return values which have repeats i.e. the same value are more than once in the tensor.r   T)device   N)
detachclonesorttorchcattensorr   nonzeronumelflatten)r   tempchangeunique
change_idxfreqatleast2 r   _/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/regression/spearman.py_find_repeats   s   0,r   c                 C   sn   |   }t| }|  }tjd|d | j| jd||d| < t| }|D ]}| |k}||  ||< q&|S )a  Calculate the rank for each element of a tensor.

    The rank refers to the indices of an element in the corresponding sorted tensor (starting from 1).
    Duplicates of the same value will be assigned the mean of their rank.

    Adopted from `Rank of element tensor`_
    r	   )dtyper   N)	r   r   
empty_likeargsortaranger   r   r   mean)r   nrankidxrepeatsr	conditionr   r   r   
_rank_data$   s   
&r(   predstargetnum_outputsc                 C   s6   |   r|  stdt| | t| || | |fS )zUpdates and returns variables required to compute Spearman Correlation Coefficient.

    Checks for same shape and type of input tensors.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
    zhExpected `preds` and `target` both to be floating point tensors, but got {pred.dtype} and {target.dtype})is_floating_point	TypeErrorr   r   )r)   r*   r+   r   r   r   _spearman_corrcoef_update8   s   

r.   ư>epsc           	      C   s   | j dkrt| } t|}ntdd | jD j} tdd |jD j}| | d }||d }|| d}t|| d}t|| d}||| |  }t|ddS )a  Computes Spearman Correlation Coefficient.

    Args:
        preds: Predicted tensor
        target: Ground truth tensor
        eps: Avoids ``ZeroDivisionError``.

    Example:
        >>> target = torch.tensor([3, -0.5, 2, 7])
        >>> preds = torch.tensor([2.5, 0.0, 2, 8])
        >>> preds, target = _spearman_corrcoef_update(preds, target, num_outputs=1)
        >>> _spearman_corrcoef_compute(preds, target)
        tensor(1.0000)
    r	   c                 S      g | ]}t |qS r   r(   ).0pr   r   r   
<listcomp>_       z._spearman_corrcoef_compute.<locals>.<listcomp>c                 S   r1   r   r2   )r3   tr   r   r   r5   `   r6   r   g      g      ?)ndimr(   r   stackTr!   sqrtclamp)	r)   r*   r0   
preds_difftarget_diffcov	preds_std
target_stdcorrcoefr   r   r   _spearman_corrcoef_computeL   s   

rC   c                 C   s0   t | || jdkr
dn| jd d\} }t| |S )a  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 x and y. Spearmans correlations
    coefficient corresponds to the standard pearsons correlation coefficient calculated on the rank variables.

    Args:
        preds: estimated scores
        target: ground truth scores

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

    Example (multi output regression):
        >>> from torchmetrics.functional import spearman_corrcoef
        >>> target = torch.tensor([[3, -0.5], [2, 7]])
        >>> preds = torch.tensor([[2.5, 0.0], [2, 8]])
        >>> spearman_corrcoef(preds, target)
        tensor([1.0000, 1.0000])
    r	   r
   )r+   )r.   r8   shaperC   )r)   r*   r   r   r   spearman_corrcoefm   s   &
rE   )r/   )typingr   r   r   (torchmetrics.functional.regression.utilsr   torchmetrics.utilities.checksr   r   r(   intr.   floatrC   rE   r   r   r   r   <module>   s   "!