Skip to content

Commit 7a2e29d

Browse files
committed
equationFromString deprecation
1 parent 5d958fb commit 7a2e29d

5 files changed

Lines changed: 83 additions & 35 deletions

File tree

src/diffpy/srfit/fitbase/fitcontribution.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from diffpy.srfit.fitbase.parameter import ParameterProxy
2929
from diffpy.srfit.fitbase.parameterset import ParameterSet
3030
from diffpy.srfit.fitbase.profile import Profile
31-
from diffpy.srfit.fitbase.recipeorganizer import equationFromString
31+
from diffpy.srfit.fitbase.recipeorganizer import get_equation_from_string
3232
from diffpy.utils._deprecator import build_deprecation_message, deprecated
3333

3434
base = "diffpy.srfit.fitbase.FitContribution"
@@ -296,7 +296,9 @@ def set_equation(self, eqstr, ns={}):
296296
variable.
297297
"""
298298
# Build the equation instance.
299-
eq = equationFromString(eqstr, self._eqfactory, buildargs=True, ns=ns)
299+
eq = get_equation_from_string(
300+
eqstr, self._eqfactory, buildargs=True, ns=ns
301+
)
300302
eq.name = "eq"
301303

302304
# Register any new Parameters.
@@ -386,7 +388,7 @@ def set_residual_equation(self, eqstr):
386388
elif eqstr == "resv":
387389
eqstr = resvstr
388390

389-
reseq = equationFromString(eqstr, self._eqfactory)
391+
reseq = get_equation_from_string(eqstr, self._eqfactory)
390392
self._eqfactory.wipeout(self._reseq)
391393
self._reseq = reseq
392394

src/diffpy/srfit/fitbase/recipeorganizer.py

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
RecipeContainer is the base class for organizing Parameters, and other
1818
RecipeContainers. RecipeOrganizer is an extended RecipeContainer that
1919
incorporates equation building, constraints and Restraints.
20-
equationFromString creates an Equation instance from a string.
20+
get_equation_from_string creates an Equation instance from a string.
2121
"""
2222

23-
__all__ = ["RecipeContainer", "RecipeOrganizer", "equationFromString"]
23+
__all__ = ["RecipeContainer", "RecipeOrganizer", "get_equation_from_string"]
2424

2525
import re
2626
from collections import OrderedDict
@@ -160,6 +160,13 @@
160160
removal_version,
161161
)
162162

163+
equationFromString_deprecation_msg = build_deprecation_message(
164+
recipeorganizer_base,
165+
"equationFromString",
166+
"get_equation_from_string",
167+
removal_version,
168+
)
169+
163170

164171
class RecipeContainer(Observable, Configurable, Validatable):
165172
"""Base class for organizing pieces of a FitRecipe.
@@ -851,7 +858,7 @@ def register_string_function(self, function_str, name, func_params={}):
851858
"""
852859

853860
# Build the equation instance.
854-
eq = equationFromString(
861+
eq = get_equation_from_string(
855862
function_str, self._eqfactory, ns=func_params, buildargs=True
856863
)
857864
eq.name = name
@@ -907,7 +914,9 @@ def evaluate_equation(self, equation_str, func_params={}):
907914
If `func_params` uses a name that is already used for a
908915
variable.
909916
"""
910-
eq = equationFromString(equation_str, self._eqfactory, func_params)
917+
eq = get_equation_from_string(
918+
equation_str, self._eqfactory, func_params
919+
)
911920
try:
912921
returned_value = eq()
913922
finally:
@@ -969,7 +978,9 @@ def constrain_parameter(self, parameter, constraint_eq, params={}):
969978

970979
if isinstance(constraint_eq, str):
971980
eqstr = constraint_eq
972-
eq = equationFromString(constraint_eq, self._eqfactory, params)
981+
eq = get_equation_from_string(
982+
constraint_eq, self._eqfactory, params
983+
)
973984
else:
974985
eq = Equation(root=constraint_eq)
975986
eqstr = constraint_eq.name
@@ -1194,7 +1205,7 @@ def add_restraint(
11941205
"""
11951206
if isinstance(param_or_eq, str):
11961207
eqstr = param_or_eq
1197-
eq = equationFromString(param_or_eq, self._eqfactory, params)
1208+
eq = get_equation_from_string(param_or_eq, self._eqfactory, params)
11981209
else:
11991210
eq = Equation(root=param_or_eq)
12001211
eqstr = param_or_eq.name
@@ -1489,38 +1500,45 @@ def show(self, pattern="", textwidth=78):
14891500
# End RecipeOrganizer
14901501

14911502

1492-
def equationFromString(
1503+
def get_equation_from_string(
14931504
eqstr, factory, ns={}, buildargs=False, argclass=Parameter, argkw={}
14941505
):
1495-
"""Make an equation from a string.
1506+
"""Make an Equation object from a string.
14961507
14971508
Attributes
14981509
----------
1499-
eqstr
1510+
eqstr : str
15001511
A string representation of the equation. The equation must
15011512
consist of numpy operators and "known" Parameters. Parameters
15021513
are known if they are in ns, or already defined in the factory.
1503-
factory
1514+
factory : EquationFactory
15041515
An EquationFactory instance.
1505-
ns
1506-
A dictionary of Parameters indexed by name that are used
1516+
ns : dict, optional
1517+
The dictionary of Parameters indexed by name that are used
15071518
in the eqstr but not already defined in the factory
15081519
(default {}).
1509-
buildargs
1520+
buildargs : bool, optional
15101521
A flag indicating whether missing Parameters can be created
15111522
by the Factory (default False). If False, then the a ValueError
15121523
will be raised if there are undefined arguments in the eqstr.
1513-
argclass
1524+
argclass : Parameter class, optional
15141525
Class to use when creating new Arguments (default
15151526
Parameter). The class constructor must accept the 'name' key
15161527
word.
1517-
argkw
1528+
argkw : dict, optional
15181529
Key word dictionary to pass to the argclass constructor
15191530
(default {}).
15201531
1532+
Returns
1533+
-------
1534+
eq : Equation
1535+
An Equation instance representing the equation in eqstr.
15211536
1522-
Raises ValueError if ns uses a name that is already defined in the factory.
1523-
Raises ValueError if the equation has undefined parameters.
1537+
Raises
1538+
------
1539+
ValueError
1540+
If buildargs is False and there are undefined parameters in eqstr
1541+
or if ns uses a name that is already defined in the factory.
15241542
"""
15251543

15261544
defined = set(factory.builders.keys())
@@ -1542,6 +1560,27 @@ def equationFromString(
15421560
return eq
15431561

15441562

1563+
@deprecated(equationFromString_deprecation_msg)
1564+
def equationFromString(
1565+
eqstr, factory, ns={}, buildargs=False, argclass=Parameter, argkw={}
1566+
):
1567+
"""This function has been deprecated and will be removed in version
1568+
4.0.0.
1569+
1570+
Please use
1571+
diffpy.srfit.fitbase.recipeorganizer.get_equation_from_string
1572+
instead.
1573+
"""
1574+
return get_equation_from_string(
1575+
eqstr,
1576+
factory,
1577+
ns=ns,
1578+
buildargs=buildargs,
1579+
argclass=argclass,
1580+
argkw=argkw,
1581+
)
1582+
1583+
15451584
def _has_clear_constraints(msg):
15461585
return hasattr(msg, "clear_all_constraints")
15471586

tests/test_constraint.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from diffpy.srfit.equation.builder import EquationFactory
2020
from diffpy.srfit.fitbase.constraint import Constraint
2121
from diffpy.srfit.fitbase.parameter import Parameter
22-
from diffpy.srfit.fitbase.recipeorganizer import equationFromString
22+
from diffpy.srfit.fitbase.recipeorganizer import get_equation_from_string
2323

2424

2525
class TestConstraint(unittest.TestCase):
@@ -37,17 +37,17 @@ def test_constrain_parameter(self):
3737

3838
c = Constraint()
3939
# Constrain p1 = 2*p2
40-
eq = equationFromString("2*p2", factory)
40+
eq = get_equation_from_string("2*p2", factory)
4141
c.constrain_parameter(p1, eq)
4242

4343
self.assertTrue(p1.constrained)
4444
self.assertFalse(p2.constrained)
4545

46-
eq2 = equationFromString("2*p2+1", factory)
46+
eq2 = get_equation_from_string("2*p2+1", factory)
4747
c2 = Constraint()
4848
self.assertRaises(ValueError, c2.constrain, p1, eq2)
4949
p2.setConst()
50-
eq3 = equationFromString("p1", factory)
50+
eq3 = get_equation_from_string("p1", factory)
5151
self.assertRaises(ValueError, c2.constrain, p2, eq3)
5252

5353
p2.set_value(2.5)
@@ -76,17 +76,17 @@ def testConstraint(self):
7676

7777
c = Constraint()
7878
# Constrain p1 = 2*p2
79-
eq = equationFromString("2*p2", factory)
79+
eq = get_equation_from_string("2*p2", factory)
8080
c.constrain(p1, eq)
8181

8282
self.assertTrue(p1.constrained)
8383
self.assertFalse(p2.constrained)
8484

85-
eq2 = equationFromString("2*p2+1", factory)
85+
eq2 = get_equation_from_string("2*p2+1", factory)
8686
c2 = Constraint()
8787
self.assertRaises(ValueError, c2.constrain, p1, eq2)
8888
p2.setConst()
89-
eq3 = equationFromString("p1", factory)
89+
eq3 = get_equation_from_string("p1", factory)
9090
self.assertRaises(ValueError, c2.constrain, p2, eq3)
9191

9292
p2.set_value(2.5)

tests/test_recipeorganizer.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@
2525
RecipeContainer,
2626
RecipeOrganizer,
2727
equationFromString,
28+
get_equation_from_string,
2829
)
2930

3031
# ----------------------------------------------------------------------------
3132

3233

3334
class TestEquationFromString(unittest.TestCase):
3435

35-
def testEquationFromString(self):
36-
"""Test the equationFromString method."""
36+
def test_get_equation_from_string(self):
37+
"""Test the get_equation_from_string method."""
3738

3839
p1 = Parameter("p1", 1)
3940
p2 = Parameter("p2", 2)
@@ -46,15 +47,17 @@ def testEquationFromString(self):
4647
factory.registerArgument("p2", p2)
4748

4849
# Check usage where all parameters are registered with the factory
49-
eq = equationFromString("p1+p2", factory)
50+
eq = get_equation_from_string("p1+p2", factory)
5051

5152
self.assertEqual(2, len(eq.args))
5253
self.assertTrue(p1 in eq.args)
5354
self.assertTrue(p2 in eq.args)
5455
self.assertEqual(3, eq())
5556

5657
# Try to use a parameter that is not registered
57-
self.assertRaises(ValueError, equationFromString, "p1+p2+p3", factory)
58+
self.assertRaises(
59+
ValueError, get_equation_from_string, "p1+p2+p3", factory
60+
)
5861

5962
# Pass that argument in the ns dictionary
6063
eq = equationFromString("p1+p2+p3", factory, {"p3": p3})
@@ -69,12 +72,16 @@ def testEquationFromString(self):
6972

7073
# Pass and use an unregistered parameter
7174
self.assertRaises(
72-
ValueError, equationFromString, "p1+p2+p3+p4", factory, {"p3": p3}
75+
ValueError,
76+
get_equation_from_string,
77+
"p1+p2+p3+p4",
78+
factory,
79+
{"p3": p3},
7380
)
7481

7582
# Try to overload a registered parameter
7683
self.assertRaises(
77-
ValueError, equationFromString, "p1+p2", factory, {"p2": p4}
84+
ValueError, get_equation_from_string, "p1+p2", factory, {"p2": p4}
7885
)
7986

8087
return

tests/test_restraint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from diffpy.srfit.equation.builder import EquationFactory
2020
from diffpy.srfit.fitbase.parameter import Parameter
21-
from diffpy.srfit.fitbase.recipeorganizer import equationFromString
21+
from diffpy.srfit.fitbase.recipeorganizer import get_equation_from_string
2222
from diffpy.srfit.fitbase.restraint import Restraint
2323

2424

@@ -36,7 +36,7 @@ def testRestraint(self):
3636
factory.registerArgument("p2", p2)
3737

3838
# Restrain 1 < p1 + p2 < 5
39-
eq = equationFromString("p1 + p2", factory)
39+
eq = get_equation_from_string("p1 + p2", factory)
4040
r = Restraint(eq, 1, 5)
4141

4242
# This should have no penalty

0 commit comments

Comments
 (0)