Skip to content

Commit 7b018f5

Browse files
authored
Merge pull request #146 from dstansby/layer-changed-test
Add test that figure is updated when layer change for HistogramWidget
2 parents 9598eeb + 9f388bb commit 7b018f5

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from io import BytesIO
2+
3+
import numpy as np
4+
import numpy.typing as npt
5+
import pytest
6+
from matplotlib.figure import Figure
7+
8+
9+
def fig_to_array(fig: Figure) -> npt.NDArray[np.uint8]:
10+
"""
11+
Convert a figure to an RGB array.
12+
"""
13+
with BytesIO() as io_buf:
14+
fig.savefig(io_buf, format="raw")
15+
io_buf.seek(0)
16+
img_arr = np.reshape(
17+
np.frombuffer(io_buf.getvalue(), dtype=np.uint8),
18+
newshape=(int(fig.bbox.bounds[3]), int(fig.bbox.bounds[2]), -1),
19+
)
20+
return img_arr
21+
22+
23+
def assert_figures_equal(fig1: Figure, fig2: Figure) -> None:
24+
np.testing.assert_equal(fig_to_array(fig1), fig_to_array(fig2))
25+
26+
27+
def assert_figures_not_equal(fig1: Figure, fig2: Figure) -> None:
28+
with pytest.raises(AssertionError, match="Arrays are not equal"):
29+
assert_figures_equal(fig1, fig2)

src/napari_matplotlib/tests/test_histogram.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import pytest
44

55
from napari_matplotlib import HistogramWidget
6+
from napari_matplotlib.tests.helpers import (
7+
assert_figures_equal,
8+
assert_figures_not_equal,
9+
)
610

711

812
@pytest.mark.mpl_image_compare
913
def test_histogram_2D(make_napari_viewer, astronaut_data):
10-
# Smoke test adding a histogram widget
1114
viewer = make_napari_viewer()
1215
viewer.add_image(astronaut_data[0], **astronaut_data[1])
1316
fig = HistogramWidget(viewer).figure
@@ -18,10 +21,32 @@ def test_histogram_2D(make_napari_viewer, astronaut_data):
1821

1922
@pytest.mark.mpl_image_compare
2023
def test_histogram_3D(make_napari_viewer, brain_data):
21-
# Smoke test adding a histogram widget
2224
viewer = make_napari_viewer()
2325
viewer.add_image(brain_data[0], **brain_data[1])
2426
fig = HistogramWidget(viewer).figure
2527
# Need to return a copy, as original figure is too eagerley garbage
2628
# collected by the widget
2729
return deepcopy(fig)
30+
31+
32+
def test_change_layer(make_napari_viewer, brain_data, astronaut_data):
33+
viewer = make_napari_viewer()
34+
widget = HistogramWidget(viewer)
35+
36+
viewer.add_image(brain_data[0], **brain_data[1])
37+
viewer.add_image(astronaut_data[0], **astronaut_data[1])
38+
39+
# Select first layer
40+
viewer.layers.selection.clear()
41+
viewer.layers.selection.add(viewer.layers[0])
42+
fig1 = deepcopy(widget.figure)
43+
44+
# Re-selecting first layer should produce identical plot
45+
viewer.layers.selection.clear()
46+
viewer.layers.selection.add(viewer.layers[0])
47+
assert_figures_equal(widget.figure, fig1)
48+
49+
# Plotting the second layer should produce a different plot
50+
viewer.layers.selection.clear()
51+
viewer.layers.selection.add(viewer.layers[1])
52+
assert_figures_not_equal(widget.figure, fig1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy