annotate tests/test_interpolation.py @ 8:39f428e8f2b7

fix import
author Jeff Hammel <k0scist@gmail.com>
date Thu, 04 Sep 2014 18:25:22 -0700
parents 2cad4536f797
children 7dd1b18c9f78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 test interpolation
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import csv
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import os
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import sys
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import unittest
8
39f428e8f2b7 fix import
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
11 from numerics.interpolation import linear_interpolation
7
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 # globals
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 here = os.path.dirname(os.path.abspath(__file__))
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 test_data = os.path.join(here, 'test_data.csv')
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 class InterpolationUnitTest(unittest.TestCase):
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 def test_linear_interpolation(self):
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 """test linear interpolation"""
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 # make a line
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 data = [(float(x), float(x)) for x in range(10)]
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 # interpolate at a point
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 point = 2.2
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 values = linear_interpolation(data, [point])
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 self.assertEqual(len(values), 1)
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 self.assertEqual(values[0], point)
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 def read_test_data(self):
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 """read test data from sample CSV file"""
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 with open(test_data, 'r') as f:
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 reader = csv.reader(f)
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 retval = [[float(col) for col in row] for row in reader]
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 return retval
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 def test_from_csv(self):
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 raw_data = self.read_test_data()
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 data = [[(row[0], row[col]) for row in raw_data]
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 for col in (1,2)]
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 for series in data:
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 x_values = range(2,10)
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 interpolated = linear_interpolation(series, x_values)
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 if __name__ == '__main__':
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 unittest.main()
2cad4536f797 new unittest
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51