Debian Codegraph @

docs/source/array-overlap.rst

raw
1 Overlapping Computations
2 ========================
3
4 Some array operations require communication of borders between neighboring
5 blocks. Example operations include the following:
6
7 * Convolve a filter across an image
8 * Sliding sum/mean/max, ...
9 * Search for image motifs like a Gaussian blob that might span the border of a
10 block
11 * Evaluate a partial derivative
12 * Play the game of Life_
13
14 Dask Array supports these operations by creating a new array where each
15 block is slightly expanded by the borders of its neighbors. This costs an
16 excess copy and the communication of many small chunks, but allows localized
17 functions to evaluate in an embarrassingly parallel manner.
18
19 The main API for these computations is the ``map_overlap`` method defined
20 below:
21
22 .. currentmodule:: dask.array
23
24 .. autosummary::
25 map_overlap
26
27 .. autofunction:: map_overlap
28 :noindex:
29
30
31 Explanation
32 -----------
33
34 Consider two neighboring blocks in a Dask array:
35
36 .. image:: images/unoverlapping-neighbors.svg
37 :width: 30%
38 :alt: Two neighboring blocks which do not overlap.
39
40 We extend each block by trading thin nearby slices between arrays:
41
42 .. image:: images/overlapping-neighbors.svg
43 :width: 30%
44 :alt: Two neighboring block with thin strips along their shared border representing data shared between them.
45
46 We do this in all directions, including also diagonal interactions with the
47 overlap function:
48
49 .. image:: images/overlapping-blocks.svg
50 :width: 40%
51 :alt: A two-dimensional grid of blocks where each one has thin strips around their borders representing data shared from their neighbors. They include small corner bits for data shared from diagonal neighbors as well.
52
53 .. code-block:: python
54
55 >>> import dask.array as da
56 >>> import numpy as np
57
58 >>> x = np.arange(64).reshape((8, 8))
59 >>> d = da.from_array(x, chunks=(4, 4))
60 >>> d.chunks
61 ((4, 4), (4, 4))
62
63 >>> g = da.overlap.overlap(d, depth={0: 2, 1: 1},
64 ... boundary={0: 100, 1: 'reflect'})
65 >>> g.chunks
66 ((8, 8), (6, 6))
67
68 >>> np.array(g)
69 array([[100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
70 [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
71 [ 0, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 7],
72 [ 8, 8, 9, 10, 11, 12, 11, 12, 13, 14, 15, 15],
73 [ 16, 16, 17, 18, 19, 20, 19, 20, 21, 22, 23, 23],
74 [ 24, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31, 31],
75 [ 32, 32, 33, 34, 35, 36, 35, 36, 37, 38, 39, 39],
76 [ 40, 40, 41, 42, 43, 44, 43, 44, 45, 46, 47, 47],
77 [ 16, 16, 17, 18, 19, 20, 19, 20, 21, 22, 23, 23],
78 [ 24, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31, 31],
79 [ 32, 32, 33, 34, 35, 36, 35, 36, 37, 38, 39, 39],
80 [ 40, 40, 41, 42, 43, 44, 43, 44, 45, 46, 47, 47],
81 [ 48, 48, 49, 50, 51, 52, 51, 52, 53, 54, 55, 55],
82 [ 56, 56, 57, 58, 59, 60, 59, 60, 61, 62, 63, 63],
83 [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
84 [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]])
85
86
87 Boundaries
88 ----------
89
90 With respect to overlapping, you can specify how to handle the boundaries. Current policies
91 include the following:
92
93 * ``periodic`` - wrap borders around to the other side
94 * ``reflect`` - reflect each border outwards
95 * ``any-constant`` - pad the border with this value
96
97 An example boundary kind argument might look like the following:
98
99 .. code-block:: python
100
101 {0: 'periodic',
102 1: 'reflect',
103 2: np.nan}
104
105 Alternatively, you can use :py:func:`dask.array.pad` for other types of
106 paddings.
107
108
109 Map a function across blocks
110 ----------------------------
111
112 Overlapping goes hand-in-hand with mapping a function across blocks. This
113 function can now use the additional information copied over from the neighbors
114 that is not stored locally in each block:
115
116 .. code-block:: python
117
118 >>> from scipy.ndimage import gaussian_filter
119 >>> def func(block):
120 ... return gaussian_filter(block, sigma=1)
121
122 >>> filt = g.map_blocks(func)
123
124 While in this case we used a SciPy function, any arbitrary function could have been
125 used instead. This is a good interaction point with Numba_.
126
127 If your function does not preserve the shape of the block, then you will need to
128 provide a ``chunks`` keyword argument. If your block size is regular, then this
129 argument can take a block shape of, for example, ``(1000, 1000)``. In case of
130 irregular block sizes, it must be a tuple with the full chunks shape like
131 ``((1000, 700, 1000), (200, 300))``.
132
133 .. code-block:: python
134
135 >>> g.map_blocks(myfunc, chunks=(5, 5))
136
137 If your function needs to know the location of the block on which it operates,
138 you can give your function a keyword argument ``block_id``:
139
140 .. code-block:: python
141
142 def func(block, block_id=None):
143 ...
144
145 This extra keyword argument will be given a tuple that provides the block
146 location like ``(0, 0)`` for the upper-left block or ``(0, 1)`` for the block
147 just to the right of that block.
148
149
150 Trim Excess
151 -----------
152
153 After mapping a blocked function, you may want to trim off the borders from each
154 block by the same amount by which they were expanded. The function
155 ``trim_internal`` is useful here and takes the same ``depth`` argument
156 given to ``overlap``:
157
158 .. code-block:: python
159
160 >>> x.chunks
161 ((10, 10, 10, 10), (10, 10, 10, 10))
162
163 >>> y = da.overlap.trim_internal(x, {0: 2, 1: 1})
164 >>> y.chunks
165 ((6, 6, 6, 6), (8, 8, 8, 8))
166
167
168 Full Workflow
169 -------------
170
171 And so, a pretty typical overlapping workflow includes ``overlap``, ``map_blocks``
172 and ``trim_internal``:
173
174 .. code-block:: python
175
176 >>> x = ...
177 >>> g = da.overlap.overlap(x, depth={0: 2, 1: 2},
178 ... boundary={0: 'periodic', 1: 'periodic'})
179 >>> g2 = g.map_blocks(myfunc)
180 >>> result = da.overlap.trim_internal(g2, {0: 2, 1: 2})
181
182 .. _Life: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
183 .. _Numba: https://numba.pydata.org/