§
    ’©ºj   ã                   ó¬   — 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mZ d dlmZ d dlmZ d dlmZ d d	lmZmZ esd
gZ G d„ de¦  «        ZdS )é    )ÚSequence)ÚAnyÚListÚOptionalÚUnion)ÚTensor)ÚLiteral)Ú_fleiss_kappa_computeÚ_fleiss_kappa_update)ÚMetric)Údim_zero_cat)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzFleissKappa.plotc                   óê   ‡ — e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   ee         ed	<   dd
ed         deddfˆ fd„Zdeddfd„Zdefd„Zddeeee         df         dee         defd„Zˆ xZS )ÚFleissKappaac  Calculatees `Fleiss kappa`_ a statistical measure for inter agreement between raters.

    .. math::
        \kappa = \frac{\bar{p} - \bar{p_e}}{1 - \bar{p_e}}

    where :math:`\bar{p}` is the mean of the agreement probability over all raters and :math:`\bar{p_e}` is the mean
    agreement probability over all raters if they were randomly assigned. If the raters are in complete agreement then
    the score 1 is returned, if there is no agreement among the raters (other than what would be expected by chance)
    then a score smaller than 0 is returned.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``ratings`` (:class:`~torch.Tensor`): Ratings of shape ``[n_samples, n_categories]`` or
      ``[n_samples, n_categories, n_raters]`` depedenent on ``mode``. If ``mode`` is ``counts``, ``ratings`` must be
      integer and contain the number of raters that chose each category. If ``mode`` is ``probs``, ``ratings`` must be
      floating point and contain the probability/logits that each rater chose each category.

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``fleiss_k`` (:class:`~torch.Tensor`): A float scalar tensor with the calculated Fleiss' kappa score.

    Args:
        mode: Whether `ratings` will be provided as counts or probabilities.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> # Ratings are provided as counts
        >>> from torch import randint
        >>> from torchmetrics.nominal import FleissKappa
        >>> ratings = randint(0, 10, size=(100, 5)).long()  # 100 samples, 5 categories, 10 raters
        >>> metric = FleissKappa(mode='counts')
        >>> metric(ratings)
        tensor(0.0089)

    Example:
        >>> # Ratings are provided as probabilities
        >>> from torch import randn
        >>> from torchmetrics.nominal import FleissKappa
        >>> ratings = randn(100, 5, 10).softmax(dim=1)  # 100 samples, 5 categories, 10 raters
        >>> metric = FleissKappa(mode='probs')
        >>> metric(ratings)
        tensor(-0.0075)

    FÚfull_state_updateÚis_differentiableTÚhigher_is_betterg      ð?Úplot_upper_boundÚcountsÚmode©r   ÚprobsÚkwargsÚreturnNc                 óž   •—  t          ¦   «         j        di |¤Ž |dvrt          d¦  «        ‚|| _        |                      dg d¬¦  «         d S )Nr   z5Argument ``mode`` must be one of 'counts' or 'probs'.r   Úcat)ÚdefaultÚdist_reduce_fx© )ÚsuperÚ__init__Ú
ValueErrorr   Ú	add_state)Úselfr   r   Ú	__class__s      €úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/nominal/fleiss_kappa.pyr#   zFleissKappa.__init__R   s`   ø€ Ø�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"ØÐ*Ð*Ð*ÝÐTÑUÔUÐUØˆŒ	Ø�Š�x¨¸EˆÑBÔBÐBÐBÐBó    Úratingsc                 ód   — t          || j        ¦  «        }| j                             |¦  «         dS )z+Updates the counts for fleiss kappa metric.N)r   r   r   Úappend)r&   r*   r   s      r(   ÚupdatezFleissKappa.updateY   s/   € å% g¨t¬yÑ9Ô9ˆØŒ×Ò˜6Ñ"Ô"Ð"Ð"Ð"r)   c                 óH   — t          | j        ¦  «        }t          |¦  «        S )zComputes Fleiss' kappa.)r   r   r
   )r&   r   s     r(   ÚcomputezFleissKappa.compute^   s   € å˜dœkÑ*Ô*ˆÝ$ VÑ,Ô,Ð,r)   ÚvalÚaxc                 ó.   — |                       ||¦  «        S )a  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> import torch
            >>> from torchmetrics.nominal import FleissKappa
            >>> metric = FleissKappa(mode="probs")
            >>> metric.update(torch.randn(100, 5, 10).softmax(dim=1))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.nominal import FleissKappa
            >>> metric = FleissKappa(mode="probs")
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.randn(100, 5, 10).softmax(dim=1)))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)r&   r0   r1   s      r(   ÚplotzFleissKappa.plotc   s   € ðL �zŠz˜#˜rÑ"Ô"Ð"r)   )r   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r   r   r   Úfloatr   r   r	   r   r#   r-   r/   r   r   r   r   r   r4   Ú__classcell__)r'   s   @r(   r   r      sN  ø€ € € € € € ð+ð +ðZ $Ð�tÐ#Ð#Ñ#Ø#Ð�tÐ#Ð#Ñ#Ø!Ð�dÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!Ø�ŒLÐÐÑðCð C˜WÐ%6Ô7ð CÈcð CÐVZð Cð Cð Cð Cð Cð Cð#˜fð #¨ð #ð #ð #ð #ð
-˜ð -ð -ð -ð -ð
&#ð &#˜˜f h¨vÔ&6¸Ð<Ô=ð &#È(ÐS[ÔJ\ð &#Ðhvð &#ð &#ð &#ð &#ð &#ð &#ð &#ð &#r)   r   N)Úcollections.abcr   Útypingr   r   r   r   Útorchr   Útyping_extensionsr	   Ú,torchmetrics.functional.nominal.fleiss_kappar
   r   Útorchmetrics.metricr   Útorchmetrics.utilities.datar   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r!   r)   r(   ú<module>rG      s  ðð %Ð $Ð $Ð $Ð $Ð $Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à dÐ dÐ dÐ dÐ dÐ dÐ dÐ dØ &Ð &Ð &Ð &Ð &Ð &Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð ,Ø*Ð+Ððk#ð k#ð k#ð k#ð k#�&ñ k#ô k#ð k#ð k#ð k#r)   