Skip to content

Fix slicing 2D images #205

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 7 commits into from
Aug 23, 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
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Changelog
=========
1.0.3
-----
Bug fixes
~~~~~~~~~
- Fixed creating 1D slices of 2D images.

1.0.2
-----
Bug fixes
Expand Down
37 changes: 27 additions & 10 deletions src/napari_matplotlib/slice.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Optional, Tuple
from typing import Any, Dict, List, Optional, Tuple

import matplotlib.ticker as mticker
import napari
Expand All @@ -12,7 +12,6 @@
__all__ = ["SliceWidget"]

_dims_sel = ["x", "y"]
_dims = ["x", "y", "z"]


class SliceWidget(SingleAxesWidget):
Expand All @@ -37,7 +36,7 @@ def __init__(
self.dim_selector = QComboBox()
button_layout.addWidget(QLabel("Slice axis:"))
button_layout.addWidget(self.dim_selector)
self.dim_selector.addItems(_dims)
self.dim_selector.addItems(["x", "y", "z"])

self.slice_selectors = {}
for d in _dims_sel:
Expand All @@ -61,7 +60,7 @@ def _layer(self) -> napari.layers.Layer:
return self.layers[0]

@property
def current_dim(self) -> str:
def current_dim_name(self) -> str:
"""
Currently selected slice dimension.
"""
Expand All @@ -74,32 +73,50 @@ def current_dim_index(self) -> int:
"""
# Note the reversed list because in napari the z-axis is the first
# numpy axis
return _dims[::-1].index(self.current_dim)
return self._dim_names[::-1].index(self.current_dim_name)

@property
def _dim_names(self) -> List[str]:
"""
List of dimension names. This is a property as it varies depending on the
dimensionality of the currently selected data.
"""
if self._layer.data.ndim == 2:
return ["x", "y"]
elif self._layer.data.ndim == 3:
return ["x", "y", "z"]
else:
raise RuntimeError("Don't know how to handle ndim != 2 or 3")

@property
def _selector_values(self) -> Dict[str, int]:
"""
Values of the slice selectors.

Mapping from dimension name to value.
"""
return {d: self.slice_selectors[d].value() for d in _dims_sel}

def _get_xy(self) -> Tuple[npt.NDArray[Any], npt.NDArray[Any]]:
"""
Get data for plotting.
"""
x = np.arange(self._layer.data.shape[self.current_dim_index])
dim_index = self.current_dim_index
if self._layer.data.ndim == 2:
dim_index -= 1
x = np.arange(self._layer.data.shape[dim_index])

vals = self._selector_values
vals.update({"z": self.current_z})

slices = []
for d in _dims:
if d == self.current_dim:
for dim_name in self._dim_names:
if dim_name == self.current_dim_name:
# Select all data along this axis
slices.append(slice(None))
else:
# Select specific index
val = vals[d]
val = vals[dim_name]
slices.append(slice(val, val + 1))

# Reverse since z is the first axis in napari
Expand All @@ -115,7 +132,7 @@ def draw(self) -> None:
x, y = self._get_xy()

self.axes.plot(x, y)
self.axes.set_xlabel(self.current_dim)
self.axes.set_xlabel(self.current_dim_name)
self.axes.set_title(self._layer.name)
# Make sure all ticks lie on integer values
self.axes.xaxis.set_major_locator(
Expand Down
Binary file modified src/napari_matplotlib/tests/baseline/test_slice_2D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/napari_matplotlib/tests/baseline/test_slice_3D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 12 additions & 3 deletions src/napari_matplotlib/tests/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
def test_slice_3D(make_napari_viewer, brain_data):
viewer = make_napari_viewer()
viewer.theme = "light"
viewer.add_image(brain_data[0], **brain_data[1])

data = brain_data[0]
assert data.ndim == 3, data.shape
viewer.add_image(data, **brain_data[1])

axis = viewer.dims.last_used
slice_no = brain_data[0].shape[0] - 1
slice_no = data.shape[0] - 1
viewer.dims.set_current_step(axis, slice_no)
fig = SliceWidget(viewer).figure
# Need to return a copy, as original figure is too eagerley garbage
Expand All @@ -23,7 +27,12 @@ def test_slice_3D(make_napari_viewer, brain_data):
def test_slice_2D(make_napari_viewer, astronaut_data):
viewer = make_napari_viewer()
viewer.theme = "light"
viewer.add_image(astronaut_data[0], **astronaut_data[1])

# Take first RGB channel
data = astronaut_data[0][:, :, 0]
assert data.ndim == 2, data.shape
viewer.add_image(data)

fig = SliceWidget(viewer).figure
# Need to return a copy, as original figure is too eagerley garbage
# collected by the widget
Expand Down