Return new dask with the given keys inlined with their values. Inlines all constants if ``inline_constants`` keyword is True. Note that the constant keys will remain in the graph, to remove them follow ``inline`` with ``cull``. Examples -------- >>> def inc(x): ...
(dsk, keys=None, inline_constants=True, dependencies=None)
| 239 | |
| 240 | |
| 241 | def inline(dsk, keys=None, inline_constants=True, dependencies=None): |
| 242 | """Return new dask with the given keys inlined with their values. |
| 243 | |
| 244 | Inlines all constants if ``inline_constants`` keyword is True. Note that |
| 245 | the constant keys will remain in the graph, to remove them follow |
| 246 | ``inline`` with ``cull``. |
| 247 | |
| 248 | Examples |
| 249 | -------- |
| 250 | >>> def inc(x): |
| 251 | ... return x + 1 |
| 252 | |
| 253 | >>> def add(x, y): |
| 254 | ... return x + y |
| 255 | |
| 256 | >>> d = {'x': 1, 'y': (inc, 'x'), 'z': (add, 'x', 'y')} |
| 257 | >>> inline(d) # doctest: +ELLIPSIS |
| 258 | {'x': 1, 'y': (<function inc at ...>, 1), 'z': (<function add at ...>, 1, 'y')} |
| 259 | |
| 260 | >>> inline(d, keys='y') # doctest: +ELLIPSIS |
| 261 | {'x': 1, 'y': (<function inc at ...>, 1), 'z': (<function add at ...>, 1, (<function inc at ...>, 1))} |
| 262 | |
| 263 | >>> inline(d, keys='y', inline_constants=False) # doctest: +ELLIPSIS |
| 264 | {'x': 1, 'y': (<function inc at ...>, 'x'), 'z': (<function add at ...>, 'x', (<function inc at ...>, 'x'))} |
| 265 | """ |
| 266 | if dependencies and isinstance(next(iter(dependencies.values())), list): |
| 267 | dependencies = {k: set(v) for k, v in dependencies.items()} |
| 268 | |
| 269 | keys = _flat_set(keys) |
| 270 | |
| 271 | if dependencies is None: |
| 272 | dependencies = {k: get_dependencies(dsk, k) for k in dsk} |
| 273 | |
| 274 | if inline_constants: |
| 275 | keys.update( |
| 276 | k |
| 277 | for k, v in dsk.items() |
| 278 | if (ishashable(v) and v in dsk) or (not dependencies[k] and not istask(v)) |
| 279 | ) |
| 280 | |
| 281 | # Keys may depend on other keys, so determine replace order with toposort. |
| 282 | # The values stored in `keysubs` do not include other keys. |
| 283 | replaceorder = toposort( |
| 284 | {k: dsk[k] for k in keys if k in dsk}, dependencies=dependencies |
| 285 | ) |
| 286 | keysubs = {} |
| 287 | for key in replaceorder: |
| 288 | val = dsk[key] |
| 289 | for dep in keys & dependencies[key]: |
| 290 | if dep in keysubs: |
| 291 | replace = keysubs[dep] |
| 292 | else: |
| 293 | replace = dsk[dep] |
| 294 | val = subs(val, dep, replace) |
| 295 | keysubs[key] = val |
| 296 | |
| 297 | # Make new dask with substitutions |
| 298 | dsk2 = keysubs.copy() |
searching dependent graphs…