<commit_before>from __future__ import division
# python libs
import ast
from collections import (OrderedDict,defaultdict)
import json
# 3rd party
from fuzz import FuzzySet, FuzzyElement
import numpy as np
# local
import util
import biom_calc as bc


def fuzzy_lookup(orig, keys):
    """
    Return the intersection of a fuzzy set and a collection of keys
    (presumably a subset).

    :type orig: set
    :param orig: FuzzySet of SampleID with OTUID and relative abundances.

    :type keys: list
    :param keys: Genus-species taxonomic identifier.

    :rtype: set
    :return: Returns a new FuzzySet of genus-species identifier and relative
             abundance for the given list of keys.
    """
    fset = FuzzySet()
    for k in keys:
        if k in orig:
            fset.add(orig[k])
    return fset


def sdi(fset):
    """
    Calculate the Shannon Diversity Index.
    H = -sum(p*ln(p))
    where p is the relative abundance of a single OTU in the set.
    Note that the Equitability Index ( $E_H = H / H_{max}$ ) could be easily
    calculated from the returned array by:
    >>>diversities = sdi(fset)
    >>>equitabilities = diversities/max(diversities)

    :type fset: FuzzySet
    :param fset: The set of OTUs and their relative abundance values

    :rtype: float
    :return: The Shannon Diversity Index.
    """
    p = np.array([e.mu for e in fset])
    return -1*np.sum(p*np.log(p))


def otu_name_biom(biom_row):
    """
    Given an OTU row from a BIOM table, determine a Genus-species identifier
    from the taxonomic specifier (see otu_name() method)

    :type biom_row: str
    :param biom_row: Row entry of a BIOM format file containing full taxonomy.

    :rtype: str
    :return: Returns the genus-species identifier.
    """
    return otu_name(biom_row['metadata']['taxonomy'])


def otu_name(tax):<commit_msg>Modified otu_calc.py<commit_after>from __future__ import division
# python libs
import ast
from collections import (OrderedDict,defaultdict)
import json
import math
# 3rd party
from fuzz import FuzzySet, FuzzyElement
import numpy as np
# local
import util
import biom_calc as bc


def fuzzy_lookup(orig, keys):
    """
    Return the intersection of a fuzzy set and a collection of keys
    (presumably a subset).

    :type orig: set
    :param orig: FuzzySet of SampleID with OTUID and relative abundances.

    :type keys: list
    :param keys: Genus-species taxonomic identifier.

    :rtype: set
    :return: Returns a new FuzzySet of genus-species identifier and relative
             abundance for the given list of keys.
    """
    fset = FuzzySet()
    for k in keys:
        if k in orig:
            fset.add(orig[k])
    return fset


def sdi(fset):
    """
    Calculate the Shannon Diversity Index.
    H = -sum(p*ln(p))
    where p is the relative abundance of a single OTU in the set.
    Note that the Equitability Index ( $E_H = H / H_{max}$ ) could be easily
    calculated from the returned array by:
    >>>diversities = sdi(fset)
    >>>equitabilities = diversities/max(diversities)

    :type fset: FuzzySet
    :param fset: The set of OTUs and their relative abundance values

    :rtype: float
    :return: The Shannon Diversity Index.
    """
    p = [e.mu for e in fset]
    return -1*sum([entry*math.log(entry) for entry in p])


def otu_name_biom(biom_row):
    """
    Given an OTU row from a BIOM table, determine a Genus-species identifier
    from the taxonomic specifier (see otu_name() method)

    :type biom_row: str
    :param biom_row: Row entry of a BIOM format file containing full taxonomy.

    :rtype: str
    :return: Returns the genus-species identifier.
    """
    return otu_name(biom_row['metadata']['taxonomy'])


def otu_name(tax):
{'fraction_non_alphanumeric': 0.0821954484605087, 'fraction_numerical': 0.00107095046854083, 'mean_word_length': 3.0564603691639523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6852032', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1135, 'n_words': 450}