class TestCos(unittest.TestCase): """Unit tests for Cos(x) function""" def setUp(self): from sas.models.Cos import Cos self.cos = Cos() def test1D(self): self.assertEqual(self.cos.run(1.13), math.cos(1.13)) def test2D(self): self.assertEqual(self.cos.run([1.13,0.56]), math.cos(1.13*math.cos(0.56))*math.cos(1.13*math.sin(0.56))) self.assertEqual(self.cos.runXY([1.13,0.56]), math.cos(1.13)*math.cos(0.56))
class TestCos(unittest.TestCase): """Unit tests for Cos(x) function""" def setUp(self): from sas.models.Cos import Cos self.cos = Cos() def test1D(self): self.assertEqual(self.cos.run(1.13), math.cos(1.13)) def test2D(self): self.assertEqual( self.cos.run([1.13, 0.56]), math.cos(1.13 * math.cos(0.56)) * math.cos(1.13 * math.sin(0.56))) self.assertEqual(self.cos.runXY([1.13, 0.56]), math.cos(1.13) * math.cos(0.56))
class TestEvalPythonMethods(unittest.TestCase): """ Testing evalDistribution for pure python models """ def setUp(self): self.model = Cos() def test_scalar_methods(self): """ Simple test comparing the run(), runXY() and evalDistribution methods """ q1 = self.model.run(0.001) q2 = self.model.runXY(0.001) qlist3 = numpy.asarray([0.001, 0.002]) q3 = self.model.evalDistribution(qlist3) q4 = self.model.run(0.002) self.assertEqual(q1, q2) self.assertEqual(q1, q3[0]) self.assertEqual(q4, q3[1]) def test_XY_methods(self): """ Compare to the runXY() method for 2D models. +--------+--------+--------+ qy=0.009 | | | | +--------+--------+--------+ qy-0.006 | | | | +--------+--------+--------+ qy=0.003 | | | | +--------+--------+--------+ qx=0.001 0.002 0.003 """ # These are the expected values for all bins expected = numpy.zeros([3, 3]) for i in range(3): for j in range(3): q_length = math.sqrt((0.001 * (i + 1.0)) * (0.001 * (i + 1.0)) + (0.003 * (j + 1.0)) * (0.003 * (j + 1.0))) expected[i][j] = self.model.run(q_length) qx_values = [0.001, 0.002, 0.003] qy_values = [0.003, 0.006, 0.009] qx = numpy.asarray(qx_values) qy = numpy.asarray(qy_values) new_x = numpy.tile(qx, (len(qy), 1)) new_y = numpy.tile(qy, (len(qx), 1)) new_y = new_y.swapaxes(0, 1) #iq is 1d array now (since 03-12-2010) qx_prime = new_x.flatten() qy_prime = new_y.flatten() iq = self.model.evalDistribution([qx_prime, qy_prime]) for i in range(3): for j in range(3): k = i + len(qx) * j self.assertAlmostEquals(iq[k], expected[i][j]) def test_rectangle_methods(self): """ Compare to the runXY() method for 2D models with a non-square matrix. TODO: Doesn't work for C models apparently +--------+--------+--------+ qy-0.006 | | | | +--------+--------+--------+ qy=0.003 | | | | +--------+--------+--------+ qx=0.001 0.002 0.003 """ # These are the expected values for all bins expected = numpy.zeros([3, 3]) for i in range(3): for j in range(2): q_length = math.sqrt((0.001 * (i + 1.0)) * (0.001 * (i + 1.0)) + (0.003 * (j + 1.0)) * (0.003 * (j + 1.0))) expected[i][j] = self.model.run(q_length) qx_values = [0.001, 0.002, 0.003] qy_values = [0.003, 0.006] qx = numpy.asarray(qx_values) qy = numpy.asarray(qy_values) new_x = numpy.tile(qx, (len(qy), 1)) new_y = numpy.tile(qy, (len(qx), 1)) new_y = new_y.swapaxes(0, 1) #iq is 1d array now (since 03-12-2010) qx_prime = new_x.flatten() qy_prime = new_y.flatten() iq = self.model.evalDistribution([qx_prime, qy_prime]) for i in range(3): for j in range(2): k = i + len(qx) * j self.assertAlmostEquals(iq[k], expected[i][j])
class TestEvalPythonMethods(unittest.TestCase): """ Testing evalDistribution for pure python models """ def setUp(self): self.model= Cos() def test_scalar_methods(self): """ Simple test comparing the run(), runXY() and evalDistribution methods """ q1 = self.model.run(0.001) q2 = self.model.runXY(0.001) qlist3 = numpy.asarray([0.001, 0.002]) q3 = self.model.evalDistribution(qlist3) q4 = self.model.run(0.002) self.assertEqual(q1, q2) self.assertEqual(q1, q3[0]) self.assertEqual(q4, q3[1]) def test_XY_methods(self): """ Compare to the runXY() method for 2D models. +--------+--------+--------+ qy=0.009 | | | | +--------+--------+--------+ qy-0.006 | | | | +--------+--------+--------+ qy=0.003 | | | | +--------+--------+--------+ qx=0.001 0.002 0.003 """ # These are the expected values for all bins expected = numpy.zeros([3,3]) for i in range(3): for j in range(3): q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) ) expected[i][j] = self.model.run(q_length) qx_values = [0.001, 0.002, 0.003] qy_values = [0.003, 0.006, 0.009] qx = numpy.asarray(qx_values) qy = numpy.asarray(qy_values) new_x = numpy.tile(qx, (len(qy),1)) new_y = numpy.tile(qy, (len(qx),1)) new_y = new_y.swapaxes(0,1) #iq is 1d array now (since 03-12-2010) qx_prime = new_x.flatten() qy_prime = new_y.flatten() iq = self.model.evalDistribution([qx_prime, qy_prime]) for i in range(3): for j in range(3): k = i+len(qx)*j self.assertAlmostEquals(iq[k], expected[i][j]) def test_rectangle_methods(self): """ Compare to the runXY() method for 2D models with a non-square matrix. TODO: Doesn't work for C models apparently +--------+--------+--------+ qy-0.006 | | | | +--------+--------+--------+ qy=0.003 | | | | +--------+--------+--------+ qx=0.001 0.002 0.003 """ # These are the expected values for all bins expected = numpy.zeros([3,3]) for i in range(3): for j in range(2): q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) ) expected[i][j] = self.model.run(q_length) qx_values = [0.001, 0.002, 0.003] qy_values = [0.003, 0.006] qx = numpy.asarray(qx_values) qy = numpy.asarray(qy_values) new_x = numpy.tile(qx, (len(qy),1)) new_y = numpy.tile(qy, (len(qx),1)) new_y = new_y.swapaxes(0,1) #iq is 1d array now (since 03-12-2010) qx_prime = new_x.flatten() qy_prime = new_y.flatten() iq = self.model.evalDistribution([qx_prime, qy_prime]) for i in range(3): for j in range(2): k = i+len(qx)*j self.assertAlmostEquals(iq[k], expected[i][j])