Mercurial > hg > numerics
comparison 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 |
comparison
equal
deleted
inserted
replaced
6:7761aa18e885 | 7:2cad4536f797 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 """ | |
4 test interpolation | |
5 """ | |
6 | |
7 import csv | |
8 import os | |
9 import sys | |
10 import unittest | |
11 from interpolation import linear_interpolation | |
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 |