From 7c71b5ca237388afea41af90139285216b4bc61a Mon Sep 17 00:00:00 2001 From: aaronkanzer Date: Sat, 24 Oct 2026 07:58:56 -0400 Subject: [PATCH] Do not treat Ellipsis or None as a floating point slice NXfield indices such as field[..., -1] or field[None, 0] on a multidimensional field raised 'NXfield must be one-dimensional for floating point slices', because is_real_slice converted Ellipsis and None to object arrays, which are not integer dtypes. --- src/nexusformat/nexus/tree.py | 4 +++- tests/test_fields.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/nexusformat/nexus/tree.py b/src/nexusformat/nexus/tree.py index 79bce50b..5b8d3574 100644 --- a/src/nexusformat/nexus/tree.py +++ b/src/nexusformat/nexus/tree.py @@ -8113,7 +8113,9 @@ def is_real_slice(idx): """True if the slice contains real values.""" def is_real(x): - if isinstance(x, slice): + if x is None or x is Ellipsis: + return False + elif isinstance(x, slice): x = [x if x is not None else 0 for x in [x.start, x.stop, x.step]] x = np.array(x) return not (np.issubdtype(x.dtype, np.integer) or x.dtype == bool) diff --git a/tests/test_fields.py b/tests/test_fields.py index 191c4698..fc5fe43a 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -108,6 +108,21 @@ def test_field_slice(arr, idx, request): assert field[idx].shape == arr[idx].shape +@pytest.mark.parametrize( + "arr,idx", [("arr2D", np.s_[..., -1]), + ("arr2D", np.s_[0, ...]), + ("arr2D", np.s_[None, 0]), + ("arr3D", np.s_[1, ..., 2:4]), + ("arr3D", np.s_[:, np.newaxis, 0])]) +def test_field_ellipsis_and_newaxis_slice(arr, idx, request): + + arr = request.getfixturevalue(arr) + field = NXfield(arr) + + assert np.array_equal(field[idx].nxvalue, arr[idx]) + assert field[idx].shape == arr[idx].shape + + def test_field_index(arr1D): field = NXfield(2*arr1D)