-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
36 lines (24 loc) · 1.09 KB
/
pipeline.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
31
32
33
34
35
36
import networkx as nx
import graphviz
class Pipeline:
def __init__(self):
self.graph = nx.DiGraph()
def add_module(self, module_name, dependencies=None):
self.graph.add_node(module_name)
if dependencies:
for i, dep in enumerate(dependencies):
self.graph.add_edge(dep, module_name, weight=i)
def get_dependencies(self, module_name):
deps = [(self.graph.get_edge_data(x, module_name)["weight"], x) for x in self.graph.predecessors(module_name)]
deps.sort(key=lambda x: x[0])
return [x[1] for x in deps]
def get_initial_modules(self):
return [node for node in self.graph.nodes if self.graph.in_degree(node) == 0]
def get_dependents(self, module_name):
return list(self.graph.successors(module_name))
def get_dependency_count(self):
return {node: self.graph.in_degree(node) for node in self.graph.nodes}
def get_execution_order(self):
return list(nx.topological_sort(self.graph))
def to_image(self) -> None:
nx.drawing.nx_pydot.write_dot(self.graph, r'./g.dot')