Skip to content

Commit

Permalink
Add test coverage for async slicing of labels (#5325)
Browse files Browse the repository at this point in the history
# References and relevant issues
Part of #4795.

# Description
Adds test coverage to check that the layer slicer handles labels layers
appropriately.

---------

Co-authored-by: Andy Sweet <andrew.d.sweet@gmail.com>
  • Loading branch information
kcpevey and andy-sweet committed Sep 5, 2023
1 parent d58ff84 commit 16ea9d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions napari/_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ def dtype(self) -> DTypeLike:
def shape(self) -> Tuple[int, ...]:
return self.data.shape

@property
def ndim(self) -> int:
# LayerDataProtocol does not have ndim, but this should be equivalent.
return len(self.data.shape)

def __getitem__(
self, key: Union[Index, Tuple[Index, ...], LayerDataProtocol]
) -> LayerDataProtocol:
Expand Down
22 changes: 21 additions & 1 deletion napari/components/_tests/test_layer_slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from napari._tests.utils import DEFAULT_TIMEOUT_SECS, LockableData
from napari.components import Dims
from napari.components._layer_slicer import _LayerSlicer
from napari.layers import Image, Points
from napari.layers import Image, Labels, Points

# The following fakes are used to control execution of slicing across
# multiple threads, while also allowing us to mimic real classes
Expand Down Expand Up @@ -336,6 +336,26 @@ def test_submit_with_one_3d_image(layer_slicer):
np.testing.assert_equal(layer_result.image.view, data[2, :, :])


def test_submit_with_3d_labels(layer_slicer):
np.random.seed(0)
data = np.random.randint(20, size=(8, 7, 6))
lockable_data = LockableData(data)
layer = Labels(lockable_data, multiscale=False)
dims = Dims(
ndim=3,
ndisplay=2,
range=((0, 8, 1), (0, 7, 1), (0, 6, 1)),
point=(2, 0, 0),
)

with lockable_data.lock:
future = layer_slicer.submit(layers=[layer], dims=dims)
assert not future.done()

layer_result = _wait_for_response(future)[layer]
np.testing.assert_equal(layer_result.image.view, data[2, :, :])


def test_submit_with_one_3d_points(layer_slicer):
"""ensure that async slicing of points does not block"""
np.random.seed(0)
Expand Down

0 comments on commit 16ea9d1

Please sign in to comment.