-
-
Notifications
You must be signed in to change notification settings - Fork 331
Add support for fancy indexing on get/setitem #725
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
Changes from all commits
0fbf889
a806e3d
d81e33e
7ff7550
5508f75
96c610b
b7c431a
3306c13
a101247
769c364
2e87b53
b0504c5
a03f9e2
92faf42
b4ea006
2a4fa96
208050c
4e7733c
2f5df3e
cf9051c
b9c8f59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import zarr | ||
from zarr.indexing import ( | ||
make_slice_selection, | ||
normalize_integer_selection, | ||
oindex, | ||
oindex_set, | ||
|
@@ -198,15 +199,15 @@ def test_get_basic_selection_1d(): | |
for selection in basic_selections_1d: | ||
_test_get_basic_selection(a, z, selection) | ||
|
||
bad_selections = basic_selections_1d_bad + [ | ||
[0, 1], # fancy indexing | ||
] | ||
for selection in bad_selections: | ||
for selection in basic_selections_1d_bad: | ||
with pytest.raises(IndexError): | ||
z.get_basic_selection(selection) | ||
with pytest.raises(IndexError): | ||
z[selection] | ||
|
||
with pytest.raises(IndexError): | ||
z.get_basic_selection([1, 0]) | ||
|
||
|
||
basic_selections_2d = [ | ||
# single row | ||
|
@@ -274,14 +275,75 @@ def test_get_basic_selection_2d(): | |
bad_selections = basic_selections_2d_bad + [ | ||
# integer arrays | ||
[0, 1], | ||
([0, 1], [0, 1]), | ||
(slice(None), [0, 1]), | ||
] | ||
for selection in bad_selections: | ||
with pytest.raises(IndexError): | ||
z.get_basic_selection(selection) | ||
with pytest.raises(IndexError): | ||
z[selection] | ||
# check fallback on fancy indexing | ||
fancy_selection = ([0, 1], [0, 1]) | ||
np.testing.assert_array_equal(z[fancy_selection], [0, 11]) | ||
|
||
|
||
def test_fancy_indexing_fallback_on_get_setitem(): | ||
z = zarr.zeros((20, 20)) | ||
z[[1, 2, 3], [1, 2, 3]] = 1 | ||
np.testing.assert_array_equal( | ||
z[:4, :4], | ||
[ | ||
[0, 0, 0, 0], | ||
[0, 1, 0, 0], | ||
[0, 0, 1, 0], | ||
[0, 0, 0, 1], | ||
], | ||
) | ||
np.testing.assert_array_equal( | ||
z[[1, 2, 3], [1, 2, 3]], 1 | ||
) | ||
# test broadcasting | ||
np.testing.assert_array_equal( | ||
z[1, [1, 2, 3]], [1, 0, 0] | ||
) | ||
# test 1D fancy indexing | ||
z2 = zarr.zeros(5) | ||
z2[[1, 2, 3]] = 1 | ||
np.testing.assert_array_equal( | ||
z2, [0, 1, 1, 1, 0] | ||
) | ||
|
||
|
||
def test_fancy_indexing_doesnt_mix_with_slicing(): | ||
z = zarr.zeros((20, 20)) | ||
with pytest.raises(IndexError): | ||
z[[1, 2, 3], :] = 2 | ||
Comment on lines
+317
to
+320
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another case would worth checking would be something like:
This doesn't look like mixed indexing but it actually is because of the implicit slice at the end. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great point! 😬 Will work on this. |
||
with pytest.raises(IndexError): | ||
np.testing.assert_array_equal( | ||
z[[1, 2, 3], :], 0 | ||
) | ||
|
||
|
||
def test_fancy_indexing_doesnt_mix_with_implicit_slicing(): | ||
z2 = zarr.zeros((5, 5, 5)) | ||
with pytest.raises(IndexError): | ||
z2[[1, 2, 3], [1, 2, 3]] = 2 | ||
with pytest.raises(IndexError): | ||
np.testing.assert_array_equal( | ||
z2[[1, 2, 3], [1, 2, 3]], 0 | ||
) | ||
with pytest.raises(IndexError): | ||
z2[[1, 2, 3]] = 2 | ||
with pytest.raises(IndexError): | ||
np.testing.assert_array_equal( | ||
z2[[1, 2, 3]], 0 | ||
) | ||
with pytest.raises(IndexError): | ||
z2[..., [1, 2, 3]] = 2 | ||
with pytest.raises(IndexError): | ||
np.testing.assert_array_equal( | ||
z2[..., [1, 2, 3]], 0 | ||
) | ||
|
||
|
||
def test_set_basic_selection_0d(): | ||
|
@@ -1373,3 +1435,10 @@ def test_PartialChunkIterator(selection, arr, expected): | |
PCI = PartialChunkIterator(selection, arr.shape) | ||
results = list(PCI) | ||
assert results == expected | ||
|
||
|
||
def test_slice_selection_uints(): | ||
arr = np.arange(24).reshape((4, 6)) | ||
idx = np.uint64(3) | ||
slice_sel = make_slice_selection((idx,)) | ||
assert arr[slice_sel].shape == (1, 6) |
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.
Note from zulip: this fixed the weird timeout issue, though we still don't know why the deadlock was platform specific.