-
Notifications
You must be signed in to change notification settings - Fork 26
Allow negative slices when indexing chunked data #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
valeriupredoi
merged 13 commits into
NCAS-CMS:main
from
davidhassell:index-negative-step
Jan 22, 2026
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8830c73
negative slices/integers
davidhassell 4712aa9
indexing test
davidhassell 4814586
dev
davidhassell 1543aef
dev
davidhassell c1ac86b
dev
davidhassell d2216ca
dev
davidhassell 8a6be77
Allow negative slices when indexing chunked data
davidhassell 082b00f
improve variable names and comments
davidhassell bacfb99
better Ellipsis error message
davidhassell 54227f2
tidy
davidhassell 432b5b0
tidy
davidhassell 097bd41
Merge branch 'main' into index-negative-step
davidhassell 677634e
linting
davidhassell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """ Test pyfive's abililty to index Datasets.""" | ||
| import os | ||
|
|
||
| import numpy as np | ||
| from numpy.testing import assert_array_equal | ||
| import pytest | ||
|
|
||
| import pyfive | ||
|
|
||
| DIRNAME = os.path.dirname(__file__) | ||
| DATASET_CHUNKED_FILE = os.path.join(DIRNAME, 'data/chunked.hdf5') | ||
|
|
||
| # Define a fixture that opens the chunked file once | ||
| @pytest.fixture(scope="module") | ||
| def chunked_file(): | ||
| with pyfive.File(DATASET_CHUNKED_FILE) as hfile: | ||
| yield hfile | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "index", [ | ||
| (slice(None, None, -1), slice(None, None, -1)), | ||
| (slice(None), slice(1000, 1, -2)), | ||
| (..., slice(None, None, -2)), | ||
| (slice(None, None, -2), ...), | ||
| (0, slice(-1, None, -3)), | ||
| (slice(-1, None, -3), 0), | ||
| ] | ||
| ) | ||
| def test_dataset_indexing_chunked_negative_step_slices(chunked_file, index): | ||
| """Test orthogonal chunked indexing with negative step slices""" | ||
| d = chunked_file['dataset1'] | ||
| assert d.shape == (21, 16) | ||
|
|
||
| array = d[...] | ||
| assert array.shape == d.shape | ||
|
|
||
| assert_array_equal(d[index], array[index]) | ||
|
|
||
| def test_dataset_indexing_replace_negative_slices(): | ||
| """Test pyfive.indexing.replace_negative_slices""" | ||
| func = pyfive.indexing.replace_negative_slices | ||
|
|
||
| r = list(range(8)) | ||
| assert func((0, slice(6, 0, -2)), (7, 8, 9)) == ( | ||
| (0, slice(2, 7, 2)), (0,) | ||
| ) | ||
| assert r[slice(6, 0, -2)] == list(reversed(r[slice(2, 7, 2)])) | ||
|
|
||
| assert func(([1, 2], slice(600, 1, -3), 0), (7, 8, 9)) == ( | ||
| ([1, 2], slice(4, 8, 3), 0), (1,) | ||
| ) | ||
| assert r[slice(600, None, -3)] == list(reversed(r[slice(1, 8, 3)])) | ||
|
|
||
| r = list(range(9)) | ||
| assert func((0, slice(None), slice(6, 0, -2)), (7, 8, 9)) == ( | ||
| (0, slice(None), slice(2, 7, 2)), (1,) | ||
| ) | ||
| assert r[slice(6, 0, -2)] == list(reversed(r[slice(2, 7, 2)])) | ||
|
|
||
| # Can't pass in in Ellipsis | ||
| with pytest.raises(ValueError) as error: | ||
| func((0, slice(6, 0, -2), ...), (7, 8, 9)) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is no longer true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. I think that negative step slices are OK in all cases, now ...
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I would remove that comment (line 50), then I think this can be approved.