Skip to content

Commit dea5482

Browse files
authored
Merge pull request #154 from cadenmyers13/fitcontrib-dep
deprecate: Deprecate `getEquation` from `FitContribution` and `BaseBuilder`
2 parents ea94c29 + 12f4ea8 commit dea5482

5 files changed

Lines changed: 85 additions & 12 deletions

File tree

news/fitcontrib-dep.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**Added:**
2+
3+
* Added ``get_equation`` method to ``BaseBuilder``.
4+
* Added ``get_equation`` method to ``FitContribution``.
5+
6+
**Changed:**
7+
8+
* <news item>
9+
10+
**Deprecated:**
11+
12+
* Deprecated ``BaseBuilder.getEquation`` method for removal in 4.0.0.
13+
* Deprecated ``FitContributution.getEquation`` method for removal in 4.0.0.
14+
15+
**Removed:**
16+
17+
* <news item>
18+
19+
**Fixed:**
20+
21+
* <news item>
22+
23+
**Security:**
24+
25+
* <news item>

src/diffpy/srfit/equation/builder.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@
5353
> # sin is defined in this module as an OperatorBuilder
5454
> sin = getBuilder("sin")
5555
> beq = A*sin(a*x)
56-
> eq = beq.getEquation()
56+
> eq = beq.get_equation()
5757
5858
The equation builder can also handle scalar constants. Staring with the above
5959
setup:
6060
> beq2 = A*sin(a*x) + 3
61-
> eq2 = beq2.getEquation()
61+
> eq2 = beq2.get_equation()
6262
Here, we didn't have to wrap '3' in an ArgumentBuilder. Non scalars, constant
6363
or otherwise, must be wrapped as ArgumentBuilders in order to be used in this
6464
way.
@@ -86,6 +86,7 @@
8686
import diffpy.srfit.equation.literals as literals
8787
from diffpy.srfit.equation.equationmod import Equation
8888
from diffpy.srfit.equation.literals.literal import Literal
89+
from diffpy.utils._deprecator import build_deprecation_message, deprecated
8990

9091
__all__ = [
9192
"EquationFactory",
@@ -172,7 +173,7 @@ def makeEquation(
172173
lit = literals.Argument(value=beq, const=True)
173174
eq = Equation(name="", root=lit)
174175
else:
175-
eq = beq.getEquation()
176+
eq = beq.get_equation()
176177
self.equations.add(eq)
177178
return eq
178179

@@ -404,6 +405,16 @@ def _get_undefined_args(self, eqstr):
404405

405406
# End class EquationFactory
406407

408+
base_basebuilder = "diffpy.srfit.equation.builder.BaseBuilder"
409+
removal_version = "4.0.0"
410+
411+
getequation_dep_msg = build_deprecation_message(
412+
base_basebuilder,
413+
"getEquation",
414+
"get_equation",
415+
removal_version,
416+
)
417+
407418

408419
class BaseBuilder(object):
409420
"""Class for building equations.
@@ -430,7 +441,7 @@ def __call__(self, *args):
430441
)
431442
raise TypeError(m)
432443

433-
def getEquation(self):
444+
def get_equation(self):
434445
"""Get the equation built by this object.
435446
436447
The equation will given the name "_eq_<root>" where "<root>" is
@@ -441,6 +452,16 @@ def getEquation(self):
441452
eq = Equation(name, self.literal)
442453
return eq
443454

455+
@deprecated(getequation_dep_msg)
456+
def getEquation(self):
457+
"""This function has been deprecated and will be removed in version
458+
4.0.0.
459+
460+
Please use diffpy.srfit.equation.builder.BaseBuilder.get_equation
461+
instead.
462+
"""
463+
return self.get_equation()
464+
444465
def __eval_binary(self, other, OperatorClass, onleft=True):
445466
"""Evaluate a binary function.
446467

src/diffpy/srfit/fitbase/fitcontribution.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@
5656
removal_version,
5757
)
5858

59+
getequation_dep_msg = build_deprecation_message(
60+
base,
61+
"getEquation",
62+
"get_equation",
63+
removal_version,
64+
)
65+
5966

6067
class FitContribution(ParameterSet):
6168
"""FitContribution class.
@@ -304,7 +311,7 @@ def setEquation(self, eqstr, ns={}):
304311
self.set_equation(eqstr, ns=ns)
305312
return
306313

307-
def getEquation(self):
314+
def get_equation(self):
308315
"""Get math expression string for the active profile equation.
309316
310317
Return normalized math expression or an empty string if profile
@@ -317,6 +324,16 @@ def getEquation(self):
317324
rv = getExpression(self._eq)
318325
return rv
319326

327+
@deprecated(getequation_dep_msg)
328+
def getEquation(self):
329+
"""This function has been deprecated and will be removed in version
330+
4.0.0.
331+
332+
Please use diffpy.srfit.fitbase.FitContribution.get_equation
333+
instead.
334+
"""
335+
return self.get_equation()
336+
320337
def setResidualEquation(self, eqstr):
321338
"""Set the residual equation for the FitContribution.
322339

tests/test_builder.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def testBuildEquation(noObserversInGlobalBuilders):
214214
x = numpy.arange(0, numpy.pi, 0.1)
215215

216216
beq = A * sin(a * x)
217-
eq = beq.getEquation()
217+
eq = beq.get_equation()
218218

219219
assert "a" in eq.argdict
220220
assert "A" in eq.argdict
@@ -235,7 +235,7 @@ def _f(a, b):
235235
b = builder.ArgumentBuilder(name="b", value=1)
236236

237237
beq = sin(f(a, b))
238-
eq = beq.getEquation()
238+
eq = beq.get_equation()
239239
assert eq() == numpy.sin(_f(2, 1))
240240

241241
# complex function
@@ -245,23 +245,23 @@ def _f(a, b):
245245
x = builder.ArgumentBuilder(name="x", value=_x, const=True)
246246
sigma = builder.ArgumentBuilder(name="sigma", value=0.1)
247247
beq = sqrt(e ** (-0.5 * (x / sigma) ** 2))
248-
eq = beq.getEquation()
248+
eq = beq.get_equation()
249249
assert numpy.allclose(eq(), numpy.sqrt(numpy.exp(-0.5 * (_x / 0.1) ** 2)))
250250

251251
# Equation with Equation
252252
A = builder.ArgumentBuilder(name="A", value=2)
253253
B = builder.ArgumentBuilder(name="B", value=4)
254254
beq = A + B
255-
eq = beq.getEquation()
255+
eq = beq.get_equation()
256256
E = builder.wrapOperator("eq", eq)
257-
eq2 = (2 * E).getEquation()
257+
eq2 = (2 * E).get_equation()
258258
# Make sure these evaluate to the same thing
259259
assert eq.args == [A.literal, B.literal]
260260
assert 2 * eq() == eq2()
261261
# Pass new arguments to the equation
262262
C = builder.ArgumentBuilder(name="C", value=5)
263263
D = builder.ArgumentBuilder(name="D", value=6)
264-
eq3 = (E(C, D) + 1).getEquation()
264+
eq3 = (E(C, D) + 1).get_equation()
265265
assert 12 == eq3()
266266
# Pass old and new arguments to the equation
267267
# If things work right, A has been given the value of C in the last

tests/test_contribution.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,22 @@ def test_set_equation(noObserversInGlobalBuilders):
313313
def test_getEquation(noObserversInGlobalBuilders):
314314
"""Check getting the current profile simulation formula."""
315315
fc = FitContribution("test")
316-
assert "" == fc.getEquation()
316+
assert "" == fc.get_equation()
317317
fc.set_equation("A * sin(x + 5)")
318318
assert "(A * sin((x + 5)))" == fc.getEquation()
319319
assert noObserversInGlobalBuilders
320320
return
321321

322322

323+
def test_get_equation(noObserversInGlobalBuilders):
324+
"""Check getting the current profile simulation formula."""
325+
fc = FitContribution("test")
326+
assert "" == fc.get_equation()
327+
fc.set_equation("A * sin(x + 5)")
328+
assert "(A * sin((x + 5)))" == fc.get_equation()
329+
assert noObserversInGlobalBuilders
330+
return
331+
332+
323333
if __name__ == "__main__":
324334
unittest.main()

0 commit comments

Comments
 (0)