diff --git a/docs/changelog.rst b/docs/changelog.rst index 103cd80e..f7425bfe 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,5 +1,11 @@ Changelog ========= +1.0.3 +----- +Bug fixes +~~~~~~~~~ +- Fixed creating 1D slices of 2D images. + 1.0.2 ----- Bug fixes diff --git a/src/napari_matplotlib/slice.py b/src/napari_matplotlib/slice.py index e3aa80b2..f0d01f3f 100644 --- a/src/napari_matplotlib/slice.py +++ b/src/napari_matplotlib/slice.py @@ -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 @@ -12,7 +12,6 @@ __all__ = ["SliceWidget"] _dims_sel = ["x", "y"] -_dims = ["x", "y", "z"] class SliceWidget(SingleAxesWidget): @@ -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: @@ -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. """ @@ -74,12 +73,27 @@ 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} @@ -87,19 +101,22 @@ 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 @@ -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( diff --git a/src/napari_matplotlib/tests/baseline/test_slice_2D.png b/src/napari_matplotlib/tests/baseline/test_slice_2D.png index 639f25b8..ee3ce3b6 100644 Binary files a/src/napari_matplotlib/tests/baseline/test_slice_2D.png and b/src/napari_matplotlib/tests/baseline/test_slice_2D.png differ diff --git a/src/napari_matplotlib/tests/baseline/test_slice_3D.png b/src/napari_matplotlib/tests/baseline/test_slice_3D.png index 43c8c3b6..c30211da 100644 Binary files a/src/napari_matplotlib/tests/baseline/test_slice_3D.png and b/src/napari_matplotlib/tests/baseline/test_slice_3D.png differ diff --git a/src/napari_matplotlib/tests/test_slice.py b/src/napari_matplotlib/tests/test_slice.py index 412e71c3..32eb9ad4 100644 --- a/src/napari_matplotlib/tests/test_slice.py +++ b/src/napari_matplotlib/tests/test_slice.py @@ -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 @@ -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