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-5) 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) def test2DXY(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.runXY([0.01, 0.02]), 14.89, 2)
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-5) 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) def test2DXY(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.runXY([0.01, 0.02]), 14.89, 2)
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 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 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 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 )
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)