def setUp(self): """ Set up ellipsoid """ from sas.models.EllipsoidModel import EllipsoidModel radius_a = 60 radius_b = 10 density = 30 self.ana = EllipsoidModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius_a', radius_a) self.ana.setParam('radius_b', radius_b) # Default orientation is there=1.57, phi=0 # Radius_a is along the x direction canvas = VolumeCanvas.VolumeCanvas() canvas.setParam('lores_density', density) self.handle = canvas.add('ellipsoid') canvas.setParam('%s.radius_x' % self.handle, radius_a) canvas.setParam('%s.radius_y' % self.handle, radius_b) canvas.setParam('%s.radius_z' % self.handle, radius_b) canvas.setParam('scale' , 1.0) canvas.setParam('%s.contrast' % self.handle, 1.0) canvas.setParam('background' , 0.0) self.canvas = canvas
def setUp(self): """ Set up ellipsoid """ from sas.models.EllipsoidModel import EllipsoidModel radius_a = 10 radius_b = 15 density = 5 self.ana = EllipsoidModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius_a', radius_a) self.ana.setParam('radius_b', radius_b) canvas = VolumeCanvas.VolumeCanvas() canvas.setParam('lores_density', density) self.handle = canvas.add('ellipsoid') canvas.setParam('%s.radius_x' % self.handle, radius_a) canvas.setParam('%s.radius_y' % self.handle, radius_b) canvas.setParam('%s.radius_z' % self.handle, radius_b) canvas.setParam('scale' , 1.0) canvas.setParam('%s.contrast' % self.handle, 1.0) canvas.setParam('background' , 0.0) self.canvas = canvas self.ana.setParam('axis_theta', 1.57) self.ana.setParam('axis_phi', 0) self.canvas.setParam('%s.orientation' % self.handle, [0,0,0])
def setUp(self): from sas.models.EllipsoidModel import EllipsoidModel self.model= EllipsoidModel() self.model.setParam('scale', 1.0) self.model.setParam('radius_a', 20.0) self.model.setParam('radius_b', 400.0) self.model.setParam('sldEll', 4.e-6) self.model.setParam('sldSolv', 1.e-6) self.model.setParam('background', 0.0) self.model.setParam('axis_theta', 0.0) self.model.setParam('axis_phi', 0.0)
class EllipsoidValidator(Validator): def __init__(self, radius_a=60, radius_b=10, density=0.01): from sas.models.EllipsoidModel import EllipsoidModel #from sas.models.SphereModel import SphereModel self.name = 'ellipsoid' self.radius_a = radius_a self.radius_b = radius_b self.density = density self.ana = EllipsoidModel() #self.ana = SphereModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius_a', radius_a) self.ana.setParam('radius_b', radius_b) #self.ana.setParam('radius', radius_a) # Default orientation is there=1.57, phi=0 # Radius_a is along the x direction self.create() def create(self): canvas = VolumeCanvas.VolumeCanvas() canvas.setParam('lores_density', self.density) handle = canvas.add('ellipsoid') canvas.setParam('%s.radius_x' % handle, self.radius_a) canvas.setParam('%s.radius_y' % handle, self.radius_b) canvas.setParam('%s.radius_z' % handle, self.radius_b) canvas.setParam('scale', 1.0) canvas.setParam('%s.contrast' % handle, 1.0) canvas.setParam('background', 0.0) self.canvas = canvas
class TestEllipsoid(unittest.TestCase): """ Unit tests for calculate_ER (Ellipsoid model) """ def setUp(self): from sas.models.EllipsoidModel import EllipsoidModel from sas.models.DiamEllipFunc import DiamEllipFunc self.comp = EllipsoidModel() self.diam = DiamEllipFunc() def test(self): """ Test 1D model for a Ellipsoid """ self.comp.setParam("radius_a", 20) self.comp.setParam("radius_b", 400) self.diam.setParam("radius_a", 20) self.diam.setParam("radius_b", 400) self.assertAlmostEqual(self.comp.calculate_ER(), self.diam.run(0.1) / 2)
class TestEllipsoid(unittest.TestCase): """ Unit tests for calculate_ER (Ellipsoid model) """ def setUp(self): from sas.models.EllipsoidModel import EllipsoidModel from sas.models.DiamEllipFunc import DiamEllipFunc self.comp = EllipsoidModel() self.diam = DiamEllipFunc() def test(self): """ Test 1D model for a Ellipsoid """ self.comp.setParam("radius_a", 20) self.comp.setParam("radius_b",400) self.diam.setParam("radius_a", 20) self.diam.setParam("radius_b",400) self.assertAlmostEqual(self.comp.calculate_ER(), self.diam.run(0.1)/2)
def makeRecipe(datname): """Create a fitting recipe for ellipsoidal SAS data.""" ## The Profile # This will be used to store the observed and calculated I(Q) data. profile = Profile() # Load data and add it to the Profile. We use a SASParser to load the data # properly and pass the metadata along. parser = SASParser() parser.parseFile(datname) profile.loadParsedData(parser) ## The ProfileGenerator # The SASGenerator is for configuring and calculating a SAS profile. We use # a sas model to configure and serve as the calculation engine of the # generator. This allows us to use the full sas model creation # capabilities, and tie this into SrFit when we want to fit a model to # data. The documentation for the various sas models can be found at # http://www.sasview.org. from sas.models.EllipsoidModel import EllipsoidModel model = EllipsoidModel() generator = SASGenerator("generator", model) ## The FitContribution # Here we associate the Profile and ProfileGenerator, as has been done # before. contribution = FitContribution("ellipsoid") contribution.addProfileGenerator(generator) contribution.setProfile(profile, xname = "q") # We want to fit the log of the signal to the log of the data so that the # higher-Q information remains significant. There are no I(Q) uncertainty # values with the data, so we do not need to worry about the effect this # will have on the estimated parameter uncertainties. contribution.setResidualEquation("log(eq) - log(y)") ## Make the FitRecipe and add the FitContribution. recipe = FitRecipe() recipe.addContribution(contribution) ## Configure the fit variables # The SASGenerator uses the parameters from the params and dispersion # attribues of the model. These vary from model to model, but are adopted # as SrFit Parameters within the generator. Whereas the dispersion # parameters are accessible as, e.g. "radius.width", within the # SASGenerator these are named like "radius_width". # # We want to fit the scale factor, radii and background factors. recipe.addVar(generator.scale, 1) recipe.addVar(generator.radius_a, 50) recipe.addVar(generator.radius_b, 500) recipe.addVar(generator.background, 0) # Give the recipe away so it can be used! return recipe
def __init__(self, radius_a=60, radius_b=10, density = 0.01): from sas.models.EllipsoidModel import EllipsoidModel #from sas.models.SphereModel import SphereModel self.name = 'ellipsoid' self.radius_a = radius_a self.radius_b = radius_b self.density = density self.ana = EllipsoidModel() #self.ana = SphereModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius_a', radius_a) self.ana.setParam('radius_b', radius_b) #self.ana.setParam('radius', radius_a) # Default orientation is there=1.57, phi=0 # Radius_a is along the x direction self.create()
class EllipsoidValidator(Validator): def __init__(self, radius_a=60, radius_b=10, density = 0.01): from sas.models.EllipsoidModel import EllipsoidModel #from sas.models.SphereModel import SphereModel self.name = 'ellipsoid' self.radius_a = radius_a self.radius_b = radius_b self.density = density self.ana = EllipsoidModel() #self.ana = SphereModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius_a', radius_a) self.ana.setParam('radius_b', radius_b) #self.ana.setParam('radius', radius_a) # Default orientation is there=1.57, phi=0 # Radius_a is along the x direction self.create() def create(self): canvas = VolumeCanvas.VolumeCanvas() canvas.setParam('lores_density', self.density) handle = canvas.add('ellipsoid') canvas.setParam('%s.radius_x' % handle, self.radius_a) canvas.setParam('%s.radius_y' % handle, self.radius_b) canvas.setParam('%s.radius_z' % handle, self.radius_b) canvas.setParam('scale' , 1.0) canvas.setParam('%s.contrast' % handle, 1.0) canvas.setParam('background' , 0.0) self.canvas = canvas
def setUp(self): from sas.models.EllipsoidModel import EllipsoidModel from sas.models.DiamEllipFunc import DiamEllipFunc self.comp = EllipsoidModel() self.diam = DiamEllipFunc()
class TestRunMethods(unittest.TestCase): """ Tests run methods for oriented (2D) systems """ def setUp(self): """ Set up ellipsoid """ from sas.models.EllipsoidModel import EllipsoidModel radius_a = 10 radius_b = 15 density = 5 self.ana = EllipsoidModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius_a', radius_a) self.ana.setParam('radius_b', radius_b) canvas = VolumeCanvas.VolumeCanvas() canvas.setParam('lores_density', density) self.handle = canvas.add('ellipsoid') canvas.setParam('%s.radius_x' % self.handle, radius_a) canvas.setParam('%s.radius_y' % self.handle, radius_b) canvas.setParam('%s.radius_z' % self.handle, radius_b) canvas.setParam('scale' , 1.0) canvas.setParam('%s.contrast' % self.handle, 1.0) canvas.setParam('background' , 0.0) self.canvas = canvas self.ana.setParam('axis_theta', 1.57) self.ana.setParam('axis_phi', 0) self.canvas.setParam('%s.orientation' % self.handle, [0,0,0]) def testRunXY_List(self): """ Testing ellipsoid along X """ ana_val = self.ana.runXY([0.1, 0.2]) sim_val = self.canvas.runXY([0.1, 0.2]) #print ana_val, sim_val, sim_val/ana_val try: self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) except: print "Error", ana_val, sim_val, sim_val/ana_val raise sys.exc_type, sys.exc_value def testRunXY_float(self): """ Testing ellipsoid along X """ ana_val = self.ana.runXY(0.1) sim_val = self.canvas.runXY(0.1) #print ana_val, sim_val, sim_val/ana_val try: self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) except: print "Error", ana_val, sim_val, sim_val/ana_val raise sys.exc_type, sys.exc_value def testRun_float(self): """ Testing ellipsoid along X """ ana_val = self.ana.run(0.1) sim_val = self.canvas.run(0.1) #print ana_val, sim_val, sim_val/ana_val try: self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) except: print "Error", ana_val, sim_val, sim_val/ana_val raise sys.exc_type, sys.exc_value def testRun_list(self): """ Testing ellipsoid along X """ ana_val = self.ana.run([0.1, 33.0]) sim_val = self.canvas.run([0.1, 33.0]) #print ana_val, sim_val, sim_val/ana_val try: self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) except: print "Error", ana_val, sim_val, sim_val/ana_val raise sys.exc_type, sys.exc_value
class TestEllipsoid(unittest.TestCase): """ Tests for oriented (2D) systems """ def setUp(self): """ Set up ellipsoid """ from sas.models.EllipsoidModel import EllipsoidModel radius_a = 60 radius_b = 10 density = 30 self.ana = EllipsoidModel() self.ana.setParam('scale', 1.0) self.ana.setParam('contrast', 1.0) self.ana.setParam('background', 0.0) self.ana.setParam('radius_a', radius_a) self.ana.setParam('radius_b', radius_b) # Default orientation is there=1.57, phi=0 # Radius_a is along the x direction canvas = VolumeCanvas.VolumeCanvas() canvas.setParam('lores_density', density) self.handle = canvas.add('ellipsoid') canvas.setParam('%s.radius_x' % self.handle, radius_a) canvas.setParam('%s.radius_y' % self.handle, radius_b) canvas.setParam('%s.radius_z' % self.handle, radius_b) canvas.setParam('scale' , 1.0) canvas.setParam('%s.contrast' % self.handle, 1.0) canvas.setParam('background' , 0.0) self.canvas = canvas def testalongX(self): """ Testing ellipsoid along X """ self.ana.setParam('axis_theta', 1.57) self.ana.setParam('axis_phi', 0) self.canvas.setParam('%s.orientation' % self.handle, [0,0,0]) ana_val = self.ana.runXY([0.1, 0.2]) sim_val = self.canvas.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 testalongZ(self): """ Testing ellipsoid along Z """ self.ana.setParam('axis_theta', 0) self.ana.setParam('axis_phi', 0) self.canvas.setParam('%s.orientation' % self.handle, [0,90,0]) ana_val = self.ana.runXY([0.1, 0.2]) sim_val = self.canvas.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 testalongY(self): """ Testing ellipsoid along Y """ self.ana.setParam('axis_theta', math.pi/2.0) self.ana.setParam('axis_phi', math.pi/2.0) self.canvas.setParam('%s.orientation' % self.handle, [0,0,90]) ana_val = self.ana.runXY([0.05, 0.15]) sim_val = self.canvas.getIq2D(0.05, 0.15) #print ana_val, sim_val, sim_val/ana_val try: self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 ) except: print "Error", ana_val, sim_val, sim_val/ana_val raise sys.exc_type, sys.exc_value
class TestEllipsoid(unittest.TestCase): """ Testing C++ Cylinder model """ def setUp(self): from sas.models.EllipsoidModel import EllipsoidModel self.model= EllipsoidModel() self.model.setParam('scale', 1.0) self.model.setParam('radius_a', 20.0) self.model.setParam('radius_b', 400.0) self.model.setParam('sldEll', 4.e-6) self.model.setParam('sldSolv', 1.e-6) self.model.setParam('background', 0.0) self.model.setParam('axis_theta', 0.0) self.model.setParam('axis_phi', 0.0) def test_simple(self): """ Test simple 1D and 2D values Numbers taken from model that passed validation, before the update to C++ underlying class. """ self.assertAlmostEqual(self.model.run(0.001), 11808.842896863147, 3) self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 11681.990374929677, 3) def test_dispersion(self): """ Test with dispersion """ from sas.models.DisperseModel import DisperseModel disp = DisperseModel(self.model, ['radius_a', 'radius_b'], [5, 50]) disp.setParam('n_pts', 10) self.assertAlmostEqual(disp.run(0.001), 11948.72581312305, 3) self.assertAlmostEqual(disp.runXY([0.001,0.001]), 11811.972359807551, 3) def test_new_disp(self): from sas.models.dispersion_models import GaussianDispersion disp_rm = GaussianDispersion() self.model.set_dispersion('radius_a', disp_rm) self.model.dispersion['radius_a']['width'] = 0.25 self.model.dispersion['radius_a']['npts'] = 10 self.model.dispersion['radius_a']['nsigmas'] = 2 disp_rr = GaussianDispersion() self.model.set_dispersion('radius_b', disp_rr) self.model.dispersion['radius_b']['width'] = 0.125 self.model.dispersion['radius_b']['npts'] = 10 self.model.dispersion['radius_b']['nsigmas'] = 2 self.assertAlmostEqual(self.model.run(0.001), 1.10650710*11948.72581312305, 3) self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 1.105898*11811.972359807551, 2) 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('axis_theta', disp_th) self.model.set_dispersion('axis_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 makeRecipe(ciffile, grdata, iqdata): """Make complex-modeling recipe where I(q) and G(r) are fit simultaneously. The fit I(q) is fed into the calculation of G(r), which provides feedback for the fit parameters of both. """ # Create a PDF contribution as before pdfprofile = Profile() pdfparser = PDFParser() pdfparser.parseFile(grdata) pdfprofile.loadParsedData(pdfparser) pdfprofile.setCalculationRange(xmin = 0.1, xmax = 20) pdfcontribution = FitContribution("pdf") pdfcontribution.setProfile(pdfprofile, xname = "r") pdfgenerator = PDFGenerator("G") pdfgenerator.setQmax(30.0) stru = loadCrystal(ciffile) pdfgenerator.setStructure(stru) pdfcontribution.addProfileGenerator(pdfgenerator) pdfcontribution.setResidualEquation("resv") # Create a SAS contribution as well. We assume the nanoparticle is roughly # elliptical. sasprofile = Profile() sasparser = SASParser() sasparser.parseFile(iqdata) sasprofile.loadParsedData(sasparser) if all(sasprofile.dy == 0): sasprofile.dy[:] = 1 sascontribution = FitContribution("sas") sascontribution.setProfile(sasprofile) from sas.models.EllipsoidModel import EllipsoidModel model = EllipsoidModel() sasgenerator = SASGenerator("generator", model) sascontribution.addProfileGenerator(sasgenerator) sascontribution.setResidualEquation("resv") # Now we set up a characteristic function calculator that depends on the # sas model. cfcalculator = SASCF("f", model) # Register the calculator with the pdf contribution and define the fitting # equation. pdfcontribution.registerCalculator(cfcalculator) # The PDF for a nanoscale crystalline is approximated by # Gnano = f * Gcryst pdfcontribution.setEquation("f * G") # Moving on recipe = FitRecipe() recipe.addContribution(pdfcontribution) recipe.addContribution(sascontribution) # PDF phase = pdfgenerator.phase for par in phase.sgpars: recipe.addVar(par) recipe.addVar(pdfgenerator.scale, 1) recipe.addVar(pdfgenerator.delta2, 0) # SAS recipe.addVar(sasgenerator.scale, 1, name = "iqscale") recipe.addVar(sasgenerator.radius_a, 10) recipe.addVar(sasgenerator.radius_b, 10) # Even though the cfcalculator and sasgenerator depend on the same sas # model, we must still constrain the cfcalculator Parameters so that it is # informed of changes in the refined parameters. recipe.constrain(cfcalculator.radius_a, "radius_a") recipe.constrain(cfcalculator.radius_b, "radius_b") return recipe