view numerics/filters.py @ 115:7fac47bb648e

mysteries and more mysteries: bokeh should really be a plugin for this, not the way to do it, but we are porting so lets ignore that and boldly walk forward
author Jeff Hammel <k0scist@gmail.com>
date Sun, 15 Mar 2015 17:03:38 -0700
parents 3a1f04f33feb
children c2f545f32025
line wrap: on
line source

"""
filter functions for stats
"""

__all__ = ['mean', 'array_mean', 'median']

def mean(data):
    return sum(data)/float(len(data))

def array_mean(data):
    if not data:
        return []
    lengths = [len(i) for i in data]
    if len(set(lengths)) != 1:
        raise AssertionError("Different lengths to array_mean: {}".format(' '.join(lengths)))
    return [mean(i) for i in zip(*data)]

def median(data):
    length = len(data)
    index = length/2
    if length % 2:
        return data[index]
    else:
        return 0.5*(data[index-1] + data[index])