Skip to content
2 changes: 1 addition & 1 deletion docs/examples/debyemodelII.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/gaussianrecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/nppdfobjcryst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/threedoublepeaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions news/fitrecipe2-dep.rst
Original file line number Diff line number Diff line change
@@ -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:**

* <news item>

**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:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
4 changes: 2 additions & 2 deletions src/diffpy/srfit/fitbase/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/diffpy/srfit/fitbase/fitcontribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/diffpy/srfit/fitbase/fithook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
156 changes: 125 additions & 31 deletions src/diffpy/srfit/fitbase/fitrecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
)
Expand All @@ -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."""
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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__"):
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/diffpy/srfit/fitbase/fitresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions src/diffpy/srfit/fitbase/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
Loading
Loading