7
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 test interpolation
|
|
5 """
|
|
6
|
|
7 import csv
|
|
8 import os
|
|
9 import sys
|
|
10 import unittest
|
8
|
11 from numerics.interpolation import linear_interpolation
|
7
|
12
|
|
13 # globals
|
|
14 here = os.path.dirname(os.path.abspath(__file__))
|
|
15 test_data = os.path.join(here, 'test_data.csv')
|
|
16
|
|
17 class InterpolationUnitTest(unittest.TestCase):
|
|
18
|
|
19 def test_linear_interpolation(self):
|
|
20 """test linear interpolation"""
|
|
21
|
|
22 # make a line
|
|
23 data = [(float(x), float(x)) for x in range(10)]
|
|
24
|
|
25 # interpolate at a point
|
|
26 point = 2.2
|
|
27 values = linear_interpolation(data, [point])
|
|
28 self.assertEqual(len(values), 1)
|
|
29 self.assertEqual(values[0], point)
|
|
30
|
|
31 def read_test_data(self):
|
|
32 """read test data from sample CSV file"""
|
|
33
|
|
34 with open(test_data, 'r') as f:
|
|
35 reader = csv.reader(f)
|
|
36 retval = [[float(col) for col in row] for row in reader]
|
|
37 return retval
|
|
38
|
|
39 def test_from_csv(self):
|
|
40
|
|
41 raw_data = self.read_test_data()
|
|
42 data = [[(row[0], row[col]) for row in raw_data]
|
|
43 for col in (1,2)]
|
|
44 for series in data:
|
|
45 x_values = range(2,10)
|
|
46 interpolated = linear_interpolation(series, x_values)
|
|
47
|
|
48
|
|
49 if __name__ == '__main__':
|
|
50 unittest.main()
|
|
51
|