§
    ’©ºjèA  ã                   ó®  — d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZ dej        d	ed
dfd„Zdej        d
ej        fd„Z	 	 	 d%dej        dej        dej        d	ededee         ded
eeej        ej        ej        ej        f                  fd„Zdeeej        ej        ej        ej        f                  d
eeej        f         fd„Zdeeej        ej        ej        ej        f                  d
eeej        f         fd„Z	 	 	 d%dej        dej        dej        d	ededee         ded
eeej        f         fd„Zdej        dej        dej        dej        d
eeej        f         f
d„Z	 	 	 d%dej        dej        dedee         ded
eeej        f         fd„Zdej        dej        dej        dej        d
eeej        f         f
d„Z 	 	 	 d%dej        dej        dej        dedee         ded
eeej        f         fd „Z!	 	 	 	 d&dej        dej        dej        d"ed#         dedee         ded
eeej        f         fd$„Z"dS )'é    )ÚOptionalN)ÚLiteral)Ú"_binary_stat_scores_arg_validationÚ_binary_stat_scores_formatÚ%_binary_stat_scores_tensor_validationÚ_binary_stat_scores_update)Úrank_zero_warn)Ú_safe_divide)Ú_flexible_bincountÚgroupsÚ
num_groupsÚreturnc                 óä   — t          j        | ¦  «        |k    r*t          dt          j        | ¦  «        › d�d|› d�¦  «        ‚| j        t           j        k    rt          d| j        › d�¦  «        ‚dS )zçValidate groups tensor.

    - The largest number in the tensor should not be larger than the number of groups. The group identifiers should
    be ``0, 1, ..., (num_groups - 1)``.
    - The group tensor should be dtype long.

    z+The largest number in the groups tensor is z$, which is larger than the specifiedznumber of groups zB. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.z2Expected dtype of argument groups to be long, not ú.N)ÚtorchÚmaxÚ
ValueErrorÚdtypeÚlong)r   r   s     ú…/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/classification/group_fairness.pyÚ_groups_validationr      s†   € õ „y�ÑÔ˜:Ò%Ð%ÝØq½%¼)ÀFÑ:KÔ:KÐqÐqÐqØn 
ÐnÐnÐnñ
ô 
ð 	
ð „|•u”zÒ!Ð!ÝÐ]ÈfÌlÐ]Ð]Ð]Ñ^Ô^Ð^ð "Ð!ó    c                 óD   — |                       | j        d         d¦  «        S )z1Reshape groups to correspond to preds and target.r   éÿÿÿÿ)ÚreshapeÚshape)r   s    r   Ú_groups_formatr   /   s   € à�>Š>˜&œ, qœ/¨2Ñ.Ô.Ð.r   ç      à?TÚpredsÚtargetÚ	thresholdÚignore_indexÚvalidate_argsc                 óv  — |r3t          |d|¦  «         t          | |d|¦  «         t          ||¦  «         t          | |||¦  «        \  } }t	          |¦  «        }t          j        |                     d¦  «        ¦  «        \  }}| |         } ||         }t          |¦  «         	                    ¦   «          
                    ¦   «                              ¦   «         }	t          t          j        | |	d¬¦  «        ¦  «        }
t          t          j        ||	d¬¦  «        ¦  «        }d„ t          |
|¦  «        D ¦   «         S )z—Compute the true/false positives and true/false negatives rates for binary classification by group.

    Related to `Type I and Type II errors`_.

    Úglobalé   r   )Údimc                 ó4   — g | ]\  }}t          ||¦  «        ‘ŒS © )r   )Ú.0Úgroup_pÚgroup_ts      r   ú
<listcomp>z._binary_groups_stat_scores.<locals>.<listcomp>S   s(   € ÐpÐpÐpÑ=M¸WÀgÕ& w°Ñ8Ô8ÐpÐpÐpr   )r   r   r   r   r   r   ÚsortÚsqueezer   ÚdetachÚcpuÚtolistÚlistÚsplitÚzip)r   r    r   r   r!   r"   r#   ÚindexesÚindicesÚsplit_sizesÚgroup_predsÚgroup_targets               r   Ú_binary_groups_stat_scoresr;   4   s&  € ð ð /Ý*¨9°hÀÑMÔMÐMÝ-¨e°V¸XÀ|ÑTÔTÐTÝ˜6 :Ñ.Ô.Ð.å.¨u°f¸iÈÑVÔV�M€Eˆ6Ý˜FÑ#Ô#€Få”z &§.¢.°Ñ"3Ô"3Ñ4Ô4Ñ€GˆWØ�'ŒN€EØ�GŒ_€Få$ WÑ-Ô-×4Ò4Ñ6Ô6×:Ò:Ñ<Ô<×CÒCÑEÔE€Kå•u”{ 5¨+¸1Ð=Ñ=Ô=Ñ>Ô>€KÝ�œ F¨K¸QÐ?Ñ?Ô?Ñ@Ô@€LàpÐpÕQTÐU`ÐbnÑQoÔQoÐpÑpÔpÐpr   Úgroup_statsc                 ó4   — d„ t          | ¦  «        D ¦   «         S )z+Compute rates for all the group statistics.c                 ó’   — i | ]D\  }}d |› �t          j        |¦  «        t          j        |¦  «                             ¦   «         z  “ŒES )Úgroup_)r   ÚstackÚsum)r*   ÚgroupÚstatss      r   ú
<dictcomp>z"_groups_reduce.<locals>.<dictcomp>Z   sO   € ÐwÐwÐwÑP\ÐPUÐW\Ð�UÐÐ�eœk¨%Ñ0Ô0µ5´;¸uÑ3EÔ3E×3IÒ3IÑ3KÔ3KÑKÐwÐwÐwr   )Ú	enumerate©r<   s    r   Ú_groups_reducerG   V   s#   € ð xÐwÕ`iÐjuÑ`vÔ`vÐwÑwÔwÐwr   c                 óð   — t          j        d„ | D ¦   «         ¦  «        t          j        d„ | D ¦   «         ¦  «        t          j        d„ | D ¦   «         ¦  «        t          j        d„ | D ¦   «         ¦  «        dœS )zCTransform group statistics by creating a tensor for each statistic.c                 ó   — g | ]
}|d          ‘ŒS )r   r)   ©r*   Ústats     r   r-   z*_groups_stat_transform.<locals>.<listcomp>b   ó   € Ð;Ð;Ð; t˜4 œ7Ð;Ð;Ð;r   c                 ó   — g | ]
}|d          ‘ŒS )r&   r)   rJ   s     r   r-   z*_groups_stat_transform.<locals>.<listcomp>c   rL   r   c                 ó   — g | ]
}|d          ‘ŒS )é   r)   rJ   s     r   r-   z*_groups_stat_transform.<locals>.<listcomp>d   rL   r   c                 ó   — g | ]
}|d          ‘ŒS )é   r)   rJ   s     r   r-   z*_groups_stat_transform.<locals>.<listcomp>e   rL   r   )ÚtpÚfpÚtnÚfn)r   r@   rF   s    r   Ú_groups_stat_transformrV   ]   sƒ   € õ
 ŒkÐ;Ð;¨{Ð;Ñ;Ô;Ñ<Ô<ÝŒkÐ;Ð;¨{Ð;Ñ;Ô;Ñ<Ô<ÝŒkÐ;Ð;¨{Ð;Ñ;Ô;Ñ<Ô<ÝŒkÐ;Ð;¨{Ð;Ñ;Ô;Ñ<Ô<ð	ð ð r   c           	      óJ   — t          | ||||||¦  «        }t          |¦  «        S )a3	  Compute the true/false positives and true/false negatives rates for binary classification by group.

    Related to `Type I and Type II errors`_.

    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, ...)``.
    - ``groups`` (int tensor): ``(N, ...)``. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.

    The additional dimensions are flatted along the batch dimension.

    Args:
        preds: Tensor with predictions.
        target: Tensor with true labels.
        groups: Tensor with group identifiers. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.
        num_groups: The number of groups.
        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.

    Returns:
        The metric returns a dict with a group identifier as key and a tensor with the tp, fp, tn and fn rates as value.

    Example (preds is int tensor):
        >>> from torchmetrics.functional.classification import binary_groups_stat_rates
        >>> target = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> binary_groups_stat_rates(preds, target, groups, 2)
        {'group_0': tensor([0., 0., 1., 0.]), 'group_1': tensor([1., 0., 0., 0.])}

    Example (preds is float tensor):
        >>> from torchmetrics.functional.classification import binary_groups_stat_rates
        >>> target = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> preds = torch.tensor([0.11, 0.84, 0.22, 0.73, 0.33, 0.92])
        >>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> binary_groups_stat_rates(preds, target, groups, 2)
        {'group_0': tensor([0., 0., 1., 0.]), 'group_1': tensor([1., 0., 0., 0.])}

    )r;   rG   )r   r    r   r   r!   r"   r#   r<   s           r   Úbinary_groups_stat_ratesrX   i   s/   € õl -¨U°F¸FÀJÐPYÐ[gÐivÑwÔw€Kå˜+Ñ&Ô&Ð&r   rR   rS   rT   rU   c                 óÒ   — t          | |z   | |z   |z   |z   ¦  «        }t          j        |¦  «        }t          j        |¦  «        }d|› d|› �t          ||         ||         ¦  «        iS )z5Compute demographic parity based on the binary stats.ÚDP_Ú_©r
   r   ÚargminÚargmax)rR   rS   rT   rU   Ú	pos_ratesÚmin_pos_rate_idÚmax_pos_rate_ids          r   Ú"_compute_binary_demographic_parityrb   ¤   sy   € õ ˜R "™W b¨2¡g°¡l°RÑ&7Ñ8Ô8€IÝ”l 9Ñ-Ô-€OÝ”l 9Ñ-Ô-€Oð 	2ˆoÐ1Ð1 Ð1Ð1µ<À	È/Ô@ZÐ\eÐfuÔ\vÑ3wÔ3wðð r   c           	      óÒ   — t          j        |¦  «        j        d         }t          j        | j        ¦  «        }t	          | ||||||¦  «        }t          |¦  «        }t          di |¤ŽS )aò	  `Demographic parity`_ compares the positivity rates between all groups.

    If more than two groups are present, the disparity between the lowest and highest group is reported. The lowest
    positivity rate is divided by the highest, so a lower value means more discrimination against the numerator.
    In the results this is also indicated as the key of dict is DP_{identifier_low_group}_{identifier_high_group}.

    .. math::
        \text{DP} = \dfrac{\min_a PR_a}{\max_a PR_a}.

    where :math:`\text{PR}` represents the positivity rate for group :math:`\text{a}`.

    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``.
    - ``groups`` (int tensor): ``(N, ...)``. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.
    - ``target`` (int tensor): ``(N, ...)``.

    The additional dimensions are flatted along the batch dimension.

    Args:
        preds: Tensor with predictions.
        groups: Tensor with group identifiers. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.
        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.

    Returns:
        The metric returns a dict where the key identifies the group with the lowest and highest positivity rates
        as follows: DP_{identifier_low_group}_{identifier_high_group}. The value is a tensor with the DP rate.

    Example (preds is int tensor):
        >>> from torchmetrics.functional.classification import demographic_parity
        >>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> demographic_parity(preds, groups)
        {'DP_0_1': tensor(0.)}

    Example (preds is float tensor):
        >>> from torchmetrics.functional.classification import demographic_parity
        >>> preds = torch.tensor([0.11, 0.84, 0.22, 0.73, 0.33, 0.92])
        >>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> demographic_parity(preds, groups)
        {'DP_0_1': tensor(0.)}

    r   r)   )r   Úuniquer   Úzerosr;   rV   rb   )	r   r   r!   r"   r#   r   r    r<   Útransformed_group_statss	            r   Údemographic_parityrg   ±   sl   € õp ”˜fÑ%Ô%Ô+¨AÔ.€JÝŒ[˜œÑ%Ô%€Få,¨U°F¸FÀJÐPYÐ[gÐivÑwÔw€Kå4°[ÑAÔAÐå-ÐHÐHÐ0GÐHÐHÐHr   c                 óÀ   — t          | | |z   ¦  «        }t          j        |¦  «        }t          j        |¦  «        }d|› d|› �t          ||         ||         ¦  «        iS )z4Compute equal opportunity based on the binary stats.ÚEO_r[   r\   )rR   rS   rT   rU   Útrue_pos_ratesr`   ra   s          r   Ú!_compute_binary_equal_opportunityrk   ó   so   € õ " " b¨2¡gÑ.Ô.€NÝ”l >Ñ2Ô2€OÝ”l >Ñ2Ô2€Oð 	2ˆoÐ1Ð1 Ð1Ð1µ<Ø˜?Ô+¨^¸OÔ-Lñ4
ô 4
ðð r   c           	      ó    — t          j        |¦  «        j        d         }t          | ||||||¦  «        }t	          |¦  «        }t          di |¤ŽS )a£
  `Equal opportunity`_ compares the true positive rates between all groups.

    If more than two groups are present, the disparity between the lowest and highest group is reported. The lowest
    true positive rate is divided by the highest, so a lower value means more discrimination against the numerator.
    In the results this is also indicated as the key of dict is EO_{identifier_low_group}_{identifier_high_group}.

    .. math::
        \text{DP} = \dfrac{\min_a TPR_a}{\max_a TPR_a}.

    where :math:`\text{TPR}` represents the true positives rate for group :math:`\text{a}`.

    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, ...)``.
    - ``groups`` (int tensor): ``(N, ...)``. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.

    The additional dimensions are flatted along the batch dimension.

    Args:
        preds: Tensor with predictions.
        target: Tensor with true labels.
        groups: Tensor with group identifiers. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.
        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.

    Returns:
        The metric returns a dict where the key identifies the group with the lowest and highest true positives rates
        as follows: EO_{identifier_low_group}_{identifier_high_group}. The value is a tensor with the EO rate.

    Example (preds is int tensor):
        >>> from torchmetrics.functional.classification import equal_opportunity
        >>> target = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> preds = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> equal_opportunity(preds, target, groups)
        {'EO_0_1': tensor(0.)}

    Example (preds is float tensor):
        >>> from torchmetrics.functional.classification import equal_opportunity
        >>> target = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> preds = torch.tensor([0.11, 0.84, 0.22, 0.73, 0.33, 0.92])
        >>> groups = torch.tensor([0, 1, 0, 1, 0, 1])
        >>> equal_opportunity(preds, target, groups)
        {'EO_0_1': tensor(0.)}

    r   r)   )r   rd   r   r;   rV   rk   )	r   r    r   r!   r"   r#   r   r<   rf   s	            r   Úequal_opportunityrm     s[   € õx ”˜fÑ%Ô%Ô+¨AÔ.€JÝ,¨U°F¸FÀJÐPYÐ[gÐivÑwÔw€Kå4°[ÑAÔAÐå,ÐGÐGÐ/FÐGÐGÐGr   ÚallÚtask©rg   rm   rn   c           	      ó®  — |dvrt          d|› d�¦  «        ‚|dk    r0|�t          dt          ¦  «         t          j        | j        ¦  «        }t          j        |¦  «        j        d         }t          | ||||||¦  «        }t          |¦  «        }	|dk    rt          d
i |	¤ŽS |dk    rt          d
i |	¤ŽS |d	k    ri t          d
i |	¤Ž¥t          d
i |	¤Ž¥S dS )a°  Compute either `Demographic parity`_ and `Equal opportunity`_ ratio for binary classification problems.

    This is done by setting the ``task`` argument to either ``'demographic_parity'``, ``'equal_opportunity'``
    or ``all``. See the documentation of
    :func:`~torchmetrics.functional.classification.demographic_parity`
    and :func:`~torchmetrics.functional.classification.equal_opportunity` for the specific details of
    each argument influence and examples.

    Args:
        preds: Tensor with predictions.
        target: Tensor with true labels (not required for demographic_parity).
        groups: Tensor with group identifiers. The group identifiers should be ``0, 1, ..., (num_groups - 1)``.
        task: The task to compute. Can be either ``demographic_parity`` or ``equal_opportunity`` or ``all``.
        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.

    rp   zfExpected argument `task` to either be ``demographic_parity``,``equal_opportunity`` or ``all`` but got r   rg   Nz6The task demographic_parity does not require a target.r   rm   rn   r)   )r   r	   ÚUserWarningr   re   r   rd   r;   rV   rb   rk   )
r   r    r   ro   r!   r"   r#   r   r<   rf   s
             r   Úbinary_fairnessrs   F  s?  € ð: ÐEÐEÐEÝð@Ø8<ð@ð @ð @ñ
ô 
ð 	
ð
 Ð#Ò#Ð#ØÐÝÐSÕU`ÑaÔaÐaÝ”˜Uœ[Ñ)Ô)ˆå”˜fÑ%Ô%Ô+¨AÔ.€JÝ,¨U°F¸FÀJÐPYÐ[gÐivÑwÔw€Kå4°[ÑAÔAÐàÐ#Ò#Ð#Ý1ÐLÐLÐ4KÐLÐLÐLàÐ"Ò"Ð"Ý0ÐKÐKÐ3JÐKÐKÐKàˆu‚}€}ð
Ý0ÐKÐKÐ3JÐKÐKð
å/ÐJÐJÐ2IÐJÐJð
ð 	
ð ˆ4r   )r   NT)rn   r   NT)#Útypingr   r   Útyping_extensionsr   Ú2torchmetrics.functional.classification.stat_scoresr   r   r   r   Útorchmetrics.utilitiesr	   Útorchmetrics.utilities.computer
   Útorchmetrics.utilities.datar   ÚTensorÚintr   r   ÚfloatÚboolr3   Útupler;   ÚdictÚstrrG   rV   rX   rb   rg   rk   rm   rs   r)   r   r   ú<module>r�      s  ðð Ð Ð Ð Ð Ð à €€€Ø %Ð %Ð %Ð %Ð %Ð %ðð ð ð ð ð ð ð ð ð ð ð ð 2Ð 1Ð 1Ð 1Ð 1Ð 1Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø :Ð :Ð :Ð :Ð :Ð :ð_˜uœ|ð _¸ð _Àð _ð _ð _ð _ð"/˜5œ<ð /¨E¬Lð /ð /ð /ð /ð Ø"&Øðqð qØŒ<ðqàŒLðqð ŒLðqð ð	qð
 ðqð ˜3”-ðqð ðqð 
ˆ%�”˜eœl¨E¬L¸%¼,ÐFÔ
GÔHðqð qð qð qðDxØ�e˜EœL¨%¬,¸¼ÀeÄlÐRÔSÔTðxà	ˆ#ˆuŒ|Ð
Ôðxð xð xð xð	Ø�e˜EœL¨%¬,¸¼ÀeÄlÐRÔSÔTð	à	ˆ#ˆuŒ|Ð
Ôð	ð 	ð 	ð 	ð" Ø"&Øð8'ð 8'ØŒ<ð8'àŒLð8'ð ŒLð8'ð ð	8'ð
 ð8'ð ˜3”-ð8'ð ð8'ð 
ˆ#ˆuŒ|Ð
Ôð8'ð 8'ð 8'ð 8'ðv
ØŒð
Øœ,ð
Ø,1¬Lð
Ø>C¼lð
à	ˆ#ˆuŒ|Ð
Ôð
ð 
ð 
ð 
ð  Ø"&Øð?Ið ?IØŒ<ð?IàŒLð?Ið ð?Ið ˜3”-ð	?Ið
 ð?Ið 
ˆ#ˆuŒ|Ð
Ôð?Ið ?Ið ?Ið ?IðDØŒðØœ,ðØ,1¬LðØ>C¼lðà	ˆ#ˆuŒ|Ð
Ôðð ð ð ð& Ø"&ØðAHð AHØŒ<ðAHàŒLðAHð ŒLðAHð ð	AHð
 ˜3”-ðAHð ðAHð 
ˆ#ˆuŒ|Ð
ÔðAHð AHð AHð AHðP GLØØ"&Øð8ð 8ØŒ<ð8àŒLð8ð ŒLð8ð ÐBÔ
Cð	8ð
 ð8ð ˜3”-ð8ð ð8ð 
ˆ#ˆuŒ|Ð
Ôð8ð 8ð 8ð 8ð 8ð 8r   