hyperspy.misc.test_utils module

hyperspy.misc.test_utils.all_warnings()

Context for use in testing to ensure that all warnings are raised. .. rubric:: Examples

>>> import warnings
>>> def foo():
...     warnings.warn(RuntimeWarning("bar"))
We raise the warning once, while the warning filter is set to "once".
Hereafter, the warning is invisible, even with custom filters:
>>> with warnings.catch_warnings():
...     warnings.simplefilter('once')
...     foo()
We can now run ``foo()`` without a warning being raised:
>>> from numpy.testing import assert_warns
>>> foo()
To catch the warning, we call in the help of ``all_warnings``:
>>> with all_warnings():
...     assert_warns(RuntimeWarning, foo)
hyperspy.misc.test_utils.assert_deep_almost_equal(actual, expected, *args, **kwargs)

Assert that two complex structures have almost equal contents. Compares lists, dicts and tuples recursively. Checks numeric values using assert_allclose() and checks all other values with assert_equal(). Accepts additional positional and keyword arguments and pass those intact to assert_allclose() (that’s how you specify comparison precision). :param actual: :type actual: lists, dicts or tuples :param expected: :type expected: lists, dicts or tuples

hyperspy.misc.test_utils.assert_warns(message=None, category=None)

Context for use in testing to catch known warnings matching regexes

Parameters:
  • message ((list of) strings or compiled regexes) – Regexes for the desired warning to catch
  • category (type or list of types) – Warning categories for the desired warning to catch

Examples

>>> from skimage import data, img_as_ubyte, img_as_float
>>> with assert_warns(['precision loss']):
...     d = img_as_ubyte(img_as_float(data.coins()))

Notes

Upon exiting, it checks the recorded warnings for the desired matching pattern(s). Raises a ValueError if any match was not found or an unexpected warning was raised. Allows for three types of behaviors: “and”, “or”, and “optional” matches. This is done to accomodate different build enviroments or loop conditions that may produce different warnings. The behaviors can be combined. If you pass multiple patterns, you get an orderless “and”, where all of the warnings must be raised. If you use the “|” operator in a pattern, you can catch one of several warnings. Finally, you can use “|AZ” in a pattern to signify it as optional.

hyperspy.misc.test_utils.check_running_tests_in_CI()
hyperspy.misc.test_utils.ignore_warning(message='', category=None)
hyperspy.misc.test_utils.sanitize_dict(dictionary)