Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples/coreshellnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def makeRecipe(stru1, stru2, datname):

# Write the fitting equation. We want to sum the PDFs from each phase and
# multiply it by a scaling factor.
contribution.setEquation("scale * (f_CdS * G_CdS + f_ZnS * G_ZnS)")
contribution.set_equation("scale * (f_CdS * G_CdS + f_ZnS * G_ZnS)")

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/crystalpdfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def makeRecipe(
"xsini", xgenerator_sini_ni, xprofile_sini
)
xcontribution_sini.add_profile_generator(xgenerator_sini_si)
xcontribution_sini.setEquation("scale * (xG_sini_ni + xG_sini_si)")
xcontribution_sini.set_equation("scale * (xG_sini_ni + xG_sini_si)")

# As explained in another example, we want to minimize using Rw^2.
xcontribution_ni.setResidualEquation("resv")
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/crystalpdftwophase.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def makeRecipe(niciffile, siciffile, datname):
# multiply it by a scaling factor. We also want a certain phase scaling
# relationship between the PDFs which we will enforce with constraints in
# the FitRecipe.
contribution.setEquation("scale * (G_ni + G_si)")
contribution.set_equation("scale * (G_ni + G_si)")

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/debyemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def makeRecipe():
# the debye equation to be positive, so we specify the input as abs(thetaD)
# in the equation below. Furthermore, we know 'm', the mass of lead, so we
# can specify that as well.
contribution.setEquation("debye(T, 207.2, abs(thetaD)) + offset")
contribution.set_equation("debye(T, 207.2, abs(thetaD)) + offset")

# The FitRecipe
# The FitRecipe lets us define what we want to fit. It is where we can
Expand Down
5 changes: 3 additions & 2 deletions docs/examples/gaussiangenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@

Extensions

- Remove the amplitude from GaussianGenerator and instead use the 'setEquation'
method of the FitContribution to account for it. Note that the
- Remove the amplitude from GaussianGenerator and instead use the
'set_equation' method of the
FitContribution to account for it. Note that the
GaussianGenerator will be accessible by its name, "g".
"""

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/gaussianrecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def makeRecipe():
# contribution by name. Since we told the contribution that our
# independent variable is named "x", this value will be substituted into
# the fitting equation whenever it is called.
contribution.setEquation("A * exp(-0.5*(x-x0)**2/sigma**2)")
contribution.set_equation("A * exp(-0.5*(x-x0)**2/sigma**2)")

# To demonstrate how these parameters are used, we will give "A" an initial
# value. Note that Parameters are not numbers, but are containers for
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main():
# "<<" - Inject a parameter value
c = FitContribution("g1")
c.set_profile(p)
c.setEquation("A * exp(-0.5*(x-x0)**2/sigma**2)")
c.set_equation("A * exp(-0.5*(x-x0)**2/sigma**2)")
c.A << 0.5
c.x0 << 5
c.sigma << 1
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/npintensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def gaussian(q, q0, width):
# convolve the signal with the Gaussian to broaden it. Recall that we don't
# need to supply arguments to the registered functions unless we want to
# make changes to their input values.
contribution.setEquation("scale * convolve(I, gaussian) + bkgd")
contribution.set_equation("scale * convolve(I, gaussian) + bkgd")

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/npintensityII.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def gaussian(q, q0, width):

# Now we can incorporate the scale and bkgd into our calculation. We also
# convolve the signal with the gaussian to broaden it.
contribution1.setEquation("scale * convolve(I, gaussian) + bkgd")
contribution2.setEquation("scale * convolve(I, gaussian) + bkgd")
contribution1.set_equation("scale * convolve(I, gaussian) + bkgd")
contribution2.set_equation("scale * convolve(I, gaussian) + bkgd")

# Make a FitRecipe and associate the FitContributions.
recipe = FitRecipe()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/nppdfcrystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def makeRecipe(ciffile, grdata):
pdfcontribution.registerFunction(sphericalCF, name="f")

# Now we set up the fitting equation.
pdfcontribution.setEquation("f * G")
pdfcontribution.set_equation("f * G")

# Now make the recipe. Make sure we fit the characteristic function shape
# parameters, in this case 'psize', which is the diameter of the particle.
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/nppdfsas.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def makeRecipe(ciffile, grdata, iqdata):
pdfcontribution.registerCalculator(cfcalculator)
# The PDF for a nanoscale crystalline is approximated by
# Gnano = f * Gcryst
pdfcontribution.setEquation("f * G")
pdfcontribution.set_equation("f * G")

# Moving on
recipe = FitRecipe()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/simplerecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main():
# Set the equation. The variable "x" is taken from the data that was just
# loaded. The other variables, "A", "x0" and "sigma" are turned into
# attributes with an initial value of 0.
recipe.setEquation("A * exp(-0.5*(x-x0)**2/sigma**2)")
recipe.set_equation("A * exp(-0.5*(x-x0)**2/sigma**2)")

# We can give them other values here.
recipe.A = 1
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/threedoublepeaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def delta(t, mu):
contribution.registerStringFunction(bkgdstr, "bkgd")

# Now define our fitting equation. We will hardcode the peak ratios.
contribution.setEquation(
contribution.set_equation(
"A1 * ( convolve( delta(t, mu11), peakshape(t, c, sig11) ) \
+ 0.23*convolve( delta(t, mu12), peakshape(t, c, sig12) ) ) + \
A2 * ( convolve( delta(t, mu21), peakshape(t, c, sig21) ) \
Expand Down
23 changes: 23 additions & 0 deletions news/setequation-dep.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added ``set_equation`` method to replace ``setEquation``.

**Changed:**

* <news item>

**Deprecated:**

* Deprecated ``setEquation`` for removal in 4.0.0.

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
23 changes: 21 additions & 2 deletions src/diffpy/srfit/fitbase/fitcontribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
base = "diffpy.srfit.fitbase.FitContribution"
removal_version = "4.0.0"

setequation_dep_msg = build_deprecation_message(
base,
"setEquation",
"set_equation",
removal_version,
)


setprofile_dep_msg = build_deprecation_message(
base,
"setProfile",
Expand Down Expand Up @@ -226,7 +234,7 @@ def add_profile_generator(self, gen, name=None):
# Make this our equation if we don't have one. This will set the
# residual equation if necessary.
if self._eq is None:
self.setEquation(name)
self.set_equation(name)

return

Expand All @@ -242,7 +250,7 @@ def addProfileGenerator(self, gen, name=None):
self.add_profile_generator(gen, name=name)
return

def setEquation(self, eqstr, ns={}):
def set_equation(self, eqstr, ns={}):
"""Set the profile equation for the FitContribution.

This sets the equation that will be used when generating the residual
Expand Down Expand Up @@ -285,6 +293,17 @@ def setEquation(self, eqstr, ns={}):

return

@deprecated(setequation_dep_msg)
def setEquation(self, eqstr, ns={}):
"""This function has been deprecated and will be removed in version
4.0.0.

Please use diffpy.srfit.fitbase.FitContribution.set_equation
instead.
"""
self.set_equation(eqstr, ns=ns)
return

def getEquation(self):
"""Get math expression string for the active profile equation.

Expand Down
4 changes: 2 additions & 2 deletions src/diffpy/srfit/fitbase/simplerecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def loadtxt(self, *args, **kw):
return self.profile.loadtxt(*args, **kw)

# FitContribution
def setEquation(self, eqstr, ns={}):
def set_equation(self, eqstr, ns={}):
"""Set the profile equation for the FitContribution.

This sets the equation that will be used when generating the residual.
Expand All @@ -222,7 +222,7 @@ def setEquation(self, eqstr, ns={}):
Raises ValueError if ns uses a name that is already used for a
variable.
"""
self.contribution.setEquation(eqstr, ns={})
self.contribution.set_equation(eqstr, ns={})
# Extract variables
for par in self.contribution:
# Skip Profile Parameters
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/srfit/pdf/pdfcontribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _setup_generator(self, gen):
gnames = self._generators.keys()
eqstr = " + ".join(gnames)
eqstr = "scale * (%s)" % eqstr
self.setEquation(eqstr)
self.set_equation(eqstr)

# Update with our metadata
gen.meta.update(self._meta)
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def build_recipe_one_contribution():
profile.setObservedProfile(x, y)
contribution = FitContribution("c1")
contribution.set_profile(profile)
contribution.setEquation("A*sin(k*x + c)")
contribution.set_equation("A*sin(k*x + c)")
recipe = FitRecipe()
recipe.add_contribution(contribution)
recipe.addVar(contribution.A, 1)
Expand All @@ -173,14 +173,14 @@ def build_recipe_two_contributions():
profile1.setObservedProfile(x, y1)
contribution1 = FitContribution("c1")
contribution1.set_profile(profile1)
contribution1.setEquation("A*sin(k*x + c)")
contribution1.set_equation("A*sin(k*x + c)")

profile2 = Profile()
y2 = 0.5 * sin(2 * x)
profile2.setObservedProfile(x, y2)
contribution2 = FitContribution("c2")
contribution2.set_profile(profile2)
contribution2.setEquation("B*sin(m*x + d)")
contribution2.set_equation("B*sin(m*x + d)")
recipe = FitRecipe()
recipe.add_contribution(contribution1)
recipe.add_contribution(contribution2)
Expand Down
38 changes: 26 additions & 12 deletions tests/test_contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def testset_profile(self):
self.assertRaises(TypeError, fc1.setProfile, "invalid")
# check if residual equation is set up when possible
fc2 = FitContribution("test2")
fc2.setEquation("A * x")
fc2.set_equation("A * x")
fc2.set_profile(profile)
self.assertFalse(fc2._reseq is None)
return
Expand Down Expand Up @@ -160,7 +160,7 @@ def test_getResidualEquation(self):
fc = self.fitcontribution
self.assertEqual("", fc.getResidualEquation())
fc.set_profile(self.profile)
fc.setEquation("A * x + B")
fc.set_equation("A * x + B")
self.assertEqual("((eq - y) / dy)", fc.getResidualEquation())
fc.setResidualEquation("2 * (eq - y)")
self.assertEqual("(2 * (eq - y))", fc.getResidualEquation())
Expand All @@ -171,7 +171,7 @@ def test_releaseOldEquations(self):
fc = self.fitcontribution
self.assertEqual(0, len(fc._eqfactory.equations))
for i in range(5):
fc.setEquation("A * x + B")
fc.set_equation("A * x + B")
self.assertEqual(1, len(fc._eqfactory.equations))
fc.set_profile(self.profile)
for i in range(5):
Expand All @@ -180,15 +180,15 @@ def test_releaseOldEquations(self):
return

def test_registerFunction(self):
"""Ensure registered function works after second setEquation call."""
"""Ensure registered function works after second set_equation call."""
fc = self.fitcontribution
fc.registerFunction(_fsquare, name="fsquare")
fc.setEquation("fsquare")
fc.set_equation("fsquare")
fc.x.setValue(5)
self.assertEqual(25, fc.evaluate())
fc.x << 6
self.assertEqual(36, fc.evaluate())
fc.setEquation("fsquare + 5")
fc.set_equation("fsquare + 5")
self.assertEqual(41, fc.evaluate())
fc.x << -1
self.assertEqual(6, fc.evaluate())
Expand Down Expand Up @@ -227,7 +227,7 @@ def testResidual(noObserversInGlobalBuilders):
assert dot(chiv, chiv) == pytest.approx(0)

# Now change the equation
fc.setEquation("2*I")
fc.set_equation("2*I")
assert fc._eq._value is None
assert fc._reseq._value is None
chiv = fc.residual()
Expand All @@ -236,15 +236,15 @@ def testResidual(noObserversInGlobalBuilders):
# Try to add a parameter
c = Parameter("c", 2)
fc._add_parameter(c)
fc.setEquation("c*I")
fc.set_equation("c*I")
assert fc._eq._value is None
assert fc._reseq._value is None
chiv = fc.residual()
assert dot(chiv, chiv) == pytest.approx(dot(yobs, yobs))

# Try something more complex
c.setValue(3)
fc.setEquation("c**2*sin(I)")
fc.set_equation("c**2*sin(I)")
assert fc._eq._value is None
assert fc._reseq._value is None
xobs = arange(0, 10, 0.5)
Expand All @@ -257,7 +257,7 @@ def testResidual(noObserversInGlobalBuilders):
assert dot(chiv, chiv) == pytest.approx(0)

# Choose a new residual.
fc.setEquation("2*I")
fc.set_equation("2*I")
fc.setResidualEquation("resv")
chiv = fc.residual()
assert dot(chiv, chiv) == pytest.approx(
Expand All @@ -276,7 +276,7 @@ def testResidual(noObserversInGlobalBuilders):
fc1.set_profile(profile)
with pytest.raises(SrFitError):
fc1.setResidualEquation("chiv")
fc1.setEquation("A * x")
fc1.set_equation("A * x")
fc1.setResidualEquation("chiv")
assert noObserversInGlobalBuilders
return
Expand All @@ -296,11 +296,25 @@ def test_setEquation(noObserversInGlobalBuilders):
return


def test_set_equation(noObserversInGlobalBuilders):
"""Check replacement of removed parameters."""
fc = FitContribution("test")
fc.set_equation("x + 5")
fc.x.setValue(2)
assert 7 == fc.evaluate()
fc.removeParameter(fc.x)
x = arange(0, 10, 0.5)
fc.newParameter("x", x)
assert np.array_equal(5 + x, fc.evaluate())
assert noObserversInGlobalBuilders
return


def test_getEquation(noObserversInGlobalBuilders):
"""Check getting the current profile simulation formula."""
fc = FitContribution("test")
assert "" == fc.getEquation()
fc.setEquation("A * sin(x + 5)")
fc.set_equation("A * sin(x + 5)")
assert "(A * sin((x + 5)))" == fc.getEquation()
assert noObserversInGlobalBuilders
return
Expand Down
Loading
Loading