Create a dask array from a dask delayed value This routine is useful for constructing dask arrays in an ad-hoc fashion using dask delayed, particularly when combined with stack and concatenate. The dask array will consist of a single chunk. Examples -------- >>> import das
(value, shape, dtype=None, meta=None, name=None)
| 3983 | |
| 3984 | |
| 3985 | def from_delayed(value, shape, dtype=None, meta=None, name=None): |
| 3986 | """Create a dask array from a dask delayed value |
| 3987 | |
| 3988 | This routine is useful for constructing dask arrays in an ad-hoc fashion |
| 3989 | using dask delayed, particularly when combined with stack and concatenate. |
| 3990 | |
| 3991 | The dask array will consist of a single chunk. |
| 3992 | |
| 3993 | Examples |
| 3994 | -------- |
| 3995 | >>> import dask |
| 3996 | >>> import dask.array as da |
| 3997 | >>> import numpy as np |
| 3998 | >>> value = dask.delayed(np.ones)(5) |
| 3999 | >>> array = da.from_delayed(value, (5,), dtype=float) |
| 4000 | >>> array |
| 4001 | dask.array<from-value, shape=(5,), dtype=float64, chunksize=(5,), chunktype=numpy.ndarray> |
| 4002 | >>> array.compute() |
| 4003 | array([1., 1., 1., 1., 1.]) |
| 4004 | """ |
| 4005 | from dask.delayed import Delayed, delayed |
| 4006 | |
| 4007 | is_future = False |
| 4008 | name = name or "from-value-" + tokenize(value, shape, dtype, meta) |
| 4009 | if isinstance(value, TaskRef): |
| 4010 | is_future = True |
| 4011 | elif not isinstance(value, Delayed) and hasattr(value, "key"): |
| 4012 | value = delayed(value) |
| 4013 | task = Alias( |
| 4014 | key=(name,) + (0,) * len(shape), |
| 4015 | target=value.key, |
| 4016 | ) |
| 4017 | |
| 4018 | dsk = {task.key: task} |
| 4019 | |
| 4020 | if is_future: |
| 4021 | dsk[value.key] = value |
| 4022 | dependencies = [] |
| 4023 | else: |
| 4024 | dependencies = [value] |
| 4025 | chunks = tuple((d,) for d in shape) |
| 4026 | # TODO: value._key may not be the name of the layer in value.dask |
| 4027 | # This should be fixed after we build full expression graphs |
| 4028 | graph = HighLevelGraph.from_collections(name, dsk, dependencies=dependencies) |
| 4029 | return Array(graph, name, chunks, dtype=dtype, meta=meta) |
| 4030 | |
| 4031 | |
| 4032 | def from_func(func, shape, dtype=None, name=None, args=(), kwargs=None): |