Skip to content

Commit

Permalink
Merge pull request mantidproject#36457 from mantidproject/ewm2941_ass…
Browse files Browse the repository at this point in the history
…ert_almost_equal

Add mantid.testing.assert_almost_equal
  • Loading branch information
peterfpeterson authored Nov 27, 2023
2 parents 5cc375a + f876037 commit 7680bac
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Framework/PythonInterface/mantid/_testing/AssertAlmostEqual.py
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.
3 changes: 3 additions & 0 deletions Framework/PythonInterface/mantid/testing.py
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"]
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_subdirectory(dataobjects)
add_subdirectory(plots)
add_subdirectory(utils)
add_subdirectory(_plugins)
add_subdirectory(_testing)

set(TEST_PY_FILES ImportModuleTest.py SimpleAPITest.py SimpleAPILoadTest.py SimpleAPIFitTest.py
SimpleAPIRenameWorkspaceTest.py FitFunctionsTest.py
Expand Down
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()
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})
1 change: 1 addition & 0 deletions docs/source/api/python/mantid/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ Submodules
api/index
plots/index
utils/index
testing/index
simpleapi
fitfunctions
19 changes: 19 additions & 0 deletions docs/source/api/python/mantid/testing/assert_almost_equal.rst
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`
13 changes: 13 additions & 0 deletions docs/source/api/python/mantid/testing/index.rst
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

*
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.

0 comments on commit 7680bac

Please sign in to comment.