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)
| 4184 | |
| 4185 | |
| 4186 | def from_delayed(value, shape, dtype=None, meta=None, name=None): |
| 4187 | """Create a dask array from a dask delayed value |
| 4188 | |
| 4189 | This routine is useful for constructing dask arrays in an ad-hoc fashion |
| 4190 | using dask delayed, particularly when combined with stack and concatenate. |
| 4191 | |
| 4192 | The dask array will consist of a single chunk. |
| 4193 | |
| 4194 | Examples |
| 4195 | -------- |
| 4196 | >>> import dask |
| 4197 | >>> import dask.array as da |
| 4198 | >>> import numpy as np |
| 4199 | >>> value = dask.delayed(np.ones)(5) |
| 4200 | >>> array = da.from_delayed(value, (5,), dtype=float) |
| 4201 | >>> array |
| 4202 | dask.array<from-value, shape=(5,), dtype=float64, chunksize=(5,), chunktype=numpy.ndarray> |
| 4203 | >>> array.compute() |
| 4204 | array([1., 1., 1., 1., 1.]) |
| 4205 | """ |
| 4206 | from dask.delayed import Delayed, delayed |
| 4207 | |
| 4208 | is_future = False |
| 4209 | name = name or "from-value-" + tokenize(value, shape, dtype, meta) |
| 4210 | if isinstance(value, TaskRef): |
| 4211 | is_future = True |
| 4212 | elif not isinstance(value, Delayed) and hasattr(value, "key"): |
| 4213 | value = delayed(value) |
| 4214 | task = Alias( |
| 4215 | key=(name,) + (0,) * len(shape), |
| 4216 | target=value.key, |
| 4217 | ) |
| 4218 | |
| 4219 | dsk = {task.key: task} |
| 4220 | |
| 4221 | if is_future: |
| 4222 | dsk[value.key] = value |
| 4223 | dependencies = [] |
| 4224 | else: |
| 4225 | dependencies = [value] |
| 4226 | chunks = tuple((d,) for d in shape) |
| 4227 | # TODO: value._key may not be the name of the layer in value.dask |
| 4228 | # This should be fixed after we build full expression graphs |
| 4229 | graph = HighLevelGraph.from_collections(name, dsk, dependencies=dependencies) |
| 4230 | return Array(graph, name, chunks, dtype=dtype, meta=meta) |
| 4231 | |
| 4232 | |
| 4233 | def from_func(func, shape, dtype=None, name=None, args=(), kwargs=None): |