Skip to content
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

Add test coverage for async slicing of labels #5325

Merged
merged 5 commits into from
Sep 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Comment on lines +143 to +146
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this protocol is only really used by images and labels? Otherwise this does not really hold true for layers that accept tuples like surface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This change was needed because Labels also needs ndim in the test case here, so (at most) LayerDataProtocol only supports Image right now.

I like the intent of it and it's often useful for prototyping and testing, but would be great to give it a more thought and care. Outside of this PR though ;)


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