2010-04-29 22:29:35 -07:00
|
|
|
#!/usr/bin/env python
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
import unittest
|
2010-10-23 12:49:15 -07:00
|
|
|
|
|
|
|
from interp import baker
|
|
|
|
from interp import grid
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import scipy.spatial
|
|
|
|
|
|
|
|
class TestSequenceFunctions(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
|
2010-01-31 19:47:03 -08:00
|
|
|
self.all_points = [
|
|
|
|
[ 0, 0], # 0
|
|
|
|
[ 1, 0], # 1
|
|
|
|
[ 1, 1], # 2
|
|
|
|
[ 0, 1], # 3
|
|
|
|
[ 1,-1], # 4
|
|
|
|
[ 0,-1], # 5
|
|
|
|
[-1, 1], # 6
|
|
|
|
[-1, 0], # 7
|
|
|
|
[-1,-1], # 8
|
|
|
|
]
|
|
|
|
self.q = [1, 0, 0, 0, 0, 0, 0, 0, 0]
|
2010-03-20 10:34:24 -07:00
|
|
|
self.X = [0.5, 0.25]
|
2010-03-19 20:27:51 -07:00
|
|
|
self.accuracy = 8
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2010-02-06 23:01:02 -08:00
|
|
|
def testImports(self):
|
|
|
|
import numpy
|
|
|
|
import scipy
|
2010-10-23 12:49:15 -07:00
|
|
|
import interp.grid
|
|
|
|
import interp.baker
|
2010-02-06 23:01:02 -08:00
|
|
|
|
2009-12-27 09:48:27 -08:00
|
|
|
def testGetPhis(self):
|
|
|
|
|
|
|
|
X = [0,0]
|
|
|
|
r = [[-1, -1], [0, 2], [1, -1]]
|
|
|
|
|
|
|
|
result = baker.get_phis(X, r)
|
2010-10-06 11:15:09 -07:00
|
|
|
#result = [round(i, self.accuracy) for i in result]
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2010-03-20 19:09:46 -07:00
|
|
|
right_answer = [1/3.0, 1/3.0, 1/3.0]
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
for a,b in zip(result, right_answer):
|
2010-03-20 19:09:46 -07:00
|
|
|
self.assertAlmostEqual(a,b)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
def testGetPhis2(self):
|
|
|
|
|
|
|
|
X = [0.5,0.25]
|
|
|
|
r = [[0, 0], [1, 0], [1, 1]]
|
|
|
|
|
|
|
|
result = baker.get_phis(X, r)
|
|
|
|
|
|
|
|
right_answer = [0.5, 0.25, 0.25]
|
|
|
|
|
|
|
|
for a,b in zip(result, right_answer):
|
|
|
|
self.assertEqual(a,b)
|
|
|
|
|
|
|
|
def testQlinear(self):
|
|
|
|
X = [0.5, 0.25]
|
|
|
|
r = [[0, 0], [1, 0], [1, 1]]
|
|
|
|
q = [1, 0, 0]
|
|
|
|
|
2010-03-05 07:58:07 -08:00
|
|
|
phi, result = baker.qlinear(X, grid.grid(r,q))
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
right_answer = 0.5
|
|
|
|
|
|
|
|
self.assertEqual(result, right_answer)
|
|
|
|
|
2010-01-31 19:47:03 -08:00
|
|
|
def testRunBaker_1(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
extra_points = 3
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2010-01-31 19:47:03 -08:00
|
|
|
R = grid.grid(self.all_points[:size_of_simplex],
|
|
|
|
self.q[:size_of_simplex])
|
2009-12-27 09:48:27 -08:00
|
|
|
|
2010-01-31 19:47:03 -08:00
|
|
|
S = grid.grid(self.all_points[size_of_simplex:size_of_simplex + extra_points],
|
|
|
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
|
|
|
|
|
|
|
answer = baker.run_baker(self.X, R, S)
|
2010-03-20 19:09:46 -07:00
|
|
|
a = answer['a']
|
|
|
|
b = answer['b']
|
|
|
|
c = answer['c']
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2010-03-20 19:09:46 -07:00
|
|
|
self.assertAlmostEqual(a, c)
|
|
|
|
self.assertAlmostEqual(c, 0.0)
|
|
|
|
self.assertAlmostEqual(b, 1/3.0)
|
2010-01-31 19:47:03 -08:00
|
|
|
|
|
|
|
def testRunBaker_2(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
extra_points = 4
|
|
|
|
|
|
|
|
R = grid.grid(self.all_points[:size_of_simplex],
|
|
|
|
self.q[:size_of_simplex])
|
|
|
|
|
|
|
|
S = grid.grid(self.all_points[size_of_simplex:size_of_simplex + extra_points],
|
|
|
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
|
|
|
|
|
|
|
answer = baker.run_baker(self.X, R, S)
|
2010-01-31 20:10:37 -08:00
|
|
|
|
2010-03-20 19:09:46 -07:00
|
|
|
a = answer['a']
|
|
|
|
b = answer['b']
|
|
|
|
c = answer['c']
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2010-03-20 19:09:46 -07:00
|
|
|
self.assertAlmostEqual(a, c)
|
|
|
|
self.assertAlmostEqual(c, 2/3.0)
|
2010-01-31 19:47:03 -08:00
|
|
|
|
|
|
|
def testRunBaker_3(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
extra_points = 5
|
|
|
|
|
|
|
|
R = grid.grid(self.all_points[:size_of_simplex],
|
|
|
|
self.q[:size_of_simplex])
|
|
|
|
|
|
|
|
S = grid.grid(self.all_points[size_of_simplex:size_of_simplex + extra_points],
|
|
|
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
|
|
|
|
|
|
|
answer = baker.run_baker(self.X, R, S)
|
2010-03-20 19:09:46 -07:00
|
|
|
a = answer['a']
|
|
|
|
b = answer['b']
|
|
|
|
c = answer['c']
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2010-03-20 19:09:46 -07:00
|
|
|
self.assertAlmostEqual(a, 13/14.0)
|
|
|
|
self.assertAlmostEqual(b, 2 / 7.0)
|
|
|
|
self.assertAlmostEqual(c, 15/14.0)
|
2010-01-31 19:47:03 -08:00
|
|
|
|
|
|
|
def testRunBaker_4(self):
|
|
|
|
size_of_simplex = 3
|
|
|
|
extra_points = 6
|
|
|
|
|
|
|
|
R = grid.grid(self.all_points[:size_of_simplex],
|
|
|
|
self.q[:size_of_simplex])
|
|
|
|
|
|
|
|
S = grid.grid(self.all_points[size_of_simplex:size_of_simplex + extra_points],
|
|
|
|
self.q[size_of_simplex:size_of_simplex + extra_points])
|
|
|
|
|
|
|
|
answer = baker.run_baker(self.X, R, S)
|
2010-03-20 19:09:46 -07:00
|
|
|
a = answer['a']
|
|
|
|
b = answer['b']
|
|
|
|
c = answer['c']
|
2010-01-31 19:47:03 -08:00
|
|
|
|
2010-03-20 19:09:46 -07:00
|
|
|
self.assertAlmostEqual(a, 48/53.0)
|
|
|
|
self.assertAlmostEqual(b, 15/53.0)
|
|
|
|
self.assertAlmostEqual(c, 54/53.0)
|
2009-12-27 09:48:27 -08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
|
2010-03-19 20:27:51 -07:00
|
|
|
unittest.TextTestRunner(verbosity=3).run(suite)
|