Skip to content

Commit a0eeed1

Browse files
committed
lambdas in tests
1 parent 5980b00 commit a0eeed1

File tree

4 files changed

+47
-88
lines changed

4 files changed

+47
-88
lines changed

tests/test_builder.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ def g2(v1):
145145

146146
def testParseEquation(noObserversInGlobalBuilders):
147147

148-
from numpy import array_equal, divide, e, sin, sqrt
149-
150148
factory = builder.EquationFactory()
151149

152150
# Scalar equation
@@ -159,8 +157,7 @@ def testParseEquation(noObserversInGlobalBuilders):
159157
eq.x.setValue(x)
160158
eq.B.setValue(B)
161159
eq.C.setValue(C)
162-
f = lambda A, x, B, C: A * sin(0.5 * x) + divide(B, C)
163-
assert array_equal(eq(), f(A, x, B, C))
160+
assert numpy.array_equal(eq(), f_equation(A, x, B, C))
164161

165162
# Make sure that the arguments of eq are listed in the order in which
166163
# they appear in the equations.
@@ -172,23 +169,21 @@ def testParseEquation(noObserversInGlobalBuilders):
172169
sigma = 0.1
173170
eq.x.setValue(x)
174171
eq.sigma.setValue(sigma)
175-
f = lambda x, sigma: sqrt(e ** (-0.5 * (x / sigma) ** 2))
176-
assert numpy.allclose(eq(), f(x, sigma))
177-
172+
assert numpy.allclose(eq(), gaussian_test(x, sigma))
178173
assert eq.args == [eq.x, eq.sigma]
179174

180175
# Equation with constants
181176
factory.registerConstant("x", x)
182177
eq = factory.makeEquation("sqrt(e**(-0.5*(x/sigma)**2))")
183178
assert "sigma" in eq.argdict
184179
assert "x" not in eq.argdict
185-
assert numpy.allclose(eq(sigma=sigma), f(x, sigma))
180+
assert numpy.allclose(eq(sigma=sigma), gaussian_test(x, sigma))
186181
assert eq.args == [eq.sigma]
187182

188183
# Equation with user-defined functions
189184
factory.registerFunction("myfunc", eq, ["sigma"])
190185
eq2 = factory.makeEquation("c*myfunc(sigma)")
191-
assert numpy.allclose(eq2(c=2, sigma=sigma), 2 * f(x, sigma))
186+
assert numpy.allclose(eq2(c=2, sigma=sigma), 2 * gaussian_test(x, sigma))
192187
assert "sigma" in eq2.argdict
193188
assert "c" in eq2.argdict
194189
assert eq2.args == [eq2.c, eq2.sigma]
@@ -251,8 +246,7 @@ def _f(a, b):
251246
sigma = builder.ArgumentBuilder(name="sigma", value=0.1)
252247
beq = sqrt(e ** (-0.5 * (x / sigma) ** 2))
253248
eq = beq.getEquation()
254-
f = lambda x, sigma: sqrt(e ** (-0.5 * (x / sigma) ** 2))
255-
assert numpy.allclose(eq(), numpy.sqrt(e ** (-0.5 * (_x / 0.1) ** 2)))
249+
assert numpy.allclose(eq(), numpy.sqrt(numpy.exp(-0.5 * (_x / 0.1) ** 2)))
256250

257251
# Equation with Equation
258252
A = builder.ArgumentBuilder(name="A", value=2)
@@ -283,5 +277,9 @@ def _f(a, b):
283277
return
284278

285279

286-
if __name__ == "__main__":
287-
unittest.main()
280+
def f_equation(a, x, b, c):
281+
return a * numpy.sin(0.5 * x) + numpy.divide(b, c)
282+
283+
284+
def gaussian_test(x, sigma):
285+
return numpy.sqrt(numpy.exp(-0.5 * (x / sigma) ** 2))

tests/test_characteristicfunctions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import diffpy.srfit.pdf.characteristicfunctions as cf
2323
from diffpy.srfit.sas.sasimport import sasimport
2424

25-
# Global variables to be assigned in setUp
26-
cf = None
25+
# # Global variables to be assigned in setUp
26+
# cf = None
27+
# Fixme: remove this code if imports don't break on testing
2728

2829
# ----------------------------------------------------------------------------
2930

tests/test_parameter.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,53 +27,53 @@ class TestParameter(unittest.TestCase):
2727

2828
def testSetValue(self):
2929
"""Test initialization."""
30-
l = Parameter("l")
30+
par_l = Parameter("l")
3131

32-
l.setValue(3.14)
33-
self.assertAlmostEqual(3.14, l.getValue())
32+
par_l.setValue(3.14)
33+
self.assertAlmostEqual(3.14, par_l.getValue())
3434

3535
# Try array
3636
import numpy
3737

3838
x = numpy.arange(0, 10, 0.1)
39-
l.setValue(x)
40-
self.assertTrue(l.getValue() is x)
41-
self.assertTrue(l.value is x)
39+
par_l.setValue(x)
40+
self.assertTrue(par_l.getValue() is x)
41+
self.assertTrue(par_l.value is x)
4242

4343
# Change the array
4444
y = numpy.arange(0, 10, 0.5)
45-
l.value = y
46-
self.assertTrue(l.getValue() is y)
47-
self.assertTrue(l.value is y)
45+
par_l.value = y
46+
self.assertTrue(par_l.getValue() is y)
47+
self.assertTrue(par_l.value is y)
4848

4949
# Back to scalar
50-
l.setValue(1.01)
51-
self.assertAlmostEqual(1.01, l.getValue())
52-
self.assertAlmostEqual(1.01, l.value)
50+
par_l.setValue(1.01)
51+
self.assertAlmostEqual(1.01, par_l.getValue())
52+
self.assertAlmostEqual(1.01, par_l.value)
5353
return
5454

5555

5656
class TestParameterProxy(unittest.TestCase):
5757

5858
def testProxy(self):
5959
"""Test the ParameterProxy class."""
60-
l = Parameter("l", 3.14)
60+
par_l = Parameter("l", 3.14)
6161

6262
# Try Accessor adaptation
63-
la = ParameterProxy("l2", l)
63+
la = ParameterProxy("l2", par_l)
6464

6565
self.assertEqual("l2", la.name)
66-
self.assertEqual(l.getValue(), la.getValue())
66+
self.assertEqual(par_l.getValue(), la.getValue())
6767

6868
# Change the parameter
69-
l.value = 2.3
70-
self.assertEqual(l.getValue(), la.getValue())
71-
self.assertEqual(l.value, la.value)
69+
par_l.value = 2.3
70+
self.assertEqual(par_l.getValue(), la.getValue())
71+
self.assertEqual(par_l.value, la.value)
7272

7373
# Change the proxy
7474
la.value = 3.2
75-
self.assertEqual(l.getValue(), la.getValue())
76-
self.assertEqual(l.value, la.value)
75+
self.assertEqual(par_l.getValue(), la.getValue())
76+
self.assertEqual(par_l.value, la.value)
7777

7878
return
7979

@@ -85,38 +85,38 @@ def testWrapper(self):
8585
8686
This adapts a Parameter to the Parameter interface. :)
8787
"""
88-
l = Parameter("l", 3.14)
88+
par_l = Parameter("l", 3.14)
8989

9090
# Try Accessor adaptation
9191
la = ParameterAdapter(
92-
"l", l, getter=Parameter.getValue, setter=Parameter.setValue
92+
"l", par_l, getter=Parameter.getValue, setter=Parameter.setValue
9393
)
9494

95-
self.assertEqual(l.name, la.name)
96-
self.assertEqual(l.getValue(), la.getValue())
95+
self.assertEqual(par_l.name, la.name)
96+
self.assertEqual(par_l.getValue(), la.getValue())
9797

9898
# Change the parameter
99-
l.setValue(2.3)
100-
self.assertEqual(l.getValue(), la.getValue())
99+
par_l.setValue(2.3)
100+
self.assertEqual(par_l.getValue(), la.getValue())
101101

102102
# Change the adapter
103103
la.setValue(3.2)
104-
self.assertEqual(l.getValue(), la.getValue())
104+
self.assertEqual(par_l.getValue(), la.getValue())
105105

106106
# Try Attribute adaptation
107-
la = ParameterAdapter("l", l, attr="value")
107+
la = ParameterAdapter("l", par_l, attr="value")
108108

109-
self.assertEqual(l.name, la.name)
109+
self.assertEqual(par_l.name, la.name)
110110
self.assertEqual("value", la.attr)
111-
self.assertEqual(l.getValue(), la.getValue())
111+
self.assertEqual(par_l.getValue(), la.getValue())
112112

113113
# Change the parameter
114-
l.setValue(2.3)
115-
self.assertEqual(l.getValue(), la.getValue())
114+
par_l.setValue(2.3)
115+
self.assertEqual(par_l.getValue(), la.getValue())
116116

117117
# Change the adapter
118118
la.setValue(3.2)
119-
self.assertEqual(l.getValue(), la.getValue())
119+
self.assertEqual(par_l.getValue(), la.getValue())
120120

121121
return
122122

tests/utils.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)