Skip to content

Add test that figure is updated when layer change for HistogramWidget #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/napari_matplotlib/tests/helpers.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 27 additions & 2 deletions src/napari_matplotlib/tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)