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 @@ -85,7 +85,7 @@ def makeRecipe(stru1, stru2, datname):

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Vary the inner radius and thickness of the shell. Constrain the core
# diameter to twice the shell radius.
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/crystalpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def makeRecipe(ciffile, datname):

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Configure the fit variables

Expand Down
8 changes: 4 additions & 4 deletions docs/examples/crystalpdfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ def makeRecipe(

# Make the FitRecipe and add the FitContributions.
recipe = FitRecipe()
recipe.addContribution(xcontribution_ni)
recipe.addContribution(xcontribution_si)
recipe.addContribution(ncontribution_ni)
recipe.addContribution(xcontribution_sini)
recipe.add_contribution(xcontribution_ni)
recipe.add_contribution(xcontribution_si)
recipe.add_contribution(ncontribution_ni)
recipe.add_contribution(xcontribution_sini)

# Now we vary and constrain Parameters as before.
for par in phase_ni.sgpars:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/crystalpdfobjcryst.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def makeRecipe(ciffile, datname):

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Configure the fit variables

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/crystalpdftwodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def makeRecipe(ciffile, xdatname, ndatname):

# Make the FitRecipe and add the FitContributions.
recipe = FitRecipe()
recipe.addContribution(xcontribution)
recipe.addContribution(ncontribution)
recipe.add_contribution(xcontribution)
recipe.add_contribution(ncontribution)

# Now we vary and constrain Parameters as before.
recipe.addVar(xgenerator.scale, 1, "xscale")
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/crystalpdftwophase.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def makeRecipe(niciffile, siciffile, datname):

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Configure the fit variables
# Start by configuring the scale factor and resolution factors.
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/debyemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def makeRecipe():
# to fit simultaneously, the contribution from each could be added to the
# recipe.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Specify which Parameters we want to refine.

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/debyemodelII.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def makeRecipeII():
# Now create a fresh FitRecipe to work with and add to it the two
# FitContributions.
recipe = FitRecipe()
recipe.addContribution(lowT)
recipe.addContribution(highT)
recipe.add_contribution(lowT)
recipe.add_contribution(highT)

# Change the fit ranges of the Profiles embedded within the
# FitContributions. We want to fit one of the contributions at low
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/ellipsoidsas.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def makeRecipe(datname):

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Configure the fit variables
# The SASGenerator uses the parameters from the params and dispersion
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/gaussiangenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def makeRecipe():
# The FitRecipe
# Now we create the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Specify which Parameters we want to vary in the fit. This will add
# Variables to the FitRecipe that directly modify the Parameters of the
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/gaussianrecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def makeRecipe():
# Here we tell the FitRecipe to use our FitContribution. When the FitRecipe
# calculates its residual function, it will call on the FitContribution to
# do part of the work.
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Specify which Parameters we want to vary in the fit. This will add
# Variables to the FitRecipe that directly modify the Parameters of the
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/npintensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def gaussian(q, q0, width):

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Specify which parameters we want to refine.
recipe.addVar(contribution.b0, 0)
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/npintensityII.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def gaussian(q, q0, width):

# Make a FitRecipe and associate the FitContributions.
recipe = FitRecipe()
recipe.addContribution(contribution1)
recipe.addContribution(contribution2)
recipe.add_contribution(contribution1)
recipe.add_contribution(contribution2)

# Specify which Parameters we want to refine. We want to refine the
# background that we just defined in the FitContributions. We have to do
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/nppdfcrystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def makeRecipe(ciffile, grdata):
# Now make the recipe. Make sure we fit the characteristic function shape
# parameters, in this case 'psize', which is the diameter of the particle.
recipe = FitRecipe()
recipe.addContribution(pdfcontribution)
recipe.add_contribution(pdfcontribution)

phase = pdfgenerator.phase
for par in phase.sgpars:
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/nppdfobjcryst.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def makeRecipe(molecule, datname):

# Make a FitRecipe.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Specify which parameters we want to refine. We'll be using the
# MoleculeParSet within the generator, so let's get a handle to it. See the
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/nppdfsas.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def makeRecipe(ciffile, grdata, iqdata):

# Moving on
recipe = FitRecipe()
recipe.addContribution(pdfcontribution)
recipe.addContribution(sascontribution)
recipe.add_contribution(pdfcontribution)
recipe.add_contribution(sascontribution)

# PDF
phase = pdfgenerator.phase
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/simplepdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def makeRecipe(ciffile, datname):

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Configure the fit variables
phase = contribution.nickel.phase
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/simplepdftwophase.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def makeRecipe(niciffile, siciffile, datname):

# Make the FitRecipe and add the FitContribution.
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Configure the fit variables
# Start by configuring the scale factor and resolution factors.
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/threedoublepeaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def delta(t, mu):
# Here we tell the FitRecipe to use our FitContribution. When the FitRecipe
# calculates its residual function, it will call on the FitContribution to
# do part of the work.
recipe.addContribution(contribution)
recipe.add_contribution(contribution)

# Vary the amplitudes for each double peak
recipe.addVar(contribution.A1, 100)
Expand Down
23 changes: 23 additions & 0 deletions news/addcontrib-dep.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added ``add_contribution`` in replace of ``addContribution``.

**Changed:**

* <news item>

**Deprecated:**

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

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
20 changes: 19 additions & 1 deletion src/diffpy/srfit/fitbase/fitrecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@
from diffpy.srfit.fitbase.recipeorganizer import RecipeOrganizer
from diffpy.srfit.interface import _fitrecipe_interface
from diffpy.srfit.util.tagmanager import TagManager
from diffpy.utils._deprecator import build_deprecation_message, deprecated

plt.style.use(all_styles["bg-style"])

base = "diffpy.srfit.fitbase.FitRecipe"
removal_version = "4.0.0"
addcontrib_dep_msg = build_deprecation_message(
base, "addContribution", "add_contribution", removal_version
)


class FitRecipe(_fitrecipe_interface, RecipeOrganizer):
"""FitRecipe class.
Expand Down Expand Up @@ -238,7 +245,7 @@ def clearFitHooks(self):
del self.fithooks[:]
return

def addContribution(self, con, weight=1.0):
def add_contribution(self, con, weight=1.0):
"""Add a FitContribution to the FitRecipe.

Attributes
Expand All @@ -255,6 +262,17 @@ def addContribution(self, con, weight=1.0):
self._weights.append(weight)
return

@deprecated(addcontrib_dep_msg)
def addContribution(self, con, weight=1.0):
"""This function has been deprecated and will be removed in version
4.0.0.

Please use diffpy.srfit.fitbase.FitRecipe.add_contribution
instead.
"""
self.add_contribution(con, weight)
return

def setWeight(self, con, weight):
"""Set the weight of a FitContribution."""
idx = list(self._contributions.values()).index(con)
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/srfit/fitbase/simplerecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(self, name="fit", conclass=FitContribution):
contribution = conclass("contribution")
self.profile = Profile()
contribution.set_profile(self.profile)
self.addContribution(contribution)
self.add_contribution(contribution)
self.results = FitResults(self, update=False)

# Adopt all the FitContribution methods
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/srfit/interface/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __ior__(self, args):

This accepts a single argument.
"""
self.addContribution(args)
self.add_contribution(args)
return self

def __iadd__(self, args):
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def build_recipe_one_contribution():
contribution.set_profile(profile)
contribution.setEquation("A*sin(k*x + c)")
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)
recipe.addVar(contribution.A, 1)
recipe.addVar(contribution.k, 1)
recipe.addVar(contribution.c, 1)
Expand All @@ -182,8 +182,8 @@ def build_recipe_two_contributions():
contribution2.set_profile(profile2)
contribution2.setEquation("B*sin(m*x + d)")
recipe = FitRecipe()
recipe.addContribution(contribution1)
recipe.addContribution(contribution2)
recipe.add_contribution(contribution1)
recipe.add_contribution(contribution2)
recipe.addVar(contribution1.A, 1)
recipe.addVar(contribution1.k, 1)
recipe.addVar(contribution1.c, 1)
Expand Down
52 changes: 50 additions & 2 deletions tests/test_fitrecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def setUp(self):
self.fitcontribution.k.setValue(1)
self.fitcontribution.c.setValue(0)

self.recipe.addContribution(self.fitcontribution)
self.recipe.add_contribution(self.fitcontribution)
return

def testFixFree(self):
Expand Down Expand Up @@ -291,6 +291,54 @@ def testPrintFitHook(capturestdout):
return


def test_add_contribution(capturestdout):
"""Duplicated test of PrintFitHooks except addContribution method has
changed to the new add_contribution method. This is because addContribution
is deprecated.

Remove this test after addContribution is removed and update
testPrintFitHook to use add_contribution instead of addContribution.
"""
recipe = FitRecipe("recipe")
recipe.fithooks[0].verbose = 0

# Set up the Profile
profile = Profile()
x = linspace(0, pi, 10)
y = sin(x)
profile.setObservedProfile(x, y)

# Set up the FitContribution
fitcontribution = FitContribution("cont")
fitcontribution.set_profile(profile)
fitcontribution.setEquation("A*sin(k*x + c)")
fitcontribution.A.setValue(1)
fitcontribution.k.setValue(1)
fitcontribution.c.setValue(0)

recipe.add_contribution(fitcontribution)

recipe.addVar(fitcontribution.c)
recipe.restrain("c", lb=5)
(pfh,) = recipe.getFitHooks()
out = capturestdout(recipe.scalarResidual)
assert "" == out
pfh.verbose = 1
out = capturestdout(recipe.scalarResidual)
assert out.strip().isdigit()
assert "\nRestraints:" not in out
pfh.verbose = 2
out = capturestdout(recipe.scalarResidual)
assert "\nResidual:" in out
assert "\nRestraints:" in out
assert "\nVariables" not in out
pfh.verbose = 3
out = capturestdout(recipe.scalarResidual)
assert "\nVariables" in out
assert "c = " in out
return


def optimize_recipe(recipe):
recipe.fithooks[0].verbose = 0
residuals = recipe.residual
Expand Down Expand Up @@ -327,7 +375,7 @@ def build_recipe_from_datafile(datafile):
contribution.set_profile(profile)
contribution.setEquation("m*x + b")
recipe = FitRecipe()
recipe.addContribution(contribution)
recipe.add_contribution(contribution)
recipe.addVar(contribution.m, 1)
recipe.addVar(contribution.b, 0)
return recipe
Expand Down
Loading