view tests/test_interpolation.py @ 9:6906e5fc6dfd

add test data file
author Jeff Hammel <k0scist@gmail.com>
date Thu, 04 Sep 2014 20:57:10 -0700
parents 39f428e8f2b7
children 7dd1b18c9f78
line wrap: on
line source

#!/usr/bin/env python

"""
test interpolation
"""

import csv
import os
import sys
import unittest
from numerics.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()