<commit_before>#!/usr/bin/python
import mathstat
import unittest


def get_slopes_capacity_bins(data):
    bins = {}
    for e in data:
        key = e.get('capacity_round')
        val = e.get('slope', 0)
        if key is not None and not mathstat.is_zero(val):
            bins.setdefault(key, []).append(val)
    return bins


def get_slopes_by_percentile(bins, percentile_val):
    x = bins.keys()
    y = [mathstat.percentile(bins[i], percentile_val) for i in x]
    return dict(zip(x, y))


def extrapolate(data, lo=0, hi=100):
    keys = data.keys()
    if len(keys) == 0:
        return data
    mx = max(keys)
    for k in xrange(mx+1, hi+1, 1):
        data[k] = data[mx]
    mn = min(keys)
    for k in xrange(mn-1, lo-1, -1):
        data[k] = data[mn]
    keys = data.keys()
    values = data.values()
    for i in xrange(1, len(keys)):
        for k in xrange(keys[i-1]+1, keys[i]):
            data[k] = (values[i-1] + values[i]) / 2
    return data


def reconstruct_timeline(slopes, ys):
    capacity = slopes.keys()
    slope = slopes.values()
    n = len(slopes)
    if n < 2:
        return []
    t = [0]
    for i in xrange(1, len(ys)):
        dy = float(ys[i] - ys[i-1])
        slope = slopes[ys[i-1]]
        dx = dy / slope
        x = t[-1] + dx
        t.append(x)
    return zip(t, ys)


class StatBateryModel:

    def __init__(self, history):
        self.history = history
        self.hdata = history.data()
        self.percentile = 0.5
        self.history_limit = 100.0

    def calculate(self, start=None):
        # split charge/discharge
        hdata = self.hdata
        hdata = filter(lambda e: e['virtual_time_hour']
                       < self.history_limit, hdata)
        charge = filter(lambda e: e['status'] == 'Charging', hdata)
        discharge = filter(lambda e: e['status'] == 'Discharging', hdata)
        # extract slopes by capacity bins
        charge_bins = get_slopes_capacity_bins(charge)
        discharge_bins = get_slopes_capacity_bins(discharge)<commit_msg>add interpolate_linear_evenly<commit_after>#!/usr/bin/python
import mathstat
import unittest


def get_slopes_capacity_bins(data):
    bins = {}
    for e in data:
        key = e.get('capacity_round')
        val = e.get('slope', 0)
        if key is not None and not mathstat.is_zero(val):
            bins.setdefault(key, []).append(val)
    return bins


def get_slopes_by_percentile(bins, percentile_val):
    x = bins.keys()
    y = [mathstat.percentile(bins[i], percentile_val) for i in x]
    return dict(zip(x, y))


def extrapolate(data, lo=0, hi=100):
    keys = data.keys()
    if len(keys) == 0:
        return data
    mx = max(keys)
    for k in xrange(mx+1, hi+1, 1):
        data[k] = data[mx]
    mn = min(keys)
    for k in xrange(mn-1, lo-1, -1):
        data[k] = data[mn]
    keys = data.keys()
    values = data.values()
    for i in xrange(1, len(keys)):
        for k in xrange(keys[i-1]+1, keys[i]):
            data[k] = (values[i-1] + values[i]) / 2
    return data


def reconstruct_timeline(slopes, ys):
    capacity = slopes.keys()
    slope = slopes.values()
    n = len(slopes)
    if n < 2:
        return []
    t = [0]
    for i in xrange(1, len(ys)):
        dy = float(ys[i] - ys[i-1])
        slope = slopes[ys[i-1]]
        dx = dy / slope
        x = t[-1] + dx
        t.append(x)
    return mathstat.interpolate_linear_evenly(t, ys, steps=100)


class StatBateryModel:

    def __init__(self, history):
        self.history = history
        self.hdata = history.data()
        self.percentile = 0.5
        self.history_limit = 100.0

    def calculate(self, start=None):
        # split charge/discharge
        hdata = self.hdata
        hdata = filter(lambda e: e['virtual_time_hour']
                       < self.history_limit, hdata)
        charge = filter(lambda e: e['status'] == 'Charging', hdata)
        discharge = filter(lambda e: e['status'] == 'Discharging', hdata)
        # extract slopes by capacity bins
        charge_bins = get_slopes_capacity_bins(charge)
        discharge_bins = get_slopes_capacity_bins(discharge)
{'fraction_non_alphanumeric': 0.1336416747809153, 'fraction_numerical': 0.014849074975657255, 'mean_word_length': 2.3190630048465266, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2923280', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1428, 'n_words': 355}