Skip to content

Commit 03cf0bd

Browse files
authored
Merge pull request #159 from cadenmyers13/fitrecipe2-dep
Deprecate: deprecate remaining functions of `Fitrecipe`
2 parents 9e7a7c8 + 526034d commit 03cf0bd

16 files changed

Lines changed: 305 additions & 98 deletions

docs/examples/debyemodelII.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def plotResults(recipe):
9898

9999
# The variable values are returned in the order in which the variables were
100100
# added to the FitRecipe.
101-
lowToffset, highToffset, thetaD = recipe.getValues()
101+
lowToffset, highToffset, thetaD = recipe.get_values()
102102

103103
# We want to extend the fitting range to its full extent so we can get a
104104
# nice full plot.

docs/examples/gaussianrecipe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ def scipyOptimize(recipe):
172172
# We're going to use the least-squares (Levenberg-Marquardt) optimizer from
173173
# scipy. We simply have to give it the function to minimize
174174
# (recipe.residual) and the starting values of the Variables
175-
# (recipe.getValues()).
175+
# (recipe.get_values()).
176176
from scipy.optimize.minpack import leastsq
177177

178178
print("Fit using scipy's LM optimizer")
179-
leastsq(recipe.residual, recipe.getValues())
179+
leastsq(recipe.residual, recipe.get_values())
180180

181181
return
182182

docs/examples/nppdfobjcryst.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def main():
143143
# Optimize
144144
from scipy.optimize import leastsq
145145

146-
leastsq(recipe.residual, recipe.getValues())
146+
leastsq(recipe.residual, recipe.get_values())
147147

148148
# Print results
149149
res = FitResults(recipe)

docs/examples/threedoublepeaks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,11 @@ def scipyOptimize(recipe):
179179
# We're going to use the least-squares (Levenberg-Marquardt) optimizer from
180180
# scipy. We simply have to give it the function to minimize
181181
# (recipe.residual) and the starting values of the Variables
182-
# (recipe.getValues()).
182+
# (recipe.get_values()).
183183
from scipy.optimize.minpack import leastsq
184184

185185
print("Fit using scipy's LM optimizer")
186-
leastsq(recipe.residual, recipe.getValues())
186+
leastsq(recipe.residual, recipe.get_values())
187187

188188
return
189189

news/fitrecipe2-dep.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
**Added:**
2+
3+
* Added ``convert_bounds_to_restraints`` method to ``FitRecipe``.
4+
* Added ``get_bounds_pairs`` method to ``FitRecipe``.
5+
* Added ``get_bounds_array`` method to ``FitRecipe``.
6+
* Added ``get_names`` method to ``FitRecipe`` and ``RecipeContainer``.
7+
* Added ``get_values`` method to ``FitRecipe`` and ``RecipeContainer``.
8+
* Added ``is_free`` method to ``FitRecipe``.
9+
10+
**Changed:**
11+
12+
* <news item>
13+
14+
**Deprecated:**
15+
16+
* Deprecated ``boundsToRestraints`` method for removal in 4.0.0.
17+
* Deprecated ``getBounds`` method for removal in 4.0.0.
18+
* Deprecated ``getBounds2`` method for removal in 4.0.0.
19+
* Deprecated ``getNames`` method for removal in 4.0.0.
20+
* Deprecated ``getValues`` method for removal in 4.0.0.
21+
* Deprecated ``isFree`` method for removal in 4.0.0.
22+
23+
**Removed:**
24+
25+
* <news item>
26+
27+
**Fixed:**
28+
29+
* <news item>
30+
31+
**Security:**
32+
33+
* <news item>

src/diffpy/srfit/fitbase/calculator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ class Calculator(Operator, ParameterSet):
7777
Properties
7878
----------
7979
names
80-
Variable names (read only). See getNames.
80+
Variable names (read only). See get_names.
8181
values
82-
Variable values (read only). See getValues.
82+
Variable values (read only). See get_values.
8383
"""
8484

8585
# define abstract attributes from the Operator base.

src/diffpy/srfit/fitbase/fitcontribution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ class FitContribution(ParameterSet):
125125
Properties
126126
----------
127127
names
128-
Variable names (read only). See getNames.
128+
Variable names (read only). See get_names.
129129
values
130-
Variable values (read only). See getValues.
130+
Variable values (read only). See get_values.
131131
"""
132132

133133
def __init__(self, name):

src/diffpy/srfit/fitbase/fithook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ def postcall(self, recipe, chiv):
164164

165165
if self.verbose >= 3:
166166
print("Variables")
167-
vnames = recipe.getNames()
168-
vals = recipe.getValues()
167+
vnames = recipe.get_names()
168+
vals = recipe.get_values()
169169
# byname = _byname()
170170
items = sorted(zip(vnames, vals), key=_byname)
171171
for name, val in items:

src/diffpy/srfit/fitbase/fitrecipe.py

Lines changed: 125 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@
9999
base, "newVar", "create_new_variable", removal_version
100100
)
101101

102+
isFree_dep_msg = build_deprecation_message(
103+
base, "isFree", "is_free", removal_version
104+
)
105+
106+
getValues_dep_msg = build_deprecation_message(
107+
base, "getValues", "get_values", removal_version
108+
)
109+
110+
getNames_dep_msg = build_deprecation_message(
111+
base, "getNames", "get_names", removal_version
112+
)
113+
114+
getBounds_dep_msg = build_deprecation_message(
115+
base, "getBounds", "get_bounds_pairs", removal_version
116+
)
117+
118+
getBounds2_dep_msg = build_deprecation_message(
119+
base, "getBounds2", "get_bounds_array", removal_version
120+
)
121+
122+
boundsToRestraints_dep_msg = build_deprecation_message(
123+
base, "boundsToRestraints", "convert_bounds_to_restraints", removal_version
124+
)
125+
102126

103127
class FitRecipe(_fitrecipe_interface, RecipeOrganizer):
104128
"""FitRecipe class.
@@ -152,24 +176,24 @@ class FitRecipe(_fitrecipe_interface, RecipeOrganizer):
152176
Properties
153177
----------
154178
names
155-
Variable names (read only). See getNames.
179+
Variable names (read only). See get_names.
156180
values
157-
Variable values (read only). See getValues.
181+
Variable values (read only). See get_values.
158182
fixednames
159183
Names of the fixed refinable variables (read only).
160184
fixedvalues
161185
Values of the fixed refinable variables (read only).
162186
bounds
163-
Bounds on parameters (read only). See getBounds.
187+
Bounds on parameters (read only). See get_bounds_pairs.
164188
bounds2
165-
Bounds on parameters (read only). See getBounds2.
189+
Bounds on parameters (read only). See get_bounds_array.
166190
"""
167191

168192
fixednames = property(
169193
lambda self: [
170194
v.name
171195
for v in self._parameters.values()
172-
if not (self.isFree(v) or self.isConstrained(v))
196+
if not (self.is_free(v) or self.isConstrained(v))
173197
],
174198
doc="names of the fixed refinable variables",
175199
)
@@ -178,13 +202,13 @@ class FitRecipe(_fitrecipe_interface, RecipeOrganizer):
178202
[
179203
v.value
180204
for v in self._parameters.values()
181-
if not (self.isFree(v) or self.isConstrained(v))
205+
if not (self.is_free(v) or self.isConstrained(v))
182206
]
183207
),
184208
doc="values of the fixed refinable variables",
185209
)
186-
bounds = property(lambda self: self.getBounds())
187-
bounds2 = property(lambda self: self.getBounds2())
210+
bounds = property(lambda self: self.get_bounds_pairs())
211+
bounds2 = property(lambda self: self.get_bounds_array())
188212

189213
def __init__(self, name="fit"):
190214
"""Initialization."""
@@ -935,10 +959,19 @@ def free(self, *args, **kw):
935959

936960
return
937961

938-
def isFree(self, var):
962+
def is_free(self, var):
939963
"""Check if a variable is fixed."""
940964
return not self._tagmanager.hasTags(var, self._fixedtag)
941965

966+
@deprecated(isFree_dep_msg)
967+
def isFree(self, var):
968+
"""This function has been deprecated and will be removed in version
969+
4.0.0.
970+
971+
Please use diffpy.srfit.fitbase.FitRecipe.is_free instead.
972+
"""
973+
return self.is_free(var)
974+
942975
def unconstrain(self, *pars):
943976
"""Unconstrain a Parameter.
944977
@@ -1034,33 +1067,78 @@ def constrain(self, par, con, ns={}):
10341067
RecipeOrganizer.constrain(self, par, con, ns)
10351068
return
10361069

1037-
def getValues(self):
1070+
def get_values(self):
10381071
"""Get the current values of the variables in a list."""
10391072
return array(
1040-
[v.value for v in self._parameters.values() if self.isFree(v)]
1073+
[v.value for v in self._parameters.values() if self.is_free(v)]
10411074
)
10421075

1043-
def getNames(self):
1076+
@deprecated(getValues_dep_msg)
1077+
def getValues(self):
1078+
"""This function has been deprecated and will be removed in version
1079+
4.0.0.
1080+
1081+
Please use diffpy.srfit.fitbase.FitRecipe.get_values instead."""
1082+
return self.get_values()
1083+
1084+
def get_names(self):
10441085
"""Get the names of the variables in a list."""
1045-
return [v.name for v in self._parameters.values() if self.isFree(v)]
1086+
return [v.name for v in self._parameters.values() if self.is_free(v)]
10461087

1047-
def getBounds(self):
1088+
@deprecated(getNames_dep_msg)
1089+
def getNames(self):
1090+
"""This function has been deprecated and will be removed in version
1091+
4.0.0.
1092+
1093+
Please use diffpy.srfit.fitbase.FitRecipe.get_names instead."""
1094+
return self.get_names()
1095+
1096+
def get_bounds_pairs(self):
10481097
"""Get the bounds on variables in a list.
10491098
1050-
Returns a list of (lb, ub) pairs, where lb is the lower bound
1051-
and ub is the upper bound.
1099+
Returns
1100+
-------
1101+
bounds_pair_list : list of tuple of float
1102+
A list of ``(lower, upper)`` bounds on the variables, in the same
1103+
order as ``get_names`` and ``get_values``.
10521104
"""
1053-
return [v.bounds for v in self._parameters.values() if self.isFree(v)]
1105+
return [v.bounds for v in self._parameters.values() if self.is_free(v)]
1106+
1107+
@deprecated(getBounds_dep_msg)
1108+
def getBounds(self):
1109+
"""This function has been deprecated and will be removed in version
1110+
4.0.0.
10541111
1112+
Please use diffpy.srfit.fitbase.FitRecipe.get_bounds_pairs
1113+
instead.
1114+
"""
1115+
return self.get_bounds_pairs()
1116+
1117+
def get_bounds_array(self):
1118+
"""Get the bounds on variables in two numpy arrays.
1119+
1120+
Returns
1121+
-------
1122+
lower_bounds : numpy.ndarray
1123+
A numpy array of lower bounds on the variables, in the same order
1124+
as ``get_names`` and ``get_values``.
1125+
upper_bounds : numpy.ndarray
1126+
A numpy array of upper bounds on the variables, in the same order
1127+
as ``get_names`` and ``get_values``.
1128+
"""
1129+
bounds = self.get_bounds_pairs()
1130+
lower_bounds = array([b[0] for b in bounds])
1131+
upper_bounds = array([b[1] for b in bounds])
1132+
return lower_bounds, upper_bounds
1133+
1134+
@deprecated(getBounds2_dep_msg)
10551135
def getBounds2(self):
1056-
"""Get the bounds on variables in two lists.
1136+
"""This function has been deprecated and will be removed in version
1137+
4.0.0.
10571138
1058-
Returns lower- and upper-bound lists of variable bounds.
1139+
Please use diffpy.srfit.fitbase.FitRecipe.get_bounds_array instead.
10591140
"""
1060-
bounds = self.getBounds()
1061-
lb = array([b[0] for b in bounds])
1062-
ub = array([b[1] for b in bounds])
1063-
return lb, ub
1141+
return self.get_bounds_array()
10641142

10651143
def set_plot_defaults(self, **kwargs):
10661144
"""Set default plotting options for all future plots.
@@ -1346,18 +1424,23 @@ def plot_recipe(self, ax=None, return_fig=False, **kwargs):
13461424
else:
13471425
return figures, axes_list
13481426

1349-
def boundsToRestraints(self, sig=1, scaled=False):
1427+
def convert_bounds_to_restraints(self, sig=1, scaled=False):
13501428
"""Turn all bounded parameters into restraints.
13511429
13521430
The bounds become limits on the restraint.
13531431
1354-
Attributes
1432+
Parameters
13551433
----------
1356-
sig
1357-
The uncertainty on the bounds (scalar or iterable,
1358-
default 1).
1359-
scaled
1360-
Scale the restraints, see restrain.
1434+
sig : float or iterable of float, optional
1435+
The number of standard deviations associated with each bound.
1436+
Smaller values produce stronger restraints. If a scalar is given,
1437+
the same value is applied to all parameters. If an iterable is
1438+
provided, it must match the number of parameters. Default is 1.
1439+
1440+
scaled : bool, optional
1441+
If True, scale each restraint by the magnitude of the corresponding
1442+
parameter, consistent with the behavior of :meth:`restrain`.
1443+
Default is False.
13611444
"""
13621445
pars = self._parameters.values()
13631446
if not hasattr(sig, "__iter__"):
@@ -1368,11 +1451,22 @@ def boundsToRestraints(self, sig=1, scaled=False):
13681451
)
13691452
return
13701453

1454+
@deprecated(boundsToRestraints_dep_msg)
1455+
def boundsToRestraints(self, sig=1, scaled=False):
1456+
"""This function has been deprecated and will be removed in version
1457+
4.0.0.
1458+
1459+
Please use diffpy.srfit.fitbase.FitRecipe.convert_bounds_to_restraints
1460+
instead.
1461+
"""
1462+
self.convert_bounds_to_restraints(sig, scaled)
1463+
return
1464+
13711465
def _apply_values(self, p):
13721466
"""Apply variable values to the variables."""
13731467
if len(p) == 0:
13741468
return
1375-
vargen = (v for v in self._parameters.values() if self.isFree(v))
1469+
vargen = (v for v in self._parameters.values() if self.is_free(v))
13761470
for var, pval in zip(vargen, p):
13771471
var.setValue(pval)
13781472
return

src/diffpy/srfit/fitbase/fitresults.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ def update(self):
152152
recipe._prepare()
153153

154154
# Store the variable names and values
155-
self.varnames = recipe.getNames()
156-
self.varvals = recipe.getValues()
155+
self.varnames = recipe.get_names()
156+
self.varvals = recipe.get_values()
157157
fixedpars = recipe._tagmanager.union(recipe._fixedtag)
158158
fixedpars = [p for p in fixedpars if not p.constrained]
159159
self.fixednames = [p.name for p in fixedpars]

0 commit comments

Comments
 (0)