def test_morph(self): """check MorphChain.morph() """ # Define the morphs config = { "rmin": 1, "rmax": 6, "rstep": 0.1, "scale": 3.0, } mgrid = MorphRGrid() mscale = MorphScale() chain = MorphChain(config, mgrid, mscale) xobj, yobj, xref, yref = chain(self.xobj, self.yobj, self.xref, self.yref) self.assertTrue((xobj == xref).all()) self.assertAlmostEqual(xobj[0], 1.0) self.assertAlmostEqual(xobj[-1], 4.9) self.assertAlmostEqual(xobj[1] - xobj[0], 0.1) self.assertAlmostEqual(xobj[0], mgrid.rmin) self.assertAlmostEqual(xobj[-1], mgrid.rmax - mgrid.rstep) self.assertAlmostEqual(xobj[1] - xobj[0], mgrid.rstep) self.assertTrue(numpy.allclose(yobj, yref)) return
def test_refine_chain(self): """refine a chain """ # Give this some texture self.yobj[30:] = 5 self.yref[33:] = 15 # Define the morphs config = {"scale": 1.0, "stretch": 0.0} mscale = MorphScale(config) mstretch = MorphStretch(config) chain = MorphChain(config, mscale, mstretch) refiner = Refiner(chain, self.xobj, self.yobj, self.xref, self.yref) res = refiner.refine() # Compare the objective to the reference. Note that due to # interpolation, there will be issues at the boundary of the step # function. xobj, yobj, xref, yref = chain.xyallout err = 15. * 2 res = sum(numpy.fabs(yref - yobj)) self.assertTrue(res < err) self.assertAlmostEqual(chain.scale, 3, 2) self.assertAlmostEqual(chain.stretch, 0.1, 2) return
def test_refine(self): config = { "scale": 1.0, "stretch": 0, "smear": 0, "baselineslope": -4 * numpy.pi * 0.0917132 } # Note that scale must go first, since it does not commute with the # PDF <--> RDF conversion. chain = MorphChain(config) chain.append(MorphScale()) chain.append(MorphStretch()) chain.append(MorphXtalPDFtoRDF()) chain.append(MorphSmear()) chain.append(MorphXtalRDFtoPDF()) refiner = Refiner(chain, self.xobj, self.yobj, self.xref, self.yref) # Do this as two-stage fit. First refine amplitude parameters, and then # position parameters. res = refiner.refine("scale", "smear") res = refiner.refine("scale", "stretch", "smear") xobj, yobj, xref, yref = chain.xyallout # We want the fit good to 1%. We will disregard the last bit of the # fit, since we know we have unavoidable edge effects there. sel = xobj < 9.5 yrsel = yref[sel] diff = yrsel - yobj[sel] rw = (numpy.dot(diff, diff) / numpy.dot(yrsel, yrsel))**0.5 self.assertTrue(rw < 0.01) return