§
    ’©ºjSC  ã                   óÊ  — 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mZmZmZmZ d dlmZ d dlmZ 	 	 d"d	ed
eed                  dee         dedef
d„Z	 	 	 	 d#dedededee         d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defd„Z	 	 	 d&dededee         d
eed                  ddf
d„Z	 	 	 	 	 d'dedededed
eed                  dee         d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
eed                  dee         d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Ú+_multilabel_confusion_matrix_arg_validationÚ#_multilabel_confusion_matrix_formatÚ._multilabel_confusion_matrix_tensor_validationÚ#_multilabel_confusion_matrix_update)Ú_safe_divide)ÚClassificationTaskç        ÚconfmatÚaverage)ÚmicroÚmacroÚweightedÚnoneÚbinaryÚignore_indexÚzero_divisionÚreturnc                 óP  — g d¢}||vrt          d|› d|› d�¦  «        ‚|                      ¦   «         } |dk    r0t          | d         | d         | d         z   | d         z   |¬	¦  «        S |d
uod|cxk    o| j        d         k     nc }| j        dk    }|r7| d
d
…ddf         }| d
d
…ddf         | d
d
…ddf         z   | d
d
…ddf         z   }nBt          j        | ¦  «        }|                      d¦  «        |                      d¦  «        z   |z
  }|dk    r5|                     ¦   «         }|                     ¦   «         |r||         ndz
  }t          |||¬	¦  «        }	|�|dk    s|dk    r|	S |dk    r<| j        dk    r| d
d
…ddf         | d
d
…ddf         z   n|                      d¦  «        }
nOt          j        |	¦  «        }
|rd|
|<   |s2d|
|                      d¦  «        |                      d¦  «        z   dk    <   |
|	z  |
                     ¦   «         z                       ¦   «         S )a'  Perform reduction of an un-normalized confusion matrix into jaccard score.

    Args:
        confmat: tensor with un-normalized confusionmatrix
        average: reduction method

            - ``'binary'``: binary reduction, expects a 2x2 matrix
            - ``'macro'``: Calculate the metric for each class separately, and average the
              metrics across classes (with equal weights for each class).
            - ``'micro'``: Calculate the metric globally, across all samples and classes.
            - ``'weighted'``: Calculate the metric for each class separately, and average the
              metrics across classes, weighting each class by its support (``tp + fn``).
            - ``'none'`` or ``None``: Calculate the metric for each class separately, and return
              the metric for every class.

        ignore_index:
            Specifies a target value that is ignored and does not contribute to the metric calculation
        zero_division:
            Value to replace when there is a division by zero. Should be `0` or `1`.

    )r   r   r   r   r   NzThe `average` has to be one of z, got ú.r   )é   r!   )r   r!   )r!   r   )r   Nr   é   r!   r   r   r   r   )	Ú
ValueErrorÚfloatr   ÚshapeÚndimÚtorchÚdiagÚsumÚ	ones_like)r   r   r   r   Úallowed_averageÚignore_index_condÚ
multilabelÚnumÚdenomÚjaccardÚweightss              ú~/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/classification/jaccard.pyÚ_jaccard_index_reducer3   &   s‹  € ð6 MÐLÐL€OØ�oÐ%Ð%ÝÐ\¸?Ð\Ð\ÐRYÐ\Ð\Ð\Ñ]Ô]Ð]Ø�mŠm‰oŒo€GØ�(ÒÐÝ˜G DœM¨G°D¬M¸GÀD¼MÑ,IÈGÐTXÌMÑ,YÐjwÐxÑxÔxÐxà$¨DÐ0ÐY°Q¸,Ð5YÐ5YÒ5YÐ5YÈÌÐWXÔIYÒ5YÐ5YÐ5YÐ5YÐØ” Ò"€JØð 6Ø�a�a�a˜˜A�gÔˆØ˜˜˜˜1˜a˜Ô  7¨1¨1¨1¨a°¨7Ô#3Ñ3°g¸a¸a¸aÀÀA¸gÔ6FÑFˆˆåŒj˜Ñ!Ô!ˆØ—’˜A‘” §¢¨Q¡¤Ñ/°#Ñ5ˆà�'ÒÐØ�gŠg‰iŒiˆØ—	’	‘”Ð6GÐP˜u \Ô2Ð2ÈSÑQˆå˜3 °]ÐCÑCÔC€Gà€˜' VÒ+Ð+¨w¸'Ò/AÐ/AØˆØ�*ÒÐØ9@¼ÈÒ9JÐ9J�'˜!˜!˜!˜Q ˜'Ô" W¨Q¨Q¨Q°°1¨WÔ%5Ñ5Ð5ÐPW×P[ÒP[Ð\]ÑP^ÔP^ˆˆå”/ 'Ñ*Ô*ˆØð 	(Ø$'ˆG�LÑ!Øð 	@Ø<?ˆG�G—K’K ‘N”N W§[¢[°¡^¤^Ñ3°qÒ8Ñ9Ø�wÑ '§+¢+¡-¤-Ñ/×4Ò4Ñ6Ô6Ð6ó    ç      à?TÚpredsÚtargetÚ	thresholdÚvalidate_argsc                 ó¶   — |r!t          ||¦  «         t          | ||¦  «         t          | |||¦  «        \  } }t          | |¦  «        }t	          |d|¬¦  «        S )aì  Calculate the Jaccard index for binary tasks.

    The `Jaccard index`_ (also known as the intersection over union or jaccard similarity coefficient) is an statistic
    that can be used to determine the similarity and diversity of a sample set. It is defined as the size of the
    intersection divided by the union of the sample sets:

    .. math:: J(A,B) = \frac{|A\cap B|}{|A\cup B|}

    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
        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.
        zero_division:
            Value to replace when there is a division by zero. Should be `0` or `1`.

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

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

    r   )r   r   )r   r   r   r	   r3   )r6   r7   r8   r   r9   r   r   s          r2   Úbinary_jaccard_indexr;   d   sk   € ðh ð PÝ/°	¸<ÑHÔHÐHÝ2°5¸&À,ÑOÔOÐOÝ3°E¸6À9ÈlÑ[Ô[�M€Eˆ6Ý-¨e°VÑ<Ô<€GÝ  °(È-ÐXÑXÔXÐXr4   Únum_classes)r   r   r   r   c                 ó^   — t          | |¦  «         d}||vrt          d|› d|› d�¦  «        ‚d S ©N)r   r   r   r   Nz)Expected argument `average` to be one of z
, but got r    )r
   r#   )r<   r   r   r+   s       r2   Ú(_multiclass_jaccard_index_arg_validationr?       sO   € õ
 0°¸\ÑJÔJÐJØB€OØ�oÐ%Ð%ÝÐjÀ_ÐjÐjÐ`gÐjÐjÐjÑkÔkÐkð &Ð%r4   r   c                 ó¼   — |r#t          |||¦  «         t          | |||¦  «         t          | ||¦  «        \  } }t          | ||¦  «        }t	          ||||¬¦  «        S )a"
  Calculate the Jaccard index for multiclass tasks.

    The `Jaccard index`_ (also known as the intersection over union or jaccard similarity coefficient) is an statistic
    that can be used to determine the similarity and diversity of a sample set. It is defined as the size of the
    intersection divided by the union of the sample sets:

    .. math:: J(A,B) = \frac{|A\cap B|}{|A\cup B|}

    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
        average:
            Defines the reduction that is applied over labels. Should be one of the following:

            - ``micro``: Sum statistics over all labels
            - ``macro``: Calculate statistics for each label and average them
            - ``weighted``: calculates statistics for each label and computes weighted average using their support
            - ``"none"`` or ``None``: calculates statistic for each label and applies no reduction

        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.
        zero_division:
            Value to replace when there is a division by zero. Should be `0` or `1`.

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

    Example (pred is float tensor):
        >>> from torchmetrics.functional.classification import multiclass_jaccard_index
        >>> 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_jaccard_index(preds, target, num_classes=3)
        tensor(0.6667)

    ©r   r   r   )r?   r   r   r   r3   )r6   r7   r<   r   r   r9   r   r   s           r2   Úmulticlass_jaccard_indexrB   «   ss   € ð@ ð aÝ0°¸lÈGÑTÔTÐTÝ6°u¸fÀkÐS_Ñ`Ô`Ð`Ý7¸¸vÀ|ÑTÔT�M€Eˆ6Ý1°%¸ÀÑMÔM€GÝ  °'ÈÐdqÐrÑrÔrÐrr4   Ú
num_labelsc                 ó`   — t          | ||¦  «         d}||vrt          d|› d|› d�¦  «        ‚d S r>   )r   r#   )rC   r8   r   r   r+   s        r2   Ú(_multilabel_jaccard_index_arg_validationrE   ó   sQ   € õ 0°
¸IÀ|ÑTÔTÐTØB€OØ�oÐ%Ð%ÝÐjÀ_ÐjÐjÐ`gÐjÐjÐjÑkÔkÐkð &Ð%r4   c                 óÀ   — |r#t          |||¦  «         t          | |||¦  «         t          | ||||¦  «        \  } }t          | ||¦  «        }t	          ||||¬¦  «        S )aZ
  Calculate the Jaccard index for multilabel tasks.

    The `Jaccard index`_ (also known as the intersection over union or jaccard similarity coefficient) is an statistic
    that can be used to determine the similarity and diversity of a sample set. It is defined as the size of the
    intersection divided by the union of the sample sets:

    .. math:: J(A,B) = \frac{|A\cap B|}{|A\cup B|}

    Accepts the following input tensors:

    - ``preds`` (int or float tensor): ``(N, C, ...)``. 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, C, ...)``

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

    Args:
        preds: Tensor with predictions
        target: Tensor with true labels
        num_labels: Integer specifying the number of labels
        threshold: Threshold for transforming probability to binary (0,1) predictions
        average:
            Defines the reduction that is applied over labels. Should be one of the following:

            - ``micro``: Sum statistics over all labels
            - ``macro``: Calculate statistics for each label and average them
            - ``weighted``: calculates statistics for each label and computes weighted average using their support
            - ``"none"`` or ``None``: calculates statistic for each label and applies no reduction

        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.
        zero_division:
            Value to replace when there is a division by zero. Should be `0` or `1`.

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

    Example (preds is float tensor):
        >>> from torchmetrics.functional.classification import multilabel_jaccard_index
        >>> target = tensor([[0, 1, 0], [1, 0, 1]])
        >>> preds = tensor([[0.11, 0.22, 0.84], [0.73, 0.33, 0.92]])
        >>> multilabel_jaccard_index(preds, target, num_labels=3)
        tensor(0.5000)

    rA   )rE   r   r   r   r3   )	r6   r7   rC   r8   r   r   r9   r   r   s	            r2   Úmultilabel_jaccard_indexrG   ÿ   sy   € ð~ ð `Ý0°¸YÈÑUÔUÐUÝ6°u¸fÀjÐR^Ñ_Ô_Ð_Ý7¸¸vÀzÐS\Ð^jÑkÔk�M€Eˆ6Ý1°%¸ÀÑLÔL€GÝ  °'ÈÐdqÐrÑrÔrÐrr4   Útask)r   Ú
multiclassr-   c
           
      ó   — t          j        |¦  «        }|t           j        k    rt          | |||||	¦  «        S |t           j        k    rJt          |t          ¦  «        s t          dt          |¦  «        › d�¦  «        ‚t          | ||||||	¦  «        S |t           j
        k    rKt          |t          ¦  «        s t          dt          |¦  «        › d�¦  «        ‚t          | |||||||	¦  «        S t          d|› �¦  «        ‚)a¿  Calculate the Jaccard index.

    The `Jaccard index`_ (also known as the intersection over union or jaccard similarity coefficient) is an statistic
    that can be used to determine the similarity and diversity of a sample set. It is defined as the size of the
    intersection divided by the union of the sample sets:

    .. math:: J(A,B) = \frac{|A\cap B|}{|A\cup B|}

    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'``, ``'multiclass'`` or ``'multilabel'``. See the documentation of
    :func:`~torchmetrics.functional.classification.binary_jaccard_index`,
    :func:`~torchmetrics.functional.classification.multiclass_jaccard_index` and
    :func:`~torchmetrics.functional.classification.multilabel_jaccard_index` for
    the specific details of each argument influence and examples.

    Legacy Example:
        >>> from torch import randint, tensor
        >>> target = randint(0, 2, (10, 25, 25))
        >>> pred = tensor(target)
        >>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15]
        >>> jaccard_index(pred, target, task="multiclass", num_classes=2)
        tensor(0.9660)

    z+`num_classes` is expected to be `int` but `z was passed.`z*`num_labels` is expected to be `int` but `zNot handled value: )r   Úfrom_strÚBINARYr;   Ú
MULTICLASSÚ
isinstanceÚintr#   ÚtyperB   Ú
MULTILABELrG   )
r6   r7   rH   r8   r<   rC   r   r   r9   r   s
             r2   Újaccard_indexrR   F  s  € õH Ô& tÑ,Ô,€DØÕ!Ô(Ò(Ð(Ý# E¨6°9¸lÈMÐ[hÑiÔiÐiØÕ!Ô,Ò,Ð,Ý˜+¥sÑ+Ô+ð 	mÝÐkÍ4ÐP[ÑK\ÔK\ÐkÐkÐkÑlÔlÐlÝ'¨¨v°{ÀGÈ\Ð[hÐjwÑxÔxÐxØÕ!Ô,Ò,Ð,Ý˜*¥cÑ*Ô*ð 	kÝÐiÍ$ÈzÑJZÔJZÐiÐiÐiÑjÔjÐjÝ'Ø�6˜: y°'¸<ÈÐXeñ
ô 
ð 	
õ Ð1¨4Ð1Ð1Ñ
2Ô
2Ð2r4   )Nr   )r5   NTr   )NN)r   NTr   )r5   Nr   )r5   r   NTr   )r5   NNr   NTr   )!Útypingr   r'   r   Útyping_extensionsr   Ú7torchmetrics.functional.classification.confusion_matrixr   r   r   r	   r
   r   r   r   r   r   r   r   Útorchmetrics.utilities.computer   Útorchmetrics.utilities.enumsr   rO   r$   r3   Úboolr;   r?   rB   rE   rG   rR   © r4   r2   ú<module>rZ      s¥  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð 8Ð 7Ð 7Ð 7Ð 7Ð 7Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;ð #'Øð	;7ð ;7Øð;7à�gÐLÔMÔNð;7ð ˜3”-ð;7ð ð	;7ð
 ð;7ð ;7ð ;7ð ;7ðB Ø"&ØØð9Yð 9YØð9Yàð9Yð ð9Yð ˜3”-ð	9Yð
 ð9Yð ð9Yð ð9Yð 9Yð 9Yð 9Yð| #'ØGKðlð lØðlà˜3”-ðlð �gÐBÔCÔDðlð 
ð	lð lð lð lð HOØ"&ØØðEsð EsØðEsàðEsð ðEsð �gÐBÔCÔDð	Esð
 ˜3”-ðEsð ðEsð ðEsð ðEsð Esð Esð EsðT Ø"&ØGNð		lð 	lØð	làð	lð ˜3”-ð	lð �gÐBÔCÔDð		lð
 
ð	lð 	lð 	lð 	lð  ØGNØ"&ØØðDsð DsØðDsàðDsð ðDsð ð	Dsð
 �gÐBÔCÔDðDsð ˜3”-ðDsð ðDsð ðDsð ðDsð Dsð Dsð DsðV Ø!%Ø $ØGNØ"&ØØð13ð 13Øð13àð13ð Ð6Ô
7ð13ð ð	13ð
 ˜#”ð13ð ˜”ð13ð �gÐBÔCÔDð13ð ˜3”-ð13ð ð13ð ð13ð ð13ð 13ð 13ð 13ð 13ð 13r4   