Skip to content

Dataset indexing: positive sampling, index lists keep coordinates, keep metadata - #323

Open
cophus wants to merge 2 commits into
electronmicroscopy:devfrom
cophus:getitem-coords
Open

cophus wants to merge 2 commits into
electronmicroscopy:devfrom
cophus:getitem-coords

Conversation

@cophus

@cophus cophus commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Dataset.__getitem__ now keeps every element at its coordinate, origin + i * sampling, for index lists and arrays as well as slices, keeps the sampling positive, and keeps the metadata.

On dev:

  • An index list does not update the origin or sampling. dset[[0, 2, 4]] keeps the old sampling, and dset[[4, 3, 2, 1, 0]] gives a mirrored view.
  • A NumPy index array raises ValueError, because Ellipsis in index compares the array elementwise.
  • A reversed slice gives a negative sampling. This encodes a reflection a second way, next to the scan rotation and transpose (q_to_r_rotation_ccw_deg, q_transpose), and the two can disagree: ptychography uses the sign of the scan sampling, while measure_scan_rotation ignores it.
  • Slicing drops the metadata, so for example dset[10:100, 10:100] loses the scan rotation and transpose, while crop() keeps them.

Changes:

  • An increasing, evenly spaced index list, array or 1D boolean mask is converted to the equivalent slice. A Dataset describes a regular grid, so unevenly spaced, multidimensional or multiple index arrays raise IndexError and point to .array.
  • The sampling stays positive: reversed slices and decreasing index lists raise IndexError. Orientation belongs in the metadata (rotation and transpose for 4D-STEM).
  • The Ellipsis check uses is.
  • The metadata dict is copied to the result. An increasing slice keeps the orientation, so the copied rotation and transpose stay valid.

test_getitem_slice_updates_origin_like_crop is extended with these cases, and it fails on dev.

🤖 Generated with Claude Code

cophus and others added 2 commits November 17, 2026 21:33
An evenly spaced index list, array or 1D boolean mask is converted to the
equivalent slice, so the origin and sampling follow the data as they do for
slices, and dset[::-1], dset[[n-1, ..., 0]] and dset[np.arange(n)[::-1]] give
the same coordinates. Unevenly spaced, multidimensional or multiple index
arrays raise IndexError. The Ellipsis check no longer compares index arrays
elementwise, and the metadata dict is copied to the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reversed slices and decreasing index lists raise IndexError instead of giving
a negative sampling. A negative sampling encodes a reflection a second way,
next to the scan rotation and transpose in the metadata, and the two can
disagree. With increasing slices only, the copied rotation and transpose stay
valid.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cophus cophus changed the title Dataset indexing: index lists keep coordinates, keep metadata Dataset indexing: positive sampling, index lists keep coordinates, keep metadata Nov 18, 2026
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