diff tests/test_interpolation.py @ 7:2cad4536f797

new unittest
author Jeff Hammel <k0scist@gmail.com>
date Thu, 04 Sep 2014 18:22:53 -0700
parents
children 39f428e8f2b7
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_interpolation.py	Thu Sep 04 18:22:53 2014 -0700
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+"""
+test interpolation
+"""
+
+import csv
+import os
+import sys
+import unittest
+from interpolation import linear_interpolation
+
+# globals
+here = os.path.dirname(os.path.abspath(__file__))
+test_data = os.path.join(here, 'test_data.csv')
+
+class InterpolationUnitTest(unittest.TestCase):
+
+    def test_linear_interpolation(self):
+        """test linear interpolation"""
+
+        # make a line
+        data = [(float(x), float(x)) for x in range(10)]
+
+        # interpolate at a point
+        point = 2.2
+        values = linear_interpolation(data, [point])
+        self.assertEqual(len(values), 1)
+        self.assertEqual(values[0], point)
+
+    def read_test_data(self):
+        """read test data from sample CSV file"""
+
+        with open(test_data, 'r') as f:
+            reader = csv.reader(f)
+            retval = [[float(col) for col in row] for row in reader]
+        return retval
+
+    def test_from_csv(self):
+
+        raw_data = self.read_test_data()
+        data = [[(row[0], row[col]) for row in raw_data]
+                for col in (1,2)]
+        for series in data:
+            x_values = range(2,10)
+            interpolated = linear_interpolation(series, x_values)
+
+
+if __name__ == '__main__':
+    unittest.main()
+