@@ -283,8 +283,6 @@ def test_get_basic_selection_2d():
283
283
for selection in bad_selections :
284
284
with pytest .raises (IndexError ):
285
285
z .get_basic_selection (selection )
286
- with pytest .raises (IndexError ):
287
- z [selection ]
288
286
# check fallback on fancy indexing
289
287
fancy_selection = ([0 , 1 ], [0 , 1 ])
290
288
np .testing .assert_array_equal (z [fancy_selection ], [0 , 11 ])
@@ -317,14 +315,179 @@ def test_fancy_indexing_fallback_on_get_setitem():
317
315
)
318
316
319
317
320
- def test_fancy_indexing_doesnt_mix_with_slicing ():
321
- z = zarr .zeros ((20 , 20 ))
322
- with pytest .raises (IndexError ):
323
- z [[1 , 2 , 3 ], :] = 2
324
- with pytest .raises (IndexError ):
325
- np .testing .assert_array_equal (
326
- z [[1 , 2 , 3 ], :], 0
318
+ @pytest .mark .parametrize ("index,expected_result" ,
319
+ [
320
+ # Single iterable of integers
321
+ (
322
+ [0 , 1 ],
323
+ [[0 , 1 , 2 ],
324
+ [3 , 4 , 5 ]]
325
+ ),
326
+ # List first, then slice
327
+ (
328
+ ([0 , 1 ], slice (None )),
329
+ [[0 , 1 , 2 ],
330
+ [3 , 4 , 5 ]]
331
+ ),
332
+ # List first, then slice
333
+ (
334
+ ([0 , 1 ], slice (1 , None )),
335
+ [[1 , 2 ],
336
+ [4 , 5 ]]
337
+ ),
338
+ # Slice first, then list
339
+ (
340
+ (slice (0 , 2 ), [0 , 2 ]),
341
+ [[0 , 2 ],
342
+ [3 , 5 ]]
343
+ ),
344
+ # Slices only
345
+ (
346
+ (slice (0 , 2 ), slice (0 , 2 )),
347
+ [[0 , 1 ],
348
+ [3 , 4 ]]
349
+ ),
350
+ # List with repeated index
351
+ (
352
+ ([1 , 0 , 1 ], slice (1 , None )),
353
+ [[4 , 5 ],
354
+ [1 , 2 ],
355
+ [4 , 5 ]]
356
+ ),
357
+ # 1D indexing
358
+ (
359
+ ([1 , 0 , 1 ]),
360
+ [
361
+ [3 , 4 , 5 ],
362
+ [0 , 1 , 2 ],
363
+ [3 , 4 , 5 ]
364
+ ]
365
+ )
366
+
367
+ ])
368
+ def test_orthogonal_indexing_fallback_on_getitem_2d (index , expected_result ):
369
+ """
370
+ Tests the orthogonal indexing fallback on __getitem__ for a 2D matrix.
371
+
372
+ In addition to checking expected behavior, all indexing
373
+ is also checked against numpy.
374
+ """
375
+ # [0, 1, 2],
376
+ # [3, 4, 5],
377
+ # [6, 7, 8]
378
+ a = np .arange (9 ).reshape (3 , 3 )
379
+ z = zarr .array (a )
380
+
381
+ np .testing .assert_array_equal (z [index ], a [index ], err_msg = "Indexing disagrees with numpy" )
382
+ np .testing .assert_array_equal (z [index ], expected_result )
383
+
384
+
385
+ @pytest .mark .parametrize ("index,expected_result" ,
386
+ [
387
+ # Single iterable of integers
388
+ (
389
+ [0 , 1 ],
390
+ [[[0 , 1 , 2 ],
391
+ [3 , 4 , 5 ],
392
+ [6 , 7 , 8 ]],
393
+ [[9 , 10 , 11 ],
394
+ [12 , 13 , 14 ],
395
+ [15 , 16 , 17 ]]]
396
+ ),
397
+ # One slice, two integers
398
+ (
399
+ (slice (0 , 2 ), 1 , 1 ),
400
+ [4 , 13 ]
401
+ ),
402
+ # One integer, two slices
403
+ (
404
+ (slice (0 , 2 ), 1 , slice (0 , 2 )),
405
+ [[3 , 4 ], [12 , 13 ]]
406
+ ),
407
+ # Two slices and a list
408
+ (
409
+ (slice (0 , 2 ), [1 , 2 ], slice (0 , 2 )),
410
+ [[[3 , 4 ], [6 , 7 ]], [[12 , 13 ], [15 , 16 ]]]
411
+ ),
412
+ ])
413
+ def test_orthogonal_indexing_fallback_on_getitem_3d (index , expected_result ):
414
+ """
415
+ Tests the orthogonal indexing fallback on __getitem__ for a 3D matrix.
416
+
417
+ In addition to checking expected behavior, all indexing
418
+ is also checked against numpy.
419
+ """
420
+ # [[[ 0, 1, 2],
421
+ # [ 3, 4, 5],
422
+ # [ 6, 7, 8]],
423
+
424
+ # [[ 9, 10, 11],
425
+ # [12, 13, 14],
426
+ # [15, 16, 17]],
427
+
428
+ # [[18, 19, 20],
429
+ # [21, 22, 23],
430
+ # [24, 25, 26]]]
431
+ a = np .arange (27 ).reshape (3 , 3 , 3 )
432
+ z = zarr .array (a )
433
+
434
+ np .testing .assert_array_equal (z [index ], a [index ], err_msg = "Indexing disagrees with numpy" )
435
+ np .testing .assert_array_equal (z [index ], expected_result )
436
+
437
+
438
+ @pytest .mark .parametrize (
439
+ "index,expected_result" ,
440
+ [
441
+ # Single iterable of integers
442
+ (
443
+ [0 , 1 ],
444
+ [
445
+ [1 , 1 , 1 ],
446
+ [1 , 1 , 1 ],
447
+ [0 , 0 , 0 ]
448
+ ]
449
+ ),
450
+ # List and slice combined
451
+ (
452
+ ([0 , 1 ], slice (1 , 3 )),
453
+ [[0 , 1 , 1 ],
454
+ [0 , 1 , 1 ],
455
+ [0 , 0 , 0 ]]
456
+ ),
457
+ # Index repetition is ignored on setitem
458
+ (
459
+ ([0 , 1 , 1 , 1 , 1 , 1 , 1 ], slice (1 , 3 )),
460
+ [[0 , 1 , 1 ],
461
+ [0 , 1 , 1 ],
462
+ [0 , 0 , 0 ]]
463
+ ),
464
+ # Slice with step
465
+ (
466
+ ([0 , 2 ], slice (None , None , 2 )),
467
+ [[1 , 0 , 1 ],
468
+ [0 , 0 , 0 ],
469
+ [1 , 0 , 1 ]]
327
470
)
471
+ ]
472
+ )
473
+ def test_orthogonal_indexing_fallback_on_setitem_2d (index , expected_result ):
474
+ """
475
+ Tests the orthogonal indexing fallback on __setitem__ for a 3D matrix.
476
+
477
+ In addition to checking expected behavior, all indexing
478
+ is also checked against numpy.
479
+ """
480
+ # Slice + fancy index
481
+ a = np .zeros ((3 , 3 ))
482
+ z = zarr .array (a )
483
+ z [index ] = 1
484
+ a [index ] = 1
485
+ np .testing .assert_array_equal (
486
+ z , expected_result
487
+ )
488
+ np .testing .assert_array_equal (
489
+ z , a , err_msg = "Indexing disagrees with numpy"
490
+ )
328
491
329
492
330
493
def test_fancy_indexing_doesnt_mix_with_implicit_slicing ():
@@ -335,12 +498,6 @@ def test_fancy_indexing_doesnt_mix_with_implicit_slicing():
335
498
np .testing .assert_array_equal (
336
499
z2 [[1 , 2 , 3 ], [1 , 2 , 3 ]], 0
337
500
)
338
- with pytest .raises (IndexError ):
339
- z2 [[1 , 2 , 3 ]] = 2
340
- with pytest .raises (IndexError ):
341
- np .testing .assert_array_equal (
342
- z2 [[1 , 2 , 3 ]], 0
343
- )
344
501
with pytest .raises (IndexError ):
345
502
z2 [..., [1 , 2 , 3 ]] = 2
346
503
with pytest .raises (IndexError ):
@@ -770,6 +927,33 @@ def test_set_orthogonal_selection_3d():
770
927
_test_set_orthogonal_selection_3d (v , a , z , ix0 , ix1 , ix2 )
771
928
772
929
930
+ def test_orthogonal_indexing_fallback_on_get_setitem ():
931
+ z = zarr .zeros ((20 , 20 ))
932
+ z [[1 , 2 , 3 ], [1 , 2 , 3 ]] = 1
933
+ np .testing .assert_array_equal (
934
+ z [:4 , :4 ],
935
+ [
936
+ [0 , 0 , 0 , 0 ],
937
+ [0 , 1 , 0 , 0 ],
938
+ [0 , 0 , 1 , 0 ],
939
+ [0 , 0 , 0 , 1 ],
940
+ ],
941
+ )
942
+ np .testing .assert_array_equal (
943
+ z [[1 , 2 , 3 ], [1 , 2 , 3 ]], 1
944
+ )
945
+ # test broadcasting
946
+ np .testing .assert_array_equal (
947
+ z [1 , [1 , 2 , 3 ]], [1 , 0 , 0 ]
948
+ )
949
+ # test 1D fancy indexing
950
+ z2 = zarr .zeros (5 )
951
+ z2 [[1 , 2 , 3 ]] = 1
952
+ np .testing .assert_array_equal (
953
+ z2 , [0 , 1 , 1 , 1 , 0 ]
954
+ )
955
+
956
+
773
957
def _test_get_coordinate_selection (a , z , selection ):
774
958
expect = a [selection ]
775
959
actual = z .get_coordinate_selection (selection )
0 commit comments