Skip to content

Commit

Permalink
Fixing data_setitem function of label layer (#6618)
Browse files Browse the repository at this point in the history
Labels layer bug fix. Adapting the test now

# References and relevant issues
This PR closes an new issue that hasn't been noticed yet.

# Description
This fixes a bug. The following error arises if the "value" input is an
array and there are unchanged values in the indices.
*** ValueError: shape mismatch: value array of shape (855416,) could not
be broadcast to indexing result of shape (88060,)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
leopold-franz and pre-commit-ci[bot] committed Jan 27, 2024
1 parent 737ae89 commit 56f2c71
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion napari/layers/labels/_tests/test_labels.py
Expand Up @@ -701,7 +701,8 @@ def test_data_setitem_multi_dim():
data = zarr.zeros((10, 10, 10), chunks=(5, 5, 5), dtype=np.uint32)
labels = Labels(data)
labels.data_setitem(
(np.array([0, 1]), np.array([1, 1]), np.array([0, 0])), [1, 2]
(np.array([0, 1, 1]), np.array([1, 1, 2]), np.array([0, 0, 0])),
[1, 2, 0],
)


Expand Down
4 changes: 4 additions & 0 deletions napari/layers/labels/labels.py
Expand Up @@ -1578,6 +1578,10 @@ def data_setitem(self, indices, value, refresh=True):
else:
value = self._slice.image.raw.dtype.type(value)

# Resize value array to remove unchanged elements
if isinstance(value, np.ndarray):
value = value[changed_indices]

if not indices or indices[0].size == 0:
return

Expand Down

0 comments on commit 56f2c71

Please sign in to comment.