o
    .wi                      @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ dededed	ed
edededededede	de
eeeeeeeef fddZd
ededededededefddZdededefddZdS )    N)Tensor) _check_data_shape_to_num_outputs)rank_zero_warn)_check_same_shapepredstargetmean_xmean_ymax_abs_dev_xmax_abs_dev_yvar_xvar_ycorr_xy	num_priornum_outputsreturnc                 C   s  t | | t| ||
 | jd }| d}|d}|| }|| }|	| }||| |  }||| |  }|dkrU|| }|| }|||  }|||  }|||  }nA| | }|| }|d d}|d d}|| d}|	| | }|| |d |  }|| |d |  }|| || |  }t|tj| |  ddd }t|tj||  ddd }||||||||fS )a  Update and returns variables required to compute Pearson Correlation Coefficient.

    Check for same shape of input tensors.

    Args:
        preds: estimated scores
        target: ground truth scores
        mean_x: current mean estimate of x tensor
        mean_y: current mean estimate of y tensor
        max_abs_dev_x: current maximum absolute value of x tensor
        max_abs_dev_y: current maximum absolute value of y tensor
        var_x: current variance estimate of x tensor
        var_y: current variance estimate of y tensor
        corr_xy: current covariance estimate between x and y tensor
        num_prior: current number of observed observations
        num_outputs: Number of outputs in multioutput setting

    r         )dim)	r   r   shapemeansumtorchmaximummaxabs)r   r   r   r	   r
   r   r   r   r   r   r   num_obsbatch_mean_xbatch_mean_ydelta_xdelta_yn_totalmx_newmy_newdelta2_xdelta2_ypreds_centeredtarget_centeredbatch_var_xbatch_var_ybatch_cov_xy
correction r,   g/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/regression/pearson.py_pearson_corrcoef_update   s8   
 


""r.   nbc           
      C   s  ||d  }||d  }||d  }|j tjkr(|jtdkr(| }| }|t| d }|t|d }|| |  }tt|j j	}||k 
 sl||k 
 slt|
  slt|
  slt|
  rvtd|j  dt ||k ||k B t| B t| B t| B }tj|td|j|j d}| }	|	
 r||	 ||	 ||	     |j ||	< t|dd	}| S )
a  Compute the final pearson correlation based on accumulated statistics.

    Args:
        max_abs_dev_x: maximum absolute value of x tensor
        max_abs_dev_y: maximum absolute value of y tensor
        var_x: variance estimate of x tensor
        var_y: variance estimate of y tensor
        corr_xy: covariance estimate between x and y tensor
        nb: number of observations

    r   cpuzThe variance of predictions or target is close to zero. This can cause instability in Pearson correlationcoefficient, leading to wrong results. Consider re-scaling the input if possible or computing using alarger dtype (currently using z.). Setting the correlation coefficient to nan.nan)devicedtypeg      g      ?)r4   r   float16r3   bfloat16powmathsqrtfinfoepsanyisfiniter   UserWarning	full_likefloatsqueezetoclamp)
r
   r   r   r   r   r/   boundzero_var_maskcorrcoef
valid_maskr,   r,   r-   _pearson_corrcoef_compute[   sF   

2&rH   c                 C   s   | j dkr
| jd nd}tj|| j| jd}| | | }}}| | | }}}	| | }
}t| ||||
|||||	| j dkrMdn| jd d\}}}
}}}}}	t|
|||||	S )a  Compute pearson correlation coefficient.

    Args:
        preds: estimated scores
        target: ground truth scores

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

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

    r   r   )r4   r3   )r   r   r   r	   r
   r   r   r   r   r   r   )	ndimr   r   zerosr4   r3   cloner.   rH   )r   r   d_tempr   r	   r   r   r   r/   r
   r   _r,   r,   r-   pearson_corrcoef   s&   rP   )r8   r   r   (torchmetrics.functional.regression.utilsr   torchmetrics.utilitiesr   torchmetrics.utilities.checksr   inttupler.   rH   rP   r,   r,   r,   r-   <module>   s^   	

C
: