fix: newaxis indexing#151
Draft
terraputix wants to merge 1 commit into
Draft
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes NumPy-style None/np.newaxis indexing for OmFileReader (and the async reader) by tracking NewAxis positions during index parsing and re-inserting size-1 dimensions after applying integer-index squeezing. It also adds a suite of Python tests for basic indexing semantics.
Changes:
- Extend
ArrayIndex::get_ranges_and_squeeze_dimsto returnnewaxis_dimsin addition to read ranges andsqueeze_dims. - Update sync/async readers to apply
newaxis_dimswhen reshaping the returned ndarray. - Add Python indexing tests (integer/slice/ellipsis/negative slice/newaxis/mixed/errors) and additional Rust unit tests for NewAxis parsing.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
tests/test_read_write.py |
Adds comprehensive indexing tests and NewAxis-specific assertions. |
src/reader.rs |
Plumbs newaxis_dims into array read/reshape path; updates an fsspec TypeError message. |
src/reader_async.rs |
Mirrors sync reader changes by passing newaxis_dims into async reshape logic. |
src/array_index.rs |
Enhances index parsing to track NewAxis positions and adjust squeeze/newaxis dims appropriately. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
201
to
+215
| squeeze_dims.sort_by(|a, b| b.cmp(a)); | ||
|
|
||
| Ok((ranges, squeeze_dims)) | ||
| // Adjust squeeze_dims from output space to read-array space: | ||
| // each NewAxis before a squeeze dim shifts it left by 1. | ||
| for pos in squeeze_dims.iter_mut() { | ||
| let shift = newaxis_dims.iter().filter(|&&n| n < *pos).count(); | ||
| *pos -= shift; | ||
| } | ||
|
|
||
| // Adjust newaxis_dims from output space to post-squeeze space: | ||
| // each squeezed dim before a NewAxis shifts it left by 1. | ||
| for pos in newaxis_dims.iter_mut() { | ||
| let shift = squeeze_dims.iter().filter(|&&s| s < *pos).count(); | ||
| *pos -= shift; | ||
| } |
Comment on lines
+510
to
+518
| ref = _ref_data() | ||
| reader = omfiles.OmFileReader(temp_om_file) | ||
| np.testing.assert_array_equal(reader[None], ref[None]) | ||
| np.testing.assert_array_equal(reader[None, :3], ref[None, :3]) | ||
| np.testing.assert_array_equal(reader[1, None], ref[1, None]) | ||
| np.testing.assert_array_equal(reader[None, 1], ref[None, 1]) | ||
| np.testing.assert_array_equal(reader[None, None], ref[None, None]) | ||
| np.testing.assert_array_equal(reader[..., None], ref[..., None]) | ||
| np.testing.assert_array_equal(reader[None, ...], ref[None, ...]) |
Comment on lines
+520
to
+522
|
|
||
|
|
||
| def test_indexing_mixed(temp_om_file): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.