<commit_before>"""
This file is part of Seten which is released under the MIT License (MIT).
See file LICENSE or go to https://github.com/gungorbudak/seten-cli/blob/master/LICENSE
for full license details.
"""
import random
import operator
import numpy
from scipy import stats
from seten.multi_comp import fdr_correction, bonferroni_correction


def compute_gene_level_score(scores, method='highest'):
    """
    Computes a gene level score from list of
    binding scores belonging to the same gene
    Methods: lowest, highest, mean, median
    """
    if method == 'lowest':
        return min(scores)
    elif method == 'highest':
        return max(scores)
    elif method == 'mean':
        return numpy.mean(scores)
    elif method == 'median':
        return numpy.median(scores)
    else:
        raise ValueError('could not find %s in available methods' % (method))


def randomize(scores, overlap_scores, operator=operator.gt, cutoff=0.05):
    """
    Random sample from data and compare the random set
    with the overlap set and return the p-value if
    it meets the conditions
    """
    random_scores = random.sample(scores, len(overlap_scores))
    try:
        if operator(numpy.median(overlap_scores), numpy.median(random_scores)):
            statistic, pvalue = stats.mannwhitneyu(overlap_scores, random_scores)
            # two-tailed test fix
            pvalue *= 2
            if pvalue < cutoff:
                return pvalue
    except ValueError:
        pass
    return None


def compute_gse_pvalue(scores, overlap_scores,
        operator=operator.gt, cutoff=0.05, times=1000):
    """
    Computes a p-value by random sampling from data
    and doing Mann-Whitney U test and median comparison
    """
    # serial execution
    pvalues = [randomize(scores, overlap_scores, operator=operator,
                          cutoff=cutoff) for _ in xrange(times)]
    # count the p-values
    n = times - pvalues.count(None)<commit_msg>making Mann Whitney U one-tailed and performance improvements<commit_after>"""
This file is part of Seten which is released under the MIT License (MIT).
See file LICENSE or go to https://github.com/gungorbudak/seten-cli/blob/master/LICENSE
for full license details.
"""
import random
import operator
import numpy
from scipy import stats
from seten.multi_comp import fdr_correction, bonferroni_correction


def compute_gene_level_score(scores, method='highest'):
    """
    Computes a gene level score from list of
    binding scores belonging to the same gene
    Methods: lowest, highest, mean, median
    """
    if method == 'lowest':
        return min(scores)
    elif method == 'highest':
        return max(scores)
    elif method == 'mean':
        return numpy.mean(scores)
    elif method == 'median':
        return numpy.median(scores)
    else:
        raise ValueError('could not find %s in available methods' % (method))


def _randomize(scores, overlap_scores, overlap_median,
        operator=operator.gt, cutoff=0.05):
    """
    Random sample from data and compare the random set
    with the overlap set and return the p-value if
    it meets the conditions
    """
    random_scores = random.sample(scores, len(overlap_scores))
    try:
        if operator(overlap_median, numpy.median(random_scores)):
            # one-sided Mann Whitney U test for testing if overlap scores
            # have significantly larger values than random scores
            statistic, pvalue = stats.mannwhitneyu(overlap_scores, random_scores)
            if pvalue < cutoff:
                return pvalue
    except ValueError:
        pass
    return None


def compute_gse_pvalue(scores, overlap_scores,
        operator=operator.gt, cutoff=0.05, times=1000):
    """
    Computes a p-value by random sampling from data
    and doing Mann-Whitney U test and median comparison
    """
    overlap_median = numpy.median(overlap_scores)
    # serial execution
    pvalues = [_randomize(scores, overlap_scores, overlap_median,
        operator=operator, cutoff=cutoff) for _ in xrange(times)]
    # count the p-values
    n = times - pvalues.count(None)
{'fraction_non_alphanumeric': 0.08272683163512858, 'fraction_numerical': 0.005094614264919942, 'mean_word_length': 3.0107003891050583, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2598970', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1121, 'n_words': 392}