Skip to content

Commit 412e1eb

Browse files
authored
Merge pull request #37708 from mantidproject/assert-almost-equal
Added kwargs to pass parameters to Compare Workspaces function.
2 parents a48b448 + 62e30d7 commit 412e1eb

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

Framework/PythonInterface/mantid/_testing/AssertAlmostEqual.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
# SPDX - License - Identifier: GPL - 3.0 +
77

88
from mantid.simpleapi import CompareWorkspaces
9+
from mantid.kernel import Property
910

1011

11-
def assert_almost_equal(Workspace1, Workspace2, rtol=0.0, atol=0.0):
12+
def assert_almost_equal(Workspace1, Workspace2, rtol=Property.EMPTY_DBL, atol=Property.EMPTY_DBL, **kwargs):
1213
"""
1314
Raises an assertion error if two workspaces are not within specified tolerance.
1415
@@ -26,23 +27,26 @@ def assert_almost_equal(Workspace1, Workspace2, rtol=0.0, atol=0.0):
2627
AssertionError
2728
If Workspace1 and Workspace2 are not equal up to specified precision
2829
ValueError
29-
If atol and rtol are both provided
30+
If atol and rtol are both not provided
3031
3132
"""
32-
33-
if rtol != 0.0 and atol != 0.0:
34-
raise ValueError("Specify rtol or atol, not both")
35-
_rel = False
36-
tolerance = 1e-10
37-
if atol:
38-
tolerance = atol
39-
40-
if rtol:
41-
tolerance = rtol
42-
_rel = True
43-
44-
result, message = CompareWorkspaces(Workspace1, Workspace2, Tolerance=tolerance, ToleranceRelErr=_rel)
45-
msg_dict = message.toDict()
46-
if not result:
47-
msg = ", ".join(msg_dict["Message"]) + f", Workspaces are not within tolerance ({tolerance})"
48-
raise AssertionError(msg)
33+
# check arguments
34+
if len(set(kwargs.keys()).intersection({"Workspace1", "Workspace2", "Tolerance", "ToleranceRelErr"})):
35+
raise ValueError("Workspace1, Workspace2, Tolerance, ToleranceRelErr cannot be passed as additional parameters")
36+
37+
if rtol == Property.EMPTY_DBL and atol == Property.EMPTY_DBL:
38+
raise ValueError("Specify rtol or atol")
39+
40+
if rtol > Property.EMPTY_DBL:
41+
result, message = CompareWorkspaces(Workspace1, Workspace2, Tolerance=rtol, ToleranceRelErr=True, **kwargs)
42+
msg_dict = message.toDict()
43+
if not result:
44+
msg = ", ".join(msg_dict["Message"]) + f", Workspaces are not within relative tolerance ({rtol})"
45+
raise AssertionError(msg)
46+
47+
if atol > Property.EMPTY_DBL:
48+
result, message = CompareWorkspaces(Workspace1, Workspace2, Tolerance=atol, ToleranceRelErr=False, **kwargs)
49+
msg_dict = message.toDict()
50+
if not result:
51+
msg = ", ".join(msg_dict["Message"]) + f", Workspaces are not within absolute tolerance ({atol})"
52+
raise AssertionError(msg)

Framework/PythonInterface/test/python/mantid/_testing/AssertAlmostEqualTest.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def setUpClass(self):
2222
self.ws3 = ws3
2323

2424
def test_simple(self):
25-
assert_almost_equal(self.ws1, self.ws1)
25+
assert_almost_equal(self.ws1, self.ws2, atol=1, rtol=1)
2626

2727
def test_atol(self):
2828
# compare (ws1 - ws2) < atol
@@ -33,11 +33,8 @@ def test_rtol(self):
3333
assert_almost_equal(self.ws1, self.ws3, rtol=0.7)
3434

3535
def test_raises(self):
36-
with self.assertRaises(AssertionError):
37-
assert_almost_equal(self.ws1, self.ws2)
38-
3936
with self.assertRaises(ValueError):
40-
assert_almost_equal(self.ws1, self.ws2, atol=1, rtol=1)
37+
assert_almost_equal(self.ws1, self.ws2)
4138

4239

4340
if __name__ == "__main__":

docs/source/api/python/mantid/testing/assert_almost_equal.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
assert_almost_equal
55
=====================
66

7-
This is a Python function for testing if two modules are within a tolerance
7+
This is a Python function for testing if two modules are within a tolerance.
8+
At least ``rtol`` or ``atol`` parameters should be specified. Neither being
9+
specified generates a ``ValueError`` exception. Both being specified runs
10+
the underlying :ref:`algm-CompareWorkspaces` algorithm
11+
twice, with relative tolerance being first.
12+
813

914

1015
.. module:`mantid.testing`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- The python function ``assert_almost_equal`` for testing if two modules are within a tolerance was reworked

0 commit comments

Comments
 (0)