-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_slicing.py
1097 lines (874 loc) · 32.8 KB
/
test_slicing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import itertools
import warnings
import pytest
from tlz import merge
np = pytest.importorskip("numpy")
import dask
import dask.array as da
from dask import config
from dask.array.chunk import getitem
from dask.array.slicing import (
_sanitize_index_element,
_slice_1d,
make_block_sorted_slices,
new_blockdim,
normalize_index,
sanitize_index,
shuffle_slice,
slice_array,
slicing_plan,
take,
take_along_axis,
)
from dask.array.utils import assert_eq, same_keys
def test_slice_1d():
expected = {0: slice(10, 25, 1), 1: slice(None, None, None), 2: slice(0, 1, 1)}
result = _slice_1d(100, [25] * 4, slice(10, 51, None))
assert expected == result
# x[100:12:-3]
expected = {
0: slice(-2, -8, -3),
1: slice(-1, -21, -3),
2: slice(-3, -21, -3),
3: slice(-2, -21, -3),
4: slice(-1, -21, -3),
}
result = _slice_1d(100, [20] * 5, slice(100, 12, -3))
assert expected == result
# x[102::-3]
expected = {
0: slice(-2, -21, -3),
1: slice(-1, -21, -3),
2: slice(-3, -21, -3),
3: slice(-2, -21, -3),
4: slice(-1, -21, -3),
}
result = _slice_1d(100, [20] * 5, slice(102, None, -3))
assert expected == result
# x[::-4]
expected = {
0: slice(-1, -21, -4),
1: slice(-1, -21, -4),
2: slice(-1, -21, -4),
3: slice(-1, -21, -4),
4: slice(-1, -21, -4),
}
result = _slice_1d(100, [20] * 5, slice(None, None, -4))
assert expected == result
# x[::-7]
expected = {
0: slice(-5, -21, -7),
1: slice(-4, -21, -7),
2: slice(-3, -21, -7),
3: slice(-2, -21, -7),
4: slice(-1, -21, -7),
}
result = _slice_1d(100, [20] * 5, slice(None, None, -7))
assert expected == result
# x=range(115)
# x[::-7]
expected = {
0: slice(-7, -24, -7),
1: slice(-2, -24, -7),
2: slice(-4, -24, -7),
3: slice(-6, -24, -7),
4: slice(-1, -24, -7),
}
result = _slice_1d(115, [23] * 5, slice(None, None, -7))
assert expected == result
# x[79::-3]
expected = {
0: slice(-1, -21, -3),
1: slice(-3, -21, -3),
2: slice(-2, -21, -3),
3: slice(-1, -21, -3),
}
result = _slice_1d(100, [20] * 5, slice(79, None, -3))
assert expected == result
# x[-1:-8:-1]
expected = {4: slice(-1, -8, -1)}
result = _slice_1d(100, [20, 20, 20, 20, 20], slice(-1, 92, -1))
assert expected == result
# x[20:0:-1]
expected = {0: slice(-1, -20, -1), 1: slice(-20, -21, -1)}
result = _slice_1d(100, [20, 20, 20, 20, 20], slice(20, 0, -1))
assert expected == result
# x[:0]
expected = {}
result = _slice_1d(100, [20, 20, 20, 20, 20], slice(0))
assert result
# x=range(99)
expected = {
0: slice(-3, -21, -3),
1: slice(-2, -21, -3),
2: slice(-1, -21, -3),
3: slice(-2, -20, -3),
4: slice(-1, -21, -3),
}
# This array has non-uniformly sized blocks
result = _slice_1d(99, [20, 20, 20, 19, 20], slice(100, None, -3))
assert expected == result
# x=range(104)
# x[::-3]
expected = {
0: slice(-1, -21, -3),
1: slice(-3, -24, -3),
2: slice(-3, -28, -3),
3: slice(-1, -14, -3),
4: slice(-1, -22, -3),
}
# This array has non-uniformly sized blocks
result = _slice_1d(104, [20, 23, 27, 13, 21], slice(None, None, -3))
assert expected == result
# x=range(104)
# x[:27:-3]
expected = {
1: slice(-3, -16, -3),
2: slice(-3, -28, -3),
3: slice(-1, -14, -3),
4: slice(-1, -22, -3),
}
# This array has non-uniformly sized blocks
result = _slice_1d(104, [20, 23, 27, 13, 21], slice(None, 27, -3))
assert expected == result
# x=range(104)
# x[100:27:-3]
expected = {
1: slice(-3, -16, -3),
2: slice(-3, -28, -3),
3: slice(-1, -14, -3),
4: slice(-4, -22, -3),
}
# This array has non-uniformly sized blocks
result = _slice_1d(104, [20, 23, 27, 13, 21], slice(100, 27, -3))
assert expected == result
# x=range(1000000000000)
# x[1000:]
expected = {0: slice(1000, 1000000000, 1)}
expected.update({ii: slice(None, None, None) for ii in range(1, 1000)})
# This array is large
result = _slice_1d(1000000000000, [1000000000] * 1000, slice(1000, None, None))
assert expected == result
def test_slice_singleton_value_on_boundary():
assert _slice_1d(15, [5, 5, 5], 10) == {2: 0}
assert _slice_1d(30, (5, 5, 5, 5, 5, 5), 10) == {2: 0}
def test_slice_array_1d():
# x[24::2]
expected = {
("y", 0): (getitem, ("x", 0), (slice(24, 25, 2),)),
("y", 1): (getitem, ("x", 1), (slice(1, 25, 2),)),
("y", 2): (getitem, ("x", 2), (slice(0, 25, 2),)),
("y", 3): (getitem, ("x", 3), (slice(1, 25, 2),)),
}
result, chunks = slice_array("y", "x", [[25] * 4], [slice(24, None, 2)], 8)
assert expected == result
# x[26::2]
expected = {
("y", 0): (getitem, ("x", 1), (slice(1, 25, 2),)),
("y", 1): (getitem, ("x", 2), (slice(0, 25, 2),)),
("y", 2): (getitem, ("x", 3), (slice(1, 25, 2),)),
}
result, chunks = slice_array("y", "x", [[25] * 4], [slice(26, None, 2)], 8)
assert expected == result
# x[24::2]
expected = {
("y", 0): (getitem, ("x", 0), (slice(24, 25, 2),)),
("y", 1): (getitem, ("x", 1), (slice(1, 25, 2),)),
("y", 2): (getitem, ("x", 2), (slice(0, 25, 2),)),
("y", 3): (getitem, ("x", 3), (slice(1, 25, 2),)),
}
result, chunks = slice_array("y", "x", [(25,) * 4], (slice(24, None, 2),), 8)
assert expected == result
# x[26::2]
expected = {
("y", 0): (getitem, ("x", 1), (slice(1, 25, 2),)),
("y", 1): (getitem, ("x", 2), (slice(0, 25, 2),)),
("y", 2): (getitem, ("x", 3), (slice(1, 25, 2),)),
}
result, chunks = slice_array("y", "x", [(25,) * 4], (slice(26, None, 2),), 8)
assert expected == result
def test_slice_array_2d():
# 2d slices: x[13::2,10::1]
expected = {
("y", 0, 0): (getitem, ("x", 0, 0), (slice(13, 20, 2), slice(10, 20, 1))),
("y", 0, 1): (
getitem,
("x", 0, 1),
(slice(13, 20, 2), slice(None, None, None)),
),
("y", 0, 2): (
getitem,
("x", 0, 2),
(slice(13, 20, 2), slice(None, None, None)),
),
}
result, chunks = slice_array(
"y",
"x",
[[20], [20, 20, 5]],
[slice(13, None, 2), slice(10, None, 1)],
itemsize=8,
)
assert expected == result
# 2d slices with one dimension: x[5,10::1]
expected = {
("y", 0): (getitem, ("x", 0, 0), (5, slice(10, 20, 1))),
("y", 1): (getitem, ("x", 0, 1), (5, slice(None, None, None))),
("y", 2): (getitem, ("x", 0, 2), (5, slice(None, None, None))),
}
result, chunks = slice_array(
"y", "x", ([20], [20, 20, 5]), [5, slice(10, None, 1)], 8
)
assert expected == result
def test_slice_optimizations():
# bar[:]
expected = {("foo", 0): ("bar", 0)}
result, chunks = slice_array("foo", "bar", [[100]], (slice(None, None, None),), 8)
assert expected == result
# bar[:,:,:]
expected = {("foo", 0): ("bar", 0), ("foo", 1): ("bar", 1), ("foo", 2): ("bar", 2)}
result, chunks = slice_array(
"foo",
"bar",
[(100, 1000, 10000)],
(slice(None, None, None), slice(None, None, None), slice(None, None, None)),
itemsize=8,
)
assert expected == result
def test_slicing_with_singleton_indices():
result, chunks = slice_array(
"y", "x", ([5, 5], [5, 5]), (slice(0, 5), 8), itemsize=8
)
expected = {("y", 0): (getitem, ("x", 0, 1), (slice(None, None, None), 3))}
assert expected == result
def test_slicing_with_newaxis():
result, chunks = slice_array(
"y",
"x",
([5, 5], [5, 5]),
(slice(0, 3), None, slice(None, None, None)),
itemsize=8,
)
expected = {
("y", 0, 0, 0): (
getitem,
("x", 0, 0),
(slice(0, 3, 1), None, slice(None, None, None)),
),
("y", 0, 0, 1): (
getitem,
("x", 0, 1),
(slice(0, 3, 1), None, slice(None, None, None)),
),
}
assert expected == result
assert chunks == ((3,), (1,), (5, 5))
def test_take():
chunks, dsk = take("y", "x", [(20, 20, 20, 20)], [5, 1, 47, 3], itemsize=8, axis=0)
expected = {
("y", 0): (getitem, ("x", 0), (np.array([5, 1]),)),
("y", 1): (getitem, ("x", 2), (np.array([7]),)),
("y", 2): (getitem, ("x", 0), (np.array([3]),)),
}
np.testing.assert_equal(sorted(dsk.items()), sorted(expected.items()))
assert chunks == ((2, 1, 1),)
chunks, dsk = take(
"y", "x", [(20, 20, 20, 20), (20, 20)], [5, 1, 47, 3], itemsize=8, axis=0
)
expected = {
("y", 0, 0): (
getitem,
("x", 0, 0),
(np.array([5, 1]), slice(None, None, None)),
),
("y", 0, 1): (
getitem,
("x", 0, 1),
(np.array([5, 1]), slice(None, None, None)),
),
("y", 1, 0): (getitem, ("x", 2, 0), (np.array([7]), slice(None, None, None))),
("y", 1, 1): (getitem, ("x", 2, 1), (np.array([7]), slice(None, None, None))),
("y", 2, 0): (getitem, ("x", 0, 0), (np.array([3]), slice(None, None, None))),
("y", 2, 1): (getitem, ("x", 0, 1), (np.array([3]), slice(None, None, None))),
}
np.testing.assert_equal(sorted(dsk.items()), sorted(expected.items()))
assert chunks == ((2, 1, 1), (20, 20))
def test_take_sorted():
chunks, dsk = take("y", "x", [(20, 20, 20, 20)], [1, 3, 5, 47], itemsize=8, axis=0)
expected = {
("y", 0): (getitem, ("x", 0), ([1, 3, 5],)),
("y", 1): (getitem, ("x", 2), ([7],)),
}
np.testing.assert_equal(dsk, expected)
assert chunks == ((3, 1),)
chunks, dsk = take(
"y", "x", [(20, 20, 20, 20), (20, 20)], [1, 3, 5, 37], itemsize=8, axis=1
)
expected = merge(
{
("y", i, 0): (getitem, ("x", i, 0), (slice(None, None, None), [1, 3, 5]))
for i in range(4)
},
{
("y", i, 1): (getitem, ("x", i, 1), (slice(None, None, None), [17]))
for i in range(4)
},
)
np.testing.assert_equal(dsk, expected)
assert chunks == ((20, 20, 20, 20), (3, 1))
def test_slicing_chunks():
result, chunks = slice_array(
"y", "x", ([5, 5], [5, 5]), (1, np.array([2, 0, 3])), itemsize=8
)
assert chunks == ((3,),)
result, chunks = slice_array(
"y", "x", ([5, 5], [5, 5]), (slice(0, 7), np.array([2, 0, 3])), itemsize=8
)
assert chunks == ((5, 2), (3,))
result, chunks = slice_array(
"y", "x", ([5, 5], [5, 5]), (slice(0, 7), 1), itemsize=8
)
assert chunks == ((5, 2),)
def test_slicing_with_numpy_arrays():
a, bd1 = slice_array(
"y",
"x",
((3, 3, 3, 1), (3, 3, 3, 1)),
(np.array([1, 2, 9]), slice(None, None, None)),
itemsize=8,
)
b, bd2 = slice_array(
"y",
"x",
((3, 3, 3, 1), (3, 3, 3, 1)),
(np.array([1, 2, 9]), slice(None, None, None)),
itemsize=8,
)
assert bd1 == bd2
np.testing.assert_equal(a, b)
i = [False, True, True, False, False, False, False, False, False, True]
index = (i, slice(None, None, None))
index = normalize_index(index, (10, 10))
c, bd3 = slice_array("y", "x", ((3, 3, 3, 1), (3, 3, 3, 1)), index, itemsize=8)
assert bd1 == bd3
np.testing.assert_equal(a, c)
def test_slicing_and_chunks():
o = da.ones((24, 16), chunks=((4, 8, 8, 4), (2, 6, 6, 2)))
t = o[4:-4, 2:-2]
assert t.chunks == ((8, 8), (6, 6))
def test_slicing_and_unknown_chunks():
a = da.ones((10, 5), chunks=5)
a._chunks = ((np.nan, np.nan), (5,))
with pytest.raises(ValueError, match="Array chunk size or shape is unknown"):
a[[0, 5]].compute()
def test_slicing_identities():
a = da.ones((24, 16), chunks=((4, 8, 8, 4), (2, 6, 6, 2)))
assert a is a[slice(None)]
assert a is a[:]
assert a is a[::]
assert a is a[...]
assert a is a[0:]
assert a is a[0::]
assert a is a[::1]
assert a is a[0 : len(a)]
assert a is a[0::1]
assert a is a[0 : len(a) : 1]
def test_slice_stop_0():
# from gh-125
a = da.ones(10, chunks=(10,))[:0].compute()
b = np.ones(10)[:0]
assert_eq(a, b)
def test_slice_list_then_None():
x = da.zeros(shape=(5, 5), chunks=(3, 3))
y = x[[2, 1]][None]
assert_eq(y, np.zeros((1, 2, 5)))
class ReturnItem:
def __getitem__(self, key):
return key
@pytest.mark.skip(reason="really long test")
def test_slicing_exhaustively():
x = np.random.default_rng().random(6, 7, 8)
a = da.from_array(x, chunks=(3, 3, 3))
I = ReturnItem()
# independent indexing along different axes
indexers = [0, -2, I[:], I[:5], [0, 1], [0, 1, 2], [4, 2], I[::-1], None, I[:0], []]
for i in indexers:
assert_eq(x[i], a[i])
for j in indexers:
assert_eq(x[i][:, j], a[i][:, j])
assert_eq(x[:, i][j], a[:, i][j])
for k in indexers:
assert_eq(x[..., i][:, j][k], a[..., i][:, j][k])
# repeated indexing along the first axis
first_indexers = [I[:], I[:5], np.arange(5), [3, 1, 4, 5, 0], np.arange(6) < 6]
second_indexers = [0, -1, 3, I[:], I[:3], I[2:-1], [2, 4], [], I[:0]]
for i in first_indexers:
for j in second_indexers:
assert_eq(x[i][j], a[i][j])
def test_slicing_with_negative_step_flops_keys():
x = da.arange(10, chunks=5)
y = x[:1:-1]
assert (x.name, 1) in y.dask[(y.name, 0)]
assert (x.name, 0) in y.dask[(y.name, 1)]
assert_eq(y, np.arange(10)[:1:-1])
assert y.chunks == ((5, 3),)
assert y.dask[(y.name, 0)] == (getitem, (x.name, 1), (slice(-1, -6, -1),))
assert y.dask[(y.name, 1)] == (getitem, (x.name, 0), (slice(-1, -4, -1),))
def test_empty_slice():
x = da.ones((5, 5), chunks=(2, 2), dtype="i4")
y = x[:0]
assert_eq(y, np.ones((5, 5), dtype="i4")[:0])
def test_multiple_list_slicing():
x = np.random.default_rng().random((6, 7, 8))
a = da.from_array(x, chunks=(3, 3, 3))
assert_eq(x[:, [0, 1, 2]][[0, 1]], a[:, [0, 1, 2]][[0, 1]])
def test_boolean_list_slicing():
with pytest.raises(IndexError):
da.asarray(range(2))[[True]]
with pytest.raises(IndexError):
da.asarray(range(2))[[False, False, False]]
x = np.arange(5)
ind = [True, False, False, False, True]
assert_eq(da.asarray(x)[ind], x[ind])
# https://github.com/dask/dask/issues/3706
ind = [True]
assert_eq(da.asarray([0])[ind], np.arange(1)[ind])
def test_boolean_numpy_array_slicing():
with pytest.raises(IndexError):
da.asarray(range(2))[np.array([True])]
with pytest.raises(IndexError):
da.asarray(range(2))[np.array([False, False, False])]
x = np.arange(5)
ind = np.array([True, False, False, False, True])
assert_eq(da.asarray(x)[ind], x[ind])
# https://github.com/dask/dask/issues/3706
ind = np.array([True])
assert_eq(da.asarray([0])[ind], np.arange(1)[ind])
def test_empty_list():
x = np.ones((5, 5, 5), dtype="i4")
dx = da.from_array(x, chunks=2)
assert_eq(dx[[], :3, :2], x[[], :3, :2])
assert_eq(dx[:3, [], :2], x[:3, [], :2])
assert_eq(dx[:3, :2, []], x[:3, :2, []])
def test_uneven_chunks():
assert da.ones(20, chunks=5)[::2].chunks == ((3, 2, 3, 2),)
def test_new_blockdim():
assert new_blockdim(20, [5, 5, 5, 5], slice(0, None, 2)) == [3, 2, 3, 2]
def test_slicing_consistent_names():
x = np.arange(100).reshape((10, 10))
a = da.from_array(x, chunks=(5, 5))
assert same_keys(a[0], a[0])
assert same_keys(a[:, [1, 2, 3]], a[:, [1, 2, 3]])
assert same_keys(a[:, 5:2:-1], a[:, 5:2:-1])
assert same_keys(a[0, ...], a[0, ...])
assert same_keys(a[...], a[...])
assert same_keys(a[[1, 3, 5]], a[[1, 3, 5]])
assert same_keys(a[-11:11], a[:])
assert same_keys(a[-11:-9], a[:1])
assert same_keys(a[-1], a[9])
assert same_keys(a[0::-1], a[0:-11:-1])
def test_slicing_consistent_names_after_normalization():
x = da.zeros(10, chunks=(5,))
assert same_keys(x[0:], x[:10])
assert same_keys(x[0:], x[0:10])
assert same_keys(x[0:], x[0:10:1])
assert same_keys(x[:], x[0:10:1])
def test_sanitize_index_element():
with pytest.raises(TypeError):
_sanitize_index_element("Hello!")
def test_sanitize_index():
pd = pytest.importorskip("pandas")
with pytest.raises(TypeError):
sanitize_index("Hello!")
np.testing.assert_equal(sanitize_index(pd.Series([1, 2, 3])), [1, 2, 3])
np.testing.assert_equal(sanitize_index((1, 2, 3)), [1, 2, 3])
def test_uneven_blockdims():
blockdims = ((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30), (100,))
index = (slice(240, 270), slice(None))
dsk_out, bd_out = slice_array("in", "out", blockdims, index, itemsize=8)
sol = {
("in", 0, 0): (getitem, ("out", 7, 0), (slice(28, 31, 1), slice(None))),
("in", 1, 0): (getitem, ("out", 8, 0), (slice(0, 27, 1), slice(None))),
}
assert dsk_out == sol
assert bd_out == ((3, 27), (100,))
blockdims = ((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30),) * 2
index = (slice(240, 270), slice(180, 230))
dsk_out, bd_out = slice_array("in", "out", blockdims, index, itemsize=8)
sol = {
("in", 0, 0): (getitem, ("out", 7, 5), (slice(28, 31, 1), slice(29, 30, 1))),
("in", 0, 1): (getitem, ("out", 7, 6), (slice(28, 31, 1), slice(None))),
("in", 0, 2): (getitem, ("out", 7, 7), (slice(28, 31, 1), slice(0, 18, 1))),
("in", 1, 0): (getitem, ("out", 8, 5), (slice(0, 27, 1), slice(29, 30, 1))),
("in", 1, 1): (getitem, ("out", 8, 6), (slice(0, 27, 1), slice(None))),
("in", 1, 2): (getitem, ("out", 8, 7), (slice(0, 27, 1), slice(0, 18, 1))),
}
assert dsk_out == sol
assert bd_out == ((3, 27), (1, 31, 18))
def test_oob_check():
x = da.ones(5, chunks=(2,))
with pytest.raises(IndexError):
x[6]
with pytest.raises(IndexError):
x[[6]]
with pytest.raises(IndexError):
x[-10]
with pytest.raises(IndexError):
x[[-10]]
with pytest.raises(IndexError):
x[0, 0]
@pytest.mark.parametrize("idx_chunks", [None, 3, 2, 1])
@pytest.mark.parametrize("x_chunks", [None, (3, 5), (2, 3), (1, 2), (1, 1)])
def test_index_with_int_dask_array(x_chunks, idx_chunks):
# test data is crafted to stress use cases:
# - pick from different chunks of x out of order
# - a chunk of x contains no matches
# - only one chunk of x
x = np.array(
[[10, 20, 30, 40, 50], [60, 70, 80, 90, 100], [110, 120, 130, 140, 150]]
)
idx = np.array([3, 0, 1])
expect = np.array([[40, 10, 20], [90, 60, 70], [140, 110, 120]])
if x_chunks is not None:
x = da.from_array(x, chunks=x_chunks)
if idx_chunks is not None:
idx = da.from_array(idx, chunks=idx_chunks)
assert_eq(x[:, idx], expect)
assert_eq(x.T[idx, :], expect.T)
@pytest.mark.parametrize("chunks", [1, 2, 3])
def test_index_with_int_dask_array_0d(chunks):
# Slice by 0-dimensional array
x = da.from_array([[10, 20, 30], [40, 50, 60]], chunks=chunks)
idx0 = da.from_array(1, chunks=1)
assert_eq(x[idx0, :], x[1, :])
assert_eq(x[:, idx0], x[:, 1])
@pytest.mark.parametrize("chunks", [1, 2, 3, 4, 5])
def test_index_with_int_dask_array_nanchunks(chunks):
# Slice by array with nan-sized chunks
a = da.arange(-2, 3, chunks=chunks)
assert_eq(a[a.nonzero()], np.array([-2, -1, 1, 2]))
# Edge case: the nan-sized chunks resolve to size 0
a = da.zeros(5, chunks=chunks)
assert_eq(a[a.nonzero()], np.array([]))
@pytest.mark.parametrize("chunks", [2, 4])
def test_index_with_int_dask_array_negindex(chunks):
a = da.arange(4, chunks=chunks)
idx = da.from_array([-1, -4], chunks=1)
assert_eq(a[idx], np.array([3, 0]))
@pytest.mark.parametrize("chunks", [2, 4])
def test_index_with_int_dask_array_indexerror(chunks):
a = da.arange(4, chunks=chunks)
idx = da.from_array([4], chunks=1)
with pytest.raises(IndexError):
a[idx].compute()
idx = da.from_array([-5], chunks=1)
with pytest.raises(IndexError):
a[idx].compute()
@pytest.mark.parametrize(
"dtype", ["int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"]
)
def test_index_with_int_dask_array_dtypes(dtype):
a = da.from_array([10, 20, 30, 40], chunks=-1)
idx = da.from_array(np.array([1, 2]).astype(dtype), chunks=1)
assert_eq(a[idx], np.array([20, 30]))
def test_index_with_int_dask_array_nocompute():
"""Test that when the indices are a dask array
they are not accidentally computed
"""
def crash():
raise NotImplementedError()
x = da.arange(5, chunks=-1)
idx = da.Array({("x", 0): (crash,)}, name="x", chunks=((2,),), dtype=np.int64)
result = x[idx]
with pytest.raises(NotImplementedError):
result.compute()
def test_index_with_bool_dask_array():
x = np.arange(36).reshape((6, 6))
d = da.from_array(x, chunks=(3, 3))
ind = np.asarray([True, True, False, True, False, False], dtype=bool)
ind = da.from_array(ind, chunks=2)
for index in [ind, (slice(1, 9, 2), ind), (ind, slice(2, 8, 1))]:
x_index = dask.compute(index)[0]
assert_eq(x[x_index], d[index])
def test_index_with_bool_dask_array_2():
rng = np.random.default_rng()
x = rng.random((10, 10, 10))
ind = rng.random(10) > 0.5
d = da.from_array(x, chunks=(3, 4, 5))
dind = da.from_array(ind, chunks=4)
index = [slice(1, 9, 1), slice(None)]
for i in range(x.ndim):
index2 = index[:]
index2.insert(i, dind)
index3 = index[:]
index3.insert(i, ind)
assert_eq(x[tuple(index3)], d[tuple(index2)])
@pytest.mark.xfail
def test_cull():
x = da.ones(1000, chunks=(10,))
for slc in [1, slice(0, 30), slice(0, None, 100)]:
y = x[slc]
assert len(y.dask) < len(x.dask)
@pytest.mark.parametrize("shape", [(2,), (2, 3), (2, 3, 5)])
@pytest.mark.parametrize(
"index", [(Ellipsis,), (None, Ellipsis), (Ellipsis, None), (None, Ellipsis, None)]
)
def test_slicing_with_Nones(shape, index):
x = np.random.default_rng().random(shape)
d = da.from_array(x, chunks=shape)
assert_eq(x[index], d[index])
indexers = [Ellipsis, slice(2), 0, 1, -2, -1, slice(-2, None), None]
"""
# We comment this out because it is 4096 tests
@pytest.mark.parametrize('a', indexers)
@pytest.mark.parametrize('b', indexers)
@pytest.mark.parametrize('c', indexers)
@pytest.mark.parametrize('d', indexers)
def test_slicing_none_int_ellipses(a, b, c, d):
if (a, b, c, d).count(Ellipsis) > 1:
return
shape = (2,3,5,7,11)
x = np.arange(np.prod(shape)).reshape(shape)
y = da.core.asarray(x)
xx = x[a, b, c, d]
yy = y[a, b, c, d]
assert_eq(xx, yy)
"""
def test_slicing_integer_no_warnings():
# https://github.com/dask/dask/pull/2457/
X = da.random.default_rng().random(size=(100, 2), chunks=(2, 2))
idx = np.array([0, 0, 1, 1])
with warnings.catch_warnings(record=True) as record:
X[idx].compute()
assert not record
@pytest.mark.slow
def test_slicing_none_int_ellipes():
shape = (2, 3, 5, 7, 11)
x = np.arange(np.prod(shape)).reshape(shape)
y = da.core.asarray(x)
for ind in itertools.product(indexers, indexers, indexers, indexers):
if ind.count(Ellipsis) > 1:
continue
assert_eq(x[ind], y[ind])
def test_None_overlap_int():
a, b, c, d = (0, slice(None, 2, None), None, Ellipsis)
shape = (2, 3, 5, 7, 11)
x = np.arange(np.prod(shape)).reshape(shape)
y = da.core.asarray(x)
xx = x[a, b, c, d]
yy = y[a, b, c, d]
assert_eq(xx, yy)
def test_negative_n_slicing():
assert_eq(da.ones(2, chunks=2)[-2], np.ones(2)[-2])
def test_negative_list_slicing():
x = np.arange(5)
dx = da.from_array(x, chunks=2)
assert_eq(dx[[0, -5]], x[[0, -5]])
assert_eq(dx[[4, -1]], x[[4, -1]])
def test_permit_oob_slices():
x = np.arange(5)
dx = da.from_array(x, chunks=2)
assert_eq(x[-102:], dx[-102:])
assert_eq(x[102:], dx[102:])
assert_eq(x[:102], dx[:102])
assert_eq(x[:-102], dx[:-102])
def test_normalize_index():
assert normalize_index((Ellipsis, None), (10,)) == (slice(None), None)
assert normalize_index(5, (np.nan,)) == (5,)
assert normalize_index(-5, (np.nan,)) == (-5,)
(result,) = normalize_index([-5, -2, 1], (np.nan,))
assert result.tolist() == [-5, -2, 1]
assert normalize_index(slice(-5, -2), (np.nan,)) == (slice(-5, -2),)
def test_take_semi_sorted():
x = da.ones(10, chunks=(5,))
index = np.arange(15) % 10
y = x[index]
assert y.chunks == ((5, 5, 5),)
@pytest.mark.parametrize(
"chunks,index,expected",
[
((5, 5, 5), np.arange(5, 15) % 10, [(1, np.arange(5)), (0, np.arange(5))]),
(
(5, 5, 5, 5),
np.arange(20) // 2,
[(0, np.arange(10) // 2), (1, np.arange(10) // 2)],
),
((10, 10), [15, 2, 3, 15], [(1, [5]), (0, [2, 3]), (1, [5])]),
],
)
def test_slicing_plan(chunks, index, expected):
plan = slicing_plan(chunks, index=index)
assert len(plan) == len(expected)
for (i, x), (j, y) in zip(plan, expected):
assert i == j
assert len(x) == len(y)
assert (x == y).all()
def test_getitem_avoids_large_chunks():
with dask.config.set({"array.chunk-size": "0.1Mb"}):
a = np.arange(2 * 128 * 128, dtype="int64").reshape(2, 128, 128)
indexer = [0] + [1] * 11
arr = da.from_array(a, chunks=(1, 8, 8))
result = arr[
indexer
] # small chunks within the chunk-size limit should NOT raise PerformanceWarning
expected = a[indexer]
assert_eq(result, expected)
arr = da.from_array(a, chunks=(1, 128, 128)) # large chunks
expected = a[indexer]
# By default, we warn
with pytest.warns(da.PerformanceWarning):
result = arr[indexer]
assert_eq(result, expected)
assert result.chunks == ((1, 11), (128,), (128,))
# Users can silence the warning
with dask.config.set({"array.slicing.split-large-chunks": False}):
with warnings.catch_warnings(record=True) as record:
result = arr[indexer]
assert_eq(result, expected)
assert not record
# Users can silence the warning
with dask.config.set({"array.slicing.split-large-chunks": True}):
with warnings.catch_warnings(record=True) as record:
result = arr[indexer]
assert_eq(result, expected)
assert not record
assert result.chunks == ((1,) * 12, (128,), (128,))
def test_getitem_avoids_large_chunks_missing():
# We cannot apply the "avoid large chunks" optimization when
# the chunks have unknown sizes.
with dask.config.set({"array.chunk-size": "0.1Mb"}):
a = np.arange(4 * 500 * 500).reshape(4, 500, 500)
arr = da.from_array(a, chunks=(1, 500, 500))
arr._chunks = ((1, 1, 1, 1), (np.nan,), (np.nan,))
indexer = [0, 1] + [2] * 100 + [3]
expected = a[indexer]
result = arr[indexer]
assert_eq(result, expected)
def test_take_avoids_large_chunks():
# unit test for https://github.com/dask/dask/issues/6270
with dask.config.set({"array.slicing.split-large-chunks": True}):
chunks = ((1, 1, 1, 1), (500,), (500,))
itemsize = 8
index = np.array([0, 1] + [2] * 101 + [3])
chunks2, dsk = take("a", "b", chunks, index, itemsize)
assert chunks2 == ((1, 1, 51, 50, 1), (500,), (500,))
assert len(dsk) == 5
index = np.array([0] * 101 + [1, 2, 3])
chunks2, dsk = take("a", "b", chunks, index, itemsize)
assert chunks2 == ((51, 50, 1, 1, 1), (500,), (500,))
assert len(dsk) == 5
index = np.array([0, 1, 2] + [3] * 101)
chunks2, dsk = take("a", "b", chunks, index, itemsize)
assert chunks2 == ((1, 1, 1, 51, 50), (500,), (500,))
assert len(dsk) == 5
chunks = ((500,), (1, 1, 1, 1), (500,))
index = np.array([0, 1, 2] + [3] * 101)
chunks2, dsk = take("a", "b", chunks, index, itemsize, axis=1)
assert chunks2 == ((500,), (1, 1, 1, 51, 50), (500,))
assert len(dsk) == 5
def test_take_uses_config():
with dask.config.set({"array.slicing.split-large-chunks": True}):
chunks = ((1, 1, 1, 1), (500,), (500,))
index = np.array([0, 1] + [2] * 101 + [3])
itemsize = 8
with config.set({"array.chunk-size": "10GB"}):
chunks2, dsk = take("a", "b", chunks, index, itemsize)
assert chunks2 == ((1, 1, 101, 1), (500,), (500,))
assert len(dsk) == 4
def test_pathological_unsorted_slicing():
x = da.ones(100, chunks=10)
# [0, 10, 20, ... 90, 1, 11, 21, ... 91, ...]
index = np.arange(100).reshape(10, 10).ravel(order="F")
with pytest.warns(da.PerformanceWarning) as info:
x[index]
assert "10" in str(info.list[0])
assert "out-of-order" in str(info.list[0])
@pytest.mark.parametrize("params", [(2, 2, 1), (5, 3, 2)])
def test_setitem_with_different_chunks_preserves_shape(params):
"""Reproducer for https://github.com/dask/dask/issues/3730.
Mutating based on an array with different chunks can cause new chunks to be
used. We need to ensure those new chunk sizes are applied to the mutated
array, otherwise the array won't generate the correct keys.
"""
array_size, chunk_size1, chunk_size2 = params
x = da.zeros(array_size, chunks=chunk_size1)
mask = da.zeros(array_size, chunks=chunk_size2)
x[mask] = 1
result = x.compute()
assert x.shape == result.shape