Skip to content

Commit

Permalink
Improve coverage and remove unused code and make spike removal test m…
Browse files Browse the repository at this point in the history
…ore robust to noise
  • Loading branch information
ericpre committed Feb 3, 2024
1 parent 1ce59c3 commit 34f1872
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 56 deletions.
3 changes: 3 additions & 0 deletions hyperspy_gui_ipywidgets/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 16 additions & 5 deletions hyperspy_gui_ipywidgets/tests/test_preferences.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
92 changes: 48 additions & 44 deletions hyperspy_gui_ipywidgets/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
7 changes: 0 additions & 7 deletions hyperspy_gui_ipywidgets/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 34f1872

Please sign in to comment.