diff --git a/docs/examples/debyemodelII.py b/docs/examples/debyemodelII.py index 631c4143..a7c896cb 100644 --- a/docs/examples/debyemodelII.py +++ b/docs/examples/debyemodelII.py @@ -98,7 +98,7 @@ def plotResults(recipe): # The variable values are returned in the order in which the variables were # added to the FitRecipe. - lowToffset, highToffset, thetaD = recipe.getValues() + lowToffset, highToffset, thetaD = recipe.get_values() # We want to extend the fitting range to its full extent so we can get a # nice full plot. diff --git a/docs/examples/gaussianrecipe.py b/docs/examples/gaussianrecipe.py index 7bf85355..fde45cc5 100644 --- a/docs/examples/gaussianrecipe.py +++ b/docs/examples/gaussianrecipe.py @@ -172,11 +172,11 @@ def scipyOptimize(recipe): # We're going to use the least-squares (Levenberg-Marquardt) optimizer from # scipy. We simply have to give it the function to minimize # (recipe.residual) and the starting values of the Variables - # (recipe.getValues()). + # (recipe.get_values()). from scipy.optimize.minpack import leastsq print("Fit using scipy's LM optimizer") - leastsq(recipe.residual, recipe.getValues()) + leastsq(recipe.residual, recipe.get_values()) return diff --git a/docs/examples/nppdfobjcryst.py b/docs/examples/nppdfobjcryst.py index 50dd9f79..70426101 100644 --- a/docs/examples/nppdfobjcryst.py +++ b/docs/examples/nppdfobjcryst.py @@ -143,7 +143,7 @@ def main(): # Optimize from scipy.optimize import leastsq - leastsq(recipe.residual, recipe.getValues()) + leastsq(recipe.residual, recipe.get_values()) # Print results res = FitResults(recipe) diff --git a/docs/examples/threedoublepeaks.py b/docs/examples/threedoublepeaks.py index 99eb4e92..8ad75304 100644 --- a/docs/examples/threedoublepeaks.py +++ b/docs/examples/threedoublepeaks.py @@ -179,11 +179,11 @@ def scipyOptimize(recipe): # We're going to use the least-squares (Levenberg-Marquardt) optimizer from # scipy. We simply have to give it the function to minimize # (recipe.residual) and the starting values of the Variables - # (recipe.getValues()). + # (recipe.get_values()). from scipy.optimize.minpack import leastsq print("Fit using scipy's LM optimizer") - leastsq(recipe.residual, recipe.getValues()) + leastsq(recipe.residual, recipe.get_values()) return diff --git a/news/fitrecipe2-dep.rst b/news/fitrecipe2-dep.rst new file mode 100644 index 00000000..0b7d9e3a --- /dev/null +++ b/news/fitrecipe2-dep.rst @@ -0,0 +1,33 @@ +**Added:** + +* Added ``convert_bounds_to_restraints`` method to ``FitRecipe``. +* Added ``get_bounds_pairs`` method to ``FitRecipe``. +* Added ``get_bounds_array`` method to ``FitRecipe``. +* Added ``get_names`` method to ``FitRecipe`` and ``RecipeContainer``. +* Added ``get_values`` method to ``FitRecipe`` and ``RecipeContainer``. +* Added ``is_free`` method to ``FitRecipe``. + +**Changed:** + +* + +**Deprecated:** + +* Deprecated ``boundsToRestraints`` method for removal in 4.0.0. +* Deprecated ``getBounds`` method for removal in 4.0.0. +* Deprecated ``getBounds2`` method for removal in 4.0.0. +* Deprecated ``getNames`` method for removal in 4.0.0. +* Deprecated ``getValues`` method for removal in 4.0.0. +* Deprecated ``isFree`` method for removal in 4.0.0. + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/srfit/fitbase/calculator.py b/src/diffpy/srfit/fitbase/calculator.py index 75083b4e..22e96540 100644 --- a/src/diffpy/srfit/fitbase/calculator.py +++ b/src/diffpy/srfit/fitbase/calculator.py @@ -77,9 +77,9 @@ class Calculator(Operator, ParameterSet): Properties ---------- names - Variable names (read only). See getNames. + Variable names (read only). See get_names. values - Variable values (read only). See getValues. + Variable values (read only). See get_values. """ # define abstract attributes from the Operator base. diff --git a/src/diffpy/srfit/fitbase/fitcontribution.py b/src/diffpy/srfit/fitbase/fitcontribution.py index c9e3df9c..d6ff6ed0 100644 --- a/src/diffpy/srfit/fitbase/fitcontribution.py +++ b/src/diffpy/srfit/fitbase/fitcontribution.py @@ -125,9 +125,9 @@ class FitContribution(ParameterSet): Properties ---------- names - Variable names (read only). See getNames. + Variable names (read only). See get_names. values - Variable values (read only). See getValues. + Variable values (read only). See get_values. """ def __init__(self, name): diff --git a/src/diffpy/srfit/fitbase/fithook.py b/src/diffpy/srfit/fitbase/fithook.py index be8cdf7c..b5b284c5 100644 --- a/src/diffpy/srfit/fitbase/fithook.py +++ b/src/diffpy/srfit/fitbase/fithook.py @@ -164,8 +164,8 @@ def postcall(self, recipe, chiv): if self.verbose >= 3: print("Variables") - vnames = recipe.getNames() - vals = recipe.getValues() + vnames = recipe.get_names() + vals = recipe.get_values() # byname = _byname() items = sorted(zip(vnames, vals), key=_byname) for name, val in items: diff --git a/src/diffpy/srfit/fitbase/fitrecipe.py b/src/diffpy/srfit/fitbase/fitrecipe.py index 5c9ef5f1..c28acc3e 100644 --- a/src/diffpy/srfit/fitbase/fitrecipe.py +++ b/src/diffpy/srfit/fitbase/fitrecipe.py @@ -99,6 +99,30 @@ base, "newVar", "create_new_variable", removal_version ) +isFree_dep_msg = build_deprecation_message( + base, "isFree", "is_free", removal_version +) + +getValues_dep_msg = build_deprecation_message( + base, "getValues", "get_values", removal_version +) + +getNames_dep_msg = build_deprecation_message( + base, "getNames", "get_names", removal_version +) + +getBounds_dep_msg = build_deprecation_message( + base, "getBounds", "get_bounds_pairs", removal_version +) + +getBounds2_dep_msg = build_deprecation_message( + base, "getBounds2", "get_bounds_array", removal_version +) + +boundsToRestraints_dep_msg = build_deprecation_message( + base, "boundsToRestraints", "convert_bounds_to_restraints", removal_version +) + class FitRecipe(_fitrecipe_interface, RecipeOrganizer): """FitRecipe class. @@ -152,24 +176,24 @@ class FitRecipe(_fitrecipe_interface, RecipeOrganizer): Properties ---------- names - Variable names (read only). See getNames. + Variable names (read only). See get_names. values - Variable values (read only). See getValues. + Variable values (read only). See get_values. fixednames Names of the fixed refinable variables (read only). fixedvalues Values of the fixed refinable variables (read only). bounds - Bounds on parameters (read only). See getBounds. + Bounds on parameters (read only). See get_bounds_pairs. bounds2 - Bounds on parameters (read only). See getBounds2. + Bounds on parameters (read only). See get_bounds_array. """ fixednames = property( lambda self: [ v.name for v in self._parameters.values() - if not (self.isFree(v) or self.isConstrained(v)) + if not (self.is_free(v) or self.isConstrained(v)) ], doc="names of the fixed refinable variables", ) @@ -178,13 +202,13 @@ class FitRecipe(_fitrecipe_interface, RecipeOrganizer): [ v.value for v in self._parameters.values() - if not (self.isFree(v) or self.isConstrained(v)) + if not (self.is_free(v) or self.isConstrained(v)) ] ), doc="values of the fixed refinable variables", ) - bounds = property(lambda self: self.getBounds()) - bounds2 = property(lambda self: self.getBounds2()) + bounds = property(lambda self: self.get_bounds_pairs()) + bounds2 = property(lambda self: self.get_bounds_array()) def __init__(self, name="fit"): """Initialization.""" @@ -935,10 +959,19 @@ def free(self, *args, **kw): return - def isFree(self, var): + def is_free(self, var): """Check if a variable is fixed.""" return not self._tagmanager.hasTags(var, self._fixedtag) + @deprecated(isFree_dep_msg) + def isFree(self, var): + """This function has been deprecated and will be removed in version + 4.0.0. + + Please use diffpy.srfit.fitbase.FitRecipe.is_free instead. + """ + return self.is_free(var) + def unconstrain(self, *pars): """Unconstrain a Parameter. @@ -1034,33 +1067,78 @@ def constrain(self, par, con, ns={}): RecipeOrganizer.constrain(self, par, con, ns) return - def getValues(self): + def get_values(self): """Get the current values of the variables in a list.""" return array( - [v.value for v in self._parameters.values() if self.isFree(v)] + [v.value for v in self._parameters.values() if self.is_free(v)] ) - def getNames(self): + @deprecated(getValues_dep_msg) + def getValues(self): + """This function has been deprecated and will be removed in version + 4.0.0. + + Please use diffpy.srfit.fitbase.FitRecipe.get_values instead.""" + return self.get_values() + + def get_names(self): """Get the names of the variables in a list.""" - return [v.name for v in self._parameters.values() if self.isFree(v)] + return [v.name for v in self._parameters.values() if self.is_free(v)] - def getBounds(self): + @deprecated(getNames_dep_msg) + def getNames(self): + """This function has been deprecated and will be removed in version + 4.0.0. + + Please use diffpy.srfit.fitbase.FitRecipe.get_names instead.""" + return self.get_names() + + def get_bounds_pairs(self): """Get the bounds on variables in a list. - Returns a list of (lb, ub) pairs, where lb is the lower bound - and ub is the upper bound. + Returns + ------- + bounds_pair_list : list of tuple of float + A list of ``(lower, upper)`` bounds on the variables, in the same + order as ``get_names`` and ``get_values``. """ - return [v.bounds for v in self._parameters.values() if self.isFree(v)] + return [v.bounds for v in self._parameters.values() if self.is_free(v)] + + @deprecated(getBounds_dep_msg) + def getBounds(self): + """This function has been deprecated and will be removed in version + 4.0.0. + Please use diffpy.srfit.fitbase.FitRecipe.get_bounds_pairs + instead. + """ + return self.get_bounds_pairs() + + def get_bounds_array(self): + """Get the bounds on variables in two numpy arrays. + + Returns + ------- + lower_bounds : numpy.ndarray + A numpy array of lower bounds on the variables, in the same order + as ``get_names`` and ``get_values``. + upper_bounds : numpy.ndarray + A numpy array of upper bounds on the variables, in the same order + as ``get_names`` and ``get_values``. + """ + bounds = self.get_bounds_pairs() + lower_bounds = array([b[0] for b in bounds]) + upper_bounds = array([b[1] for b in bounds]) + return lower_bounds, upper_bounds + + @deprecated(getBounds2_dep_msg) def getBounds2(self): - """Get the bounds on variables in two lists. + """This function has been deprecated and will be removed in version + 4.0.0. - Returns lower- and upper-bound lists of variable bounds. + Please use diffpy.srfit.fitbase.FitRecipe.get_bounds_array instead. """ - bounds = self.getBounds() - lb = array([b[0] for b in bounds]) - ub = array([b[1] for b in bounds]) - return lb, ub + return self.get_bounds_array() def set_plot_defaults(self, **kwargs): """Set default plotting options for all future plots. @@ -1346,18 +1424,23 @@ def plot_recipe(self, ax=None, return_fig=False, **kwargs): else: return figures, axes_list - def boundsToRestraints(self, sig=1, scaled=False): + def convert_bounds_to_restraints(self, sig=1, scaled=False): """Turn all bounded parameters into restraints. The bounds become limits on the restraint. - Attributes + Parameters ---------- - sig - The uncertainty on the bounds (scalar or iterable, - default 1). - scaled - Scale the restraints, see restrain. + sig : float or iterable of float, optional + The number of standard deviations associated with each bound. + Smaller values produce stronger restraints. If a scalar is given, + the same value is applied to all parameters. If an iterable is + provided, it must match the number of parameters. Default is 1. + + scaled : bool, optional + If True, scale each restraint by the magnitude of the corresponding + parameter, consistent with the behavior of :meth:`restrain`. + Default is False. """ pars = self._parameters.values() if not hasattr(sig, "__iter__"): @@ -1368,11 +1451,22 @@ def boundsToRestraints(self, sig=1, scaled=False): ) return + @deprecated(boundsToRestraints_dep_msg) + def boundsToRestraints(self, sig=1, scaled=False): + """This function has been deprecated and will be removed in version + 4.0.0. + + Please use diffpy.srfit.fitbase.FitRecipe.convert_bounds_to_restraints + instead. + """ + self.convert_bounds_to_restraints(sig, scaled) + return + def _apply_values(self, p): """Apply variable values to the variables.""" if len(p) == 0: return - vargen = (v for v in self._parameters.values() if self.isFree(v)) + vargen = (v for v in self._parameters.values() if self.is_free(v)) for var, pval in zip(vargen, p): var.setValue(pval) return diff --git a/src/diffpy/srfit/fitbase/fitresults.py b/src/diffpy/srfit/fitbase/fitresults.py index 1949526a..c79c2a17 100644 --- a/src/diffpy/srfit/fitbase/fitresults.py +++ b/src/diffpy/srfit/fitbase/fitresults.py @@ -152,8 +152,8 @@ def update(self): recipe._prepare() # Store the variable names and values - self.varnames = recipe.getNames() - self.varvals = recipe.getValues() + self.varnames = recipe.get_names() + self.varvals = recipe.get_values() fixedpars = recipe._tagmanager.union(recipe._fixedtag) fixedpars = [p for p in fixedpars if not p.constrained] self.fixednames = [p.name for p in fixedpars] diff --git a/src/diffpy/srfit/fitbase/parameter.py b/src/diffpy/srfit/fitbase/parameter.py index 90691e55..d7887f7d 100644 --- a/src/diffpy/srfit/fitbase/parameter.py +++ b/src/diffpy/srfit/fitbase/parameter.py @@ -55,7 +55,7 @@ class Parameter(_parameter_interface, Argument, Validatable): bounds A 2-list defining the bounds on the Parameter. This can be used by some optimizers when the Parameter is varied. See - FitRecipe.getBounds and FitRecipe.boundsToRestraints. + FitRecipe.get_bounds_pairs and FitRecipe.convert_bounds_to_restraints. """ def __init__(self, name, value=None, const=False): @@ -238,8 +238,8 @@ def bounds(self): """List of lower and upper bounds of the proxied Parameter. This can be used by some optimizers when the Parameter is - varied. See FitRecipe.getBounds and - FitRecipe.boundsToRestraints. + varied. See FitRecipe.get_bounds_pairs and + FitRecipe.convert_bounds_to_restraints. """ return self.par.bounds diff --git a/src/diffpy/srfit/fitbase/parameterset.py b/src/diffpy/srfit/fitbase/parameterset.py index 6d200ca9..e5e51e39 100644 --- a/src/diffpy/srfit/fitbase/parameterset.py +++ b/src/diffpy/srfit/fitbase/parameterset.py @@ -75,9 +75,9 @@ class ParameterSet(RecipeOrganizer): Properties ---------- names - Variable names (read only). See getNames. + Variable names (read only). See get_names. values - Variable values (read only). See getValues. + Variable values (read only). See get_values. """ def __init__(self, name): diff --git a/src/diffpy/srfit/fitbase/profilegenerator.py b/src/diffpy/srfit/fitbase/profilegenerator.py index c6adabbc..c45e93aa 100644 --- a/src/diffpy/srfit/fitbase/profilegenerator.py +++ b/src/diffpy/srfit/fitbase/profilegenerator.py @@ -110,9 +110,9 @@ class ProfileGenerator(Operator, ParameterSet): Properties ---------- names - Variable names (read only). See getNames. + Variable names (read only). See get_names. values - Variable values (read only). See getValues. + Variable values (read only). See get_values. """ # define abstract attributes from the Operator base. diff --git a/src/diffpy/srfit/fitbase/recipeorganizer.py b/src/diffpy/srfit/fitbase/recipeorganizer.py index 3b95a216..ca7b273b 100644 --- a/src/diffpy/srfit/fitbase/recipeorganizer.py +++ b/src/diffpy/srfit/fitbase/recipeorganizer.py @@ -42,6 +42,24 @@ from diffpy.srfit.util import sortKeyForNumericString as numstr from diffpy.srfit.util.nameutils import validateName from diffpy.srfit.util.observable import Observable +from diffpy.utils._deprecator import build_deprecation_message, deprecated + +recipecontainer_base = "diffpy.srfit.fitbase.recipeorganizer.RecipeContainer" +removal_version = "4.0.0" + +getValues_deprecation_msg = build_deprecation_message( + recipecontainer_base, + "getValues", + "get_values", + removal_version, +) + +getNames_deprecation_msg = build_deprecation_message( + recipecontainer_base, + "getNames", + "get_names", + removal_version, +) class RecipeContainer(Observable, Configurable, Validatable): @@ -83,13 +101,13 @@ class RecipeContainer(Observable, Configurable, Validatable): Properties ---------- names - Variable names (read only). See getNames. + Variable names (read only). See get_names. values - Variable values (read only). See getValues. + Variable values (read only). See get_values. """ - names = property(lambda self: self.getNames()) - values = property(lambda self: self.getValues()) + names = property(lambda self: self.get_names()) + values = property(lambda self: self.get_values()) def __init__(self, name): Observable.__init__(self) @@ -229,14 +247,36 @@ def get(self, name, default=None): return default - def getNames(self): + def get_names(self): """Get the names of managed parameters.""" return [p.name for p in self._parameters.values()] - def getValues(self): + @deprecated(getNames_deprecation_msg) + def getNames(self): + """This function has been deprecated and will be removed in version + 4.0.0. + + Please use + diffpy.srfit.fitbase.recipeorganizer.RecipeContainer.get_names + instead. + """ + return self.get_names() + + def get_values(self): """Get the values of managed parameters.""" return [p.value for p in self._parameters.values()] + @deprecated(getValues_deprecation_msg) + def getValues(self): + """This function has been deprecated and will be removed in version + 4.0.0. + + Please use + diffpy.srfit.fitbase.recipeorganizer.RecipeContainer.get_values + instead. + """ + return self.get_values() + def _add_object(self, obj, d, check=True): """Add an object to a managed dictionary. @@ -400,9 +440,9 @@ class RecipeOrganizer(_recipeorganizer_interface, RecipeContainer): Properties ---------- names - Variable names (read only). See getNames. + Variable names (read only). See get_names. values - Variable values (read only). See getValues. + Variable values (read only). See get_values. Raises ValueError if the name is not a valid attribute identifier diff --git a/src/diffpy/srfit/fitbase/simplerecipe.py b/src/diffpy/srfit/fitbase/simplerecipe.py index 36391e86..048520af 100644 --- a/src/diffpy/srfit/fitbase/simplerecipe.py +++ b/src/diffpy/srfit/fitbase/simplerecipe.py @@ -121,9 +121,9 @@ class SimpleRecipe(FitRecipe): Properties ---------- names - Variable names (read only). See getNames. + Variable names (read only). See get_names. values - Variable values (read only). See getValues. + Variable values (read only). See get_values. """ def __init__(self, name="fit", conclass=FitContribution): diff --git a/tests/test_fitrecipe.py b/tests/test_fitrecipe.py index 0391c991..48e0bf15 100644 --- a/tests/test_fitrecipe.py +++ b/tests/test_fitrecipe.py @@ -54,7 +54,7 @@ def setUp(self): self.recipe.add_contribution(self.fitcontribution) return - def testFixFree(self): + def test_fix_free(self): recipe = self.recipe con = self.fitcontribution @@ -63,36 +63,36 @@ def testFixFree(self): recipe.add_variable(con.c, 0) recipe.create_new_variable("B", 0) - self.assertTrue(recipe.isFree(recipe.A)) + self.assertTrue(recipe.is_free(recipe.A)) recipe.fix("tagA") - self.assertFalse(recipe.isFree(recipe.A)) + self.assertFalse(recipe.is_free(recipe.A)) recipe.free("tagA") - self.assertTrue(recipe.isFree(recipe.A)) + self.assertTrue(recipe.is_free(recipe.A)) recipe.fix("A") - self.assertFalse(recipe.isFree(recipe.A)) + self.assertFalse(recipe.is_free(recipe.A)) recipe.free("A") - self.assertTrue(recipe.isFree(recipe.A)) + self.assertTrue(recipe.is_free(recipe.A)) recipe.fix(recipe.A) - self.assertFalse(recipe.isFree(recipe.A)) + self.assertFalse(recipe.is_free(recipe.A)) recipe.free(recipe.A) - self.assertTrue(recipe.isFree(recipe.A)) + self.assertTrue(recipe.is_free(recipe.A)) recipe.fix(recipe.A) - self.assertFalse(recipe.isFree(recipe.A)) + self.assertFalse(recipe.is_free(recipe.A)) recipe.free("all") - self.assertTrue(recipe.isFree(recipe.A)) - self.assertTrue(recipe.isFree(recipe.k)) - self.assertTrue(recipe.isFree(recipe.c)) - self.assertTrue(recipe.isFree(recipe.B)) + self.assertTrue(recipe.is_free(recipe.A)) + self.assertTrue(recipe.is_free(recipe.k)) + self.assertTrue(recipe.is_free(recipe.c)) + self.assertTrue(recipe.is_free(recipe.B)) 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)) - self.assertTrue(recipe.isFree(recipe.B)) + self.assertFalse(recipe.is_free(recipe.A)) + self.assertFalse(recipe.is_free(recipe.k)) + self.assertFalse(recipe.is_free(recipe.c)) + self.assertTrue(recipe.is_free(recipe.B)) self.assertEqual(3, recipe.c.value) recipe.fix("all") - self.assertFalse(recipe.isFree(recipe.A)) - self.assertFalse(recipe.isFree(recipe.k)) - self.assertFalse(recipe.isFree(recipe.c)) + self.assertFalse(recipe.is_free(recipe.A)) + self.assertFalse(recipe.is_free(recipe.k)) + self.assertFalse(recipe.is_free(recipe.c)) self.assertFalse(recipe.isFree(recipe.B)) self.assertRaises(ValueError, recipe.free, "junk") @@ -110,38 +110,38 @@ def test_variables(self): recipe.add_variable(con.c, 0) recipe.create_new_variable("B", 0) - names = recipe.getNames() + names = recipe.get_names() self.assertEqual(names, ["A", "k", "c", "B"]) - values = recipe.getValues() + values = recipe.get_values() self.assertTrue((values == [2, 1, 0, 0]).all()) # Constrain a parameter to the B-variable to give it a value p = Parameter("Bpar", -1) recipe.constrain(recipe.B, p) - values = recipe.getValues() + values = recipe.get_values() self.assertTrue((values == [2, 1, 0]).all()) recipe.delete_variable(recipe.B) recipe.fix(recipe.k) - names = recipe.getNames() + names = recipe.get_names() self.assertEqual(names, ["A", "c"]) - values = recipe.getValues() + values = recipe.get_values() self.assertTrue((values == [2, 0]).all()) recipe.fix("all") - names = recipe.getNames() + names = recipe.get_names() self.assertEqual(names, []) - values = recipe.getValues() + values = recipe.get_values() self.assertTrue((values == []).all()) recipe.free("all") - names = recipe.getNames() + names = recipe.get_names() self.assertEqual(3, len(names)) self.assertTrue("A" in names) self.assertTrue("k" in names) self.assertTrue("c" in names) - values = recipe.getValues() + values = recipe.get_values() self.assertEqual(3, len(values)) self.assertTrue(0 in values) self.assertTrue(1 in values) @@ -158,29 +158,29 @@ def testVars(self): recipe.addVar(con.c, 0) recipe.newVar("B", 0) - names = recipe.getNames() + names = recipe.get_names() self.assertEqual(names, ["A", "k", "c", "B"]) - values = recipe.getValues() + values = recipe.get_values() self.assertTrue((values == [2, 1, 0, 0]).all()) # Constrain a parameter to the B-variable to give it a value p = Parameter("Bpar", -1) recipe.constrain(recipe.B, p) - values = recipe.getValues() + values = recipe.get_values() self.assertTrue((values == [2, 1, 0]).all()) recipe.delVar(recipe.B) recipe.fix(recipe.k) - names = recipe.getNames() + names = recipe.get_names() self.assertEqual(names, ["A", "c"]) - values = recipe.getValues() + values = recipe.get_values() self.assertTrue((values == [2, 0]).all()) recipe.fix("all") - names = recipe.getNames() + names = recipe.get_names() self.assertEqual(names, []) - values = recipe.getValues() + values = recipe.get_values() self.assertTrue((values == []).all()) recipe.free("all") @@ -297,6 +297,46 @@ def testResidual(self): # ---------------------------------------------------------------------------- +def test_boundsToRestraints(): + recipe = FitRecipe("recipe") + + # create a bounded variable + recipe.create_new_variable("var1", 1) + expected_lower_bound = -1 + expected_upper_bound = 1 + expected_sigma = 2 + recipe.var1.bounds = (expected_lower_bound, expected_upper_bound) + + # apply restraints from bounds + recipe.boundsToRestraints(sig=expected_sigma, scaled=True) + restraints = list(recipe._restraints) + assert len(restraints) == 1 + r = restraints[0] + actual_lower_bound = r.lb + actual_upper_bound = r.ub + actual_sigma = r.sig + assert actual_lower_bound == expected_lower_bound + assert actual_upper_bound == expected_upper_bound + assert actual_sigma == expected_sigma + assert r.scaled is True + + +def test_convert_bounds_to_restraints(): + recipe = FitRecipe("recipe") + # create a bounded variable + recipe.create_new_variable("var1", 1) + recipe.var1.bounds = (-1, 1) + # apply restraints from bounds + recipe.convert_bounds_to_restraints(sig=2, scaled=True) + restraints = list(recipe._restraints) + assert len(restraints) == 1 + r = restraints[0] + assert r.lb == -1 + assert r.ub == 1 + assert r.sig == 2 + assert r.scaled is True + + def testPrintFitHook(capturestdout): "check output from default PrintFitHook." recipe = FitRecipe("recipe")