forked from mantidproject/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mantidproject#36457 from mantidproject/ewm2941_ass…
…ert_almost_equal Add mantid.testing.assert_almost_equal
- Loading branch information
Showing
10 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
Framework/PythonInterface/mantid/_testing/AssertAlmostEqual.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Mantid Repository : https://github.com/mantidproject/mantid | ||
# | ||
# Copyright © 2023 ISIS Rutherford Appleton Laboratory UKRI, | ||
# NScD Oak Ridge National Laboratory, European Spallation Source, | ||
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS | ||
# SPDX - License - Identifier: GPL - 3.0 + | ||
|
||
from mantid.simpleapi import CompareWorkspaces | ||
|
||
|
||
def assert_almost_equal(Workspace1, Workspace2, rtol=0.0, atol=0.0): | ||
""" | ||
Raises an assertion error if two workspaces are not within specified tolerance. | ||
Parameters | ||
---------- | ||
Workspace1, Workspace2 : Workspace | ||
Input workspaces to compare. | ||
rtol : float | ||
The relative tolerance parameter. | ||
atol : float | ||
The absolute tolerance parameter. | ||
Raises | ||
------- | ||
AssertionError | ||
If Workspace1 and Workspace2 are not equal up to specified precision | ||
ValueError | ||
If atol and rtol are both provided | ||
""" | ||
|
||
if rtol != 0.0 and atol != 0.0: | ||
raise ValueError("Specify rtol or atol, not both") | ||
_rel = False | ||
tolerance = 1e-10 | ||
if atol: | ||
tolerance = atol | ||
|
||
if rtol: | ||
tolerance = rtol | ||
_rel = True | ||
|
||
result, message = CompareWorkspaces(Workspace1, Workspace2, Tolerance=tolerance, ToleranceRelErr=_rel) | ||
msg_dict = message.toDict() | ||
if not result: | ||
msg = ", ".join(msg_dict["Message"]) + f", Workspaces are not within tolerance ({tolerance})" | ||
raise AssertionError(msg) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from mantid._testing.AssertAlmostEqual import assert_almost_equal | ||
|
||
__all__ = ["assert_almost_equal"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Framework/PythonInterface/test/python/mantid/_testing/AssertAlmostEqualTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Mantid Repository : https://github.com/mantidproject/mantid | ||
# | ||
# Copyright © 2023 ISIS Rutherford Appleton Laboratory UKRI, | ||
# NScD Oak Ridge National Laboratory, European Spallation Source, | ||
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS | ||
# SPDX - License - Identifier: GPL - 3.0 + | ||
|
||
import unittest | ||
from unittest import mock | ||
from mantid.testing import assert_almost_equal | ||
from mantid.simpleapi import CreateWorkspace | ||
|
||
|
||
class AssertAlmostEqualTest(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(self): | ||
ws1 = CreateWorkspace(DataX=[0, 1, 2, 3, 4, 5], DataY=[1, 1, 1, 1, 1, 1]) | ||
ws2 = CreateWorkspace(DataX=[0.09, 1.09, 2.09, 3.09, 4.09, 5.09], DataY=[1, 1, 1, 1, 1, 1]) | ||
ws3 = CreateWorkspace(DataX=[0, 2, 4, 6, 8, 10], DataY=[1, 1, 1, 1, 1, 1]) | ||
self.ws1 = ws1 | ||
self.ws2 = ws2 | ||
self.ws3 = ws3 | ||
|
||
def test_simple(self): | ||
assert_almost_equal(self.ws1, self.ws1) | ||
|
||
def test_atol(self): | ||
# compare (ws1 - ws2) < atol | ||
assert_almost_equal(self.ws1, self.ws2, atol=0.1) | ||
|
||
def test_rtol(self): | ||
# compare (ws1 - ws2) / (0.5 * (ws1 + ws2)) < rtol | ||
assert_almost_equal(self.ws1, self.ws3, rtol=0.7) | ||
|
||
def test_raises(self): | ||
with self.assertRaises(AssertionError): | ||
assert_almost_equal(self.ws1, self.ws2) | ||
|
||
with self.assertRaises(ValueError): | ||
assert_almost_equal(self.ws1, self.ws2, atol=1, rtol=1) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
8 changes: 8 additions & 0 deletions
8
Framework/PythonInterface/test/python/mantid/_testing/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# mantid._testing tests | ||
|
||
set(TEST_PY_FILES AssertAlmostEqualTest.py) | ||
|
||
check_tests_valid(${CMAKE_CURRENT_SOURCE_DIR} ${TEST_PY_FILES}) | ||
|
||
# Prefix for test=PythonInterfaceTesting | ||
pyunittest_add_test(${CMAKE_CURRENT_SOURCE_DIR} python._testing ${TEST_PY_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,6 @@ Submodules | |
api/index | ||
plots/index | ||
utils/index | ||
testing/index | ||
simpleapi | ||
fitfunctions |
19 changes: 19 additions & 0 deletions
19
docs/source/api/python/mantid/testing/assert_almost_equal.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.. _mantid.testing.assert_almost_equal: | ||
|
||
===================== | ||
assert_almost_equal | ||
===================== | ||
|
||
This is a Python function for testing if two modules are within a tolerance | ||
|
||
|
||
.. module:`mantid.testing` | ||
.. automodule:: mantid.testing.assert_almost_equal | ||
:members: | ||
:undoc-members: | ||
:inherited-members: | ||
|
||
See Also | ||
-------- | ||
:ref:`algm-CompareWorkspaces` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.. _Mantid_testing: | ||
|
||
====================== | ||
:mod:`mantid.testing` | ||
====================== | ||
|
||
.. module:: mantid.testing | ||
|
||
.. toctree:: | ||
:glob: | ||
:maxdepth: 1 | ||
|
||
* |
1 change: 1 addition & 0 deletions
1
docs/source/release/v6.9.0/Framework/Python/New_features/36457.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- New function :ref:`assert_almost_equal <mantid.testing.assert_almost_equal>` to make testing equality between workspaces more convenient. |