Skip to content

Flake8 #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lambdas in tests
  • Loading branch information
sbillinge committed Jun 26, 2025
commit a0eeed1bf84d6e6d73361dbaff7ee0bdcc666a39
24 changes: 11 additions & 13 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ def g2(v1):

def testParseEquation(noObserversInGlobalBuilders):

from numpy import array_equal, divide, e, sin, sqrt

factory = builder.EquationFactory()

# Scalar equation
Expand All @@ -159,8 +157,7 @@ def testParseEquation(noObserversInGlobalBuilders):
eq.x.setValue(x)
eq.B.setValue(B)
eq.C.setValue(C)
f = lambda A, x, B, C: A * sin(0.5 * x) + divide(B, C)
assert array_equal(eq(), f(A, x, B, C))
assert numpy.array_equal(eq(), f_equation(A, x, B, C))

# Make sure that the arguments of eq are listed in the order in which
# they appear in the equations.
Expand All @@ -172,23 +169,21 @@ def testParseEquation(noObserversInGlobalBuilders):
sigma = 0.1
eq.x.setValue(x)
eq.sigma.setValue(sigma)
f = lambda x, sigma: sqrt(e ** (-0.5 * (x / sigma) ** 2))
assert numpy.allclose(eq(), f(x, sigma))

assert numpy.allclose(eq(), gaussian_test(x, sigma))
assert eq.args == [eq.x, eq.sigma]

# Equation with constants
factory.registerConstant("x", x)
eq = factory.makeEquation("sqrt(e**(-0.5*(x/sigma)**2))")
assert "sigma" in eq.argdict
assert "x" not in eq.argdict
assert numpy.allclose(eq(sigma=sigma), f(x, sigma))
assert numpy.allclose(eq(sigma=sigma), gaussian_test(x, sigma))
assert eq.args == [eq.sigma]

# Equation with user-defined functions
factory.registerFunction("myfunc", eq, ["sigma"])
eq2 = factory.makeEquation("c*myfunc(sigma)")
assert numpy.allclose(eq2(c=2, sigma=sigma), 2 * f(x, sigma))
assert numpy.allclose(eq2(c=2, sigma=sigma), 2 * gaussian_test(x, sigma))
assert "sigma" in eq2.argdict
assert "c" in eq2.argdict
assert eq2.args == [eq2.c, eq2.sigma]
Expand Down Expand Up @@ -251,8 +246,7 @@ def _f(a, b):
sigma = builder.ArgumentBuilder(name="sigma", value=0.1)
beq = sqrt(e ** (-0.5 * (x / sigma) ** 2))
eq = beq.getEquation()
f = lambda x, sigma: sqrt(e ** (-0.5 * (x / sigma) ** 2))
assert numpy.allclose(eq(), numpy.sqrt(e ** (-0.5 * (_x / 0.1) ** 2)))
assert numpy.allclose(eq(), numpy.sqrt(numpy.exp(-0.5 * (_x / 0.1) ** 2)))

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


if __name__ == "__main__":
unittest.main()
def f_equation(a, x, b, c):
return a * numpy.sin(0.5 * x) + numpy.divide(b, c)


def gaussian_test(x, sigma):
return numpy.sqrt(numpy.exp(-0.5 * (x / sigma) ** 2))
5 changes: 3 additions & 2 deletions tests/test_characteristicfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import diffpy.srfit.pdf.characteristicfunctions as cf
from diffpy.srfit.sas.sasimport import sasimport

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

# ----------------------------------------------------------------------------

Expand Down
66 changes: 33 additions & 33 deletions tests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,53 @@ class TestParameter(unittest.TestCase):

def testSetValue(self):
"""Test initialization."""
l = Parameter("l")
par_l = Parameter("l")

l.setValue(3.14)
self.assertAlmostEqual(3.14, l.getValue())
par_l.setValue(3.14)
self.assertAlmostEqual(3.14, par_l.getValue())

# Try array
import numpy

x = numpy.arange(0, 10, 0.1)
l.setValue(x)
self.assertTrue(l.getValue() is x)
self.assertTrue(l.value is x)
par_l.setValue(x)
self.assertTrue(par_l.getValue() is x)
self.assertTrue(par_l.value is x)

# Change the array
y = numpy.arange(0, 10, 0.5)
l.value = y
self.assertTrue(l.getValue() is y)
self.assertTrue(l.value is y)
par_l.value = y
self.assertTrue(par_l.getValue() is y)
self.assertTrue(par_l.value is y)

# Back to scalar
l.setValue(1.01)
self.assertAlmostEqual(1.01, l.getValue())
self.assertAlmostEqual(1.01, l.value)
par_l.setValue(1.01)
self.assertAlmostEqual(1.01, par_l.getValue())
self.assertAlmostEqual(1.01, par_l.value)
return


class TestParameterProxy(unittest.TestCase):

def testProxy(self):
"""Test the ParameterProxy class."""
l = Parameter("l", 3.14)
par_l = Parameter("l", 3.14)

# Try Accessor adaptation
la = ParameterProxy("l2", l)
la = ParameterProxy("l2", par_l)

self.assertEqual("l2", la.name)
self.assertEqual(l.getValue(), la.getValue())
self.assertEqual(par_l.getValue(), la.getValue())

# Change the parameter
l.value = 2.3
self.assertEqual(l.getValue(), la.getValue())
self.assertEqual(l.value, la.value)
par_l.value = 2.3
self.assertEqual(par_l.getValue(), la.getValue())
self.assertEqual(par_l.value, la.value)

# Change the proxy
la.value = 3.2
self.assertEqual(l.getValue(), la.getValue())
self.assertEqual(l.value, la.value)
self.assertEqual(par_l.getValue(), la.getValue())
self.assertEqual(par_l.value, la.value)

return

Expand All @@ -85,38 +85,38 @@ def testWrapper(self):

This adapts a Parameter to the Parameter interface. :)
"""
l = Parameter("l", 3.14)
par_l = Parameter("l", 3.14)

# Try Accessor adaptation
la = ParameterAdapter(
"l", l, getter=Parameter.getValue, setter=Parameter.setValue
"l", par_l, getter=Parameter.getValue, setter=Parameter.setValue
)

self.assertEqual(l.name, la.name)
self.assertEqual(l.getValue(), la.getValue())
self.assertEqual(par_l.name, la.name)
self.assertEqual(par_l.getValue(), la.getValue())

# Change the parameter
l.setValue(2.3)
self.assertEqual(l.getValue(), la.getValue())
par_l.setValue(2.3)
self.assertEqual(par_l.getValue(), la.getValue())

# Change the adapter
la.setValue(3.2)
self.assertEqual(l.getValue(), la.getValue())
self.assertEqual(par_l.getValue(), la.getValue())

# Try Attribute adaptation
la = ParameterAdapter("l", l, attr="value")
la = ParameterAdapter("l", par_l, attr="value")

self.assertEqual(l.name, la.name)
self.assertEqual(par_l.name, la.name)
self.assertEqual("value", la.attr)
self.assertEqual(l.getValue(), la.getValue())
self.assertEqual(par_l.getValue(), la.getValue())

# Change the parameter
l.setValue(2.3)
self.assertEqual(l.getValue(), la.getValue())
par_l.setValue(2.3)
self.assertEqual(par_l.getValue(), la.getValue())

# Change the adapter
la.setValue(3.2)
self.assertEqual(l.getValue(), la.getValue())
self.assertEqual(par_l.getValue(), la.getValue())

return

Expand Down
40 changes: 0 additions & 40 deletions tests/utils.py

This file was deleted.

Loading