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
8 changes: 4 additions & 4 deletions docs/examples/crystalpdfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def makeRecipe(
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")
xcontribution_si.setResidualEquation("resv")
ncontribution_ni.setResidualEquation("resv")
xcontribution_sini.setResidualEquation("resv")
xcontribution_ni.set_residual_equation("resv")
xcontribution_si.set_residual_equation("resv")
ncontribution_ni.set_residual_equation("resv")
xcontribution_sini.set_residual_equation("resv")

# Make the FitRecipe and add the FitContributions.
recipe = FitRecipe()
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/crystalpdftwodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def makeRecipe(ciffile, xdatname, ndatname):
# The contribution's residual can be either chi^2, Rw^2, or custom crafted.
# In this case, we should minimize Rw^2 of each contribution so that each
# one can contribute roughly equally to the fit.
xcontribution.setResidualEquation("resv")
ncontribution.setResidualEquation("resv")
xcontribution.set_residual_equation("resv")
ncontribution.set_residual_equation("resv")

# Make the FitRecipe and add the FitContributions.
recipe = FitRecipe()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/ellipsoidsas.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def makeRecipe(datname):
# 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)")
contribution.set_residual_equation("log(eq) - log(y)")

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/nppdfsas.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def makeRecipe(ciffile, grdata, iqdata):
stru = loadCrystal(ciffile)
pdfgenerator.setStructure(stru)
pdfcontribution.add_profile_generator(pdfgenerator)
pdfcontribution.setResidualEquation("resv")
pdfcontribution.set_residual_equation("resv")

# Create a SAS contribution as well. We assume the nanoparticle is roughly
# elliptical.
Expand All @@ -78,7 +78,7 @@ def makeRecipe(ciffile, grdata, iqdata):
model = EllipsoidModel()
sasgenerator = SASGenerator("generator", model)
sascontribution.add_profile_generator(sasgenerator)
sascontribution.setResidualEquation("resv")
sascontribution.set_residual_equation("resv")

# Now we set up a characteristic function calculator that depends on the
# sas model.
Expand Down
25 changes: 25 additions & 0 deletions news/fitcontrib-dep-final.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**Added:**

* Added ``get_residual_equation`` method to ``FitContribution``.
* Added ``set_residual_equation`` method to ``FitContribution``.

**Changed:**

* <news item>

**Deprecated:**

* Deprecated ``getResidualEquation`` method of ``FitContribution`` for removal in 4.0.0.
* Deprecated ``setResidualEquation`` method of ``FitContribution`` for removal in 4.0.0.

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
50 changes: 44 additions & 6 deletions src/diffpy/srfit/fitbase/fitcontribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@
removal_version,
)

setresidualequation_dep_msg = build_deprecation_message(
base,
"setResidualEquation",
"set_residual_equation",
removal_version,
)

getresidualequation_dep_msg = build_deprecation_message(
base,
"getResidualEquation",
"get_residual_equation",
removal_version,
)


class FitContribution(ParameterSet):
"""FitContribution class.
Expand Down Expand Up @@ -186,7 +200,7 @@ def set_profile(self, profile, xname=None, yname=None, dyname=None):

# If we have _eq, but not _reseq, set the residual
if self._eq is not None and self._reseq is None:
self.setResidualEquation("chiv")
self.set_residual_equation("chiv")

return

Expand Down Expand Up @@ -262,7 +276,7 @@ def set_equation(self, eqstr, ns={}):

This sets the equation that will be used when generating the residual
for this FitContribution. The equation will be usable within
setResidualEquation as "eq", and it takes no arguments.
set_residual_equation as "eq", and it takes no arguments.

Attributes
----------
Expand Down Expand Up @@ -296,7 +310,7 @@ def set_equation(self, eqstr, ns={}):

# Set the residual if we need to
if self.profile is not None and self._reseq is None:
self.setResidualEquation("chiv")
self.set_residual_equation("chiv")

return

Expand Down Expand Up @@ -334,7 +348,7 @@ def getEquation(self):
"""
return self.get_equation()

def setResidualEquation(self, eqstr):
def set_residual_equation(self, eqstr):
"""Set the residual equation for the FitContribution.

Attributes
Expand Down Expand Up @@ -378,7 +392,19 @@ def setResidualEquation(self, eqstr):

return

def getResidualEquation(self):
@deprecated(setresidualequation_dep_msg)
def setResidualEquation(self, eqstr):
"""This function has been deprecated and will be removed in version
4.0.0.

Please use
diffpy.srfit.fitbase.FitContribution.set_residual_equation
instead.
"""
self.set_residual_equation(eqstr)
return

def get_residual_equation(self):
"""Get math expression string for the active residual equation.

Return normalized math formula or an empty string if residual
Expand All @@ -391,6 +417,18 @@ def getResidualEquation(self):
rv = getExpression(self._reseq, eqskip="eq$")
return rv

@deprecated(getresidualequation_dep_msg)
def getResidualEquation(self):
"""This function has been deprecated and will be removed in version
4.0.0.

Please use
diffpy.srfit.fitbase.FitContribution.get_residual_equation
instead.
"""

return self.get_residual_equation()

def residual(self):
"""Calculate the residual for this fitcontribution.

Expand All @@ -402,7 +440,7 @@ def residual(self):
chiv = (eq() - self.profile.y) / self.profile.dy
The value that is optimized is dot(chiv, chiv).

The residual equation can be changed with the setResidualEquation
The residual equation can be changed with the set_residual_equation
method.
"""
# Assign the calculated profile
Expand Down
24 changes: 22 additions & 2 deletions src/diffpy/srfit/fitbase/simplerecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
removal_version,
)

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


class SimpleRecipe(FitRecipe):
"""SimpleRecipe class.
Expand Down Expand Up @@ -224,6 +231,7 @@ def set_calculation_range(self, xmin=None, xmax=None, dx=None):
"""
return self.profile.set_calculation_range(xmin, xmax, dx)

@deprecated(setCalculationRange_dep_msg)
def setCalculationRange(self, xmin=None, xmax=None, dx=None):
"""This function has been deprecated and will be removed in version
4.0.0.
Expand Down Expand Up @@ -280,8 +288,8 @@ 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.
The equation will be usable within setResidualEquation as "eq", and it
takes no arguments.
The equation will be usable within set_residual_equation as "eq", and
it takes no arguments.

Attributes
----------
Expand Down Expand Up @@ -309,6 +317,18 @@ def set_equation(self, eqstr, ns={}):
self.addVar(par)
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.simplerecipe.SimpleRecipe.set_equation
instead.
"""
self.set_equation(eqstr, ns)
return

def __call__(self):
"""Evaluate the contribution equation."""
return self.contribution.evaluate()
Expand Down
27 changes: 19 additions & 8 deletions tests/test_contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,26 @@ def testReplacements(self):
self.assertEqual(len(xobs2), len(fc.residual()))
return

def test_get_residual_equation(self):
"""Check getting the current formula for residual equation."""
fc = self.fitcontribution
self.assertEqual("", fc.get_residual_equation())
fc.set_profile(self.profile)
fc.set_equation("A * x + B")
self.assertEqual("((eq - y) / dy)", fc.get_residual_equation())
fc.set_residual_equation("2 * (eq - y)")
self.assertEqual("(2 * (eq - y))", fc.get_residual_equation())
return

def test_getResidualEquation(self):
"""Check getting the current formula for residual equation."""
fc = self.fitcontribution
self.assertEqual("", fc.getResidualEquation())
self.assertEqual("", fc.get_residual_equation())
fc.set_profile(self.profile)
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())
self.assertEqual("(2 * (eq - y))", fc.get_residual_equation())
return

def test_releaseOldEquations(self):
Expand All @@ -175,7 +186,7 @@ def test_releaseOldEquations(self):
self.assertEqual(1, len(fc._eqfactory.equations))
fc.set_profile(self.profile)
for i in range(5):
fc.setResidualEquation("chiv")
fc.set_residual_equation("chiv")
self.assertEqual(2, len(fc._eqfactory.equations))
return

Expand Down Expand Up @@ -258,26 +269,26 @@ def testResidual(noObserversInGlobalBuilders):

# Choose a new residual.
fc.set_equation("2*I")
fc.setResidualEquation("resv")
fc.set_residual_equation("resv")
chiv = fc.residual()
assert dot(chiv, chiv) == pytest.approx(
sum((2 * xobs - yobs) ** 2) / sum(yobs**2)
)

# Make a custom residual.
fc.setResidualEquation("abs(eq-y)**0.5")
fc.set_residual_equation("abs(eq-y)**0.5")
chiv = fc.residual()
assert dot(chiv, chiv) == pytest.approx(sum(abs(2 * xobs - yobs)))

# Test configuration checks
fc1 = FitContribution("test1")
with pytest.raises(SrFitError):
fc1.setResidualEquation("chiv")
fc1.set_residual_equation("chiv")
fc1.set_profile(profile)
with pytest.raises(SrFitError):
fc1.setResidualEquation("chiv")
fc1.set_residual_equation("chiv")
fc1.set_equation("A * x")
fc1.setResidualEquation("chiv")
fc1.set_residual_equation("chiv")
assert noObserversInGlobalBuilders
return

Expand Down
Loading