map_blocks

This function/method maps through the blocks in a dask array and apply a customized function on it.

map_blocks function from straight dask.array can take multiple dask arrays.

 
da.map_blocks(lambda x: x * 2, data)
 
# or
 
data.map_blocks(lambda x: x * 2)

lambda gets numpy.ndarray as chunks.

Note

Chunks passed to the lambda are not fully evaluated. For instance, accessing any other element rather than first one will give error. chunk[2] will give error whilst chunk[0] won’t.

Examples

  1. Map on two dask arrays of different block numbers
d5 = da.from_array(np.random.random(10), chunks=2)
d6 = da.arange(25, chunks=(5))
 
res = da.map_blocks(lambda x, y: np.array([x.max(), y.max()]), d5, d6)
 
res.compute()

lambda returns chunk with max values from two corresponding chunks. So, res.compute() returns max values from corresponding chunks of two dask arrays.