Skip to content

Commit 1f301a3

Browse files
committed
restrain deprecation
1 parent 9993779 commit 1f301a3

8 files changed

Lines changed: 50 additions & 26 deletions

File tree

docs/examples/crystalpdftwophase.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,18 @@ def makeRecipe(niciffile, siciffile, datname):
123123
# derived has no uncertainty. Thus, we will tell the recipe to scale the
124124
# residual, which means that it will be weighted as much as the average
125125
# data point during the fit.
126-
recipe.restrain("a_ni", lb=3.527, ub=3.527, scaled=True)
126+
recipe.add_restraint("a_ni", lb=3.527, ub=3.527, scaled=True)
127127
# Now we do the same with the delta2 and Biso parameters (remember that
128128
# Biso = 8*pi**2*Uiso)
129-
recipe.restrain("delta2_ni", lb=2.22, ub=2.22, scaled=True)
130-
recipe.restrain("Biso_0_ni", lb=0.454, ub=0.454, scaled=True)
129+
recipe.add_restraint("delta2_ni", lb=2.22, ub=2.22, scaled=True)
130+
recipe.add_restraint("Biso_0_ni", lb=0.454, ub=0.454, scaled=True)
131131
#
132132
# We can do the same with the silicon values. We haven't done a thorough
133133
# job of measuring the uncertainties in the results, so we'll scale these
134134
# as well.
135-
recipe.restrain("a_si", lb=5.430, ub=5.430, scaled=True)
136-
recipe.restrain("delta2_si", lb=3.54, ub=3.54, scaled=True)
137-
recipe.restrain("Biso_0_si", lb=0.645, ub=0.645, scaled=True)
135+
recipe.add_restraint("a_si", lb=5.430, ub=5.430, scaled=True)
136+
recipe.add_restraint("delta2_si", lb=3.54, ub=3.54, scaled=True)
137+
recipe.add_restraint("Biso_0_si", lb=0.645, ub=0.645, scaled=True)
138138

139139
# Give the recipe away so it can be used!
140140
return recipe

docs/examples/debyemodel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def makeRecipe():
152152
# breaking the restraint by the point-average chi^2 value so that the
153153
# restraint is roughly as significant as any other data point throughout
154154
# the fit.
155-
recipe.restrain(recipe.offset, lb=0, scaled=True)
155+
recipe.add_restraint(recipe.offset, lb=0, scaled=True)
156156

157157
# We're done setting up the recipe. We can now do other things with it.
158158
return recipe

docs/examples/simplepdftwophase.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ def makeRecipe(niciffile, siciffile, datname):
8282
# derived has no uncertainty. Thus, we will tell the recipe to scale the
8383
# residual, which means that it will be weighted as much as the average
8484
# data point during the fit.
85-
recipe.restrain("a_ni", lb=3.527, ub=3.527, scaled=True)
85+
recipe.add_restraint("a_ni", lb=3.527, ub=3.527, scaled=True)
8686
# Now we do the same with the delta2 and Biso parameters (remember that
8787
# Biso = 8*pi**2*Uiso)
88-
recipe.restrain("delta2_ni", lb=2.22, ub=2.22, scaled=True)
89-
recipe.restrain("Biso_0_ni", lb=0.454, ub=0.454, scaled=True)
88+
recipe.add_restraint("delta2_ni", lb=2.22, ub=2.22, scaled=True)
89+
recipe.add_restraint("Biso_0_ni", lb=0.454, ub=0.454, scaled=True)
9090
#
9191
# We can do the same with the silicon values. We haven't done a thorough
9292
# job of measuring the uncertainties in the results, so we'll scale these
9393
# as well.
94-
recipe.restrain("a_si", lb=5.430, ub=5.430, scaled=True)
95-
recipe.restrain("delta2_si", lb=3.54, ub=3.54, scaled=True)
96-
recipe.restrain("Biso_0_si", lb=0.645, ub=0.645, scaled=True)
94+
recipe.add_restraint("a_si", lb=5.430, ub=5.430, scaled=True)
95+
recipe.add_restraint("delta2_si", lb=3.54, ub=3.54, scaled=True)
96+
recipe.add_restraint("Biso_0_si", lb=0.645, ub=0.645, scaled=True)
9797

9898
# Give the recipe away so it can be used!
9999
return recipe

src/diffpy/srfit/fitbase/fitrecipe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,7 @@ def convert_bounds_to_restraints(self, sig=1, scaled=False):
17561756
if not hasattr(sig, "__iter__"):
17571757
sig = [sig] * len(pars)
17581758
for par, x in zip(pars, sig):
1759-
self.restrain(
1759+
self.add_restraint(
17601760
par, par.bounds[0], par.bounds[1], sig=x, scaled=scaled
17611761
)
17621762
return

src/diffpy/srfit/fitbase/recipeorganizer.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@
139139
removal_version,
140140
)
141141

142+
restrain_deprecation_msg = build_deprecation_message(
143+
recipeorganizer_base,
144+
"restrain",
145+
"add_restraint",
146+
removal_version,
147+
)
148+
142149

143150
class RecipeContainer(Observable, Configurable, Validatable):
144151
"""Base class for organizing pieces of a FitRecipe.
@@ -1122,7 +1129,7 @@ def clearConstraints(self, recurse=False):
11221129
"""
11231130
return self.clear_all_constraints(recurse=recurse)
11241131

1125-
def restrain(
1132+
def add_restraint(
11261133
self, param_or_eq, lb=-inf, ub=inf, sig=1, scaled=False, params={}
11271134
):
11281135
"""Restrain an expression to specified bounds.
@@ -1184,6 +1191,21 @@ def restrain(
11841191
self.register_restraint(param_or_eq)
11851192
return param_or_eq
11861193

1194+
@deprecated(restrain_deprecation_msg)
1195+
def restrain(
1196+
self, param_or_eq, lb=-inf, ub=inf, sig=1, scaled=False, params={}
1197+
):
1198+
"""This function has been deprecated and will be removed in
1199+
version 4.0.0.
1200+
1201+
Please use
1202+
diffpy.srfit.fitbase.recipeorganizer.RecipeOrganizer.add_restraint
1203+
instead.
1204+
"""
1205+
return self.add_restraint(
1206+
param_or_eq, lb=lb, ub=ub, sig=sig, scaled=scaled, params=params
1207+
)
1208+
11871209
def register_restraint(self, res):
11881210
"""Add a Restraint instance to the RecipeOrganizer.
11891211

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ def build_recipe_two_contributions():
218218
recipe.constrain_parameter(contribution2.m, "2*k")
219219
recipe.constrain_parameter(contribution2.d, contribution1.c)
220220
recipe.constrain_parameter(contribution2.B, "0.5*A")
221-
recipe.restrain(contribution1.A, 0.5, 1.5)
222-
recipe.restrain(contribution1.k, 0.8, 1.2)
221+
recipe.add_restraint(contribution1.A, 0.5, 1.5)
222+
recipe.add_restraint(contribution1.k, 0.8, 1.2)
223223

224224
return recipe
225225

tests/test_fitrecipe.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def testResidual(self):
232232

233233
# Now try some restraints. We want c to be exactly zero. It should give
234234
# a penalty of (c-0)**2, which is 4 in this case
235-
r1 = self.recipe.restrain(self.fitcontribution.c, 0, 0, 1)
235+
r1 = self.recipe.add_restraint(self.fitcontribution.c, 0, 0, 1)
236236
self.recipe._ready = False
237237
res = self.recipe.residual()
238238
chi2 = 4 + dot(y - self.profile.y, y - self.profile.y)
@@ -263,7 +263,9 @@ def testResidual(self):
263263
self.assertTrue(array_equal(y - self.profile.y, res))
264264

265265
# Add a restraint at the fitcontribution level.
266-
r1 = self.fitcontribution.restrain(self.fitcontribution.c, 0, 0, 1)
266+
r1 = self.fitcontribution.add_restraint(
267+
self.fitcontribution.c, 0, 0, 1
268+
)
267269
self.recipe._ready = False
268270
# The chi2 is the same as above, plus 4
269271
res = self.recipe.residual()
@@ -361,7 +363,7 @@ def testPrintFitHook(capturestdout):
361363
recipe.addContribution(fitcontribution)
362364

363365
recipe.add_variable(fitcontribution.c)
364-
recipe.restrain("c", lb=5)
366+
recipe.add_restraint("c", lb=5)
365367
(pfh,) = recipe.getFitHooks()
366368
out = capturestdout(recipe.scalar_residual)
367369
assert "" == out
@@ -437,7 +439,7 @@ def test_add_contribution(capturestdout):
437439
recipe.add_contribution(fitcontribution)
438440

439441
recipe.add_variable(fitcontribution.c)
440-
recipe.restrain("c", lb=5)
442+
recipe.add_restraint("c", lb=5)
441443
(pfh,) = recipe.get_fit_hooks()
442444
out = capturestdout(recipe.scalar_residual)
443445
assert "" == out

tests/test_recipeorganizer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_constrain_parameter(self):
328328

329329
return
330330

331-
def testRestrain(self):
331+
def test_add_restraint(self):
332332
"""Test the restrain method."""
333333

334334
p1 = Parameter("p1", 1)
@@ -338,7 +338,7 @@ def testRestrain(self):
338338
self.m._eqfactory.registerArgument("p2", p2)
339339

340340
self.assertEqual(0, len(self.m._restraints))
341-
r = self.m.restrain("p1+p2", ub=10)
341+
r = self.m.add_restraint("p1+p2", ub=10)
342342
self.assertEqual(1, len(self.m._restraints))
343343
p2.set_value(10)
344344
self.assertEqual(1, r.penalty())
@@ -402,8 +402,8 @@ def testGetRestraints(self):
402402
m2._add_parameter(p3)
403403
m2._add_parameter(p4)
404404

405-
r1 = self.m.restrain("p1 + p2")
406-
r2 = m2.restrain("2*p3 + p4")
405+
r1 = self.m.add_restraint("p1 + p2")
406+
r2 = m2.add_restraint("2*p3 + p4")
407407

408408
res = self.m._get_restraints()
409409
self.assertTrue(r1 in res)
@@ -632,7 +632,7 @@ def capture_show(*args, **kwargs):
632632
assert "Parameters" in lines2
633633
assert "Constraints" in lines2
634634
assert "Restraints" not in lines2
635-
organizer.restrain("z", lb=2, ub=3, sig=0.001)
635+
organizer.add_restraint("z", lb=2, ub=3, sig=0.001)
636636
out3 = capture_show()
637637
lines3 = out3.strip().split("\n")
638638
assert 13 == len(lines3)

0 commit comments

Comments
 (0)