From 3a8ad95406031b972ece07d508cc03e4778717ec Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Sun, 14 Jul 2024 00:14:40 +0800 Subject: [PATCH 1/3] Fix E231 E305 error for coreshellnnp.py --- .idea/.gitignore | 8 ++++++ doc/examples/coreshellnp.py | 55 +++++++++++++++++++------------------ 2 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/doc/examples/coreshellnp.py b/doc/examples/coreshellnp.py index 9c806647..2f1da057 100644 --- a/doc/examples/coreshellnp.py +++ b/doc/examples/coreshellnp.py @@ -31,21 +31,22 @@ from diffpy.srfit.fitbase import FitContribution, FitRecipe from diffpy.srfit.fitbase import FitResults -####### Example Code +# Example Code + def makeRecipe(stru1, stru2, datname): """Create a fitting recipe for crystalline PDF data.""" - ## The Profile + # The Profile profile = Profile() # Load data and add it to the profile parser = PDFParser() parser.parseFile(datname) profile.loadParsedData(parser) - profile.setCalculationRange(xmin=1.5, xmax = 45, dx = 0.1) + profile.setCalculationRange(xmin=1.5, xmax=45, dx=0.1) - ## The ProfileGenerator + # The ProfileGenerator # In order to fit the core and shell phases simultaneously, we must use two # PDFGenerators. # @@ -61,20 +62,20 @@ def makeRecipe(stru1, stru2, datname): generator_zns.setQmax(26) generator_zns.qdamp.value = 0.0396 - ## The FitContribution + # The FitContribution # Add both generators and the profile to the FitContribution. contribution = FitContribution("cdszns") contribution.addProfileGenerator(generator_cds) contribution.addProfileGenerator(generator_zns) - contribution.setProfile(profile, xname = "r") + contribution.setProfile(profile, xname="r") # Set up the characteristic functions. We use a spherical CF for the core # and a spherical shell CF for the shell. Since this is set up as two # phases, we implicitly assume that the core-shell correlations contribute # very little to the PDF. from diffpy.srfit.pdf.characteristicfunctions import sphericalCF, shellCF - contribution.registerFunction(sphericalCF, name = "f_CdS") - contribution.registerFunction(shellCF, name = "f_ZnS") + contribution.registerFunction(sphericalCF, name="f_CdS") + contribution.registerFunction(shellCF, name="f_ZnS") # Write the fitting equation. We want to sum the PDFs from each phase and # multiply it by a scaling factor. @@ -90,7 +91,7 @@ def makeRecipe(stru1, stru2, datname): recipe.addVar(contribution.thickness, 11) recipe.constrain(contribution.psize, "2 * radius") - ## Configure the fit variables + # Configure the fit variables # Start by configuring the scale factor and resolution factors. # We want the sum of the phase scale factors to be 1. recipe.newVar("scale_CdS", 0.7) @@ -106,27 +107,28 @@ def makeRecipe(stru1, stru2, datname): # subsequent refinement. phase_cds = generator_cds.phase for par in phase_cds.sgpars.latpars: - recipe.addVar(par, name = par.name + "_cds", tag = "lat") + recipe.addVar(par, name=par.name + "_cds", tag="lat") for par in phase_cds.sgpars.adppars: - recipe.addVar(par, 1, name = par.name + "_cds", tag = "adp") - recipe.addVar(phase_cds.sgpars.xyzpars.z_1, name = "z_1_cds", tag = "xyz") + recipe.addVar(par, 1, name=par.name + "_cds", tag="adp") + recipe.addVar(phase_cds.sgpars.xyzpars.z_1, name="z_1_cds", tag="xyz") # Since we know these have stacking disorder, constrain the B33 adps for # each atom type. recipe.constrain("B33_1_cds", "B33_0_cds") - recipe.addVar(generator_cds.delta2, name = "delta2_cds", value = 5) + recipe.addVar(generator_cds.delta2, name="delta2_cds", value=5) phase_zns = generator_zns.phase for par in phase_zns.sgpars.latpars: - recipe.addVar(par, name = par.name + "_zns", tag = "lat") + recipe.addVar(par, name=par.name + "_zns", tag="lat") for par in phase_zns.sgpars.adppars: - recipe.addVar(par, 1, name = par.name + "_zns", tag = "adp") - recipe.addVar(phase_zns.sgpars.xyzpars.z_1, name = "z_1_zns", tag = "xyz") + recipe.addVar(par, 1, name=par.name + "_zns", tag="adp") + recipe.addVar(phase_zns.sgpars.xyzpars.z_1, name="z_1_zns", tag="xyz") recipe.constrain("B33_1_zns", "B33_0_zns") - recipe.addVar(generator_zns.delta2, name = "delta2_zns", value = 2.5) + recipe.addVar(generator_zns.delta2, name="delta2_zns", value=2.5) # Give the recipe away so it can be used! return recipe + def plotResults(recipe): """Plot the results contained within a refined FitRecipe.""" @@ -138,10 +140,10 @@ def plotResults(recipe): diff = g - gcalc + diffzero import pylab - pylab.plot(r,g,'bo',label="G(r) Data") - pylab.plot(r, gcalc,'r-',label="G(r) Fit") - pylab.plot(r,diff,'g-',label="G(r) diff") - pylab.plot(r,diffzero,'k-') + pylab.plot(r, g, 'bo', label="G(r) Data") + pylab.plot(r, gcalc, 'r-', label="G(r) Fit") + pylab.plot(r, diff, 'g-', label="G(r) diff") + pylab.plot(r, diffzero, 'k-') pylab.xlabel(r"$r (\AA)$") pylab.ylabel(r"$G (\AA^{-2})$") pylab.legend(loc=1) @@ -172,23 +174,23 @@ def main(): # Start with the lattice parameters. In makeRecipe, these were tagged with # "lat". Here is how we use that. recipe.free("lat") - leastsq(recipe.residual, recipe.values, maxfev = 50) + leastsq(recipe.residual, recipe.values, maxfev=50) # Now the scale and phase fraction. recipe.free("scale", "scale_CdS") - leastsq(recipe.residual, recipe.values, maxfev = 50) + leastsq(recipe.residual, recipe.values, maxfev=50) # The ADPs. recipe.free("adp") - leastsq(recipe.residual, recipe.values, maxfev = 100) + leastsq(recipe.residual, recipe.values, maxfev=100) # The delta2 parameters. recipe.free("delta2_cds", "delta2_zns") - leastsq(recipe.residual, recipe.values, maxfev = 50) + leastsq(recipe.residual, recipe.values, maxfev=50) # The shape parameters. recipe.free("radius", "thickness") - leastsq(recipe.residual, recipe.values, maxfev = 50) + leastsq(recipe.residual, recipe.values, maxfev=50) # The positional parameters. recipe.free("xyz") @@ -202,6 +204,7 @@ def main(): plotResults(recipe) return + if __name__ == "__main__": main() From afdb6e6b632cd80246823518238c3de94cdfb9de Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Sun, 14 Jul 2024 00:26:30 +0800 Subject: [PATCH 2/3] Fix lint check errors on calculatorand testfitrecipe.py --- src/diffpy/srfit/fitbase/calculator.py | 5 +--- src/diffpy/srfit/tests/testfitrecipe.py | 31 ++++++++++++------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/diffpy/srfit/fitbase/calculator.py b/src/diffpy/srfit/fitbase/calculator.py index 43f91ec1..6bbdeb56 100644 --- a/src/diffpy/srfit/fitbase/calculator.py +++ b/src/diffpy/srfit/fitbase/calculator.py @@ -29,6 +29,7 @@ from diffpy.srfit.fitbase.parameterset import ParameterSet from diffpy.srfit.equation.literals.operators import Operator + class Calculator(Operator, ParameterSet): """Base class for calculators. @@ -75,12 +76,10 @@ def __init__(self, name): Operator.__init__(self, name) return - @property def symbol(self): return self.name - # Overload me! def __call__(self, *args): """Calculate something. @@ -92,12 +91,10 @@ def __call__(self, *args): """ return 0 - def operation(self, *args): self._value = self.__call__(*args) return self._value - def _validate(self): """Validate my state. diff --git a/src/diffpy/srfit/tests/testfitrecipe.py b/src/diffpy/srfit/tests/testfitrecipe.py index 3df264e9..75bf7c28 100644 --- a/src/diffpy/srfit/tests/testfitrecipe.py +++ b/src/diffpy/srfit/tests/testfitrecipe.py @@ -53,8 +53,8 @@ def testFixFree(self): recipe = self.recipe con = self.fitcontribution - recipe.addVar(con.A, 2, tag = "tagA") - recipe.addVar(con.k, 1, tag = "tagk") + recipe.addVar(con.A, 2, tag="tagA") + recipe.addVar(con.k, 1, tag="tagk") recipe.addVar(con.c, 0) recipe.newVar("B", 0) @@ -78,7 +78,7 @@ def testFixFree(self): self.assertTrue(recipe.isFree(recipe.k)) self.assertTrue(recipe.isFree(recipe.c)) self.assertTrue(recipe.isFree(recipe.B)) - recipe.fix(recipe.A, "tagk", c = 3) + recipe.fix(recipe.A, "tagk", c=3) self.assertFalse(recipe.isFree(recipe.A)) self.assertFalse(recipe.isFree(recipe.k)) self.assertFalse(recipe.isFree(recipe.c)) @@ -91,7 +91,7 @@ def testFixFree(self): self.assertFalse(recipe.isFree(recipe.B)) self.assertRaises(ValueError, recipe.free, "junk") - self.assertRaises(ValueError, recipe.fix, tagA = 1) + self.assertRaises(ValueError, recipe.fix, tagA=1) self.assertRaises(ValueError, recipe.fix, "junk") return @@ -143,7 +143,6 @@ def testVars(self): self.assertTrue(2 in values) return - def testResidual(self): """Test the residual and everything that can change it.""" @@ -156,13 +155,13 @@ def testResidual(self): y = sin(x+1) self.recipe.cont.c.setValue(1) res = self.recipe.residual() - self.assertTrue( array_equal(y-self.profile.y, res) ) + self.assertTrue(array_equal(y-self.profile.y, res)) # Try some constraints # Make c = 2*A, A = Avar var = self.recipe.newVar("Avar") - self.recipe.constrain(self.fitcontribution.c, "2*A", - {"A" : self.fitcontribution.A}) + self.recipe.constrain(self.fitcontribution.c, + "2*A", {"A": self.fitcontribution.A}) self.assertEqual(2, self.fitcontribution.c.value) self.recipe.constrain(self.fitcontribution.A, var) self.assertEqual(1, var.getValue()) @@ -173,7 +172,7 @@ def testResidual(self): x = self.profile.x y = sin(x+2) res = self.recipe.residual() - self.assertTrue( array_equal(y-self.profile.y, res) ) + self.assertTrue(array_equal(y-self.profile.y, res)) # Now try some restraints. We want c to be exactly zero. It should give # a penalty of (c-0)**2, which is 4 in this case @@ -181,7 +180,7 @@ def testResidual(self): self.recipe._ready = False res = self.recipe.residual() chi2 = 4 + dot(y - self.profile.y, y - self.profile.y) - self.assertAlmostEqual(chi2, dot(res, res) ) + self.assertAlmostEqual(chi2, dot(res, res)) # Clear the constraint and restore the value of c to 0. This should # give us chi2 = 0 again. @@ -189,7 +188,7 @@ def testResidual(self): self.fitcontribution.c.setValue(0) res = self.recipe.residual([self.recipe.cont.A.getValue()]) chi2 = 0 - self.assertAlmostEqual(chi2, dot(res, res) ) + self.assertAlmostEqual(chi2, dot(res, res)) # Remove the restraint and variable self.recipe.unrestrain(r1) @@ -197,7 +196,7 @@ def testResidual(self): self.recipe._ready = False res = self.recipe.residual() chi2 = 0 - self.assertAlmostEqual(chi2, dot(res, res) ) + self.assertAlmostEqual(chi2, dot(res, res)) # Add constraints at the fitcontribution level. self.fitcontribution.constrain(self.fitcontribution.c, "2*A") @@ -205,7 +204,7 @@ def testResidual(self): x = self.profile.x y = sin(x+2) res = self.recipe.residual() - self.assertTrue( array_equal(y-self.profile.y, res) ) + self.assertTrue(array_equal(y-self.profile.y, res)) # Add a restraint at the fitcontribution level. r1 = self.fitcontribution.restrain(self.fitcontribution.c, 0, 0, 1) @@ -215,7 +214,7 @@ def testResidual(self): x = self.profile.x y = sin(x+2) chi2 = 4 + dot(y - self.profile.y, y - self.profile.y) - self.assertAlmostEqual(chi2, dot(res, res) ) + self.assertAlmostEqual(chi2, dot(res, res)) # Remove those self.fitcontribution.unrestrain(r1) @@ -224,7 +223,7 @@ def testResidual(self): self.fitcontribution.c.setValue(0) res = self.recipe.residual() chi2 = 0 - self.assertAlmostEqual(chi2, dot(res, res) ) + self.assertAlmostEqual(chi2, dot(res, res)) # Now try to use the observed profile inside of the equation # Set the equation equal to the data @@ -239,7 +238,6 @@ def testResidual(self): return - def testPrintFitHook(self): "check output from default PrintFitHook." self.recipe.addVar(self.fitcontribution.c) @@ -266,5 +264,6 @@ def testPrintFitHook(self): # ---------------------------------------------------------------------------- + if __name__ == "__main__": unittest.main() From aab29dc0bf03e34e0d9839a2a6fb39d466dc5084 Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Sun, 14 Jul 2024 00:35:55 +0800 Subject: [PATCH 3/3] fix lint-check for diffpyparset.py --- src/diffpy/srfit/structure/diffpyparset.py | 75 +++++++++++----------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/src/diffpy/srfit/structure/diffpyparset.py b/src/diffpy/srfit/structure/diffpyparset.py index 4278bf13..60773f69 100644 --- a/src/diffpy/srfit/structure/diffpyparset.py +++ b/src/diffpy/srfit/structure/diffpyparset.py @@ -94,24 +94,24 @@ def __init__(self, name, atom): self.atom = atom a = atom # x, y, z, occupancy - self.addParameter(ParameterAdapter("x", a, _xyzgetter(0), - _xyzsetter(0))) - self.addParameter(ParameterAdapter("y", a, _xyzgetter(1), - _xyzsetter(1))) - self.addParameter(ParameterAdapter("z", a, _xyzgetter(2), - _xyzsetter(2))) - occupancy = ParameterAdapter("occupancy", a, attr = "occupancy") + self.addParameter(ParameterAdapter("x", a, + _xyzgetter(0), _xyzsetter(0))) + self.addParameter(ParameterAdapter("y", a, + _xyzgetter(1), _xyzsetter(1))) + self.addParameter(ParameterAdapter("z", a, + _xyzgetter(2), _xyzsetter(2))) + occupancy = ParameterAdapter("occupancy", a, attr="occupancy") self.addParameter(occupancy) self.addParameter(ParameterProxy("occ", occupancy)) # U - self.addParameter(ParameterAdapter("U11", a, attr = "U11")) - self.addParameter(ParameterAdapter("U22", a, attr = "U22")) - self.addParameter(ParameterAdapter("U33", a, attr = "U33")) - U12 = ParameterAdapter("U12", a, attr = "U12") + self.addParameter(ParameterAdapter("U11", a, attr="U11")) + self.addParameter(ParameterAdapter("U22", a, attr="U22")) + self.addParameter(ParameterAdapter("U33", a, attr="U33")) + U12 = ParameterAdapter("U12", a, attr="U12") U21 = ParameterProxy("U21", U12) - U13 = ParameterAdapter("U13", a, attr = "U13") + U13 = ParameterAdapter("U13", a, attr="U13") U31 = ParameterProxy("U31", U13) - U23 = ParameterAdapter("U23", a, attr = "U23") + U23 = ParameterAdapter("U23", a, attr="U23") U32 = ParameterProxy("U32", U23) self.addParameter(U12) self.addParameter(U21) @@ -119,16 +119,16 @@ def __init__(self, name, atom): self.addParameter(U31) self.addParameter(U23) self.addParameter(U32) - self.addParameter(ParameterAdapter("Uiso", a, attr = "Uisoequiv")) + self.addParameter(ParameterAdapter("Uiso", a, attr="Uisoequiv")) # B - self.addParameter(ParameterAdapter("B11", a, attr = "B11")) - self.addParameter(ParameterAdapter("B22", a, attr = "B22")) - self.addParameter(ParameterAdapter("B33", a, attr = "B33")) - B12 = ParameterAdapter("B12", a, attr = "B12") + self.addParameter(ParameterAdapter("B11", a, attr="B11")) + self.addParameter(ParameterAdapter("B22", a, attr="B22")) + self.addParameter(ParameterAdapter("B33", a, attr="B33")) + B12 = ParameterAdapter("B12", a, attr="B12") B21 = ParameterProxy("B21", B12) - B13 = ParameterAdapter("B13", a, attr = "B13") + B13 = ParameterAdapter("B13", a, attr="B13") B31 = ParameterProxy("B31", B13) - B23 = ParameterAdapter("B23", a, attr = "B23") + B23 = ParameterAdapter("B23", a, attr="B23") B32 = ParameterProxy("B32", B23) self.addParameter(B12) self.addParameter(B21) @@ -136,7 +136,7 @@ def __init__(self, name, atom): self.addParameter(B31) self.addParameter(B23) self.addParameter(B32) - self.addParameter(ParameterAdapter("Biso", a, attr = "Bisoequiv")) + self.addParameter(ParameterAdapter("Biso", a, attr="Bisoequiv")) return def __repr__(self): @@ -156,6 +156,7 @@ def _setElem(self, el): def _latgetter(par): return bind2nd(getattr, par) + def _latsetter(par): return bind2nd(setattr, par) @@ -185,19 +186,19 @@ def __init__(self, lattice): ParameterSet.__init__(self, "lattice") self.angunits = "deg" self.lattice = lattice - l = lattice - self.addParameter(ParameterAdapter("a", l, _latgetter("a"), - _latsetter("a"))) - self.addParameter(ParameterAdapter("b", l, _latgetter("b"), - _latsetter("b"))) - self.addParameter(ParameterAdapter("c", l, _latgetter("c"), - _latsetter("c"))) - self.addParameter(ParameterAdapter("alpha", l, _latgetter("alpha"), - _latsetter("alpha"))) - self.addParameter(ParameterAdapter("beta", l, _latgetter("beta"), - _latsetter("beta"))) - self.addParameter(ParameterAdapter("gamma", l, _latgetter("gamma"), - _latsetter("gamma"))) + lat = lattice + self.addParameter(ParameterAdapter("a", lat, + _latgetter("a"), _latsetter("a"))) + self.addParameter(ParameterAdapter("b", lat, + _latgetter("b"), _latsetter("b"))) + self.addParameter(ParameterAdapter("c", lat, + _latgetter("c"), _latsetter("c"))) + self.addParameter(ParameterAdapter("alpha", lat, _latgetter("alpha"), + _latsetter("alpha"))) + self.addParameter(ParameterAdapter("beta", lat, _latgetter("beta"), + _latsetter("beta"))) + self.addParameter(ParameterAdapter("gamma", lat, _latgetter("gamma"), + _latsetter("gamma"))) return def __repr__(self): @@ -242,10 +243,10 @@ def __init__(self, name, stru): for a in stru: el = a.element.title() # Try to sanitize the name. - el = el.replace("+","p") - el = el.replace("-","m") + el = el.replace("+", "p") + el = el.replace("-", "m") i = cdict.get(el, 0) - aname = "%s%i"%(el,i) + aname = "%s%i" % (el, i) cdict[el] = i+1 atom = DiffpyAtomParSet(aname, a) self.addParameterSet(atom)