§
    ’©ºjÃ  ã                   ó(  — 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
mZmZ dedefd	„Z	 	 ddedededed         dee         defd„Zdedefd„Z	 	 ddededed         dee         def
d„Z	 	 ddeded         dee         defd„ZdS )é    N)ÚOptional)ÚTensor)ÚLiteral)Ú#_multiclass_confusion_matrix_update)Ú_drop_empty_rows_and_colsÚ_handle_nan_in_dataÚ_nominal_input_validationÚconfmatÚreturnc                 óB  — t          | ¦  «        } |                      ¦   «         }| |z  }|                      d¦  «        |z  }|                     d¦  «                             d|j        d         ¦  «        }t          j        |t          j        ||z  ¦  «        z  ¦  «        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   ÚsumÚ	unsqueezeÚrepeatÚshapeÚtorchÚnansumÚlog)r
   Útotal_occurrencesÚp_xy_mÚp_yÚp_y_ms        úx/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/nominal/theils_u.pyÚ_conditional_entropy_computer      s‹   € õ (¨Ñ0Ô0€GØŸš™œÐàÐ(Ñ(€Fà
�+Š+�a‰.Œ.Ð,Ñ
,€Cà�MŠM˜!ÑÔ×#Ò# A v¤|°A¤Ñ7Ô7€Eõ Œ<˜¥¤¨5°6©>Ñ!:Ô!:Ñ:Ñ;Ô;Ð;ó    Úreplaceç        ÚpredsÚtargetÚnum_classesÚnan_strategy)r   ÚdropÚnan_replace_valuec                 óÖ   — | 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 specifying 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   )ÚndimÚargmaxr   r   )r   r   r    r!   r#   s        r   Ú_theils_u_updater(   7   sm   € ð(  %œz¨Qš˜ˆE�LŠL˜‰OŒOˆO°E€EØ!'¤°Ò!1Ð!1ˆV�]Š]˜1ÑÔÐ°v€FÝ'¨¨v°|ÐEVÑWÔW�M€Eˆ6Ý.¨u°f¸kÑJÔJÐJr   c                 ó<  — t          | ¦  «        } t          | ¦  «        }|                      ¦   «         }|                      d¦  «        |z  }t          j        |t          j        |¦  «        z  ¦  «         }|dk    rt          j        d| j        ¬¦  «        S ||z
  |z  S )z´Compute 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   Útensorr+   )r
   Ús_xyr   Úp_xÚs_xs        r   Ú_theils_u_computer0   Q   s•   € õ (¨Ñ0Ô0€Gõ (¨Ñ0Ô0€Dð  Ÿš™œÐØ
�+Š+�a‰.Œ.Ð,Ñ
,€CÝŒ9�S�5œ9 S™>œ>Ñ)Ñ*Ô*Ð
*€Cð ˆa‚x€xÝŒ|˜A g¤nÐ5Ñ5Ô5Ð5à�$‰J˜#ÑÐr   c                 ó°   — t          t          j        | |g¦  «                             ¦   «         ¦  «        }t	          | ||||¦  «        }t          |¦  «        S )aG  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 torch import randint
        >>> from torchmetrics.functional.nominal import theils_u
        >>> preds = randint(10, (10,))
        >>> target = randint(10, (10,))
        >>> theils_u(preds, target)
        tensor(0.8530)

    )Úlenr   ÚcatÚuniquer(   r0   )r   r   r!   r#   r    r
   s         r   Útheils_ur5   l   sP   € õR •e”i ¨ Ñ0Ô0×7Ò7Ñ9Ô9Ñ:Ô:€KÝ˜u f¨k¸<ÐIZÑ[Ô[€GÝ˜WÑ%Ô%Ð%r   Úmatrixc                 óâ  — t          ||¦  «         | j        d         }t          j        ||| j        ¬¦  «        }t          j        t          |¦  «        d¦  «        D ]’\  }}| dd…|f         | dd…|f         }}t          t          j	        ||g¦  «         
                    ¦   «         ¦  «        }	t          |||	||¦  «        }
t          |
¦  «        |||f<   t          |
j        ¦  «        |||f<   Œ“|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 torch import randint
        >>> from torchmetrics.functional.nominal import theils_u_matrix
        >>> matrix = 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   Úonesr+   Ú	itertoolsÚcombinationsÚranger2   r3   r4   r(   r0   ÚT)r6   r!   r#   Únum_variablesÚtheils_u_matrix_valueÚiÚjÚxÚyr    r
   s              r   Útheils_u_matrixrC   š   sû   € õ@ ˜lÐ,=Ñ>Ô>Ð>Ø”L ”O€MÝ!œJ }°mÈFÌMÐZÑZÔZÐÝÔ&¥u¨]Ñ';Ô';¸QÑ?Ô?ð Cð C‰ˆˆ1Ø�a�a�a˜�dŒ|˜V A A A q Dœ\ˆ1ˆÝ�%œ) Q¨ FÑ+Ô+×2Ò2Ñ4Ô4Ñ5Ô5ˆÝ" 1 a¨°lÐDUÑVÔVˆÝ&7¸Ñ&@Ô&@Ð˜a ˜dÑ#Ý&7¸¼	Ñ&BÔ&BÐ˜a ˜dÑ#Ð#Ø Ð r   )r   r   )r9   Útypingr   r   r   Útyping_extensionsr   Ú7torchmetrics.functional.classification.confusion_matrixr   Ú%torchmetrics.functional.nominal.utilsr   r   r	   r   ÚintÚfloatr(   r0   r5   rC   © r   r   ú<module>rK      sû  ðð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à gÐ gÐ gÐ gÐ gÐ gðð ð ð ð ð ð ð ð ð ð<¨&ð <°Vð <ð <ð <ð <ð< 09Ø),ðKð KØðKàðKð ðKð Ð+Ô,ð	Kð
   ”ðKð ðKð Kð Kð Kð4˜vð ¨&ð ð ð ð ð< 09Ø),ð	+&ð +&Øð+&àð+&ð Ð+Ô,ð+&ð   ”ð	+&ð
 ð+&ð +&ð +&ð +&ð` 09Ø),ð)!ð )!Øð)!àÐ+Ô,ð)!ð   ”ð)!ð ð	)!ð )!ð )!ð )!ð )!ð )!r   