def filter_graph_collapse_sequence()

in lucid/scratch/pretty_graphs/graph.py [0:0]


def filter_graph_collapse_sequence(graph, sequence):
  exclude_nodes = []

  for node in graph.nodes:
    remainder = sequence[:]
    matches = []
    while remainder:
      if len(node.consumers) > 1 and len(remainder) > 1:
        break
      if node.op == remainder[0]:
        matches.append(node.name)
        node = node.consumers[0]
        remainder = remainder[1:]
      else:
        break
    if len(remainder) == 0:
      exclude_nodes += matches[:-1]

  include_nodes = [node.name for node in graph.nodes
                   if node.name not in exclude_nodes]

  return filter_graph(graph, include_nodes)