class TestSphere(unittest.TestCase): """ Unit tests for sphere model using evalDistribution function """ def setUp(self): from sas.models.SphereModel import SphereModel self.comp = SphereModel() self.x = numpy.array([1.0,2.0,3.0, 4.0]) self.y = self.x +1 def test1D(self): """ Test 1D model for a sphere with vector as input""" answer = numpy.array([5.63877831e-05,2.57231782e-06,2.73704050e-07,2.54229069e-08]) testvector= self.comp.evalDistribution(self.x) self.assertAlmostEqual(len(testvector),4) for i in xrange(len(answer)): self.assertAlmostEqual(testvector[i],answer[i]) def test1D_1(self): """ Test 2D model for a sphere with scalar as input""" self.assertAlmostEqual(self.comp.run(1.0),5.63877831e-05, 4) def test1D_2(self): """ Test 2D model for a sphere for 2 scalar """ self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 56.3878e-06, 4) def test1D_3(self): """ Test 2D model for a Shpere for 2 vectors as input """ #x= numpy.reshape(self.x, [len(self.x),1]) #y= numpy.reshape(self.y, [1,len(self.y)]) vect = self.comp.evalDistribution([self.x,self.y]) self.assertAlmostEqual(vect[0],9.2985e-07, 4) self.assertAlmostEqual(vect[len(self.x)-1],1.3871e-08, 4)
class TestSphere(unittest.TestCase): """ Unit tests for sphere model using evalDistribution function """ def setUp(self): from sas.models.SphereModel import SphereModel self.comp = SphereModel() self.x = numpy.array([1.0, 2.0, 3.0, 4.0]) self.y = self.x + 1 def test1D(self): """ Test 1D model for a sphere with vector as input""" answer = numpy.array( [5.63877831e-05, 2.57231782e-06, 2.73704050e-07, 2.54229069e-08]) testvector = self.comp.evalDistribution(self.x) self.assertAlmostEqual(len(testvector), 4) for i in xrange(len(answer)): self.assertAlmostEqual(testvector[i], answer[i]) def test1D_1(self): """ Test 2D model for a sphere with scalar as input""" self.assertAlmostEqual(self.comp.run(1.0), 5.63877831e-05, 4) def test1D_2(self): """ Test 2D model for a sphere for 2 scalar """ self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 56.3878e-06, 4) def test1D_3(self): """ Test 2D model for a Shpere for 2 vectors as input """ #x= numpy.reshape(self.x, [len(self.x),1]) #y= numpy.reshape(self.y, [1,len(self.y)]) vect = self.comp.evalDistribution([self.x, self.y]) self.assertAlmostEqual(vect[0], 9.2985e-07, 4) self.assertAlmostEqual(vect[len(self.x) - 1], 1.3871e-08, 4)
class smear_test_1Dpinhole(unittest.TestCase): def setUp(self): # NIST sample data self.data = Loader().load("CMSphere5.txt") # NIST smeared sphere w/ param values below self.answer = Loader().load("CMSphere5smearsphere.txt") # call spheremodel self.model = SphereModel() # setparams consistent with Igor default self.model.setParam('scale', 1.0) self.model.setParam('background', 0.01) self.model.setParam('radius', 60.0) self.model.setParam('sldSolv', 6.3e-06) self.model.setParam('sldSph', 1.0e-06) def test_q(self): """ Compare Pinhole resolution smearing with NIST """ # x values input = numpy.zeros(len(self.data.x)) # set time st1 = time() # cal I w/o smear input = self.model.evalDistribution(self.data.x) # Cal_smear (first call) for i in range(1000): s = QSmearer(self.data, self.model) # stop and record time taken first_call_time = time()-st1 # set new time st = time() # cal I w/o smear (this is not neccessary to call but just to be fare input = self.model.evalDistribution(self.data.x) # smear cal (after first call done above) for i in range(1000): output = s(input) # record time taken last_call_time = time()-st # compare the ratio of ((NIST_answer-SsanView_answer)/NIST_answer) # If the ratio less than 1%, pass the test for i in range(len(self.data.x)): ratio = math.fabs((self.answer.y[i]-output[i])/self.answer.y[i]) if ratio > 0.006: ratio = 0.006 self.assertEqual(math.fabs((self.answer.y[i]-output[i])/ \ self.answer.y[i]), ratio) # print print "\n NIST_time = 10sec:" print "Cal_time(1000 times of first_calls; ) = ", first_call_time print "Cal_time(1000 times of calls) = ", last_call_time
class smear_test_1Dpinhole(unittest.TestCase): def setUp(self): # NIST sample data self.data = Loader().load("CMSphere5.txt") # NIST smeared sphere w/ param values below self.answer = Loader().load("CMSphere5smearsphere.txt") # call spheremodel self.model = SphereModel() # setparams consistent with Igor default self.model.setParam('scale', 1.0) self.model.setParam('background', 0.01) self.model.setParam('radius', 60.0) self.model.setParam('sldSolv', 6.3e-06) self.model.setParam('sldSph', 1.0e-06) def test_q(self): """ Compare Pinhole resolution smearing with NIST """ # x values input = numpy.zeros(len(self.data.x)) # set time st1 = time() # cal I w/o smear input = self.model.evalDistribution(self.data.x) # Cal_smear (first call) for i in range(1000): s = QSmearer(self.data, self.model) # stop and record time taken first_call_time = time()-st1 # set new time st = time() # cal I w/o smear (this is not neccessary to call but just to be fare input = self.model.evalDistribution(self.data.x) # smear cal (after first call done above) for i in range(1000): output = s(input) # record time taken last_call_time = time()-st # compare the ratio of ((NIST_answer-SsanView_answer)/NIST_answer) # If the ratio less than 1%, pass the test for i in range(len(self.data.x)): ratio = math.fabs((self.answer.y[i]-output[i])/self.answer.y[i]) if ratio > 0.006: ratio = 0.006 self.assertEqual(math.fabs((self.answer.y[i]-output[i])/ \ self.answer.y[i]), ratio) # print print "\n NIST_time = 10sec:" print "Cal_time(1000 times of first_calls; ) = ", first_call_time print "Cal_time(1000 times of calls) = ", last_call_time
class TestEvalMethods(unittest.TestCase): """ Testing evalDistribution for C models """ def setUp(self): self.model = SphereModel() 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) q4 = self.model.run(0.002) qlist3 = numpy.asarray([0.001, 0.002]) q3 = self.model.evalDistribution(qlist3) 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): # convert index into 1d array k = i + len(qx) * j self.assertAlmostEquals(iq[k], expected[i][j])
class TestEvalMethods(unittest.TestCase): """ Testing evalDistribution for C models """ def setUp(self): self.model= SphereModel() 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) q4 = self.model.run(0.002) qlist3 = numpy.asarray([0.001, 0.002]) q3 = self.model.evalDistribution(qlist3) 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): # convert index into 1d array k = i+len(qx)*j self.assertAlmostEquals(iq[k], expected[i][j])