a
    d                  	   @   s   d dl Z d dlmZ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mZmZ eeddd	Zdeeeed eeeef  edddZeedddZdeeed eeeef  edddZdeed eeeef  edddZdS )    N)OptionalUnion)Tensor)Literal)#_multiclass_confusion_matrix_update)_drop_empty_rows_and_cols_handle_nan_in_data_nominal_input_validation)confmatreturnc                 C   sV   t | } |  }| | }| d| }|dd|jd }t|t||  S )a  Compute Conditional Entropy Statistic based on a pre-computed confusion matrix.

    .. math::
        H(X|Y) = \sum_{x, y ~ (X, Y)} p(x, y)\frac{p(y)}{p(x, y)}

    Args:
        confmat: Confusion matrix for observed data

    Returns:
        Conditional Entropy Value

       )r   sumZ	unsqueezerepeatshapetorchZnansumlog)r
   total_occurrencesZp_xy_mZp_yZp_y_m r   q/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/nominal/theils_u.py_conditional_entropy_compute   s    r   replace        )r   Zdrop)predstargetnum_classesnan_strategynan_replace_valuer   c                 C   sN   | j dkr| dn| } |j dkr,|dn|}t| |||\} }t| ||S )a  Compute the bins to update the confusion matrix with for Theil's U calculation.

    Args:
        preds: 1D or 2D tensor of categorical (nominal) data
        target: 1D or 2D tensor of categorical (nominal) data
        num_classes: Integer specifing the number of classes
        nan_strategy: Indication of whether to replace or drop ``NaN`` values
        nan_replace_value: Value to replace ``NaN`s when ``nan_strategy = 'replace```

    Returns:
        Non-reduced confusion matrix

       r   )ndimZargmaxr   r   )r   r   r   r   r   r   r   r   _theils_u_update7   s    r   c                 C   s`   t | } t| }|  }| d| }t|t|  }|dkrTtjd| jdS || | S )zCompute Theil's U statistic based on a pre-computed confusion matrix.

    Args:
        confmat: Confusion matrix for observed data

    Returns:
        Theil's U statistic

    r   device)r   r   r   r   r   Ztensorr!   )r
   Zs_xyr   Zp_xZs_xr   r   r   _theils_u_computeQ   s    
r"   )r   r   r   r   r   c                 C   s.   t t| |g }t| ||||}t|S )aS  Compute `Theils Uncertainty coefficient`_ statistic measuring the association between two nominal data series.

    .. math::
        U(X|Y) = \frac{H(X) - H(X|Y)}{H(X)}

    where :math:`H(X)` is entropy of variable :math:`X` while :math:`H(X|Y)` is the conditional entropy of :math:`X`
    given :math:`Y`.

    Theils's U is an asymmetric coefficient, i.e. :math:`TheilsU(preds, target) \neq TheilsU(target, preds)`.

    The output values lies in [0, 1]. 0 means y has no information about x while value 1 means y has complete
    information about x.

    Args:
        preds: 1D or 2D tensor of categorical (nominal) data
            - 1D shape: (batch_size,)
            - 2D shape: (batch_size, num_classes)
        target: 1D or 2D tensor of categorical (nominal) data
            - 1D shape: (batch_size,)
            - 2D shape: (batch_size, num_classes)
        nan_strategy: Indication of whether to replace or drop ``NaN`` values
        nan_replace_value: Value to replace ``NaN``s when ``nan_strategy = 'replace'``

    Returns:
        Tensor containing Theil's U statistic

    Example:
        >>> from torchmetrics.functional.nominal import theils_u
        >>> _ = torch.manual_seed(42)
        >>> preds = torch.randint(10, (10,))
        >>> target = torch.randint(10, (10,))
        >>> theils_u(preds, target)
        tensor(0.8530)

    )lenr   catuniquer   r"   )r   r   r   r   r   r
   r   r   r   theils_ul   s    )r&   )matrixr   r   r   c                 C   s   t || | jd }tj||| jd}tt|dD ]r\}}| dd|f | dd|f  }}tt	||g
 }	t|||	||}
t|
|||f< t|
j|||f< q6|S )a  Compute `Theil's U`_ statistic between a set of multiple variables.

    This can serve as a convenient tool to compute Theil's U statistic for analyses of correlation between categorical
    variables in your dataset.

    Args:
        matrix: A tensor of categorical (nominal) data, where:
            - rows represent a number of data points
            - columns represent a number of categorical (nominal) features
        nan_strategy: Indication of whether to replace or drop ``NaN`` values
        nan_replace_value: Value to replace ``NaN``s when ``nan_strategy = 'replace'``

    Returns:
        Theil's U statistic for a dataset of categorical variables

    Example:
        >>> from torchmetrics.functional.nominal import theils_u_matrix
        >>> _ = torch.manual_seed(42)
        >>> matrix = torch.randint(0, 4, (200, 5))
        >>> theils_u_matrix(matrix)
        tensor([[1.0000, 0.0202, 0.0142, 0.0196, 0.0353],
                [0.0202, 1.0000, 0.0070, 0.0136, 0.0065],
                [0.0143, 0.0070, 1.0000, 0.0125, 0.0206],
                [0.0198, 0.0137, 0.0125, 1.0000, 0.0312],
                [0.0352, 0.0065, 0.0204, 0.0308, 1.0000]])

    r   r    r   N)r	   r   r   Zonesr!   	itertoolscombinationsranger#   r$   r%   r   r"   T)r'   r   r   Znum_variablesZtheils_u_matrix_valueijxyr   r
   r   r   r   theils_u_matrix   s     

"r0   )r   r   )r   r   )r   r   )r(   typingr   r   r   r   Ztyping_extensionsr   Z7torchmetrics.functional.classification.confusion_matrixr   Z%torchmetrics.functional.nominal.utilsr   r   r	   r   intfloatr   r"   r&   r0   r   r   r   r   <module>   sF       0  