changeset 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
files numerics/histogram.py tests/test_histogram.py
diffstat 2 files changed, 48 insertions(+), 5 deletions(-) [+]
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"""
--- a/tests/test_histogram.py	Sat Feb 28 17:05:04 2015 -0800
+++ b/tests/test_histogram.py	Sat Feb 28 21:28:17 2015 -0800
@@ -19,12 +19,49 @@
         """basic histogram test"""
 
         # make some test data
-        data = [0,0,1,1,2,3,4,5,6,7,8,8,8]
+        data = [0.1, 0.1,
+                1.1, 1.2,
+                2,
+                3,
+                5,
+                6,
+                7,
+                8, 8, 8, 8.1]
         bins = range(0,10)
 
         # make a histogram
         h = Histogram(bins)
 
+        # add some data to it
+        h.add(*data)
+
+        # now make sure what we did works
+
+        # First, let's check the bins
+        expected_bins = [(0,1),
+                         (1,2),
+                         (2,3),
+                         (3,4),
+                         (4,5),
+                         (5,6),
+                         (6,7),
+                         (7,8),
+                         (8,9)]
+        self.assertEqual(h.keys(), expected_bins)
+
+        # now let's check the values
+        values = h()
+        self.assertEqual(values[(0,1)], 2)
+        self.assertEqual(values[(1,2)], 2)
+        self.assertEqual(values[(2,3)], 1)
+        self.assertEqual(values[(3,4)], 1)
+        self.assertEqual(values[(4,5)], 0)
+        self.assertEqual(values[(5,6)], 1)
+        self.assertEqual(values[(6,7)], 1)
+        self.assertEqual(values[(7,8)], 1)
+        self.assertEqual(values[(8,9)], 4)
+
+
 if __name__ == '__main__':
     unittest.main()