view tests/test_interpolation.py @ 63:0df8bcb6d521

stubbing: unicode histograms
author Jeff Hammel <k0scist@gmail.com>
date Thu, 26 Feb 2015 15:12:48 -0800
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()