o
    y“©i¥  ã                   @   sè   d dl mZ d dlZd dlmZ d dlmZ dededefdd	„Zdededefd
d„Zdededefdd„Zdededeeef fdd„Z	ddedede
dedef
dd„Zddedededefdd„Zddedededefdd„ZdS )é    )ÚTupleN)ÚTensor)Ú_TORCH_GREATER_EQUAL_1_9ÚxÚyÚreturnc                 C   s8   | j tjks|j tjkr|  ¡ |j ¡   ¡ S | |j S )z~Safe calculation of matrix multiplication.

    If input is float16, will cast to float32 for computation and back again.
    )ÚdtypeÚtorchÚfloat16ÚfloatÚTÚhalf©r   r   © r   úR/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/utilities/compute.pyÚ_safe_matmul   s   
r   c                 C   s   | t  |¡ }d|| dk< |S )z§Computes x * log(y). Returns 0 if x=0.

    Example:
        >>> import torch
        >>> x = torch.zeros(1)
        >>> _safe_xlogy(x, 1/x)
        tensor([0.])

    ç        r   )r	   Úlog)r   r   Úresr   r   r   Ú_safe_xlogy    s   
r   ÚnumÚdenomc                 C   s<   d||dk< |   ¡ r| n|  ¡ } |  ¡ r|n| ¡ }| | S )zSafe division, by preventing division by zero.

    Additionally casts to float if input is not already to secure backwards compatibility.
    é   r   )Úis_floating_pointr   )r   r   r   r   r   Ú_safe_divide/   s   r   c                 C   sŒ   | j dkr	|  ¡ n| } |j dkr| ¡ n|}| j dks |j dkr,td| j › d|j › ƒ‚|  ¡ | ¡ krBtd|  ¡ › d| ¡ › ƒ‚| |fS )z!Checks that auc input is correct.r   zJExpected both `x` and `y` tensor to be 1d, but got tensors with dimension z and zHExpected the same number of elements in `x` and `y` tensor but received )ÚndimÚsqueezeÚ
ValueErrorÚnumelr   r   r   r   Ú_auc_format_inputs:   s   ÿÿr   éÿÿÿÿÚ	directionÚaxisc                 C   sB   t  ¡  t j|| |d| }W d  ƒ |S 1 sw   Y  |S )zrComputes area under the curve using the trapezoidal rule.

    Assumes increasing or decreasing order of `x`.
    )ÚdimN)r	   Úno_gradÚtrapz)r   r   r!   r"   Úauc_r   r   r   Ú_auc_compute_without_checkJ   s   

ÿþr'   FÚreorderc                 C   s¦   t  ¡ E |rtrt j| ddnt  | ¡\} }|| }| dd … | d d…  }|dk  ¡ r:|dk ¡ r6d}ntdƒ‚d}t| ||ƒW  d   ƒ S 1 sLw   Y  d S )	NT)Ústabler   r    r   g      ð¿z_The `x` tensor is neither increasing or decreasing. Try setting the reorder argument to `True`.g      ð?)r	   r$   r   ÚsortÚanyÚallr   r'   )r   r   r(   Úx_idxÚdxr!   r   r   r   Ú_auc_computeT   s   
 ÿ
$ñr/   c                 C   s   t | |ƒ\} }t| ||dS )a8  Computes Area Under the Curve (AUC) using the trapezoidal rule.

    Args:
        x: x-coordinates, must be either increasing or decreasing
        y: y-coordinates
        reorder: if True, will reorder the arrays to make it either increasing or decreasing

    Return:
        Tensor containing AUC score
    )r(   )r   r/   )r   r   r(   r   r   r   Úaucg   s   r0   )r    )F)Útypingr   r	   r   Útorchmetrics.utilities.importsr   r   r   r   r   r   Úintr'   Úboolr/   r0   r   r   r   r   Ú<module>   s   
 
 