From 565f659dfe60315e30e6d2d024eb02f56adc5423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Althviz=20Mor=C3=A9?= <16781833+dalthviz@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:27:15 -0500 Subject: [PATCH] Initialize ndim value for Points layer using data shape if available (#6593) # References and relevant issues Closes #6587 # Description Add handling for layer dimensionality initialization by using passed data shape value if available to set `ndim` when kwarg is not passed --- napari/layers/points/_tests/test_points.py | 6 ++++++ napari/layers/points/points.py | 11 +++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/napari/layers/points/_tests/test_points.py b/napari/layers/points/_tests/test_points.py index 00279b0420f..5d691ac0dc4 100644 --- a/napari/layers/points/_tests/test_points.py +++ b/napari/layers/points/_tests/test_points.py @@ -49,6 +49,12 @@ def _make_cycled_properties(values, length): def test_empty_points(): pts = Points() assert pts.data.shape == (0, 2) + assert pts.ndim == 2 + + +def test_3d_empty_points(): + pts = Points(np.empty((0, 3))) + assert pts.ndim == 3 def test_empty_points_with_features(): diff --git a/napari/layers/points/points.py b/napari/layers/points/points.py index 37f3d65845c..44f31f26013 100644 --- a/napari/layers/points/points.py +++ b/napari/layers/points/points.py @@ -378,8 +378,15 @@ def __init__( shown=True, projection_mode='none', ) -> None: - if ndim is None and scale is not None: - ndim = len(scale) + if ndim is None: + if scale is not None: + ndim = len(scale) + elif ( + data is not None + and hasattr(data, 'shape') + and len(data.shape) == 2 + ): + ndim = data.shape[1] data, ndim = fix_data_points(data, ndim)