a
    dz                     @   s   d dl Z 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 dl	m
Z
 eeeeeeeeeeeeeeeef d
dd	Zeeeeed
ddZeeedddZdS )    N)Tuple)Tensor) _check_data_shape_to_num_outputs)rank_zero_warn)_check_same_shape)
predstargetmean_xmean_yvar_xvar_ycorr_xyn_priornum_outputsreturnc	                 C   s"  t | | t| || | jd }	| dkp2|	dk}
|
rn|| | d ||	  }|| |d ||	  }n| d}|d}||	7 }|
r|| | | |  d7 }||| ||  d7 }n,|| d|	d  7 }||d|	d  7 }|| | ||  d7 }|}|}||||||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
        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
        n_prior: current number of observed observations
        num_outputs: Number of outputs in multioutput setting

    r      )r   r   shapemeansumvar)r   r   r	   r
   r   r   r   r   r   Zn_obsZcondZmx_newZmy_new r   s/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/regression/pearson.py_pearson_corrcoef_update   s&    



r   )r   r   r   nbr   c                 C   s   | |d  } ||d  }||d  }| j tjkrP| jtdkrP|  } | }tt| j j}| |k 	 s|||k 	 rt
d| j  dt || |    }t|ddS )a  Compute the final pearson correlation based on accumulated statistics.

    Args:
        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).g      g      ?)dtypetorchfloat16deviceZbfloat16mathsqrtZfinfoZepsanyr   UserWarningZsqueezeclamp)r   r   r   r   boundZcorrcoefr   r   r   _pearson_corrcoef_computeP   s     r%   )r   r   r   c                 C   s   | j dkr| jd nd}tj|| j| jd}| | |   }}}| | |   }}}	t| |||||||	| j dkrd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   r   )r   )	ndimr   r   zerosr   r   cloner   r%   )r   r   d_tempr	   r
   r   r   r   r   _r   r   r   pearson_corrcoefu   s    &r.   )r   typingr   r   r   Z(torchmetrics.functional.regression.utilsr   Ztorchmetrics.utilitiesr   Ztorchmetrics.utilities.checksr   intr   r%   r.   r   r   r   r   <module>   s0   8%