a
    d                     @   sd   d dl Z d dl mZ d dlmZ deed edddZeed	d
dZdeed edddZdS )    N)Tensor)Literalcountsr   probs)ratingsmodereturnc                 C   s   |dkr^| j dks|  s"td| jdd} tjjj| | jd d	ddd}|j
d	d} n"|d
kr| j dksx|  rt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.   Zdim)Z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.)ndimZis_floating_point
ValueErrorZargmaxtorchnnZ
functionalone_hotshapeZpermutesum)r   r   r    r   u/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/functional/nominal/fleiss_kappa.py_fleiss_kappa_update   s    "r   )r   r	   c                 C   sx   | 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   totalZn_raterZ
num_ratersZp_iZp_jZp_barZ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
        >>> import torch
        >>> from torchmetrics.functional.nominal import fleiss_kappa
        >>> _ = torch.manual_seed(42)
        >>> ratings = torch.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
        >>> import torch
        >>> from torchmetrics.functional.nominal import fleiss_kappa
        >>> _ = torch.manual_seed(42)
        >>> ratings = torch.randn(100, 5, 10).softmax(dim=1)  # 100 samples, 5 categories, 10 raters
        >>> fleiss_kappa(ratings, mode='probs')
        tensor(-0.0105)

    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   r   Ztyping_extensionsr   r   r   r   r   r   r   r   <module>   s
   