-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptimize.py
executable file
·30 lines (24 loc) · 917 Bytes
/
optimize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import networkx as nx
from .graph_tasks import GraphPruningTask, GraphConvertTask
def optimizePruning(graph):
pruneGraph = {}
for n in graph:
nTask = graph.node[n]['task']
if isinstance(nTask, GraphPruningTask):
for p in graph.predecessors(n):
pTask = graph.node[p]['task']
if isinstance(pTask, GraphConvertTask):
pred = p
break
name = nTask.name
depth = nTask.maxDepth
if name not in pruneGraph:
pruneGraph[name] = []
pruneGraph[name].append((n, pred, depth))
for n, D in pruneGraph.items():
D = sorted(D, key=lambda x: x[2], reverse=True)
for i in range(1, len(D)):
prune, pred, _ = D[i]
nPred, _, _ = D[i-1]
graph.remove_edge(pred, prune)
graph.add_edge(nPred, prune)