-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_graph.py
executable file
·71 lines (62 loc) · 2.18 KB
/
gen_graph.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
#
# Name: Dependency Graph Generator
# Author: Vincent Perricone
# License: Released under "Simplified BSD License"
# Description:
# gen_graph.py generates fancy graphs from files in serializable NetworkX json format [1].
# These json files are read from the 'dots' directory. All files in this directory need to
# have the extension '.dot.json'.
#
# To get started there are a few examples in the 'dots' directory. Familiarity with graphviz
# is pretty important too. The NetworkX documentation is pretty amazing so check it out.
#
# [1]: https://networkx.github.io/documentation/stable/reference/readwrite/json_graph.html
#
import os
import json
import networkx as nx
import matplotlib.pyplot as plt
from networkx.readwrite import json_graph
#Directory and extension of files expected.
dots_dir = 'dots'
desired_file_extension = '.dot.json'
#Output png image filename
output_file_png = 'generated_graph.png'
# Create default generic directed graph
G = nx.DiGraph(name='Graph', strict=False, directed=True)
# Store graphs in node-link-format after being deserialized into memory
graphs = []
def output_graph(output_file='generated_graph.png'):
"""
Save generate image to path defined by output_file parameter to disk.
"""
nx.draw(G, with_labels=True)
plt.savefig(output_file_png)
def build_graph():
"""
Loop over graphs in list graphs and compose larger graph.
We access global initial graph G for ease
"""
global G
for graph in graphs:
G = nx.compose(G, graph)
def load_json_dots(search_path):
"""
Find json files that meet extension parameters defined by
desired_file_extension variable.
Deserialize into memory and store in graphs list
"""
for root, dirs, files in os.walk(search_path):
for file in files:
if file.endswith(desired_file_extension):
with open(os.path.join(root, file)) as f:
file_data = json.load(f)
graph_data = json_graph.node_link_graph(file_data)
graphs.append(graph_data)
def main():
load_json_dots(dots_dir)
build_graph()
output_graph()
if __name__== "__main__":
main()