forked from matplotlib/napari-matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_layer_changes.py
More file actions
107 lines (92 loc) · 3.23 KB
/
test_layer_changes.py
File metadata and controls
107 lines (92 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from copy import deepcopy
from typing import Any
import numpy as np
import numpy.typing as npt
import pytest
from napari.viewer import Viewer
from napari_matplotlib import (
FeaturesScatterWidget,
HistogramWidget,
ScatterWidget,
SliceWidget,
)
from napari_matplotlib.base import NapariMPLWidget
from napari_matplotlib.tests.helpers import (
assert_figures_equal,
assert_figures_not_equal,
)
@pytest.mark.parametrize(
"widget_cls, n_layers",
[(HistogramWidget, 1), (SliceWidget, 1), (ScatterWidget, 2)],
)
def test_change_one_layer(
make_napari_viewer,
brain_data,
astronaut_data,
widget_cls,
n_layers,
):
"""
Test all widgets that take one layer as input to make sure the plot changes
when the napari layer selection changes.
"""
viewer = make_napari_viewer()
widget = widget_cls(viewer)
# Add n copies of two different datasets
for _ in range(n_layers):
viewer.add_image(brain_data[0], **brain_data[1])
for _ in range(n_layers):
viewer.add_image(astronaut_data[0], **astronaut_data[1])
assert len(viewer.layers) == 2 * n_layers
assert_plot_changes(viewer, widget, n_layers=n_layers)
@pytest.mark.parametrize("widget_cls", [FeaturesScatterWidget])
def test_change_features_layer(
make_napari_viewer, points_with_features_data, widget_cls
):
"""
Test all widgets that take one layer with features as input to make sure the
plot changes when the napari layer selection changes.
"""
viewer = make_napari_viewer()
assert_features_plot_changes(viewer, widget_cls, points_with_features_data)
def assert_features_plot_changes(
viewer: Viewer,
widget_cls: type[NapariMPLWidget],
data: tuple[npt.NDArray[np.generic], dict[str, Any]],
) -> None:
"""
When the selected layer is changed, make sure the plot generated
by `widget_cls` also changes.
"""
widget = widget_cls(viewer)
viewer.add_points(data[0], **data[1])
# Change the features data for the second layer
data[1]["features"] = {name: data + 1 for name, data in data[1]["features"].items()}
viewer.add_points(data[0], **data[1])
assert_plot_changes(viewer, widget, n_layers=1)
def assert_plot_changes(
viewer: Viewer, widget: NapariMPLWidget, *, n_layers: int
) -> None:
"""
Assert that a widget plot changes when the layer selection
is changed. The passed viewer must already have (2 * n_layers) layers
loaded.
"""
# Select first layer(s)
viewer.layers.selection.clear()
for i in range(n_layers):
viewer.layers.selection.add(viewer.layers[i])
assert len(viewer.layers.selection) == n_layers
fig1 = deepcopy(widget.figure)
# Re-selecting first layer(s) should produce identical plot
viewer.layers.selection.clear()
for i in range(n_layers):
viewer.layers.selection.add(viewer.layers[i])
assert len(viewer.layers.selection) == n_layers
assert_figures_equal(widget.figure, fig1)
# Plotting the second layer(s) should produce a different plot
viewer.layers.selection.clear()
for i in range(n_layers):
viewer.layers.selection.add(viewer.layers[n_layers + i])
assert len(viewer.layers.selection) == n_layers
assert_figures_not_equal(widget.figure, fig1)