diff --git a/docs/examples/coreshellnp.py b/docs/examples/coreshellnp.py index 5b8c6eb2..a66b1b62 100644 --- a/docs/examples/coreshellnp.py +++ b/docs/examples/coreshellnp.py @@ -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. diff --git a/docs/examples/crystalpdf.py b/docs/examples/crystalpdf.py index 835eb977..36f33ad4 100644 --- a/docs/examples/crystalpdf.py +++ b/docs/examples/crystalpdf.py @@ -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 diff --git a/docs/examples/crystalpdfall.py b/docs/examples/crystalpdfall.py index 8926952a..d43e98c1 100644 --- a/docs/examples/crystalpdfall.py +++ b/docs/examples/crystalpdfall.py @@ -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: diff --git a/docs/examples/crystalpdfobjcryst.py b/docs/examples/crystalpdfobjcryst.py index d28af6a3..e04f26f0 100644 --- a/docs/examples/crystalpdfobjcryst.py +++ b/docs/examples/crystalpdfobjcryst.py @@ -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 diff --git a/docs/examples/crystalpdftwodata.py b/docs/examples/crystalpdftwodata.py index b13da27f..c6080232 100644 --- a/docs/examples/crystalpdftwodata.py +++ b/docs/examples/crystalpdftwodata.py @@ -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") diff --git a/docs/examples/crystalpdftwophase.py b/docs/examples/crystalpdftwophase.py index 95e659b7..c2acfffc 100644 --- a/docs/examples/crystalpdftwophase.py +++ b/docs/examples/crystalpdftwophase.py @@ -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. diff --git a/docs/examples/debyemodel.py b/docs/examples/debyemodel.py index 6abbb04c..d9009127 100644 --- a/docs/examples/debyemodel.py +++ b/docs/examples/debyemodel.py @@ -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. diff --git a/docs/examples/debyemodelII.py b/docs/examples/debyemodelII.py index 9872f71d..bc50fe54 100644 --- a/docs/examples/debyemodelII.py +++ b/docs/examples/debyemodelII.py @@ -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 diff --git a/docs/examples/ellipsoidsas.py b/docs/examples/ellipsoidsas.py index 5c0826ec..1777da3f 100644 --- a/docs/examples/ellipsoidsas.py +++ b/docs/examples/ellipsoidsas.py @@ -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 diff --git a/docs/examples/gaussiangenerator.py b/docs/examples/gaussiangenerator.py index f806e4a2..718116e7 100644 --- a/docs/examples/gaussiangenerator.py +++ b/docs/examples/gaussiangenerator.py @@ -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 diff --git a/docs/examples/gaussianrecipe.py b/docs/examples/gaussianrecipe.py index 4641f9ec..c203052f 100644 --- a/docs/examples/gaussianrecipe.py +++ b/docs/examples/gaussianrecipe.py @@ -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 diff --git a/docs/examples/npintensity.py b/docs/examples/npintensity.py index c193922e..c0a05096 100644 --- a/docs/examples/npintensity.py +++ b/docs/examples/npintensity.py @@ -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) diff --git a/docs/examples/npintensityII.py b/docs/examples/npintensityII.py index 552eba96..eb2e5179 100644 --- a/docs/examples/npintensityII.py +++ b/docs/examples/npintensityII.py @@ -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 diff --git a/docs/examples/nppdfcrystal.py b/docs/examples/nppdfcrystal.py index 49ed32e6..e55bc828 100644 --- a/docs/examples/nppdfcrystal.py +++ b/docs/examples/nppdfcrystal.py @@ -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: diff --git a/docs/examples/nppdfobjcryst.py b/docs/examples/nppdfobjcryst.py index 70c0bf83..038a8372 100644 --- a/docs/examples/nppdfobjcryst.py +++ b/docs/examples/nppdfobjcryst.py @@ -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 diff --git a/docs/examples/nppdfsas.py b/docs/examples/nppdfsas.py index 665e2377..54dea79b 100644 --- a/docs/examples/nppdfsas.py +++ b/docs/examples/nppdfsas.py @@ -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 diff --git a/docs/examples/simplepdf.py b/docs/examples/simplepdf.py index 027a6d76..4574a5d7 100644 --- a/docs/examples/simplepdf.py +++ b/docs/examples/simplepdf.py @@ -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 diff --git a/docs/examples/simplepdftwophase.py b/docs/examples/simplepdftwophase.py index a8017c23..e00ae302 100644 --- a/docs/examples/simplepdftwophase.py +++ b/docs/examples/simplepdftwophase.py @@ -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. diff --git a/docs/examples/threedoublepeaks.py b/docs/examples/threedoublepeaks.py index 60f16f64..e336fff1 100644 --- a/docs/examples/threedoublepeaks.py +++ b/docs/examples/threedoublepeaks.py @@ -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) diff --git a/news/addcontrib-dep.rst b/news/addcontrib-dep.rst new file mode 100644 index 00000000..8a8cf420 --- /dev/null +++ b/news/addcontrib-dep.rst @@ -0,0 +1,23 @@ +**Added:** + +* Added ``add_contribution`` in replace of ``addContribution``. + +**Changed:** + +* + +**Deprecated:** + +* Deprecated ``addContribution`` for removal in 4.0.0. + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/srfit/fitbase/fitrecipe.py b/src/diffpy/srfit/fitbase/fitrecipe.py index 4d7be2c9..4568f7a7 100644 --- a/src/diffpy/srfit/fitbase/fitrecipe.py +++ b/src/diffpy/srfit/fitbase/fitrecipe.py @@ -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. @@ -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 @@ -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) diff --git a/src/diffpy/srfit/fitbase/simplerecipe.py b/src/diffpy/srfit/fitbase/simplerecipe.py index 81a0094a..2dfbe167 100644 --- a/src/diffpy/srfit/fitbase/simplerecipe.py +++ b/src/diffpy/srfit/fitbase/simplerecipe.py @@ -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 diff --git a/src/diffpy/srfit/interface/interface.py b/src/diffpy/srfit/interface/interface.py index 63ccb573..f2b3ea17 100644 --- a/src/diffpy/srfit/interface/interface.py +++ b/src/diffpy/srfit/interface/interface.py @@ -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): diff --git a/tests/conftest.py b/tests/conftest.py index f251746f..4f03b166 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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) @@ -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) diff --git a/tests/test_fitrecipe.py b/tests/test_fitrecipe.py index 53fe7d72..0f8761ac 100644 --- a/tests/test_fitrecipe.py +++ b/tests/test_fitrecipe.py @@ -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): @@ -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 @@ -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