§
    ’©ºjq  ã                   ó^  — 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edee	         ddfd	„Z
d
edefd„Zd
ededefd„Zd
edefd„Zdededededef
d„Zdedededeeef         fd„Zdededededeeeef         f
d„Z	 	 ddededed         dee	         deeef         f
d„Zdeddfd„ZdS )é    )ÚOptionalN)ÚTensor)ÚLiteral©Úrank_zero_warnÚnan_strategyÚnan_replace_valueÚreturnc                 óž   — | dvrt          d| › �¦  «        ‚| dk    r.t          |t          t          f¦  «        st          d|› �¦  «        ‚d S d S )N©ÚreplaceÚdropzPArgument `nan_strategy` is expected to be one of `['replace', 'drop']`, but got r   zlArgument `nan_replace` is expected to be of a type `int` or `float` when `nan_strategy = 'replace`, but got )Ú
ValueErrorÚ
isinstanceÚfloatÚint)r   r	   s     úu/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/nominal/utils.pyÚ_nominal_input_validationr      s   € ØÐ.Ð.Ð.ÝØmÐ_kÐmÐmñ
ô 
ð 	
ð �yÒ Ð ­Ð4EÍÍsÀ|Ñ)TÔ)TÐ Ýð+Ø(ð+ð +ñ
ô 
ð 	
ð !Ð Ð Ð ó    Úconfmatc                 ó¬   — |                       d¦  «        |                       d¦  «        }}t          j        d||¦  «        |                       ¦   «         z  S )zDCompute the expected frequenceis from the provided confusion matrix.é   r   z
r, c -> rc)ÚsumÚtorchÚeinsum)r   Úmargin_sum_rowsÚmargin_sum_colss      r   Ú_compute_expected_freqsr   #   s@   € à'.§{¢{°1¡~¤~°w·{²{À1±~´~�_€OÝŒ<˜ o°ÑGÔGÈ'Ï+Ê+É-Ì-ÑWÐWr   Úbias_correctionc                 óÊ  — t          | ¦  «        }|                     ¦   «         t          |j        ¦  «        z
  |j        z   dz
  }|dk    rt          j        d| j        ¬¦  «        S |dk    r]|r[|| z
  }|                     ¦   «         }| |t          j	        dt          j
        |¦  «        z  |                     ¦   «         ¦  «        z  z  } t          j        | |z
  dz  |z  ¦  «        S )z¨Chi-square test of independenc of variables in a confusion matrix table.

    Adapted from: https://github.com/scipy/scipy/blob/v1.9.2/scipy/stats/contingency.py.

    r   r   ç        ©Údeviceg      à?é   )r   Únumelr   ÚshapeÚndimr   Útensorr#   ÚsignÚminimumÚ	ones_likeÚabs)r   r   Úexpected_freqsÚdfÚdiffÚ	directions         r   Ú_compute_chi_squaredr1   )   sÕ   € õ -¨WÑ5Ô5€Nà	×	Ò	Ñ	Ô	¥# nÔ&:Ñ";Ô";Ñ	;¸nÔ>QÑ	QÐTUÑ	U€BØ	ˆQ‚w€wÝŒ|˜C¨¬Ð7Ñ7Ô7Ð7à	ˆQ‚w€w�?€wØ Ñ'ˆØ—I’I‘K”Kˆ	Ø�9�uœ}¨Sµ5´?À9Ñ3MÔ3MÑ-MÈyÏ}Ê}ÉÌÑ_Ô_Ñ_Ñ_ˆåŒ9�g Ñ.°1Ñ4°~ÑEÑFÔFÐFr   c                 ó†   — | |                       d¦  «        dk             } | dd…|                       d¦  «        dk    f         S )aû  Drop all rows and columns containing only zeros.

    Example:
        >>> from torch import randint
        >>> from torchmetrics.functional.nominal.utils import _drop_empty_rows_and_cols
        >>> matrix = randint(10, size=(4, 3))
        >>> matrix[1, :] = matrix[:, 1] = 0
        >>> matrix
        tensor([[2, 0, 6],
                [0, 0, 0],
                [0, 0, 0],
                [3, 0, 4]])
        >>> _drop_empty_rows_and_cols(matrix)
        tensor([[2, 6],
                [3, 4]])

    r   r   N)r   )r   s    r   Ú_drop_empty_rows_and_colsr3   =   s@   € ð$ �g—k’k !‘n”n¨Ò)Ô*€GØ�1�1�1�g—k’k !‘n”n¨Ò)Ð)Ô*Ð*r   Úphi_squaredÚnum_rowsÚnum_colsÚconfmat_sumc                 ó‚   — t          j        t          j        d| j        ¬¦  «        | |dz
  |dz
  z  |dz
  z  z
  ¦  «        S )z#Compute bias-corrected Phi Squared.r!   r"   r   )r   Úmaxr(   r#   )r4   r5   r6   r7   s       r   Ú_compute_phi_squared_correctedr:   S   sK   € õ Œ9ÝŒ�S Ô!3Ð4Ñ4Ô4Ø˜ 1™¨°A©Ñ6¸;È¹?ÑKÑKñô ð r   c                 óN   — | | dz
  dz  |dz
  z  z
  }||dz
  dz  |dz
  z  z
  }||fS )z2Compute bias-corrected number of rows and columns.r   r$   © )r5   r6   r7   Úrows_correctedÚcols_correcteds        r   Ú _compute_rows_and_cols_correctedr?   `   sE   € à ¨A¡°!Ñ 3°{ÀQ±Ñ GÑG€NØ ¨A¡°!Ñ 3°{ÀQ±Ñ GÑG€NØ˜>Ð)Ð)r   c                 óX   — t          | |||¦  «        }t          |||¦  «        \  }}|||fS )zBCompute bias-corrected Phi Squared and number of rows and columns.)r:   r?   )r4   r5   r6   r7   Úphi_squared_correctedr=   r>   s          r   Ú_compute_bias_corrected_valuesrB   g   s?   € õ ;¸;ÈÐRZÐ\gÑhÔhÐÝ%EÀhÐPXÐZeÑ%fÔ%fÑ"€N�NØ  .°.Ð@Ð@r   r   r!   ÚpredsÚtargetr   c                 óø   — |dk    r*|                       |¦  «        |                      |¦  «        fS t          j        |                      ¦   «         |                     ¦   «         ¦  «        }| |          ||          fS )a0  Handle ``NaN`` values in input data.

    If ``nan_strategy = 'replace'``, all ``NaN`` values are replaced with ``nan_replace_value``.
    If ``nan_strategy = 'drop'``, all rows containing ``NaN`` in any of two vectors are dropped.

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

    Returns:
        Updated ``preds`` and ``target`` tensors which contain no ``Nan``

    Raises:
        ValueError: If ``nan_strategy`` is not from ``['replace', 'drop']``.
        ValueError: If ``nan_strategy = replace`` and ``nan_replace_value`` is not of a type ``int`` or ``float``.

    r   )Ú
nan_to_numr   Ú
logical_orÚisnan)rC   rD   r   r	   Úrows_contain_nans        r   Ú_handle_nan_in_datarJ   p   sx   € ð2 �yÒ Ð Ø×ÒÐ 1Ñ2Ô2°F×4EÒ4EÐFWÑ4XÔ4XÐXÐXÝÔ'¨¯ª©¬°v·|²|±~´~ÑFÔFÐØÐ"Ð"Ô# VÐ-=Ð,=Ô%>Ð>Ð>r   Úmetric_namec                 ó,   — t          d| › d�¦  «         d S )NzUnable to compute zG using bias correction. Please consider to set `bias_correction=False`.r   )rK   s    r   Ú&_unable_to_use_bias_correction_warningrM   �   s,   € ÝØq˜[ÐqÐqÐqñô ð ð ð r   )r   r!   )Útypingr   r   r   Útyping_extensionsr   Útorchmetrics.utilities.printsr   Ústrr   r   r   Úboolr1   r3   r   r:   Útupler?   rB   rJ   rM   r<   r   r   ú<module>rT      s_  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à 8Ð 8Ð 8Ð 8Ð 8Ð 8ð	
¨Cð 	
ÀHÈUÄOð 	
ÐX\ð 	
ð 	
ð 	
ð 	
ðX Vð X°ð Xð Xð Xð XðG &ð G¸4ð GÀFð Gð Gð Gð Gð(+ vð +°&ð +ð +ð +ð +ð,
Øð
àð
ð ð
ð ð	
ð
 ð
ð 
ð 
ð 
ð*¨sð *¸cð *ÐPVð *Ð[`ÐagÐioÐaoÔ[pð *ð *ð *ð *ðAØðAØ#&ðAØ25ðAØDJðAà
ˆ6�6˜6Ð!Ô"ðAð Að Að Að 09Ø),ð	?ð ?Øð?àð?ð Ð+Ô,ð?ð   ”ð	?ð
 ˆ6�6ˆ>Ôð?ð ?ð ?ð ?ð>¸ð Àð ð ð ð ð ð r   