Create dask array in a single block by calling a function Calling the provided function with func(*args, **kwargs) should return a NumPy array of the indicated shape and dtype. Examples -------- >>> a = from_func(np.arange, (3,), dtype='i8', args=(3,)) >>> a.compute()
(func, shape, dtype=None, name=None, args=(), kwargs=None)
| 4030 | |
| 4031 | |
| 4032 | def from_func(func, shape, dtype=None, name=None, args=(), kwargs=None): |
| 4033 | """Create dask array in a single block by calling a function |
| 4034 | |
| 4035 | Calling the provided function with func(*args, **kwargs) should return a |
| 4036 | NumPy array of the indicated shape and dtype. |
| 4037 | |
| 4038 | Examples |
| 4039 | -------- |
| 4040 | |
| 4041 | >>> a = from_func(np.arange, (3,), dtype='i8', args=(3,)) |
| 4042 | >>> a.compute() |
| 4043 | array([0, 1, 2]) |
| 4044 | |
| 4045 | This works particularly well when coupled with dask.array functions like |
| 4046 | concatenate and stack: |
| 4047 | |
| 4048 | >>> arrays = [from_func(np.array, (), dtype='i8', args=(n,)) for n in range(5)] |
| 4049 | >>> stack(arrays).compute() |
| 4050 | array([0, 1, 2, 3, 4]) |
| 4051 | """ |
| 4052 | if kwargs is None: |
| 4053 | kwargs = {} |
| 4054 | |
| 4055 | name = name or "from_func-" + tokenize(func, shape, dtype, args, kwargs) |
| 4056 | if args or kwargs: |
| 4057 | func = partial(func, *args, **kwargs) |
| 4058 | dsk = {(name,) + (0,) * len(shape): (func,)} |
| 4059 | chunks = tuple((i,) for i in shape) |
| 4060 | return Array(dsk, name, chunks, dtype) |
| 4061 | |
| 4062 | |
| 4063 | def common_blockdim(blockdims): |