diff --git a/src/napari_matplotlib/tests/helpers.py b/src/napari_matplotlib/tests/helpers.py new file mode 100644 index 00000000..15259f54 --- /dev/null +++ b/src/napari_matplotlib/tests/helpers.py @@ -0,0 +1,29 @@ +from io import BytesIO + +import numpy as np +import numpy.typing as npt +import pytest +from matplotlib.figure import Figure + + +def fig_to_array(fig: Figure) -> npt.NDArray[np.uint8]: + """ + Convert a figure to an RGB array. + """ + with BytesIO() as io_buf: + fig.savefig(io_buf, format="raw") + io_buf.seek(0) + img_arr = np.reshape( + np.frombuffer(io_buf.getvalue(), dtype=np.uint8), + newshape=(int(fig.bbox.bounds[3]), int(fig.bbox.bounds[2]), -1), + ) + return img_arr + + +def assert_figures_equal(fig1: Figure, fig2: Figure) -> None: + np.testing.assert_equal(fig_to_array(fig1), fig_to_array(fig2)) + + +def assert_figures_not_equal(fig1: Figure, fig2: Figure) -> None: + with pytest.raises(AssertionError, match="Arrays are not equal"): + assert_figures_equal(fig1, fig2) diff --git a/src/napari_matplotlib/tests/test_histogram.py b/src/napari_matplotlib/tests/test_histogram.py index e397029c..14375887 100644 --- a/src/napari_matplotlib/tests/test_histogram.py +++ b/src/napari_matplotlib/tests/test_histogram.py @@ -3,11 +3,14 @@ import pytest from napari_matplotlib import HistogramWidget +from napari_matplotlib.tests.helpers import ( + assert_figures_equal, + assert_figures_not_equal, +) @pytest.mark.mpl_image_compare def test_histogram_2D(make_napari_viewer, astronaut_data): - # Smoke test adding a histogram widget viewer = make_napari_viewer() viewer.add_image(astronaut_data[0], **astronaut_data[1]) fig = HistogramWidget(viewer).figure @@ -18,10 +21,32 @@ def test_histogram_2D(make_napari_viewer, astronaut_data): @pytest.mark.mpl_image_compare def test_histogram_3D(make_napari_viewer, brain_data): - # Smoke test adding a histogram widget viewer = make_napari_viewer() viewer.add_image(brain_data[0], **brain_data[1]) fig = HistogramWidget(viewer).figure # Need to return a copy, as original figure is too eagerley garbage # collected by the widget return deepcopy(fig) + + +def test_change_layer(make_napari_viewer, brain_data, astronaut_data): + viewer = make_napari_viewer() + widget = HistogramWidget(viewer) + + viewer.add_image(brain_data[0], **brain_data[1]) + viewer.add_image(astronaut_data[0], **astronaut_data[1]) + + # Select first layer + viewer.layers.selection.clear() + viewer.layers.selection.add(viewer.layers[0]) + fig1 = deepcopy(widget.figure) + + # Re-selecting first layer should produce identical plot + viewer.layers.selection.clear() + viewer.layers.selection.add(viewer.layers[0]) + assert_figures_equal(widget.figure, fig1) + + # Plotting the second layer should produce a different plot + viewer.layers.selection.clear() + viewer.layers.selection.add(viewer.layers[1]) + assert_figures_not_equal(widget.figure, fig1) 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