view tests/test_interpolation.py @ 138:488cb433576c

add d3 from http://d3js.org/d3.v3.min.js
author Jeff Hammel <k0scist@gmail.com>
date Sat, 21 Mar 2015 14:54:38 -0700
parents a09d5ffd2fc9
children
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):
        """test interpolation from a CSV file"""

        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, 5)
            interpolated = linear_interpolation(series, x_values)


if __name__ == '__main__':
    unittest.main()