§
    ’©ºj×  ã                   ó4  — 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mZmZmZ 	 	 dd	ed
ededed         dee         defd„Zde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dededed         dee         def
d„ZdS )é    N)ÚOptional)ÚTensor)ÚLiteral)Ú#_multiclass_confusion_matrix_update)Ú_compute_bias_corrected_valuesÚ_compute_chi_squaredÚ_drop_empty_rows_and_colsÚ_handle_nan_in_dataÚ_nominal_input_validationÚ&_unable_to_use_bias_correction_warningÚreplaceç        ÚpredsÚtargetÚnum_classesÚnan_strategy)r   ÚdropÚnan_replace_valueÚreturnc                 óÖ   — | 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 Tschuprow's T 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

    é   é   )ÚndimÚargmaxr
   r   )r   r   r   r   r   s        úz/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/nominal/tschuprows.pyÚ_tschuprows_t_updater       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ÐJó    ÚconfmatÚbias_correctionc                 óº  — t          | ¦  «        } |                      ¦   «         }t          | |¦  «        }||z  }| j        \  }}|ršt	          ||||¦  «        \  }}}	t          j        ||	¦  «        dk    r8t          d¬¦  «         t          j        t          d¦  «        | j
        ¬¦  «        S t          j        |t          j        |dz
  |	dz
  z  ¦  «        z  ¦  «        }
nht          j        ||j
        ¬¦  «        }t          j        ||j
        ¬¦  «        }t          j        |t          j        |dz
  |dz
  z  ¦  «        z  ¦  «        }
|
                     dd¦  «        S )a  Compute Tschuprow's T statistic based on a pre-computed confusion matrix.

    Args:
        confmat: Confusion matrix for observed data
        bias_correction: Indication of whether to use bias correction.

    Returns:
        Tschuprow's T statistic

    r   zTschuprow's T)Úmetric_nameÚnan©Údevicer   g      ð?)r	   Úsumr   Úshaper   ÚtorchÚminr   ÚtensorÚfloatr$   ÚsqrtÚclamp)r   r   Úcm_sumÚchi_squaredÚphi_squaredÚnum_rowsÚnum_colsÚphi_squared_correctedÚrows_correctedÚcols_correctedÚtschuprows_t_valueÚn_rows_tensorÚn_cols_tensors                r   Ú_tschuprows_t_computer8   :   s_  € õ (¨Ñ0Ô0€GØ�[Š[‰]Œ]€FÝ& w°Ñ@Ô@€KØ Ñ&€KØ œÑ€Hˆhàð mÝ@^Ø˜ 8¨VñA
ô A
Ñ=Ð˜~¨~õ Œ9�^ ^Ñ4Ô4¸Ò9Ð9Ý2¸ÐOÑOÔOÐOÝ”<¥ e¡¤°W´^ÐDÑDÔDÐDÝ"œZÐ(=ÅÄ
ÈNÐ]^ÑL^ÐcqÐtuÑcuÑKvÑ@wÔ@wÑ(wÑxÔxÐÐåœ X°kÔ6HÐIÑIÔIˆÝœ X°kÔ6HÐIÑIÔIˆÝ"œZ¨µe´jÀ-ÐRSÑBSÐXeÐhiÑXiÑAjÑ6kÔ6kÑ(kÑlÔlÐØ×#Ò# C¨Ñ-Ô-Ð-r   Tc                 óÒ   — t          ||¦  «         t          t          j        | |g¦  «                             ¦   «         ¦  «        }t          | ||||¦  «        }t          ||¦  «        S )a;  Compute `Tschuprow's T`_ statistic measuring the association between two categorical (nominal) data series.

    .. math::
        T = \sqrt{\frac{\chi^2 / n}{\sqrt{(r - 1) * (k - 1)}}}

    where

    .. math::
        \chi^2 = \sum_{i,j} \ frac{\left(n_{ij} - \frac{n_{i.} n_{.j}}{n}\right)^2}{\frac{n_{i.} n_{.j}}{n}}

    where :math:`n_{ij}` denotes the number of times the values :math:`(A_i, B_j)` are observed with :math:`A_i, B_j`
    represent frequencies of values in ``preds`` and ``target``, respectively.

    Tschuprow's T is a symmetric coefficient, i.e. :math:`T(preds, target) = T(target, preds)`.

    The output values lies in [0, 1] with 1 meaning the perfect association.

    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)

        bias_correction: Indication of whether to use bias correction.
        nan_strategy: Indication of whether to replace or drop ``NaN`` values
        nan_replace_value: Value to replace ``NaN``s when ``nan_strategy = 'replace'``

    Returns:
        Tschuprow's T statistic

    Example:
        >>> from torch import randint, round
        >>> from torchmetrics.functional.nominal import tschuprows_t
        >>> preds = randint(0, 4, (100,))
        >>> target = round(preds + torch.randn(100)).clamp(0, 4)
        >>> tschuprows_t(preds, target)
        tensor(0.4930)

    )r   Úlenr'   ÚcatÚuniquer   r8   )r   r   r   r   r   r   r   s          r   Útschuprows_tr=   Z   sc   € õf ˜lÐ,=Ñ>Ô>Ð>Ý•e”i ¨ Ñ0Ô0×7Ò7Ñ9Ô9Ñ:Ô:€KÝ" 5¨&°+¸|ÐM^Ñ_Ô_€GÝ  ¨/Ñ:Ô:Ð: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          ||¦  «        x|||f<   |||f<   Œ‚|S )aí  Compute `Tschuprow's T`_ statistic between a set of multiple variables.

    This can serve as a convenient tool to compute Tschuprow's T 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

        bias_correction: Indication of whether to use bias correction.
        nan_strategy: Indication of whether to replace or drop ``NaN`` values
        nan_replace_value: Value to replace ``NaN``s when ``nan_strategy = 'replace'``

    Returns:
        Tschuprow's T statistic for a dataset of categorical variables

    Example:
        >>> from torch import randint
        >>> from torchmetrics.functional.nominal import tschuprows_t_matrix
        >>> matrix = randint(0, 4, (200, 5))
        >>> tschuprows_t_matrix(matrix)
        tensor([[1.0000, 0.0637, 0.0000, 0.0542, 0.1337],
                [0.0637, 1.0000, 0.0000, 0.0000, 0.0000],
                [0.0000, 0.0000, 1.0000, 0.0000, 0.0649],
                [0.0542, 0.0000, 0.0000, 1.0000, 0.1100],
                [0.1337, 0.0000, 0.0649, 0.1100, 1.0000]])

    r   r#   r   N)r   r&   r'   Úonesr$   Ú	itertoolsÚcombinationsÚranger:   r;   r<   r   r8   )r>   r   r   r   Únum_variablesÚtschuprows_t_matrix_valueÚiÚjÚxÚyr   r   s               r   Útschuprows_t_matrixrJ   “   sþ   € õH ˜lÐ,=Ñ>Ô>Ð>Ø”L ”O€MÝ %¤
¨=¸-ÐPVÔP]Ð ^Ñ ^Ô ^ÐÝÔ&¥u¨]Ñ';Ô';¸QÑ?Ô?ð 
ð 
‰ˆˆ1Ø�a�a�a˜�dŒ|˜V A A A q Dœ\ˆ1ˆÝ�%œ) Q¨ FÑ+Ô+×2Ò2Ñ4Ô4Ñ5Ô5ˆÝ& q¨!¨[¸,ÐHYÑZÔZˆÝLaØ�_ñM
ô M
ð 	
Ð! ! Q $Ñ'Ð*CÀAÀqÀDÑ*IÐ*Ið %Ð$r   )r   r   )Tr   r   )rA   Útypingr   r'   r   Útyping_extensionsr   Ú7torchmetrics.functional.classification.confusion_matrixr   Ú%torchmetrics.functional.nominal.utilsr   r   r	   r
   r   r   Úintr*   r   Úboolr8   r=   rJ   © r   r   ú<module>rR      s  ðð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à gÐ gÐ gÐ gÐ gÐ gðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð 09Ø),ðKð KØðKàðKð ðKð Ð+Ô,ð	Kð
   ”ðKð ðKð Kð Kð Kð4. 6ð .¸Dð .ÀVð .ð .ð .ð .ðF !Ø/8Ø),ð6;ð 6;Øð6;àð6;ð ð6;ð Ð+Ô,ð	6;ð
   ”ð6;ð ð6;ð 6;ð 6;ð 6;ðv !Ø/8Ø),ð	.%ð .%Øð.%àð.%ð Ð+Ô,ð.%ð   ”ð	.%ð
 ð.%ð .%ð .%ð .%ð .%ð .%r   