def testGenerator2(self): # Test generator with a profile EllipsoidModel = sasimport('sas.models.EllipsoidModel').EllipsoidModel model = EllipsoidModel() gen = SASGenerator("ellipsoid", model) # Load the data using SAS tools Loader = sasimport('sas.dataloader.loader').Loader loader = Loader() data = datafile("sas_ellipsoid_testdata.txt") datainfo = loader.load(data) profile = SASProfile(datainfo) gen.setProfile(profile) gen.scale.value = 1.0 gen.radius_a.value = 20 gen.radius_b.value = 400 gen.background.value = 0.01 y = gen(profile.xobs) diff = profile.yobs - y res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) return
def testParser1(self): data = datafile("ni-q27r100-neutron.gr") parser = PDFParser() parser.parseFile(data) meta = parser._meta self.assertEqual(data, meta['filename']) self.assertEqual(1, meta['nbanks']) self.assertEqual('N', meta['stype']) self.assertEqual(27, meta['qmax']) self.assertEquals(300, meta.get('temperature')) self.assertEquals(None, meta.get('qdamp')) self.assertEquals(None, meta.get('qbroad')) self.assertEquals(None, meta.get('spdiameter')) self.assertEquals(None, meta.get('scale')) self.assertEquals(None, meta.get('doping')) x, y, dx, dy = parser.getData() self.assertTrue(dx is None) self.assertTrue(dy is None) testx = numpy.linspace(0.01, 100, 10000) diff = testx - x res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) testy = numpy.array([1.144, 2.258, 3.312, 4.279, 5.135, 5.862, 6.445, 6.875, 7.150, 7.272]) diff = testy - y[:10] res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) return
def testLoadtxt(self): """Test the loadtxt method""" prof = self.profile data = datafile("testdata.txt") def _test(p): self.assertAlmostEqual(1e-2, p.x[0]) self.assertAlmostEqual(1.105784e-1, p.y[0]) self.assertAlmostEqual(1.802192e-3, p.dy[0]) # Test normal load prof.loadtxt(data, usecols=(0,1,3)) _test(prof) # Test trying to not set unpack prof.loadtxt(data, usecols=(0,1,3), unpack = False) _test(prof) prof.loadtxt(data, float, '#', None, None, 0, (0,1,3), False) _test(prof) # Try not including dy prof.loadtxt(data, usecols=(0,1)) self.assertAlmostEqual(1e-2, prof.x[0]) self.assertAlmostEqual(1.105784e-1, prof.y[0]) self.assertAlmostEqual(1, prof.dy[0]) # Try to include too little self.assertRaises(ValueError, prof.loadtxt, data, usecols=(0,)) return
def testParser(self): data = datafile("sas_ascii_test_1.txt") parser = SASParser() parser.parseFile(data) x, y, dx, dy = parser.getData() testx = numpy.array([0.002618, 0.007854, 0.01309, 0.01832, 0.02356, 0.02879, 0.03402, 0.03925, 0.04448, 0.0497]) diff = testx - x res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) testy = numpy.array([ 0.02198, 0.02201, 0.02695, 0.02645, 0.03024, 0.3927, 7.305, 17.43, 13.43, 8.346]) diff = testy - y res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) testdy = numpy.array([ 0.002704, 0.001643, 0.002452, 0.001769, 0.001531, 0.1697, 1.006, 0.5351, 0.3677, 0.191]) diff = testdy - dy res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) testdx = numpy.array([0.0004091, 0.005587, 0.005598, 0.005624, 0.005707, 0.005975, 0.006264, 0.006344, 0.006424, 0.006516]) diff = testdx - dx res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) return
def testGenerator2(self): # Test generator with a profile from sans.models.EllipsoidModel import EllipsoidModel model = EllipsoidModel() gen = sas.SASGenerator("ellipsoid", model) # Load the data using SAS tools from diffpy.srfit.sas.sasparser import _import_sans_Loader Loader = _import_sans_Loader() loader = Loader() data = datafile("sas_ellipsoid_testdata.txt") datainfo = loader.load(data) profile = sas.SASProfile(datainfo) gen.setProfile(profile) gen.scale.value = 1.0 gen.radius_a.value = 20 gen.radius_b.value = 400 gen.background.value = 0.01 y = gen(profile.xobs) diff = profile.yobs - y res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) return
def test_pickling(self): "validate PDFContribution.residual() after pickling." from itertools import chain from diffpy.structure import loadStructure pc = self.pc pc.loadData(datafile("ni-q27r100-neutron.gr")) ni = loadStructure(datafile("ni.cif")) ni.Uisoequiv = 0.003 pc.addStructure('ni', ni) pc.setCalculationRange(0, 10) pc2 = pickle.loads(pickle.dumps(pc)) res0 = pc.residual() self.assertTrue(numpy.array_equal(res0, pc2.residual())) for p in chain(pc.iterPars('Uiso'), pc2.iterPars('Uiso')): p.value = 0.004 res1 = pc.residual() self.assertFalse(numpy.allclose(res0, res1)) self.assertTrue(numpy.array_equal(res1, pc2.residual())) return
def setUp(self): self.recipe = recipe = FitRecipe("recipe") recipe.newVar("A", 0) recipe.newVar("sig", 0) recipe.newVar("x0", 0) self.filename = datafile("results.res") self.Aval = 5.77619823e-01 self.sigval = -9.22758690e-01 self.x0val = 6.12422115e+00 return
def test_savetxt(self): "check PDFContribution.savetxt()" from diffpy.structure import Structure pc = self.pc pc.loadData(datafile("si-q27r60-xray.gr")) pc.setCalculationRange(0, 10) pc.addStructure('empty', Structure()) fp = io.BytesIO() self.assertRaises(SrFitError, pc.savetxt, fp) pc.evaluate() pc.savetxt(fp) txt = fp.getvalue().decode() nlines = len(txt.strip().split('\n')) self.assertEqual(1001, nlines) return
def testGenerator(self): qmax = 27.0 gen = self.gen gen.setScatteringType('N') self.assertEqual('N', gen.getScatteringType()) gen.setQmax(qmax) self.assertAlmostEqual(qmax, gen.getQmax()) from diffpy.Structure import PDFFitStructure stru = PDFFitStructure() ciffile = datafile("ni.cif") stru.read(ciffile) for i in range(4): stru[i].Bisoequiv = 1 gen.setStructure(stru) calc = gen._calc # Test parameters for par in gen.iterPars(recurse=False): pname = par.name defval = calc._getDoubleAttr(pname) self.assertEquals(defval, par.getValue()) # Test setting values par.setValue(1.0) self.assertEquals(1.0, par.getValue()) par.setValue(defval) self.assertEquals(defval, par.getValue()) r = numpy.arange(0, 10, 0.1) y = gen(r) # Now create a reference PDF. Since the calculator is testing its # output, we just have to make sure we can calculate from the # PDFGenerator interface. from diffpy.srreal.pdfcalculator import PDFCalculator calc = PDFCalculator() calc.rstep = r[1] - r[0] calc.rmin = r[0] calc.rmax = r[-1] + 0.5 * calc.rstep calc.qmax = qmax calc.setScatteringFactorTableByType('N') calc.eval(stru) yref = calc.pdf diff = y - yref res = numpy.dot(diff, diff) self.assertAlmostEquals(0, res) return
def testGenerator(self): qmax = 27.0 gen = PDFGenerator() gen.setScatteringType('N') self.assertEqual('N', gen.getScatteringType()) gen.setQmax(qmax) self.assertAlmostEqual(qmax, gen.getQmax()) from diffpy.Structure import PDFFitStructure stru = PDFFitStructure() ciffile = datafile("ni.cif") stru.read(ciffile) for i in range(4): stru[i].Bisoequiv = 1 gen.setStructure(stru) calc = gen._calc # Test parameters for par in gen.iterPars(recurse = False): pname = par.name defval = calc._getDoubleAttr(pname) self.assertEquals(defval, par.getValue()) # Test setting values par.setValue(1.0) self.assertEquals(1.0, par.getValue()) par.setValue(defval) self.assertEquals(defval, par.getValue()) r = numpy.arange(0, 10, 0.1) y = gen(r) # Now create a reference PDF. Since the calculator is testing its # output, we just have to make sure we can calculate from the # PDFGenerator interface. from diffpy.srreal.pdfcalculator import PDFCalculator calc = PDFCalculator() calc.rstep = r[1] - r[0] calc.rmin = r[0] calc.rmax = r[-1] + 0.5 * calc.rstep calc.qmax = qmax calc.setScatteringFactorTableByType('N') calc.eval(stru) yref = calc.pdf diff = y - yref res = numpy.dot(diff, diff) self.assertAlmostEquals(0, res) return
def testParser2(self): data = datafile("si-q27r60-xray.gr") parser = PDFParser() parser.parseFile(data) meta = parser._meta self.assertEqual(data, meta['filename']) self.assertEqual(1, meta['nbanks']) self.assertEqual('X', meta['stype']) self.assertEqual(27, meta['qmax']) self.assertEquals(300, meta.get('temperature')) self.assertEquals(None, meta.get('qdamp')) self.assertEquals(None, meta.get('qbroad')) self.assertEquals(None, meta.get('spdiameter')) self.assertEquals(None, meta.get('scale')) self.assertEquals(None, meta.get('doping')) x, y, dx, dy = parser.getData() testx = numpy.linspace(0.01, 60, 5999, endpoint=False) diff = testx - x res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) testy = numpy.array([ 0.1105784, 0.2199684, 0.3270088, 0.4305913, 0.5296853, 0.6233606, 0.7108060, 0.7913456, 0.8644501, 0.9297440 ]) diff = testy - y[:10] res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) testdy = numpy.array([ 0.001802192, 0.003521449, 0.005079115, 0.006404892, 0.007440527, 0.008142955, 0.008486813, 0.008466340, 0.008096858, 0.007416456 ]) diff = testdy - dy[:10] res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) self.assertTrue(dx is None) return
def testParser2(self): data = datafile("si-q27r60-xray.gr") parser = PDFParser() parser.parseFile(data) meta = parser._meta self.assertEqual(data, meta['filename']) self.assertEqual(1, meta['nbanks']) self.assertEqual('X', meta['stype']) self.assertEqual(27, meta['qmax']) self.assertEquals(300, meta.get('temperature')) self.assertEquals(None, meta.get('qdamp')) self.assertEquals(None, meta.get('qbroad')) self.assertEquals(None, meta.get('spdiameter')) self.assertEquals(None, meta.get('scale')) self.assertEquals(None, meta.get('doping')) x, y, dx, dy = parser.getData() testx = numpy.linspace(0.01, 60, 5999, endpoint=False) diff = testx - x res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) testy = numpy.array([0.1105784, 0.2199684, 0.3270088, 0.4305913, 0.5296853, 0.6233606, 0.7108060, 0.7913456, 0.8644501, 0.9297440]) diff = testy - y[:10] res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) testdy = numpy.array([0.001802192, 0.003521449, 0.005079115, 0.006404892, 0.007440527, 0.008142955, 0.008486813, 0.008466340, 0.008096858, 0.007416456]) diff = testdy - dy[:10] res = numpy.dot(diff, diff) self.assertAlmostEqual(0, res) self.assertTrue(dx is None) return
def makeLaMnO3_P1(): from diffpy.structure import Structure stru = Structure() stru.read(datafile('LaMnO3.stru')) return stru