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