Skip to content

Do not treat Ellipsis or None as a floating point slice - #278

Open
aaronkanzer wants to merge 1 commit into
nexpy:mainfrom
aaronkanzer:fix/ellipsis-index
Open

aaronkanzer wants to merge 1 commit into
nexpy:mainfrom
aaronkanzer:fix/ellipsis-index

Conversation

@aaronkanzer

Copy link
Copy Markdown

Indexing a multidimensional NXfield with ... or None (np.newaxis) raises an error, whether the field is in memory or loaded from a file.

import numpy as np
from nexusformat.nexus import NXfield

x = NXfield(np.arange(20).reshape(4, 5))
x[..., -1]

Observed: NeXusError: NXfield must be one-dimensional for floating point slices. x[0, ...], x[None, 0] and x[:, None] fail the same way.

Expected: [4 9 14 19], the same as NumPy.

Cause: is_real_slice checks each index element with np.array(x).dtype. np.array(Ellipsis) and np.array(None) are object arrays, so they count as real values, and convert_index then rejects the 2D field.

Fix: return False for Ellipsis and None in the inner is_real check. A parametrized test covers ... and np.newaxis on 2D and 3D fields.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant