def setUp(self): """ initialize data""" self.data1 = Loader().load("cyl_400_20.txt") self.data2 = Loader().load("cyl_400_40.txt") # Receives the type of model for the fitting from sas.models.CylinderModel import CylinderModel cyl1 = CylinderModel() cyl1.name = "C1" self.model1 = Model(cyl1) self.model1.set(scale=1.0) self.model1.set(radius=18) self.model1.set(length=200) self.model1.set(sldCyl=3e-006, sldSolv=0.0) self.model1.set(background=0.0) cyl2 = CylinderModel() cyl2.name = "C2" self.model2 = Model(cyl2) self.model2.set(scale=1.0) self.model2.set(radius=37) self.model2.set(length=300) self.model2.set(sldCyl=3e-006, sldSolv=0.0) self.model2.set(background=0.0)
class TestSingleFit(unittest.TestCase): """ test single fitting """ def setUp(self): """ initialize data""" self.data = Loader().load("cyl_400_20.txt") # Create model that fitter understands from sas.models.CylinderModel import CylinderModel self.model = CylinderModel() self.model.setParam("scale", 1.0) self.model.setParam("radius", 18) self.model.setParam("length", 397) self.model.setParam("sldCyl", 3e-006) self.model.setParam("sldSolv", 0.0) self.model.setParam("background", 0.0) # select parameters to fit self.pars1 = ["length", "radius", "scale"] def test_fit(self): """Simple cylinder model fit""" fitter = Fit() fitter.set_data(self.data, 1) fitter.set_model(self.model, 1, self.pars1) fitter.select_problem_for_fit(id=1, value=1) result1, = fitter.fit() self.assert_(result1) self.assertTrue(len(result1.pvec) > 0 or len(result1.pvec) == 0) self.assertTrue(len(result1.stderr) > 0 or len(result1.stderr) == 0) self.assertTrue(math.fabs(result1.pvec[0] - 400.0) / 3.0 < result1.stderr[0]) self.assertTrue(math.fabs(result1.pvec[1] - 20.0) / 3.0 < result1.stderr[1]) self.assertTrue(math.fabs(result1.pvec[2] - 1.0) / 3.0 < result1.stderr[2]) self.assertTrue(result1.fitness < 1.0)
class TestDispModel(unittest.TestCase): def setUp(self): from sas.models.CylinderModel import CylinderModel self.model = CylinderModel() def test_disp_params(self): self.assertEqual(self.model.dispersion['radius']['width'], 0.0) self.model.setParam('radius.width', 0.25) self.assertEqual(self.model.dispersion['radius']['width'], 0.25) self.assertEqual(self.model.getParam('radius.width'), 0.25) self.assertEqual(self.model.dispersion['radius']['type'], 'gaussian')
def setUp(self): """ Set up cylinder model """ from sas.models.CylinderModel import CylinderModel radius = 5 length = 40 density = 20 # Analytical model self.ana = CylinderModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius', radius) self.ana.setParam('length', length) self.ana.setParam('cyl_theta', math.pi/2.0) self.ana.setParam('cyl_phi', math.pi/2.0) # Simulation model self.model = VolumeCanvas.VolumeCanvas() self.handle = self.model.add('cylinder') self.model.setParam('lores_density', density) self.model.setParam('%s.radius' % self.handle, radius) self.model.setParam('%s.length' % self.handle, length) self.model.setParam('scale' , 1.0) self.model.setParam('%s.contrast' % self.handle, 1.0) self.model.setParam('background' , 0.0) self.model.setParam('%s.orientation' % self.handle, [0,0,0])
def setUp(self): """ Set up cylinder model """ from sas.models.CylinderModel import CylinderModel radius = 5 length = 40 density = 20 # Analytical model self.ana = CylinderModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius', radius) self.ana.setParam('length', length) # Simulation model self.model = VolumeCanvas.VolumeCanvas() cyl = VolumeCanvas.CylinderDescriptor() self.handle = self.model.addObject(cyl) self.model.setParam('lores_density', density) self.model.setParam('%s.radius' % self.handle, radius) self.model.setParam('%s.length' % self.handle, length) self.model.setParam('scale' , 1.0) self.model.setParam('%s.contrast' % self.handle, 1.0) self.model.setParam('background' , 0.0)
class TestCylinder(unittest.TestCase): """ Unit tests for calculate_ER (cylinder model) """ def setUp(self): from sas.models.CylinderModel import CylinderModel from sas.models.DiamCylFunc import DiamCylFunc self.comp = CylinderModel() self.diam = DiamCylFunc() def test(self): """ Test 1D model for a Cylinder """ self.comp.setParam("radius", 20) self.comp.setParam("length",400) self.diam.setParam("radius", 20) self.diam.setParam("length",400) self.assertAlmostEqual(self.comp.calculate_ER(), self.diam.run(0.1)/2)
def test_cylinder_fit(self): """ Simple cylinder model fit """ out= Loader().load("cyl_400_20.txt") fitter = Fit() # Receives the type of model for the fitting from sas.models.CylinderModel import CylinderModel model = CylinderModel() model.setParam('sldCyl', 1) model.setParam('sldSolv', 0) model.setParam('scale', 1e-10) pars1 =['length','radius','scale'] fitter.set_data(out,1) fitter.set_model(model,1,pars1, constraints=()) fitter.select_problem_for_fit(id=1,value=1) result1, = fitter.fit() #print result1 #print result1.__dict__ self.assert_(result1) self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) self.assertTrue( math.fabs(result1.pvec[2]-9.0e-12)/3.0 < result1.stderr[2] ) self.assertTrue( result1.fitness < 1.0 )
def setUp(self): from sas.models.CylinderModel import CylinderModel from sas.models.HayterMSAStructure import HayterMSAStructure from sas.models.DiamCylFunc import DiamCylFunc from sas.models.MultiplicationModel import MultiplicationModel self.model = CylinderModel() self.model2 = HayterMSAStructure() self.model3 = MultiplicationModel(self.model, self.model2) self.modelD = DiamCylFunc()
def setUp(self): from sas.models.CylinderModel import CylinderModel self.model= CylinderModel() self.model.setParam('scale', 1.0) self.model.setParam('radius', 20.0) self.model.setParam('length', 400.0) self.model.setParam('sldCyl', 4.e-6) self.model.setParam('sldSolv', 1.e-6) self.model.setParam('background', 0.0) self.model.setParam('cyl_theta', 0.0) self.model.setParam('cyl_phi', 90.0)
def setUp(self): """ initialize data""" self.data = Loader().load("cyl_400_20.txt") # Create model that fitter understands from sas.models.CylinderModel import CylinderModel self.model = CylinderModel() self.model.setParam("scale", 1.0) self.model.setParam("radius", 18) self.model.setParam("length", 397) self.model.setParam("sldCyl", 3e-006) self.model.setParam("sldSolv", 0.0) self.model.setParam("background", 0.0) # select parameters to fit self.pars1 = ["length", "radius", "scale"]
def __init__(self, radius=15, length=100, density = 0.01): from sas.models.CylinderModel import CylinderModel self.name = 'cylinder' self.radius = radius self.length = length self.density = density self.ana = CylinderModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius', radius) self.ana.setParam('length', length) self.ana.setParam('cyl_theta', math.pi/2.0) self.ana.setParam('cyl_phi', math.pi/2.0) self.create()
class TestCylinder(unittest.TestCase): """ Unit tests for Cylinder model using evalDistribution function """ def setUp(self): from sas.models.CylinderModel import CylinderModel self.comp = CylinderModel() 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 cylinder with vector as input""" answer = numpy.array([1.98860592e-04,7.03686335e-05,2.89144683e-06,2.04282827e-06]) 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 cylinder with scalar as input""" self.assertAlmostEqual(self.comp.run(0.2), 0.041761386790780453, 4) def test1D_2(self): """ Test 2D model of a cylinder """ self.comp.setParam('cyl_theta', 10.0) self.comp.setParam('cyl_phi', 10.0) self.assertAlmostEqual(self.comp.run([0.2, 2.5]), 0.038176446608393366, 2) def test1D_3(self): """ Test 2D model for a cylinder for 2 vectors as input """ vect = self.comp.evalDistribution([self.x,self.y]) self.assertAlmostEqual(vect[0],5.06121018e-08,4) self.assertAlmostEqual(vect[len(self.x)-1],2.5978e-11, 4)
class TestCyl(unittest.TestCase): """Unit tests for cylinder""" def setUp(self): from sas.models.CylinderModel import CylinderModel self.comp = CylinderModel() def test1D(self): """ Test 1D model of a cylinder """ self.assertAlmostEqual(self.comp.run(0.2), 0.041761386790780453, 4) def testTime(self): """ Time profiling """ self.comp.run(2.0) t0 = time.clock() self.assertTrue(time.clock()-t0<1e-4) def test2D(self): """ Test 2D model of a cylinder """ self.comp.setParam('cyl_theta', 10.0) self.comp.setParam('cyl_phi', 10.0) self.assertAlmostEqual(self.comp.run([0.2, 2.5]), 0.038176446608393366, 2)
class TestDisperserModel(unittest.TestCase): """ Unit tests for sphere model """ def setUp(self): self.model = CylinderModel() self.model.setParam("cyl_theta", 1.57) self.model.setParam("cyl_phi", 0.1) def test2Disp(self): q = 0.005 phi = 0.10 sigma = 0.3 value_0 = self.model.run([q, phi]) app = Smear(self.model, ['cyl_phi', 'cyl_theta'], [sigma, sigma]) val_py = app.run([q, phi]) # Check that the parameters were returned to original values self.assertEqual(value_0, self.model.run([q, phi])) d = DisperseModel(self.model, ["cyl_phi", "cyl_theta"], [sigma, sigma]) val_c = d.run([q, phi]) self.assertEqual(val_py, val_c)
def checkCylinder(self, points): """ Compare the average over all orientations of the main cylinder axis for a cylinder and the elliptical cylinder with r_ratio = 1 @param points: number of points to average over @return: True if the test passed, otherwise False """ from sas.models.CylinderModel import CylinderModel passed = True npts =points model = EllipticalCylinderModel() model.setParam('r_ratio', 1.0) model.setParam("r_minor", 20) model.setParam("cyl_theta", 90) model.setParam("cyl_phi", 0.0) model.setParam("length", 400) model.setParam("sldEll", 2.0e-6) model.setParam("sldSolv", 1.0e-6) cyl = CylinderModel() cyl.setParam("cyl_theta", 90) cyl.setParam("cyl_phi", 0.0) cyl.setParam("radius", 20) cyl.setParam("length", 400) cyl.setParam("sldCyl", 2.0e-6) cyl.setParam("sldSolv", 1.0e-6) output_f = open("average_func.txt",'w') output_f.write("<q_average> <2d_average> <1d_average>\n") for i_q in range(1, 15): q = 0.3/15.0*i_q value = self.average_point_2D(model, q, npts) ana = cyl.run(q) if q<0.3 and math.fabs(value-ana)/ana>0.05: passed = False output_f.write("%10g %10g %10g\n" % (q, value, ana)) if self.verbose: print "Q=%g: %10g %10g %10g %10g" % (q, value, ana, value-ana, value/ana) output_f.close() return passed
class CylinderValidator(Validator): def __init__(self, radius=15, length=100, density = 0.01): from sas.models.CylinderModel import CylinderModel self.name = 'cylinder' self.radius = radius self.length = length self.density = density self.ana = CylinderModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius', radius) self.ana.setParam('length', length) self.ana.setParam('cyl_theta', math.pi/2.0) self.ana.setParam('cyl_phi', math.pi/2.0) self.create() def create(self): canvas = VolumeCanvas.VolumeCanvas() canvas.setParam('lores_density', self.density) handle = canvas.add('cylinder') canvas.setParam('%s.radius' % handle, self.radius) canvas.setParam('%s.length' % handle, self.length) canvas.setParam('scale' , 1.0) canvas.setParam('%s.contrast' % handle, 1.0) canvas.setParam('background' , 0.0) self.canvas = canvas
class TestcylinderHayterM(unittest.TestCase): """ Unit tests for CylinderModel(Q) * HayterMSAStructure(Q) """ def setUp(self): from sas.models.CylinderModel import CylinderModel from sas.models.HayterMSAStructure import HayterMSAStructure from sas.models.DiamCylFunc import DiamCylFunc from sas.models.MultiplicationModel import MultiplicationModel self.model = CylinderModel() self.model2 = HayterMSAStructure() self.model3 = MultiplicationModel(self.model, self.model2) self.modelD = DiamCylFunc() #Radius of model1.calculate_ER should be equal to the output/2 of DiamFunctions def test_multplication_radius(self): """ test multiplication model (check the effective radius & the output of the multiplication) """ self.model.setParam("radius", 60) self.modelD.setParam("radius", 60) modelDrun = self.modelD.run(0.1)/2 self.model2.setParam("volfraction", 0.2) self.model2.setParam("effect_radius", modelDrun ) #Compare new method with old method self.assertEqual(self.model3.run(0.1), self.model.run(0.1)*self.model2.run(0.1)) #Compare radius from two different calculations. Note: modelD.run(0.0) is DIAMETER self.assertEqual(self.model.calculate_ER(), modelDrun) def testMultiplicationParam(self): """ Test Multiplication (check the parameters)""" ## test details dictionary ## test parameters list list3= self.model3.getParamList() for item in self.model.getParamList(): if not 'scale' in item: self.assert_(item in list3) for item in self.model2.getParamList(): #model3 parameters should not include effect_radius* if not 'effect_radius' in item: self.assert_(item in list3) ## test set value for parameters and get paramaters #self.model3.setParam("scale", 15) #self.assertEqual(self.model3.getParam("scale"), 15) self.model3.setParam("scale_factor", 15) self.assertEqual(self.model3.getParam("scale_factor"), 15) self.model3.setParam("radius", 20) self.assertEqual(self.model3.getParam("radius"), 20) self.model3.setParam("radius.width", 15) self.assertEqual(self.model3.getParam("radius.width"), 15) self.model3.setParam("scale_factor", 15) self.assertEqual(self.model3.getParam("scale_factor"), 15) self.assertEqual(self.model3.getParam("volfraction"), self.model.getParam("scale")) ## Dispersity list3= self.model3.getDispParamList() self.assertEqual(list3, ['radius.npts', 'radius.nsigmas', 'radius.width', 'length.npts', \ 'length.nsigmas', 'length.width', 'cyl_theta.npts', 'cyl_theta.nsigmas', 'cyl_theta.width',\ 'cyl_phi.npts', 'cyl_phi.nsigmas', 'cyl_phi.width']) from sas.models.dispersion_models import ArrayDispersion disp_th = ArrayDispersion() values_th = numpy.zeros(100) weights = numpy.zeros(100) for i in range(100): values_th[i]=(math.pi/99.0*i) weights[i]=(1.0) disp_th.set_weights(values_th, weights) self.model3.set_dispersion('radius', disp_th) model4= self.model3.clone() self.assertEqual(model4.getParam("radius"), 20)
def test_without_resolution(self): """ Simple cylinder model fit """ out=Loader().load("cyl_400_20.txt") # This data file has not error, add them #out.dy = out.y fitter = Fit() fitter.set_data(out,1) # Receives the type of model for the fitting model1 = CylinderModel() model1.setParam("scale", 1.0) model1.setParam("radius",18) model1.setParam("length", 397) model1.setParam("sldCyl",3e-006 ) model1.setParam("sldSolv",0.0 ) model1.setParam("background", 0.0) model = Model(model1) pars1 =['length','radius','scale'] fitter.set_model(model,1,pars1) # What the hell is this line for? fitter.select_problem_for_fit(id=1,value=1) result1, = fitter.fit() #print "result1",result1 self.assert_(result1) self.assertTrue(len(result1.pvec) > 0) self.assertTrue(len(result1.stderr) > 0) self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) self.assertTrue( math.fabs(result1.pvec[2]-1)/3.0 < result1.stderr[2] ) self.assertTrue( result1.fitness < 1.0 )
def _dispersion(self, fitter): # Load data # This data is for a cylinder with # length=400, radius=20, radius disp=5, scale=1e-10 out=Loader().load("cyl_400_20_disp5r.txt") out.dy = numpy.zeros(len(out.y)) for i in range(len(out.y)): out.dy[i] = math.sqrt(out.y[i]) # Receives the type of model for the fitting model1 = CylinderModel() model1.setParam("scale", 10.0) model1.setParam("radius",18) model1.setParam("length", 397) model1.setParam("sldCyl",3e-006 ) model1.setParam("sldSolv",0.0 ) model1.setParam("background", 0.0) # Dispersion parameters model1.dispersion['radius']['width'] = 0.25 model1.dispersion['radius']['npts'] = 50 model = Model(model1) pars1 =['length','radius','scale','radius.width'] fitter.set_data(out,1) fitter.set_model(model,1,pars1) fitter.select_problem_for_fit(id=1,value=1) #import time; T0 = time.time() result1, = fitter.fit() self.assert_(result1) self.assertTrue(len(result1.pvec)>0) self.assertTrue(len(result1.stderr)>0) #print [z for z in zip(result1.param_list,result1.pvec,result1.stderr)] self.assertTrue( math.fabs(result1.pvec[0]-399.8)/3.0 < result1.stderr[0] ) self.assertTrue( math.fabs(result1.pvec[1]-17.5)/3.0 < result1.stderr[1] ) self.assertTrue( math.fabs(result1.pvec[2]-11.1)/3.0 < result1.stderr[2] ) self.assertTrue( math.fabs(result1.pvec[3]-0.276)/3.0 < result1.stderr[3] ) self.assertTrue( result1.fitness < 1.0 )
def test_6(): from sas.models.CylinderModel import CylinderModel radius = 5 length = 40 density = 20 ana = CylinderModel() ana.setParam('scale', 1.0) ana.setParam('contrast', 1.0) ana.setParam('background', 0.0) ana.setParam('radius', radius) ana.setParam('length', length) # Along Y ana.setParam('cyl_theta', 1.57) ana.setParam('cyl_phi', 1.57) # Along Z #ana.setParam('cyl_theta', 0) #ana.setParam('cyl_phi', 0) model = VolumeCanvas.VolumeCanvas() handle = model.add('cylinder') model.setParam('lores_density', density) model.setParam('%s.radius' % handle, radius) model.setParam('%s.length' % handle, length) model.setParam('scale', 1.0) model.setParam('%s.contrast' % handle, 1.0) model.setParam('background', 0.0) # Along Y model.setParam('%s.orientation' % handle, [0, 0, 0]) # Along Z #model.setParam('%s.orientation' % handle, [1.57,0,0]) print(model.npts) for i in range(40): qmax = 0.5 anaX = ana.runXY([qmax * i / 40.0, 0.0]) simX = model.getIq2D(qmax * i / 40.0, 0.0) anaY = ana.runXY([0, qmax * i / 40.0]) simY = model.getIq2D(0, qmax * i / 40.0) print(anaX, simX, simX / anaX, '|', anaY, simY, simY / anaY)
class TestParamChange(unittest.TestCase): """ Tests for oriented (2D) systems """ def setUp(self): """ Set up cylinder model """ from sas.models.CylinderModel import CylinderModel radius = 5 length = 40 density = 20 # Analytical model self.ana = CylinderModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius', radius) self.ana.setParam('length', length) self.ana.setParam('cyl_theta', math.pi/2.0) self.ana.setParam('cyl_phi', math.pi/2.0) # Simulation model self.model = VolumeCanvas.VolumeCanvas() self.handle = self.model.add('cylinder') self.model.setParam('lores_density', density) self.model.setParam('%s.radius' % self.handle, radius) self.model.setParam('%s.length' % self.handle, length) self.model.setParam('scale' , 1.0) self.model.setParam('%s.contrast' % self.handle, 1.0) self.model.setParam('background' , 0.0) self.model.setParam('%s.orientation' % self.handle, [0,0,0]) def testalongY(self): """ Test that a parameter change forces the generation of new space points """ ana_val = self.ana.runXY([0.1, 0.2]) sim_val = self.model.getIq2D(0.1, 0.2) self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) # Change the radius a re-evaluate self.ana.setParam('radius', 10) self.model.setParam('%s.radius' % self.handle, 10) ana_val = self.ana.runXY([0.1, 0.2]) sim_val = self.model.getIq2D(0.1, 0.2) self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 )
def setUp(self): from sas.models.CylinderModel import CylinderModel from sas.models.DiamCylFunc import DiamCylFunc self.comp = CylinderModel() self.diam = DiamCylFunc()
class TestCylinder(unittest.TestCase): """ Testing C++ Cylinder model """ def setUp(self): from sas.models.CylinderModel import CylinderModel self.model= CylinderModel() self.model.setParam('scale', 1.0) self.model.setParam('radius', 20.0) self.model.setParam('length', 400.0) self.model.setParam('sldCyl', 4.e-6) self.model.setParam('sldSolv', 1.e-6) self.model.setParam('background', 0.0) self.model.setParam('cyl_theta', 0.0) self.model.setParam('cyl_phi', 90.0) def test_simple(self): self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) def test_constant(self): from sas.models.dispersion_models import DispersionModel disp = DispersionModel() self.model.setParam('scale', 10.0) self.model.set_dispersion('radius', disp) self.model.dispersion['radius']['width'] = 0.25 self.model.dispersion['radius']['npts'] = 100 self.model.dispersion['radius']['nsigmas'] = 2.5 self.assertAlmostEqual(self.model.run(0.001), 1.021051*4527.47250339, 3) self.assertAlmostEqual(self.model.runXY([0.001, 0.001]), 1.021048*4546.997777604715, 2) def test_gaussian(self): from sas.models.dispersion_models import GaussianDispersion disp = GaussianDispersion() self.model.set_dispersion('radius', disp) self.model.dispersion['radius']['width'] = 0.25 self.model.dispersion['radius']['npts'] = 100 self.model.dispersion['radius']['nsigmas'] = 2 self.model.setParam('scale', 10.0) self.assertAlmostEqual(self.model.run(0.001), 1.1804794*4723.32213339, 3) self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 1.180454*4743.56, 2) def test_clone(self): from sas.models.dispersion_models import GaussianDispersion disp = GaussianDispersion() self.model.set_dispersion('radius', disp) self.model.dispersion['radius']['width'] = 0.25 self.model.dispersion['radius']['npts'] = 100 self.model.dispersion['radius']['nsigmas'] = 2 self.model.setParam('scale', 10.0) new_model = self.model.clone() self.assertAlmostEqual(new_model.run(0.001), 1.1804794*4723.32213339, 3) self.assertAlmostEqual(new_model.runXY([0.001,0.001]), 1.180454*4743.56, 2) def test_gaussian_zero(self): from sas.models.dispersion_models import GaussianDispersion disp = GaussianDispersion() self.model.set_dispersion('radius', disp) self.model.dispersion['radius']['width'] = 0.0 self.model.dispersion['radius']['npts'] = 100 self.model.dispersion['radius']['nsigmas'] = 2.5 self.model.setParam('scale', 1.0) self.assertAlmostEqual(self.model.run(0.001), 450.355, 3) self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 452.299, 3) def test_array(self): """ Perform complete rotational average and compare to 1D """ from sas.models.dispersion_models import ArrayDispersion disp_ph = ArrayDispersion() disp_th = ArrayDispersion() values_ph = numpy.zeros(100) values_th = numpy.zeros(100) weights = numpy.zeros(100) for i in range(100): values_ph[i]=(360/99.0*i) values_th[i]=(180/99.0*i) weights[i]=(1.0) disp_ph.set_weights(values_ph, weights) disp_th.set_weights(values_th, weights) self.model.set_dispersion('cyl_theta', disp_th) self.model.set_dispersion('cyl_phi', disp_ph) val_1d = self.model.run(math.sqrt(0.0002)) val_2d = self.model.runXY([0.01,0.01]) self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02)
def checkCylinder2D(self, phi): """ Check that the 2D scattering intensity reduces to a cylinder when r_ratio = 1.0 @param phi: angle of the vector q on the detector @return: True if the test passed, otherwise False """ from sas.models.CylinderModel import CylinderModel cyl = CylinderModel() cyl.setParam("cyl_theta", 90) cyl.setParam("cyl_phi", 0.0) cyl.setParam("radius", 20) cyl.setParam("length", 400) cyl.setParam("sldCyl", 2.0e-6) cyl.setParam("sldSolv", 1.0e-6) ell = EllipticalCylinderModel() ell.setParam("r_ratio", 1.0) ell.setParam("r_minor", 20) ell.setParam("cyl_theta", 90) ell.setParam("cyl_phi", 0.0) ell.setParam("length", 400) ell.setParam("sldCyl", 2.0e-6) ell.setParam("sldSolv", 1.0e-6) passed = True for i_q in range(1, 30): q = 0.025*i_q ell_val = ell.run([q, phi]) cyl_val = cyl.run([q, phi]) if self.verbose: print "Q=%g Ell=%g Cyl=%g R=%g" %(q, ell_val, cyl_val, ell_val/cyl_val) if math.fabs(ell_val-cyl_val)/cyl_val>0.05: passed= False return passed
import pickle from sas.models.CylinderModel import CylinderModel import copy model = CylinderModel() model.setParam('cyl_theta', 1.234) p = copy.deepcopy(model) assert p.getParam('cyl_theta') == model.getParam('cyl_theta')
def setUp(self): from sas.models.CylinderModel import CylinderModel self.comp = CylinderModel() self.x = numpy.array([1.0, 2.0, 3.0, 4.0]) self.y = self.x + 1
def setUp(self): from sas.models.CylinderModel import CylinderModel self.comp = CylinderModel() self.x = numpy.array([1.0,2.0,3.0, 4.0]) self.y = self.x +1
def setUp(self): self.model = CylinderModel() self.model.setParam("cyl_theta", 1.57) self.model.setParam("cyl_phi", 0.1)
class TestCylinderAddObject(unittest.TestCase): """ Tests for oriented (2D) systems """ def setUp(self): """ Set up cylinder model """ from sas.models.CylinderModel import CylinderModel radius = 5 length = 40 density = 20 # Analytical model self.ana = CylinderModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius', radius) self.ana.setParam('length', length) # Simulation model self.model = VolumeCanvas.VolumeCanvas() cyl = VolumeCanvas.CylinderDescriptor() self.handle = self.model.addObject(cyl) self.model.setParam('lores_density', density) self.model.setParam('%s.radius' % self.handle, radius) self.model.setParam('%s.length' % self.handle, length) self.model.setParam('scale' , 1.0) self.model.setParam('%s.contrast' % self.handle, 1.0) self.model.setParam('background' , 0.0) def testalongY(self): """ Testing cylinder along Y axis """ self.ana.setParam('cyl_theta', math.pi/2.0) self.ana.setParam('cyl_phi', math.pi/2.0) self.model.setParam('%s.orientation' % self.handle, [0,0,0]) ana_val = self.ana.runXY([0.1, 0.2]) sim_val = self.model.getIq2D(0.1, 0.2) #print ana_val, sim_val, sim_val/ana_val self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 )
def checkCylinder2D(self, phi): """ Check that the 2D scattering intensity reduces to a cylinder when r_ratio = 1.0 @param phi: angle of the vector q on the detector @return: True if the test passed, otherwise False """ from sas.models.CylinderModel import CylinderModel cyl = CylinderModel() cyl.setParam("cyl_theta", 90) cyl.setParam("cyl_phi", 0.0) cyl.setParam("radius", 20) cyl.setParam("length", 400) cyl.setParam("sldCyl", 2.0e-6) cyl.setParam("sldSolv", 1.0e-6) ell = EllipticalCylinderModel() ell.setParam("r_ratio", 1.0) ell.setParam("r_minor", 20) ell.setParam("cyl_theta", 90) ell.setParam("cyl_phi", 0.0) ell.setParam("length", 400) ell.setParam("sldCyl", 2.0e-6) ell.setParam("sldSolv", 1.0e-6) passed = True for i_q in range(1, 30): q = 0.025 * i_q ell_val = ell.run([q, phi]) cyl_val = cyl.run([q, phi]) if self.verbose: print "Q=%g Ell=%g Cyl=%g R=%g" % (q, ell_val, cyl_val, ell_val / cyl_val) if math.fabs(ell_val - cyl_val) / cyl_val > 0.05: passed = False return passed
def setUp(self): from sas.models.CylinderModel import CylinderModel self.model = CylinderModel()
def test_6(): from sas.models.CylinderModel import CylinderModel radius = 5 length = 40 density = 20 ana = CylinderModel() ana.setParam('scale', 1.0) ana.setParam('contrast', 1.0) ana.setParam('background', 0.0) ana.setParam('radius', radius) ana.setParam('length', length) # Along Y ana.setParam('cyl_theta', 1.57) ana.setParam('cyl_phi', 1.57) # Along Z #ana.setParam('cyl_theta', 0) #ana.setParam('cyl_phi', 0) model = VolumeCanvas.VolumeCanvas() handle = model.add('cylinder') model.setParam('lores_density', density) model.setParam('%s.radius' % handle, radius) model.setParam('%s.length' % handle, length) model.setParam('scale' , 1.0) model.setParam('%s.contrast' % handle, 1.0) model.setParam('background' , 0.0) # Along Y model.setParam('%s.orientation' % handle, [0,0,0]) # Along Z #model.setParam('%s.orientation' % handle, [1.57,0,0]) print model.npts for i in range(40): qmax = 0.5 anaX = ana.runXY([qmax*i/40.0, 0.0]) simX = model.getIq2D(qmax*i/40.0, 0.0) anaY = ana.runXY([0, qmax*i/40.0]) simY = model.getIq2D(0, qmax*i/40.0) print anaX, simX, simX/anaX, '|', anaY, simY, simY/anaY
def _reset_helper(self, path=None, npts=NPTS): """ Set value to fitter and prepare inputs for map function """ for i in range(npts): data = Loader().load(path) fitter = Fit() #create model model = CylinderModel() model.setParam('scale', 1.0) model.setParam('radius', 20.0) model.setParam('length', 400.0) model.setParam('sldCyl', 4e-006) model.setParam('sldSolv', 1e-006) model.setParam('background', 0.0) for param in model.dispersion.keys(): model.set_dispersion(param, self.polydisp['gaussian']()) model.setParam('cyl_phi.width', 10) model.setParam('cyl_phi.npts', 3) model.setParam('cyl_theta.nsigmas', 10) # for 2 data cyl_theta = 60.0 [deg] cyl_phi= 60.0 [deg] fitter.set_model(model, i, self.param_to_fit, self.list_of_constraints) #smear data current_smearer = smear_selection(data, model) import cPickle p = cPickle.dumps(current_smearer) sm = cPickle.loads(p) fitter.set_data(data=data, id=i, smearer=current_smearer, qmin=self.qmin, qmax=self.qmax) fitter.select_problem_for_fit(id=i, value=1) self.list_of_fitter.append(copy.deepcopy(fitter)) self.list_of_function.append('fit') self.list_of_mapper.append(classMapper)
def checkCylinder(self, points): """ Compare the average over all orientations of the main cylinder axis for a cylinder and the elliptical cylinder with r_ratio = 1 @param points: number of points to average over @return: True if the test passed, otherwise False """ from sas.models.CylinderModel import CylinderModel passed = True npts = points model = EllipticalCylinderModel() model.setParam('r_ratio', 1.0) model.setParam("r_minor", 20) model.setParam("cyl_theta", 90) model.setParam("cyl_phi", 0.0) model.setParam("length", 400) model.setParam("sldEll", 2.0e-6) model.setParam("sldSolv", 1.0e-6) cyl = CylinderModel() cyl.setParam("cyl_theta", 90) cyl.setParam("cyl_phi", 0.0) cyl.setParam("radius", 20) cyl.setParam("length", 400) cyl.setParam("sldCyl", 2.0e-6) cyl.setParam("sldSolv", 1.0e-6) output_f = open("average_func.txt", 'w') output_f.write("<q_average> <2d_average> <1d_average>\n") for i_q in range(1, 15): q = 0.3 / 15.0 * i_q value = self.average_point_2D(model, q, npts) ana = cyl.run(q) if q < 0.3 and math.fabs(value - ana) / ana > 0.05: passed = False output_f.write("%10g %10g %10g\n" % (q, value, ana)) if self.verbose: print "Q=%g: %10g %10g %10g %10g" % (q, value, ana, value - ana, value / ana) output_f.close() return passed