MCPcopy Create free account
hub / github.com/dask/dask / simplify_once

Method simplify_once

dask/_expr.py:367–435  ·  view source on GitHub ↗

Simplify an expression This leverages the ``._simplify_down`` and ``._simplify_up`` methods defined on each class Parameters ---------- dependents: defaultdict[list] The dependents for every node. simplified: dict Cache of si

(self, dependents: defaultdict, simplified: dict)

Source from the content-addressed store, hash-verified

365 return expr
366
367 def simplify_once(self, dependents: defaultdict, simplified: dict):
368 """Simplify an expression
369
370 This leverages the ``._simplify_down`` and ``._simplify_up``
371 methods defined on each class
372
373 Parameters
374 ----------
375
376 dependents: defaultdict[list]
377 The dependents for every node.
378 simplified: dict
379 Cache of simplified expressions for these dependents.
380
381 Returns
382 -------
383 expr:
384 output expression
385 """
386 # Check if we've already simplified for these dependents
387 if self._name in simplified:
388 return simplified[self._name]
389
390 expr = self
391
392 while True:
393 out = expr._simplify_down()
394 if out is None:
395 out = expr
396 if not isinstance(out, Expr):
397 return out
398 if out._name != expr._name:
399 expr = out
400
401 # Allow children to simplify their parents
402 for child in expr.dependencies():
403 out = child._simplify_up(expr, dependents)
404 if out is None:
405 out = expr
406
407 if not isinstance(out, Expr):
408 return out
409 if out is not expr and out._name != expr._name:
410 expr = out
411 break
412
413 # Rewrite all of the children
414 new_operands = []
415 changed = False
416 for operand in expr.operands:
417 if isinstance(operand, Expr):
418 # Bandaid for now, waiting for Singleton
419 dependents[operand._name].append(weakref.ref(expr))
420 new = operand.simplify_once(
421 dependents=dependents, simplified=simplified
422 )
423 simplified[operand._name] = new
424 if new._name != operand._name:

Callers 1

simplifyMethod · 0.80

Calls 4

refMethod · 0.80
_simplify_downMethod · 0.45
dependenciesMethod · 0.45
_simplify_upMethod · 0.45

Tested by

no test coverage detected