Debian Codegraph @

docs/source/array-slicing.rst

raw
1 Slicing
2 =======
3
4 Dask Array supports most of the NumPy slicing syntax. In particular, it
5 supports the following:
6
7 * Slicing by integers and slices: ``x[0, :5]``
8 * Slicing by lists/arrays of integers: ``x[[1, 2, 4]]``
9 * Slicing by lists/arrays of booleans: ``x[[False, True, True, False, True]]``
10 * Slicing one :class:`~dask.array.Array` with an :class:`~dask.array.Array` of bools: ``x[x > 0]``
11 * Slicing one :class:`~dask.array.Array` with a zero or one-dimensional :class:`~dask.array.Array`
12 of ints: ``a[b.argtopk(5)]``
13
14 However, it does not currently support the following:
15
16 * Slicing with lists in multiple axes: ``x[[1, 2, 3], [3, 2, 1]]``
17
18 This is straightforward to add though. If you have a use case then raise an
19 issue. Also, users interested in this should take a look at
20 :attr:`~dask.array.Array.vindex`.
21
22 * Slicing one :class:`~dask.array.Array` with a multi-dimensional :class:`~dask.array.Array` of ints
23
24 .. _array.slicing.efficiency:
25
26 Efficiency
27 ----------
28
29 The normal Dask schedulers are smart enough to compute only those blocks that
30 are necessary to achieve the desired slicing. Hence, large operations may be cheap
31 if only a small output is desired.
32
33 In the example below, we create a Dask array with a trillion elements with million
34 element sized blocks. We then operate on the entire array and finally slice out
35 only a portion of the output:
36
37 .. code-block:: python
38
39 >>> # Trillion element array of ones, in 1000 by 1000 blocks
40 >>> x = da.ones((1000000, 1000000), chunks=(1000, 1000))
41
42 >>> da.exp(x)[:1500, :1500]
43 ...
44
45 This only needs to compute the top-left four blocks to achieve the result. We
46 are slightly wasteful on those blocks where we need only partial results. Moreover,
47 we are also a bit wasteful in that we still need to manipulate the Dask graph
48 with a million or so tasks in it. This can cause an interactive overhead of a
49 second or two.
50
51 Slicing with concrete indexers (a list of integers, say) has a couple of possible
52 failure modes that are worth mentioning. First, when you're indexing a chunked
53 axis, Dask will typically "match" the chunking on the output.
54
55 .. code-block:: python
56
57 # Array of ones, chunked along axis 0
58 >>> a = da.ones((4, 10000, 10000), chunks=(1, -1, -1))
59
60 If we slice that with a *sorted* sequence of integers, Dask will return one chunk
61 per input chunk (notice the output `chunksize` is 1, since the indices ``0``
62 and ``1`` are in separate chunks in the input).
63
64 >>> a[[0, 1], :, :] #doctest: +SKIP
65 dask.array<getitem, shape=(2, 10000, 10000), dtype=float64, chunksize=(1, 10000, 10000), chunktype=numpy.ndarray>
66
67 But what about repeated indices? Dask continues to return one chunk per input chunk,
68 but if you have many repetitions from the same input chunk, your output chunk could
69 be much larger.
70
71 .. code-block:: python
72
73 >>> a[[0] * 15, :, :]
74 PerformanceWarning: Slicing is producing a large chunk. To accept the large
75 chunk and silence this warning, set the option
76 >>> with dask.config.set({'array.slicing.split_large_chunks': False}):
77 ... array[indexer]
78
79 To avoid creating the large chunks, set the option
80 >>> with dask.config.set({'array.slicing.split_large_chunks': True}):
81 ... array[indexer]
82 dask.array<getitem, shape=(15, 10000, 10000), dtype=float64, chunksize=(15, 10000, 10000), chunktype=numpy.ndarray>
83
84 Previously we had a chunksize of ``1`` along the first dimension since we selected
85 just one element from each input chunk. But now we've selected 15 elements
86 from the first chunk, producing a large output chunk.
87
88 Dask warns when indexing like this produces a chunk that's 5x larger
89 than the ``array.chunk-size`` config option. You have two options to deal with
90 that warning:
91
92 1. Set ``dask.config.set({"array.slicing.split_large_chunks": False})`` to
93 allow the large chunk and silence the warning.
94 2. Set ``dask.config.set({"array.slicing.split_large_chunks": True})`` to
95 avoid creating the large chunk in the first place.
96
97 The right choice will depend on your downstream operations. See :ref:`array.chunks`
98 for more on choosing chunk sizes.