Mercurial > hg > numerics
annotate tests/test_interpolation.py @ 91:bbe8f3e9615d
fix bug
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Mon, 02 Mar 2015 15:59:17 -0800 (2015-03-02) |
| parents | a09d5ffd2fc9 |
| children |
| rev | line source |
|---|---|
| 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): | |
|
66
7dd1b18c9f78
minor cleanup and better error
Jeff Hammel <k0scist@gmail.com>
parents:
8
diff
changeset
|
40 """test interpolation from a CSV file""" |
| 7 | 41 |
| 42 raw_data = self.read_test_data() | |
| 43 data = [[(row[0], row[col]) for row in raw_data] | |
| 44 for col in (1,2)] | |
| 45 for series in data: | |
| 67 | 46 x_values = range(2, 5) |
| 7 | 47 interpolated = linear_interpolation(series, x_values) |
| 48 | |
| 49 | |
| 50 if __name__ == '__main__': | |
| 51 unittest.main() | |
| 52 |
