
    i                          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Zd
ededededededefdZdededefdZy)    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                 0   t        | |       t        | ||
       | j                  d   }| j                  d      }|j                  d      }||z
  }||z
  }|	|z   }|||z  |z  z   }|||z  |z  z   }|dk(  r#||z
  }||z
  }|||z  z   }|||z  z   }|||z  z   }nx| |z
  }||z
  }|dz  j	                  d      }|dz  j	                  d      }||z  j	                  d      }|	|z  |z  }||z   |dz  |z  z   }||z   |dz  |z  z   }||z   ||z  |z  z   }t        j                  |t        j                  | |z
  j                         d      d         }t        j                  |t        j                  ||z
  j                         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
corrections                              /Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/functional/regression/pearson.py_pearson_corrcoef_updater/      s   @ eV$$UFK@kk!nG::a=L;;q>LV#GV#G'!Gg''11Fg''11F!|&(&(7**7**Gh..- </%q(--a0&)..q1&8==a@(72
#gqj:&==#gqj:&==L(7W+<z+IIMM-EFN;O;O;QWX1YZ[1\]MMM-FVO;P;P;RXY1Z[\1]^M6=-wPWWW    nbc                 N   ||dz
  z  }||dz
  z  }||dz
  z  }|j                   t        j                  k(  rB|j                  t        j                  d      k(  r |j	                         }|j	                         }|t        j
                  | d      z  }|t        j
                  |d      z  }|| |z  z  }t        j                  t        j                  |j                         j                        }||k  j                         s||k  j                         slt        j                  |      j                          sHt        j                  |      j                          s$t        j                  |      j                          rt        d|j                    dt               ||k  ||k  z  t        j                  |       z  t        j                  |       z  t        j                  |       z  }t        j                  |t        d      |j                  |j                         }| }	|	j                         r`||	   ||	   ||	   z  j                         z  j!                         j#                  |j                         ||	<   t        j$                  |dd	      }|j!                         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      ?)r7   r   float16r6   bfloat16powmathsqrtfinfoepsanyisfiniter   UserWarning	full_likefloatsqueezetoclamp)
r   r   r   r   r   r1   boundzero_var_maskcorrcoef
valid_masks
             r.   _pearson_corrcoef_computerK   [   sD   ( R!VER!VEa G {{emm#U8K(K  EIImR00EEIImR00E67GIIekk%++.223E	EM NN5!%%''NN5!%%''NN7#''))--2[[M9gi 		
 
55=)U^^E-B,BBennUZF[E[[_d_m_mnu_v^vv  weW^^SZS`S`aHJ~~Z E*$5j8I$I#O#O#QQZZ\__`h`n`no 	 ;;xs3r0   c                 4   | j                   dk(  r| j                  d   nd}t        j                  || j                  | j
                        }|j                         |j                         |j                         }}}|j                         |j                         |j                         }	}}|j                         |j                         }}
t        | ||||
|||||	| j                   dk(  rdn| j                  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   )r7   r6   )r   r   r	   r
   r   r   r   r   r   r   r   )	ndimr   r   zerosr7   r6   cloner/   rK   )r   r   d_tempr	   r
   r   r   r   r1   r   r   _s                r.   pearson_corrcoefrT      s    ,  **/AqAKKU\\BE!KKM5;;=%++-EFFu{{}B7E#(;;=%++-=MD\##qAekk"oEAAq-ugr %]M5%QXZ\]]r0   )r;   r   r   (torchmetrics.functional.regression.utilsr   torchmetrics.utilitiesr   torchmetrics.utilities.checksr   inttupler/   rK   rT    r0   r.   <module>r[      s)      U 1 ;@X@X@X @X 	@X
 @X @X @X @X @X @X @X 66666666IJ@XF777 7 	7
 7 	7 7t(^F (^F (^v (^r0   