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)
| 4236 | |
| 4237 | |
| 4238 | def from_func(func, shape, dtype=None, name=None, args=(), kwargs=None): |
| 4239 | """Create dask array in a single block by calling a function |
| 4240 | |
| 4241 | Calling the provided function with func(*args, **kwargs) should return a |
| 4242 | NumPy array of the indicated shape and dtype. |
| 4243 | |
| 4244 | Examples |
| 4245 | -------- |
| 4246 | |
| 4247 | >>> a = from_func(np.arange, (3,), dtype='i8', args=(3,)) |
| 4248 | >>> a.compute() |
| 4249 | array([0, 1, 2]) |
| 4250 | |
| 4251 | This works particularly well when coupled with dask.array functions like |
| 4252 | concatenate and stack: |
| 4253 | |
| 4254 | >>> arrays = [from_func(np.array, (), dtype='i8', args=(n,)) for n in range(5)] |
| 4255 | >>> stack(arrays).compute() |
| 4256 | array([0, 1, 2, 3, 4]) |
| 4257 | """ |
| 4258 | if kwargs is None: |
| 4259 | kwargs = {} |
| 4260 | |
| 4261 | name = name or "from_func-" + tokenize(func, shape, dtype, args, kwargs) |
| 4262 | if args or kwargs: |
| 4263 | func = partial(func, *args, **kwargs) |
| 4264 | dsk = {(name,) + (0,) * len(shape): (func,)} |
| 4265 | chunks = tuple((i,) for i in shape) |
| 4266 | return Array(dsk, name, chunks, dtype) |
| 4267 | |
| 4268 | |
| 4269 | def common_blockdim(blockdims): |
searching dependent graphs…