Skip to content

Commit

Permalink
Fix handling of units and axis_labels in add_image (napari#7089)
Browse files Browse the repository at this point in the history
# Description

The new `axis_labels` and `units` arguments for layers are iterables;
they need to be added to the iterable arguments to avoid problems with
the channel splitting in the case of an image stack.

Currently, this fails:

```py
import napari
import numpy as np
v = napari.Viewer()
a = np.random.rand(2, 2)
v.add_image(a, axis_labels=('x', 'y'))
```

with:

```
TypeError: Received sequence for argument 'axis_labels', did you mean to specify a 'channel_axis'?
```
  • Loading branch information
brisvag authored Jul 16, 2024
1 parent c51e291 commit 5e685a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions napari/components/_tests/test_viewer_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ def test_add_image_colormap_variants():
assert "did you mean to specify a 'channel_axis'" in str(err.value)


def test_add_image_accepts_all_arguments_as_sequence():
"""See https://github.com/napari/napari/pull/7089."""
viewer = ViewerModel(ndisplay=3)
img = viewer.add_image(np.random.rand(2, 2))
viewer.add_image(**img._get_state())


def test_add_volume():
"""Test adding volume."""
viewer = ViewerModel(ndisplay=3)
Expand Down
2 changes: 2 additions & 0 deletions napari/components/viewer_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,8 @@ def add_image(
'metadata',
'experimental_clipping_planes',
'custom_interpolation_kernel_2d',
'axis_labels',
'units',
}

if channel_axis is None:
Expand Down

0 comments on commit 5e685a2

Please sign in to comment.