Calculate reference and response likelihood.

    :param item : TOP or P
    :type item : dict
    :param POP: population
    :type POP : dict
    :return: reference or response likelihood as float
198313
def entropy_calc(item, POP):
    
    try:
        result = 0
        for i in item.keys():
            likelihood = item[i] / POP[i]
            if likelihood != 0:
                result += likelihood * math.log(likelihood, 2)
        return -result
    except Exception:
        return "None"
1096835
def get_stats_value(self, item, value):
        
        if not isinstance(self.stats, list):
            return None
        else:
            if value.isdigit():
                value = int(value)
            try:
                return self._json_dumps({value: [i for i in self.stats if i[item] == value]})
            except (KeyError, ValueError) as e:
                logger.error(
                    "Cannot get item({})=value({}) ({})".format(item, value, e))
                return None
317301
def population(self):
        ""
        try:
            return self._p
        except AttributeError:
            self._p = self._population_class(base=self,
                                             tournament_size=self._tournament_size,
                                             classifier=self.classifier,
                                             labels=self._labels,
                                             es_extra_test=self.es_extra_test,
                                             popsize=self._popsize,
                                             random_generations=self._random_generations,
                                             negative_selection=self._negative_selection)
            return self._p
247251
def p0(self, e):
        """"""
        body = self.body(e)
        p = self.p(e)
        if body == p.getparent():
            return p
        else:
            raise ElementNotFound("could not find top level w:p element in %s" % e)
953986
def rank(self, item):
        ''''''
        score = self._dict.get(item)
        if score is not None:
            return self._sl.rank(score)
74312
def max_likelihood(self, data, weights=None, stats=None):
        
        if isinstance(data, list):
            x = np.vstack([d[0] for d in data])
            y = np.vstack([d[1] for d in data])
        elif isinstance(data, tuple):
            assert len(data) == 2
        elif isinstance(data, np.ndarray):
            x, y = data[:,:self.D_in], data[:, self.D_in:]
        else:
            raise Exception("Invalid data type")

        from sklearn.linear_model import LogisticRegression
        for n in progprint_xrange(self.D_out):
            lr = LogisticRegression(fit_intercept=False)
            lr.fit(x, y[:,n])
            self.A[n] = lr.coef_
1237280
def calc_pvalue(self, study_count, study_n, pop_count, pop_n):
        """"""
        # http://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.stats.fisher_exact.html
        #
        #         Atlantic  Indian                              YES       NO
        # whales     8        2    | 10 whales    study_genes    8 scnt   2    | 10 = study_n
        # sharks     1        5    |  6 sharks    not s_genes    1        5    |  6
        #         --------  ------                            --------   -----
        #            9        7      16 = pop_n     pop_genes    9 pcnt   7      16 = pop_n
        #
        # We use the preceeding table to find the p-value for whales/sharks:
        #
        # >>> import scipy.stats as stats
        # >>> oddsratio, pvalue = stats.fisher_exact([[8, 2], [1, 5]])
        #                                              a  b    c  d
        avar = study_count
        bvar = study_n - study_count
        cvar = pop_count - study_count
        dvar = pop_n - pop_count - bvar
        assert cvar >= 0, self.fmterr.format(
            A=avar, B=bvar, C=cvar, D=dvar, scnt=study_count, stot=study_n, pcnt=pop_count, ptot=pop_n)
        # stats.fisher_exact returns oddsratio, pval_uncorrected
        _, p_uncorrected = self.pval_fnc([[avar, bvar], [cvar, dvar]])
        return p_uncorrected
341539
def density(self):
        
        tot_nonsparse = sum(ser.sp_index.npoints
                            for _, ser in self.items())
        tot = len(self.index) * len(self.columns)
        return tot_nonsparse / float(tot)
3354
def _initialize_pop(self, pop_size):
        """"""
        self.toolbox.register("individual", self._generate)
        self.toolbox.register("population", tools.initRepeat,
                              list, self.toolbox.individual)
        self.population = self.toolbox.population(n=pop_size)
        if self.neighbours:
            for i in range(len(self.population)):
                self.population[i].ident = i
                self.population[i].neighbours = list(
                    set(
                        [(i - x) % len(self.population)
                         for x in range(1, self.neighbours + 1)] +
                        [(i + x) % len(self.population)
                         for x in range(1, self.neighbours + 1)]
                    ))
        self.assign_fitnesses(self.population)
        return
1374135
def _known_populations(row, pops):
    
    cutoff = 0.01
    out = set([])
    for pop, base in [("esp", "af_esp_all"), ("1000g", "af_1kg_all"),
                      ("exac", "af_exac_all"), ("anypop", "max_aaf_all")]:
        for key in [x for x in pops if x.startswith(base)]:
            val = row[key]
            if val and val > cutoff:
                out.add(pop)
    return sorted(list(out))
664184
def overall_accuracy_calc(TP, POP):
    
    try:
        overall_accuracy = sum(TP.values()) / POP
        return overall_accuracy
    except Exception:
        return "None"
676721
def get_percentage_from_prob(prob):
    
    assert isinstance(prob, (float, int))
    prob = float(prob)
    assert prob >= 0
    assert prob <= 1

    percentages = list(probability_list.keys())
    percentages.sort()
    for percentage in percentages:
        if prob < probability_list[percentage]:
            return percentage - 1
    return 100
647767
def get_stats_item(self, item):
        
        if isinstance(self.stats, dict):
            try:
                return self._json_dumps({item: self.stats[item]})
            except KeyError as e:
                logger.error("Cannot get item {} ({})".format(item, e))
                return None
        elif isinstance(self.stats, list):
            try:
                # Source:
                # http://stackoverflow.com/questions/4573875/python-get-index-of-dictionary-item-in-list
                # But https://github.com/nicolargo/glances/issues/1401
                return self._json_dumps({item: list(map(itemgetter(item), self.stats))})
            except (KeyError, ValueError) as e:
                logger.error("Cannot get item {} ({})".format(item, e))
                return None
        else:
            return None
1049818
def _get_popularity_baseline(self):

        

        response = self.__proxy__.get_popularity_baseline()
        from .popularity_recommender import PopularityRecommender
        return PopularityRecommender(response)
1032744
def getKendallTauScore(myResponse, otherResponse):
    
    # variables
    kt = 0
    list1 = myResponse.values()
    list2 = otherResponse.values()

    if len(list1) <= 1:
        return kt

    # runs through list1
    for itr1 in range(0, len(list1) - 1):
        # runs through list2
        for itr2 in range(itr1 + 1, len(list2)):
            # checks if there is a discrepancy. If so, adds
            if ((list1[itr1] > list1[itr2]
                 and list2[itr1] < list2[itr2])
                or (list1[itr1] < list1[itr2]
                    and list2[itr1] > list2[itr2])):
                kt += 1
    # normalizes between 0 and 1
    kt = (kt * 2) / (len(list1) * (len(list1) - 1))

    # returns found value
    return kt
1133662
def _update_pop(self, pop_size):
        """"""
        candidates = []
        for ind in self.population:
            candidates.append(self._crossover(ind))
        self._model_count += len(candidates)
        self.assign_fitnesses(candidates)
        for i in range(len(self.population)):
            if candidates[i].fitness > self.population[i].fitness:
                self.population[i] = candidates[i]
        return
92004
def _sample_item(self, **kwargs):
        
        loc = np.random.choice(self._n_items, p = self._inst_pmf)
        weight = (1/self._n_items)/self._inst_pmf[loc]
        return loc, weight, {}