|
4 | 4 |
|
5 | 5 | import zarr
|
6 | 6 | from zarr.indexing import (
|
| 7 | + make_slice_selection, |
7 | 8 | normalize_integer_selection,
|
8 | 9 | oindex,
|
9 | 10 | oindex_set,
|
@@ -198,15 +199,15 @@ def test_get_basic_selection_1d():
|
198 | 199 | for selection in basic_selections_1d:
|
199 | 200 | _test_get_basic_selection(a, z, selection)
|
200 | 201 |
|
201 |
| - bad_selections = basic_selections_1d_bad + [ |
202 |
| - [0, 1], # fancy indexing |
203 |
| - ] |
204 |
| - for selection in bad_selections: |
| 202 | + for selection in basic_selections_1d_bad: |
205 | 203 | with pytest.raises(IndexError):
|
206 | 204 | z.get_basic_selection(selection)
|
207 | 205 | with pytest.raises(IndexError):
|
208 | 206 | z[selection]
|
209 | 207 |
|
| 208 | + with pytest.raises(IndexError): |
| 209 | + z.get_basic_selection([1, 0]) |
| 210 | + |
210 | 211 |
|
211 | 212 | basic_selections_2d = [
|
212 | 213 | # single row
|
@@ -274,14 +275,75 @@ def test_get_basic_selection_2d():
|
274 | 275 | bad_selections = basic_selections_2d_bad + [
|
275 | 276 | # integer arrays
|
276 | 277 | [0, 1],
|
277 |
| - ([0, 1], [0, 1]), |
278 | 278 | (slice(None), [0, 1]),
|
279 | 279 | ]
|
280 | 280 | for selection in bad_selections:
|
281 | 281 | with pytest.raises(IndexError):
|
282 | 282 | z.get_basic_selection(selection)
|
283 | 283 | with pytest.raises(IndexError):
|
284 | 284 | z[selection]
|
| 285 | + # check fallback on fancy indexing |
| 286 | + fancy_selection = ([0, 1], [0, 1]) |
| 287 | + np.testing.assert_array_equal(z[fancy_selection], [0, 11]) |
| 288 | + |
| 289 | + |
| 290 | +def test_fancy_indexing_fallback_on_get_setitem(): |
| 291 | + z = zarr.zeros((20, 20)) |
| 292 | + z[[1, 2, 3], [1, 2, 3]] = 1 |
| 293 | + np.testing.assert_array_equal( |
| 294 | + z[:4, :4], |
| 295 | + [ |
| 296 | + [0, 0, 0, 0], |
| 297 | + [0, 1, 0, 0], |
| 298 | + [0, 0, 1, 0], |
| 299 | + [0, 0, 0, 1], |
| 300 | + ], |
| 301 | + ) |
| 302 | + np.testing.assert_array_equal( |
| 303 | + z[[1, 2, 3], [1, 2, 3]], 1 |
| 304 | + ) |
| 305 | + # test broadcasting |
| 306 | + np.testing.assert_array_equal( |
| 307 | + z[1, [1, 2, 3]], [1, 0, 0] |
| 308 | + ) |
| 309 | + # test 1D fancy indexing |
| 310 | + z2 = zarr.zeros(5) |
| 311 | + z2[[1, 2, 3]] = 1 |
| 312 | + np.testing.assert_array_equal( |
| 313 | + z2, [0, 1, 1, 1, 0] |
| 314 | + ) |
| 315 | + |
| 316 | + |
| 317 | +def test_fancy_indexing_doesnt_mix_with_slicing(): |
| 318 | + z = zarr.zeros((20, 20)) |
| 319 | + with pytest.raises(IndexError): |
| 320 | + z[[1, 2, 3], :] = 2 |
| 321 | + with pytest.raises(IndexError): |
| 322 | + np.testing.assert_array_equal( |
| 323 | + z[[1, 2, 3], :], 0 |
| 324 | + ) |
| 325 | + |
| 326 | + |
| 327 | +def test_fancy_indexing_doesnt_mix_with_implicit_slicing(): |
| 328 | + z2 = zarr.zeros((5, 5, 5)) |
| 329 | + with pytest.raises(IndexError): |
| 330 | + z2[[1, 2, 3], [1, 2, 3]] = 2 |
| 331 | + with pytest.raises(IndexError): |
| 332 | + np.testing.assert_array_equal( |
| 333 | + z2[[1, 2, 3], [1, 2, 3]], 0 |
| 334 | + ) |
| 335 | + with pytest.raises(IndexError): |
| 336 | + z2[[1, 2, 3]] = 2 |
| 337 | + with pytest.raises(IndexError): |
| 338 | + np.testing.assert_array_equal( |
| 339 | + z2[[1, 2, 3]], 0 |
| 340 | + ) |
| 341 | + with pytest.raises(IndexError): |
| 342 | + z2[..., [1, 2, 3]] = 2 |
| 343 | + with pytest.raises(IndexError): |
| 344 | + np.testing.assert_array_equal( |
| 345 | + z2[..., [1, 2, 3]], 0 |
| 346 | + ) |
285 | 347 |
|
286 | 348 |
|
287 | 349 | def test_set_basic_selection_0d():
|
@@ -1373,3 +1435,10 @@ def test_PartialChunkIterator(selection, arr, expected):
|
1373 | 1435 | PCI = PartialChunkIterator(selection, arr.shape)
|
1374 | 1436 | results = list(PCI)
|
1375 | 1437 | assert results == expected
|
| 1438 | + |
| 1439 | + |
| 1440 | +def test_slice_selection_uints(): |
| 1441 | + arr = np.arange(24).reshape((4, 6)) |
| 1442 | + idx = np.uint64(3) |
| 1443 | + slice_sel = make_slice_selection((idx,)) |
| 1444 | + assert arr[slice_sel].shape == (1, 6) |
0 commit comments