Mercurial > hg > numerics
diff numerics/histogram.py @ 73:8e93d7357c6b
working histogram w tests
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 28 Feb 2015 21:28:17 -0800 |
parents | 06094870fdd7 |
children | 630cde28928a |
line wrap: on
line diff
--- a/numerics/histogram.py Sat Feb 28 17:05:04 2015 -0800 +++ b/numerics/histogram.py Sat Feb 28 21:28:17 2015 -0800 @@ -33,16 +33,17 @@ """historgram""" def __init__(self, bins): - bins = sorted(bin) + self.bins = sorted(bins) assert len(bins) > 1 - self.data = self.OrderedDict(zip(bins[:-1], - bins[1:])) + self.data = OrderedDict([(bin, []) + for bin in zip(bins[:-1], + bins[1:])]) def add(self, *values): """add values to the histogram""" for value in values: for vmin, vmax in self.data.keys(): - if vmin <= value <= vmax: + if vmin <= value < vmax: self.data[(vmin, vmax)].append(value) def __iadd__(self, value): @@ -55,6 +56,11 @@ OrderedDict of counts """ self.add(*values) + return OrderedDict([(bin, len(value)) + for bin, value in self.data.items()]) + + def keys(self): + return self.data.keys() class HistogramParser(CSVParser): """histogram CLI option parser"""