def extract_diff_graph(graph, diff):
diff_graph = graph.copy()
diff_graph.root_nodes = list()
# pnodes = collections.defaultdict(set)
# for m in diff[0] | diff[2]:
# for n in diff_graph.nodes():
# if nx.has_path(diff_graph, n, m):
# pnodes[m].add(n)
#
# nodes = set.union(*pnodes.values())
nodes = set()
logging.debug('Searching on-paths nodes...')
for m in graph.root_nodes:
for i in diff[0] | diff[2]:
if m == i:
nodes.add(m)
try:
nodes.update(*list(search_paths(diff_graph, m, i)))
except:
pass
if m in nodes:
diff_graph.root_nodes.append(m)
logging.debug('Removing nodes off-path...')
for n in diff_graph.nodes()[:]:
if n not in nodes:
diff_graph.remove_node(n)
logging.debug('Setting node colors...')
for n in diff_graph.nodes():
diff_graph.node[n]['color'] = 'turquoise'
for n in diff[0]:
diff_graph.node[n]['color'] = 'red'
if isinstance(n, Stack):
diff_graph.node[n]['color'] = 'deeppink'
for n in diff[2]:
diff_graph.node[n]['color'] = 'green'
logging.debug('Exporting diff_graph.dot...')
nx.write_dot(diff_graph, 'diff_graph.dot')
return diff_graph
# internal classes
# internal functions
评论列表
文章目录