From 34f187228d4ca6c7524f32cdac202c0c40a2f871 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Sat, 3 Feb 2024 09:57:54 +0000 Subject: [PATCH] Improve coverage and remove unused code and make spike removal test more robust to noise --- hyperspy_gui_ipywidgets/conftest.py | 3 + .../tests/test_preferences.py | 21 ++++- hyperspy_gui_ipywidgets/tests/test_tools.py | 92 ++++++++++--------- hyperspy_gui_ipywidgets/tools.py | 7 -- 4 files changed, 67 insertions(+), 56 deletions(-) diff --git a/hyperspy_gui_ipywidgets/conftest.py b/hyperspy_gui_ipywidgets/conftest.py index 5a3ae99..cb5e793 100755 --- a/hyperspy_gui_ipywidgets/conftest.py +++ b/hyperspy_gui_ipywidgets/conftest.py @@ -5,3 +5,6 @@ hs.preferences.GUIs.enable_traitsui_gui = False hs.preferences.GUIs.enable_ipywidgets_gui = True + +# Use matplotlib fixture to clean up figure, setup backend, etc. +from matplotlib.testing.conftest import mpl_test_settings # noqa: F401 diff --git a/hyperspy_gui_ipywidgets/tests/test_preferences.py b/hyperspy_gui_ipywidgets/tests/test_preferences.py index 98ac22d..bc9f2a3 100644 --- a/hyperspy_gui_ipywidgets/tests/test_preferences.py +++ b/hyperspy_gui_ipywidgets/tests/test_preferences.py @@ -1,18 +1,29 @@ -from numpy.random import random, uniform import ipywidgets +from numpy.random import random, uniform +import pytest import hyperspy.api as hs from hyperspy_gui_ipywidgets.tests.utils import KWARGS -def test_preferences(): - wd = hs.preferences.gui(**KWARGS)["ipywidgets"]["wdict"] +module_list = [hs] +try: + import exspy + module_list.append(exspy) +except Exception: + # exspy is not installed + pass + + +@pytest.mark.parametrize("module", module_list) +def test_preferences(module): + wd = module.preferences.gui(**KWARGS)["ipywidgets"]["wdict"] for tabkey, tabvalue in wd.items(): if tabkey.startswith("tab_"): for key, value in tabvalue.items(): assert getattr( - getattr(hs.preferences, tabkey[4:]), key) == value.value + getattr(module.preferences, tabkey[4:]), key) == value.value value_bk = value.value if isinstance(value, ipywidgets.Checkbox): value.value = not value @@ -26,5 +37,5 @@ def test_preferences(): options = set(value.options) - set(value.value) value.value = options.pop() assert getattr( - getattr(hs.preferences, tabkey[4:]), key) == value.value + getattr(module.preferences, tabkey[4:]), key) == value.value value.value = value_bk diff --git a/hyperspy_gui_ipywidgets/tests/test_tools.py b/hyperspy_gui_ipywidgets/tests/test_tools.py index e6d3f4b..c9b6ffc 100644 --- a/hyperspy_gui_ipywidgets/tests/test_tools.py +++ b/hyperspy_gui_ipywidgets/tests/test_tools.py @@ -123,50 +123,6 @@ def test_remove_background(self): np.testing.assert_allclose(s.data[2:], s2.data[2:], atol=1E-5) np.testing.assert_allclose(np.zeros(2), s2.data[:2]) - # Test will need to be update to be more robust - # for now, mark it as flaky - @pytest.mark.flaky(reruns=3) - def test_spikes_removal_tool(self): - s = hs.signals.Signal1D(np.ones((2, 3, 30))) - # Add three spikes - s.data[1, 0, 1] += 2 - s.data[0, 2, 29] += 1 - s.data[1, 2, 14] += 5 - wd = s.spikes_removal_tool(**KWARGS)["ipywidgets"]["wdict"] - - def next(): - wd["next_button"]._click_handlers(wd["next_button"]) - - def previous(): - wd["previous_button"]._click_handlers(wd["previous_button"]) - - def remove(): - wd["remove_button"]._click_handlers(wd["remove_button"]) - wd["threshold"].value = 1.5 - next() - assert s.axes_manager.indices == (0, 1) - wd["threshold"].value = 0.5 - assert s.axes_manager.indices == (0, 0) - next() - assert s.axes_manager.indices == (2, 0) - next() - assert s.axes_manager.indices == (0, 1) - previous() - assert s.axes_manager.indices == (2, 0) - wd["add_noise"].value = False - remove() - assert s.data[0, 2, 29] == 1 - assert s.axes_manager.indices == (0, 1) - remove() - assert s.data[1, 0, 1] == 1 - assert s.axes_manager.indices == (2, 1) - np.random.seed(1) - wd["add_noise"].value = True - wd["spline_order"].value = 1 - remove() - assert s.data[1, 2, 14] <= 2 - assert s.axes_manager.indices == (0, 0) - def test_constrast_editor(self): # To get this test to work, matplotlib backend needs to set to 'Agg' np.random.seed(1) @@ -272,3 +228,51 @@ def test_calibration_2d(): assert s.axes_manager[1].scale == 0.5 assert s.axes_manager[0].units == "mm" assert s.axes_manager[1].units == "mm" + + +def test_spikes_removal_tool(): + s = hs.signals.Signal1D(np.ones((2, 3, 30))) + s.add_gaussian_noise(std=1, random_state=0) + + # The maximum value that we expect after removing a spikes + max_value_after_spike_removal = 10 + + # Add three spikes + s.data[1, 0, 1] += 40 + s.data[0, 2, 29] += 20 + s.data[1, 2, 14] += 100 + wd = s.spikes_removal_tool(**KWARGS)["ipywidgets"]["wdict"] + + def next(): + wd["next_button"]._click_handlers(wd["next_button"]) + + def previous(): + wd["previous_button"]._click_handlers(wd["previous_button"]) + + def remove(): + wd["remove_button"]._click_handlers(wd["remove_button"]) + wd["threshold"].value = 25 + next() + assert s.axes_manager.indices == (0, 1) + wd["threshold"].value = 15 + assert s.axes_manager.indices == (0, 0) + next() + assert s.axes_manager.indices == (2, 0) + next() + assert s.axes_manager.indices == (0, 1) + previous() + assert s.axes_manager.indices == (2, 0) + wd["add_noise"].value = False + remove() + assert s.data[0, 2, 29] < max_value_after_spike_removal + assert s.axes_manager.indices == (0, 1) + remove() + assert s.data[1, 0, 1] < max_value_after_spike_removal + assert s.axes_manager.indices == (2, 1) + np.random.seed(1) + wd["add_noise"].value = True + wd["spline_order"].value = 1 + remove() + assert s.data[1, 2, 14] < max_value_after_spike_removal + # After going through the whole dataset, come back to (0, 0) position + assert s.axes_manager.indices == (0, 0) diff --git a/hyperspy_gui_ipywidgets/tools.py b/hyperspy_gui_ipywidgets/tools.py index d5fb70c..4c01407 100644 --- a/hyperspy_gui_ipywidgets/tools.py +++ b/hyperspy_gui_ipywidgets/tools.py @@ -813,13 +813,6 @@ def on_remove_clicked(b): remove.on_click(on_remove_clicked) labeled_spline_order = labelme("Spline order", spline_order) - def enable_interpolator_kind(change): - if change.new == "Spline": - for child in labeled_spline_order.children: - child.layout.display = "" - else: - for child in labeled_spline_order.children: - child.layout.display = "none" link((obj, "threshold"), (threshold, "value")) link((obj, "add_noise"), (add_noise, "value")) link((obj, "default_spike_width"),