From 014e34f1cebc9c3d755b65bdce98fafe33876d24 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 1 Jun 2023 13:49:45 +0100 Subject: [PATCH 1/4] Remove old smoke test comments --- src/napari_matplotlib/tests/test_histogram.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/napari_matplotlib/tests/test_histogram.py b/src/napari_matplotlib/tests/test_histogram.py index e397029c..e836f829 100644 --- a/src/napari_matplotlib/tests/test_histogram.py +++ b/src/napari_matplotlib/tests/test_histogram.py @@ -7,7 +7,6 @@ @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,7 +17,6 @@ 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 From b5dfebcb513fe1b19e2ddae52cb3e82f1e9c7c0a Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 1 Jun 2023 14:10:17 +0100 Subject: [PATCH 2/4] Add histogram test for changing layers --- src/napari_matplotlib/tests/helpers.py | 29 +++++++++++++++++++ src/napari_matplotlib/tests/test_histogram.py | 27 +++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/napari_matplotlib/tests/helpers.py diff --git a/src/napari_matplotlib/tests/helpers.py b/src/napari_matplotlib/tests/helpers.py new file mode 100644 index 00000000..05c99f9a --- /dev/null +++ b/src/napari_matplotlib/tests/helpers.py @@ -0,0 +1,29 @@ +from io import BytesIO + +import numpy as np +from matplotlib.figure import Figure + +import pytest + + +def fig_to_array(fig: Figure) -> np.ndarray: + """ + 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 e836f829..14375887 100644 --- a/src/napari_matplotlib/tests/test_histogram.py +++ b/src/napari_matplotlib/tests/test_histogram.py @@ -3,6 +3,10 @@ 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 @@ -23,3 +27,26 @@ def test_histogram_3D(make_napari_viewer, brain_data): # 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) From 015d23cb96de189cf4d68a6212d657a77f81dad4 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 1 Jun 2023 14:15:44 +0100 Subject: [PATCH 3/4] Make pre-commit happy --- src/napari_matplotlib/tests/helpers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/napari_matplotlib/tests/helpers.py b/src/napari_matplotlib/tests/helpers.py index 05c99f9a..fb9317c0 100644 --- a/src/napari_matplotlib/tests/helpers.py +++ b/src/napari_matplotlib/tests/helpers.py @@ -1,9 +1,8 @@ from io import BytesIO import numpy as np -from matplotlib.figure import Figure - import pytest +from matplotlib.figure import Figure def fig_to_array(fig: Figure) -> np.ndarray: From 9f388bb13492b8c1f858079d54e7a9cf936fb0be Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 1 Jun 2023 14:16:22 +0100 Subject: [PATCH 4/4] Fix array typing --- src/napari_matplotlib/tests/helpers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/napari_matplotlib/tests/helpers.py b/src/napari_matplotlib/tests/helpers.py index fb9317c0..15259f54 100644 --- a/src/napari_matplotlib/tests/helpers.py +++ b/src/napari_matplotlib/tests/helpers.py @@ -1,11 +1,12 @@ 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) -> np.ndarray: +def fig_to_array(fig: Figure) -> npt.NDArray[np.uint8]: """ Convert a figure to an RGB array. """ 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