o
    .wi                     @   sn   d dl Z d dl mZ d dlmZ ddeded defd	d
ZdedefddZddeded defddZdS )    N)Tensor)Literalcountsratingsmoder   probsreturnc                 C   s   |dkr0| j dks|  std| jdd} tjjj| | jd d	ddd}|j
d	d} | S |d
krA| j dks=|  rAtd| S )zUpdates the counts for fleiss kappa metric.

    Args:
        ratings: ratings matrix
        mode: whether ratings are provided as counts or probabilities

    r      zIf argument ``mode`` is 'probs', ratings must have 3 dimensions with the format [n_samples, n_categories, n_raters] and be floating point.   dim)num_classesr      r   zIf argument ``mode`` is `counts`, ratings must have 2 dimensions with the format [n_samples, n_categories] and be none floating point.)ndimis_floating_point
ValueErrorargmaxtorchnn
functionalone_hotshapepermutesum)r   r   r    r   i/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/nominal/fleiss_kappa.py_fleiss_kappa_update   s   "r   c                 C   st   | j d }| d }| jdd||  }| d jdd| ||d   }| }|d  }|| d| d  S )z{Computes fleiss kappa from counts matrix.

    Args:
        counts: counts matrix of shape [n_samples, n_categories]

    r   r   r   r   gh㈵>)r   r   maxmean)r   total
num_ratersp_ip_jp_barpe_barr   r   r   _fleiss_kappa_compute,   s   
 r'   c                 C   s"   |dvrt dt| |}t|S )a  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.

    Args:
        ratings: 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.
        mode: Whether `ratings` will be provided as counts or probabilities.

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

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

    r   z5Argument ``mode`` must be one of ['counts', 'probs'].)r   r   r'   )r   r   r   r   r   r   fleiss_kappa=   s   #
r(   )r   )r   r   typing_extensionsr   r   r'   r(   r   r   r   r   <module>   s    