`map_blocks` leads to downstream TypeError: can not serialize 'function' object
See original GitHub issueWhat happened:
The motivation for this bug report comes from the observation that using dask.array.map_blocks
on a function def func(block_info=None)
in combination with a distributed.Client
to visualize arrays in napari
leads to the error TypeError: can not serialize 'function' object
.
The error occurs only when using the dask.distributed
backend, it works with a dask
backend.
The error occurs only when mapping a function def func(block_info=None)
and not when using def func()
or def func(x, block_info=None)
.
For more context, see https://github.com/dask/dask-image/issues/194.
What you expected to happen:
That passing dask arrays to napari
(which slices the array and calls np.asarray
) should work independently of whether a dask
or dask.distributed
backend is used.
Minimal Complete Verifiable Example:
The original error occurring in combination with napari
:
%gui qt
import dask.array as da
import numpy as np
import napari
from dask.distributed import Client
from dask_image.imread import imread
client = Client()
xn = np.random.randint(0, 100, (20, 30, 30))
xd = da.from_array(xn, chunks=(1, 5, 5))
def func(block_info=None):
return np.random.randint(0, 100, (1, 5, 5))
# this instead works (no `block_info` argument)
# def func():
# return np.random.randint(0, 100, (1, 5, 5))
xm = da.map_blocks(func, chunks=xd.chunks, dtype=xd.dtype)
napari.view_image(xm)
Likely, this minimal example reproduces the error without napari
:
import dask.array as da
import numpy as np
from dask.core import flatten
from dask.distributed import Client
client = Client()
xn = np.random.randint(0, 100, (2, 4, 4))
xd = da.from_array(xn, chunks=(1, 2, 2))
# fails
def func(block_info=None):
return np.random.randint(0, 100, (1, 2, 2))
# works
# def func():
# return np.random.randint(0, 100, (1, 2, 2))
xm = da.map_blocks(func, chunks=xd.chunks, dtype=xd.dtype)
xm.dask.__dask_distributed_pack__(client, set(flatten(xm.__dask_keys__())))
Anything else we need to know?:
The original error does not occur with dask
and dask.distributed
version 2.30.0 and lower (the minimal example is incompatible with older versions).
Environment:
- Dask version: 2021.03.0
- Python version: 3.9.2
- Operating System: macOS
- Install method (conda, pip, source): pip
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Closed via https://github.com/dask/dask/pull/7353. Thanks @m-albert for reporting and @madsbk for fixing!
Also as @jni points out, it seems a critical piece here is whether array fusion is enabled/disabled
Traceback: