§
    ’©ºjq-  ã                   ó0  — d dl mZ d dl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mZmZ d dlmZ ddedeed	                  d
efd„Z	 	 	 ddedee         deed	                  d
dfd„Z	 	 	 	 ddedededeed	                  dee         ded
efd„Z	 	 ddedee         deed	                  d
dfd„Z	 	 	 ddedededeed	                  dee         ded
efd„Z	 	 	 	 	 d dededed         dedee         deed	                  dee         ded
efd„ZdS )!é    )ÚOptionalN)ÚTensor)ÚLiteral)Ú'_binary_confusion_matrix_arg_validationÚ_binary_confusion_matrix_formatÚ*_binary_confusion_matrix_tensor_validationÚ_binary_confusion_matrix_updateÚ+_multiclass_confusion_matrix_arg_validationÚ#_multiclass_confusion_matrix_formatÚ._multiclass_confusion_matrix_tensor_validationÚ#_multiclass_confusion_matrix_update)ÚClassificationTaskNoMultilabelÚconfmatÚweights)ÚlinearÚ	quadraticÚnoneÚreturnc                 ó&  — |                       ¦   «         s|                      ¦   «         n| } | j        d         }|                      dd¬¦  «        }|                      dd¬¦  «        }||z  |                     ¦   «         z  }|�|dk    rHt	          j        | ¦  «                             ¦   «         }d|dd|dz   …<   |                     ||¦  «        }n�|dv rxt	          j        | ¦  «        }|t	          j	        ||j
        |j        ¬¦  «        z  }|d	k    rt	          j        ||j        z
  ¦  «        nt	          j        ||j        z
  d
¦  «        }nt          d|› d�¦  «        ‚t	          j        || z  ¦  «        t	          j        ||z  ¦  «        z  }d|z
  S )zdReduce an un-normalized confusion matrix of shape (n_classes, n_classes) into the cohen kappa score.r   T)ÚdimÚkeepdimé   Nr   )r   r   )ÚdtypeÚdevicer   g       @z	Received zL for argument ``weights`` but should be either None, 'linear' or 'quadratic')Úis_floating_pointÚfloatÚshapeÚsumÚtorchÚ	ones_likeÚflattenÚreshapeÚ
zeros_likeÚaranger   r   ÚabsÚTÚpowÚ
ValueError)r   r   Únum_classesÚsum0Úsum1ÚexpectedÚw_matÚks           ú‚/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/classification/cohen_kappa.pyÚ_cohen_kappa_reducer0   !   s�  € à%,×%>Ò%>Ñ%@Ô%@ÐMˆg�mŠm‰oŒoˆoÀg€GØ”- Ô"€KØ�;Š;˜1 dˆ;Ñ+Ô+€DØ�;Š;˜1 dˆ;Ñ+Ô+€DØ�d‰{˜TŸXšX™ZœZÑ'€Hà€˜' VÒ+Ð+Ý” Ñ(Ô(×0Ò0Ñ2Ô2ˆØ$%ˆÐ Ð �˜q‘Ð Ñ!Ø—’˜k¨;Ñ7Ô7ˆˆØ	Ð+Ð	+Ð	+ÝÔ  Ñ)Ô)ˆØ•”˜k°´ÀUÄ\ÐRÑRÔRÑRˆØ.5¸Ò.AÐ.A•”	˜% %¤'™/Ñ*Ô*Ð*ÅuÄyÐQVÐY^ÔY`ÑQ`ÐbeÑGfÔGfˆˆåØm˜ÐmÐmÐmñ
ô 
ð 	
õ 	Œ	�%˜'‘/Ñ"Ô"¥U¤Y¨u°xÑ/?Ñ%@Ô%@Ñ@€AØˆq‰5€Ló    ç      à?Ú	thresholdÚignore_indexc                 ób   — t          | |d¬¦  «         d}||vrt          d|› d|› d�¦  «        ‚dS )zÊValidate non tensor input.

    - ``threshold`` has to be a float in the [0,1] range
    - ``ignore_index`` has to be None or int
    - ``weights`` has to be "linear" | "quadratic" | "none" | None

    N©Ú	normalize©r   r   r   Nú(Expected argument `weight` to be one of ú
, but got ú.)r   r(   )r3   r4   r   Úallowed_weightss       r/   Ú"_binary_cohen_kappa_arg_validationr=   9   sT   € õ ,¨I°|ÈtÐTÑTÔTÐTØ;€OØ�oÐ%Ð%ÝÐiÀOÐiÐiÐ_fÐiÐiÐiÑjÔjÐjð &Ð%r1   TÚpredsÚtargetÚvalidate_argsc                 ó´   — |r"t          |||¦  «         t          | ||¦  «         t          | |||¦  «        \  } }t          | |¦  «        }t	          ||¦  «        S )a¹  Calculate `Cohen's kappa score`_ that measures inter-annotator agreement for binary tasks.

    .. math::
        \kappa = (p_o - p_e) / (1 - p_e)

    where :math:`p_o` is the empirical probability of agreement and :math:`p_e` is
    the expected agreement when both annotators assign labels randomly. Note that
    :math:`p_e` is estimated using a per-annotator empirical prior over the
    class labels.

    Accepts the following input tensors:

    - ``preds`` (int or float tensor): ``(N, ...)``. If preds is a floating point tensor with values outside
      [0,1] range we consider the input to be logits and will auto apply sigmoid per element. Additionally,
      we convert to int tensor with thresholding using the value in ``threshold``.
    - ``target`` (int tensor): ``(N, ...)``

    Additional dimension ``...`` will be flattened into the batch dimension.

    Args:
        preds: Tensor with predictions
        target: Tensor with true labels
        threshold: Threshold for transforming probability to binary (0,1) predictions
        weights: Weighting type to calculate the score. Choose from:

            - ``None`` or ``'none'``: no weighting
            - ``'linear'``: linear weighting
            - ``'quadratic'``: quadratic weighting
        ignore_index:
            Specifies a target value that is ignored and does not contribute to the metric calculation
        validate_args: bool indicating if input arguments and tensors should be validated for correctness.
            Set to ``False`` for faster computations.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example (preds is int tensor):
        >>> from torch import tensor
        >>> from torchmetrics.functional.classification import binary_cohen_kappa
        >>> target = tensor([1, 1, 0, 0])
        >>> preds = tensor([0, 1, 0, 0])
        >>> binary_cohen_kappa(preds, target)
        tensor(0.5000)

    Example (preds is float tensor):
        >>> from torchmetrics.functional.classification import binary_cohen_kappa
        >>> target = tensor([1, 1, 0, 0])
        >>> preds = tensor([0.35, 0.85, 0.48, 0.01])
        >>> binary_cohen_kappa(preds, target)
        tensor(0.5000)

    )r=   r   r   r	   r0   )r>   r?   r3   r   r4   r@   r   s          r/   Úbinary_cohen_kapparB   K   sh   € ðt ð PÝ*¨9°lÀGÑLÔLÐLÝ2°5¸&À,ÑOÔOÐOÝ3°E¸6À9ÈlÑ[Ô[�M€Eˆ6Ý-¨e°VÑ<Ô<€GÝ˜w¨Ñ0Ô0Ð0r1   r)   c                 ób   — t          | |d¬¦  «         d}||vrt          d|› d|› d�¦  «        ‚dS )zÅValidate non tensor input.

    - ``num_classes`` has to be a int larger than 1
    - ``ignore_index`` has to be None or int
    - ``weights`` has to be "linear" | "quadratic" | "none" | None

    Nr6   r8   r9   r:   r;   )r
   r(   )r)   r4   r   r<   s       r/   Ú&_multiclass_cohen_kappa_arg_validationrD   �   sU   € õ 0°¸\ÐUYÐZÑZÔZÐZØ;€OØ�oÐ%Ð%ÝÐiÀOÐiÐiÐ_fÐiÐiÐiÑjÔjÐjð &Ð%r1   c                 ó¶   — |r#t          |||¦  «         t          | |||¦  «         t          | ||¦  «        \  } }t          | ||¦  «        }t	          ||¦  «        S )a#	  Calculate `Cohen's kappa score`_ that measures inter-annotator agreement for multiclass tasks.

    .. math::
        \kappa = (p_o - p_e) / (1 - p_e)

    where :math:`p_o` is the empirical probability of agreement and :math:`p_e` is
    the expected agreement when both annotators assign labels randomly. Note that
    :math:`p_e` is estimated using a per-annotator empirical prior over the
    class labels.

    Accepts the following input tensors:

    - ``preds``: ``(N, ...)`` (int tensor) or ``(N, C, ..)`` (float tensor). If preds is a floating point
      we apply ``torch.argmax`` along the ``C`` dimension to automatically convert probabilities/logits into
      an int tensor.
    - ``target`` (int tensor): ``(N, ...)``

    Additional dimension ``...`` will be flattened into the batch dimension.

    Args:
        preds: Tensor with predictions
        target: Tensor with true labels
        num_classes: Integer specifying the number of classes
        weights: Weighting type to calculate the score. Choose from:

            - ``None`` or ``'none'``: no weighting
            - ``'linear'``: linear weighting
            - ``'quadratic'``: quadratic weighting


        ignore_index:
            Specifies a target value that is ignored and does not contribute to the metric calculation
        validate_args: bool indicating if input arguments and tensors should be validated for correctness.
            Set to ``False`` for faster computations.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example (pred is integer tensor):
        >>> from torch import tensor
        >>> from torchmetrics.functional.classification import multiclass_cohen_kappa
        >>> target = tensor([2, 1, 0, 0])
        >>> preds = tensor([2, 1, 0, 1])
        >>> multiclass_cohen_kappa(preds, target, num_classes=3)
        tensor(0.6364)

    Example (pred is float tensor):
        >>> from torchmetrics.functional.classification import multiclass_cohen_kappa
        >>> target = tensor([2, 1, 0, 0])
        >>> preds = tensor([[0.16, 0.26, 0.58],
        ...                 [0.22, 0.61, 0.17],
        ...                 [0.71, 0.09, 0.20],
        ...                 [0.05, 0.82, 0.13]])
        >>> multiclass_cohen_kappa(preds, target, num_classes=3)
        tensor(0.6364)

    )rD   r   r   r   r0   )r>   r?   r)   r   r4   r@   r   s          r/   Úmulticlass_cohen_kapparF   Ÿ   sk   € ð~ ð aÝ.¨{¸LÈ'ÑRÔRÐRÝ6°u¸fÀkÐS_Ñ`Ô`Ð`Ý7¸¸vÀ|ÑTÔT�M€Eˆ6Ý1°%¸ÀÑMÔM€GÝ˜w¨Ñ0Ô0Ð0r1   Útask)ÚbinaryÚ
multiclassc                 óH  — t          j        |¦  «        }|t           j        k    rt          | |||||¦  «        S |t           j        k    rIt          |t          ¦  «        s t          dt          |¦  «        › d�¦  «        ‚t          | |||||¦  «        S t          d|› �¦  «        ‚)a7  Calculate `Cohen's kappa score`_ that measures inter-annotator agreement. It is defined as.

    .. math::
        \kappa = (p_o - p_e) / (1 - p_e)

    where :math:`p_o` is the empirical probability of agreement and :math:`p_e` is
    the expected agreement when both annotators assign labels randomly. Note that
    :math:`p_e` is estimated using a per-annotator empirical prior over the
    class labels.

    This function is a simple wrapper to get the task specific versions of this metric, which is done by setting the
    ``task`` argument to either ``'binary'`` or ``'multiclass'``. See the documentation of
    :func:`~torchmetrics.functional.classification.binary_cohen_kappa` and
    :func:`~torchmetrics.functional.classification.multiclass_cohen_kappa` for the specific details of
    each argument influence and examples.

    Legacy Example:
        >>> from torch import tensor
        >>> target = tensor([1, 1, 0, 0])
        >>> preds = tensor([0, 1, 0, 0])
        >>> cohen_kappa(preds, target, task="multiclass", num_classes=2)
        tensor(0.5000)

    z+`num_classes` is expected to be `int` but `z was passed.`zNot handled value: )
r   Úfrom_strÚBINARYrB   Ú
MULTICLASSÚ
isinstanceÚintr(   ÚtyperF   )r>   r?   rG   r3   r)   r   r4   r@   s           r/   Úcohen_kapparQ   æ   s²   € õD *Ô2°4Ñ8Ô8€DØÕ-Ô4Ò4Ð4Ý! %¨°¸GÀ\ÐS`ÑaÔaÐaØÕ-Ô8Ò8Ð8Ý˜+¥sÑ+Ô+ð 	mÝÐkÍ4ÐP[ÑK\ÔK\ÐkÐkÐkÑlÔlÐlÝ% e¨V°[À'È<ÐYfÑgÔgÐgÝ
Ð1¨4Ð1Ð1Ñ
2Ô
2Ð2r1   )N)r2   NN)r2   NNT)NN)NNT)r2   NNNT)Útypingr   r   r   Útyping_extensionsr   Ú7torchmetrics.functional.classification.confusion_matrixr   r   r   r	   r
   r   r   r   Útorchmetrics.utilities.enumsr   r0   r   rO   r=   ÚboolrB   rD   rF   rQ   © r1   r/   ú<module>rX      sV  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð HÐ GÐ GÐ GÐ GÐ Gðð  ð °(¸7ÐC`Ô;aÔ2bð Ðntð ð ð ð ð2 Ø"&Ø@Dðkð kØðkà˜3”-ðkð �gÐ;Ô<Ô=ðkð 
ð	kð kð kð kð* Ø@DØ"&Øð?1ð ?1Øð?1àð?1ð ð?1ð �gÐ;Ô<Ô=ð	?1ð
 ˜3”-ð?1ð ð?1ð ð?1ð ?1ð ?1ð ?1ðH #'Ø@Dðkð kØðkà˜3”-ðkð �gÐ;Ô<Ô=ðkð 
ð	kð kð kð kð, AEØ"&ØðD1ð D1ØðD1àðD1ð ðD1ð �gÐ;Ô<Ô=ð	D1ð
 ˜3”-ðD1ð ðD1ð ðD1ð D1ð D1ð D1ðV Ø!%Ø@DØ"&Øð)3ð )3Øð)3àð)3ð Ð(Ô
)ð)3ð ð	)3ð
 ˜#”ð)3ð �gÐ;Ô<Ô=ð)3ð ˜3”-ð)3ð ð)3ð ð)3ð )3ð )3ð )3ð )3ð )3r1   