MCPcopy
hub / github.com/dask/dask / inline

Function inline

dask/optimization.py:241–304  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

239
240
241def 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()

Callers 3

test_inlineFunction · 0.90
inline_functionsFunction · 0.85

Calls 10

get_dependenciesFunction · 0.90
ishashableFunction · 0.90
istaskFunction · 0.90
toposortFunction · 0.90
subsFunction · 0.90
setClass · 0.85
_flat_setFunction · 0.85
valuesMethod · 0.45
itemsMethod · 0.45
copyMethod · 0.45

Tested by 2

test_inlineFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…