<commit_before># -*- coding: utf-8 -*-

from __future__ import division, print_function, absolute_import

import scipy.misc
import scipy.random

def compute_ts(ratings):
    """
    Compute the test statistic
    .. math:: \rho_s \equiv
        \frac{1}{N_s {R \choose 2}} \sum_{i=1}^{N_s} \sum_{r=1}^{R-1}
            \sum_{v=r+1}^R 1(L_{s,i,r} = L_{s,i,v})

    Parameters
    ----------
    ratings: array_like
             Input array of dimension [R, Ns]

    Returns
    -------
    rho_s: float
           concordance of the ratings, where perfect concordance is 1.0
    """
    R, Ns = ratings.shape
    tmp = ratings.sum(0)
    counts = scipy.misc.comb(tmp, 2) + scipy.misc.comb(R-tmp, 2)
    rho_s = counts.sum()/(Ns*scipy.misc.comb(R, 2))
    return rho_s

def permute_rows(ratings):
    """
    Permute elements of each row in the ratings matrix except the top row.
    Each row corresponds to the ratings given by a single rater; columns correspond to items rated.
    Parameters
    ----------
    ratings: array_like
             Input array of dimension [R, Ns]

    Returns
    -------
    True

    Action
    ------
    Permutes the elements of each row of <ratings> in place; leaves the top row unchanged
    """
    np.apply_along_axis(np.random.shuffle, axis=1, arr=ratings)
    return True

def simulate_ts_dist(ratings, obs_ts = None, iter=10000, keep_dist = False):
    """
    Simulates the permutation distribution of the irr test statistic for a matrix of
    ratings <ratings>

    If obs_ts is not null, computes the reference value of the test statistic before
    the first permutation. Otherwise, uses the value obs_ts for comparison.

    If <keep_dist>, return the distribution of values of the test statistic; otherwise,
    return only the number of permutations for which the value of the irr test statistic is
    at least as large as obs_ts.


    Parameters
    ----------
    ratings : array_like
              Input array of dimension [R, Ns]

    obs_ts : float
             if None, obs_ts is calculated as the value of the test statistic for the
             original data

    iter : integer
           number of random permutation of the elements of each row of ratings

    keep_dist : bool
                flag for whether to store and return the array of values of the irr
                test statistic
<commit_msg>improve documentation<commit_after># -*- coding: utf-8 -*-

from __future__ import division, print_function, absolute_import

import scipy.misc
import scipy.random

def compute_ts(ratings):
    """
    Compute the test statistic
    .. math:: \rho_s \equiv
        \frac{1}{N_s {R \choose 2}} \sum_{i=1}^{N_s} \sum_{r=1}^{R-1}
            \sum_{v=r+1}^R 1(L_{s,i,r} = L_{s,i,v})

    Parameters
    ----------
    ratings: array_like
             Input array of dimension [R, Ns]
             Each row corresponds to the ratings given by a single rater;
             columns correspond to items rated.

    Returns
    -------
    rho_s: float
           concordance of the ratings, where perfect concordance is 1.0
    """
    R, Ns = ratings.shape
    tmp = ratings.sum(0)
    counts = scipy.misc.comb(tmp, 2) + scipy.misc.comb(R-tmp, 2)
    rho_s = counts.sum()/(Ns*scipy.misc.comb(R, 2))
    return rho_s

def permute_rows(ratings):
    """
    Permute elements of each row in the ratings matrix.
    Each row corresponds to the ratings given by a single rater; columns correspond
    to items rated.

    Parameters
    ----------
    ratings: array_like
             Input array of dimension [R, Ns]

    Returns
    -------
    True

    Action
    ------
    Permutes the elements of each row of <ratings> in place; leaves the top row unchanged
    """
    np.apply_along_axis(np.random.shuffle, axis=0, arr=ratings)
    return True

def simulate_ts_dist(ratings, obs_ts = None, iter=10000, keep_dist = False):
    """
    Simulates the permutation distribution of the irr test statistic for a matrix of
    ratings <ratings>

    If obs_ts is not null, computes the reference value of the test statistic before
    the first permutation. Otherwise, uses the value obs_ts for comparison.

    If <keep_dist>, return the distribution of values of the test statistic; otherwise,
    return only the number of permutations for which the value of the irr test statistic is
    at least as large as obs_ts.


    Parameters
    ----------
    ratings : array_like
              Input array of dimension [R, Ns]

    obs_ts : float
             if None, obs_ts is calculated as the value of the test statistic for the
             original data

    iter : integer
           number of random permutation of the elements of each row of ratings

    keep_dist : bool
                flag for whether to store and return the array of values of the irr
                test statistic

{'fraction_non_alphanumeric': 0.09154639175257732, 'fraction_numerical': 0.008247422680412371, 'mean_word_length': 2.291044776119403, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4351789', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1384, 'n_words': 551}