Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1385b5d

Browse filesBrowse files
authored
Merge pull request #159 from dstansby/features-layer-change-test
Features layer change test
2 parents 8344154 + 267526e commit 1385b5d
Copy full SHA for 1385b5d

File tree

Expand file treeCollapse file tree

3 files changed

+66
-17
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+66
-17
lines changed

‎src/napari_matplotlib/tests/conftest.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/tests/conftest.py
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ def brain_data():
2626
return data.brain(), {"rgb": False}
2727

2828

29+
@pytest.fixture
30+
def points_with_features_data():
31+
n_points = 100
32+
np.random.seed(10)
33+
points_data = 100 * np.random.random((100, 2))
34+
points_features = {
35+
"feature_0": np.random.random((n_points,)),
36+
"feature_1": np.random.random((n_points,)),
37+
"feature_2": np.random.random((n_points,)),
38+
}
39+
40+
return points_data, {"features": points_features}
41+
42+
2943
@pytest.fixture(autouse=True, scope="session")
3044
def set_strict_qt():
3145
env_var = "NAPARI_STRICT_QT"

‎src/napari_matplotlib/tests/scatter/test_scatter_features.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/tests/scatter/test_scatter_features.py
+8-15Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,22 @@
99

1010

1111
@pytest.mark.mpl_image_compare
12-
def test_features_scatter_widget_2D(make_napari_viewer):
12+
def test_features_scatter_widget_2D(
13+
make_napari_viewer, points_with_features_data
14+
):
1315
viewer = make_napari_viewer()
1416
viewer.theme = "light"
1517
widget = FeaturesScatterWidget(viewer)
1618

17-
# make the points data
18-
n_points = 100
19-
np.random.seed(10)
20-
points_data = 100 * np.random.random((100, 2))
21-
points_features = {
22-
"feature_0": np.random.random((n_points,)),
23-
"feature_1": np.random.random((n_points,)),
24-
"feature_2": np.random.random((n_points,)),
25-
}
26-
27-
viewer.add_points(points_data, features=points_features)
19+
viewer.add_points(
20+
points_with_features_data[0], **points_with_features_data[1]
21+
)
22+
assert len(viewer.layers) == 1
2823
# De-select existing selection
2924
viewer.layers.selection.clear()
3025

3126
# Select points data and chosen features
32-
viewer.layers.selection.add(
33-
viewer.layers["points_data"]
34-
) # images need to be selected
27+
viewer.layers.selection.add(viewer.layers[0]) # images need to be selected
3528
widget.x_axis_key = "feature_0"
3629
widget.y_axis_key = "feature_1"
3730

‎src/napari_matplotlib/tests/test_layer_changes.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/tests/test_layer_changes.py
+44-2Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import pytest
77
from napari.viewer import Viewer
88

9-
from napari_matplotlib import HistogramWidget, SliceWidget
9+
from napari_matplotlib import (
10+
FeaturesScatterWidget,
11+
HistogramWidget,
12+
SliceWidget,
13+
)
1014
from napari_matplotlib.base import NapariMPLWidget
1115
from napari_matplotlib.tests.helpers import (
1216
assert_figures_equal,
@@ -39,10 +43,48 @@ def assert_one_layer_plot_changes(
3943
by `widget_cls` also changes.
4044
"""
4145
widget = widget_cls(viewer)
42-
4346
viewer.add_image(data1[0], **data1[1])
4447
viewer.add_image(data2[0], **data2[1])
48+
assert_plot_changes(viewer, widget)
49+
4550

51+
@pytest.mark.parametrize("widget_cls", [FeaturesScatterWidget])
52+
def test_change_features_layer(
53+
make_napari_viewer, points_with_features_data, widget_cls
54+
):
55+
"""
56+
Test all widgets that take one layer with features as input to make sure the
57+
plot changes when the napari layer selection changes.
58+
"""
59+
viewer = make_napari_viewer()
60+
assert_features_plot_changes(viewer, widget_cls, points_with_features_data)
61+
62+
63+
def assert_features_plot_changes(
64+
viewer: Viewer,
65+
widget_cls: Type[NapariMPLWidget],
66+
data: Tuple[npt.NDArray[np.generic], Dict[str, Any]],
67+
) -> None:
68+
"""
69+
When the selected layer is changed, make sure the plot generated
70+
by `widget_cls` also changes.
71+
"""
72+
widget = widget_cls(viewer)
73+
viewer.add_points(data[0], **data[1])
74+
# Change the features data for the second layer
75+
data[1]["features"] = {
76+
name: data + 1 for name, data in data[1]["features"].items()
77+
}
78+
viewer.add_points(data[0], **data[1])
79+
assert_plot_changes(viewer, widget)
80+
81+
82+
def assert_plot_changes(viewer: Viewer, widget: NapariMPLWidget) -> None:
83+
"""
84+
Assert that a widget plot changes when the layer selection
85+
is changed. The passed viewer must already have two layers
86+
loaded.
87+
"""
4688
# Select first layer
4789
viewer.layers.selection.clear()
4890
viewer.layers.selection.add(viewer.layers[0])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.