-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_to_dot.py
56 lines (37 loc) · 947 Bytes
/
json_to_dot.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
'''
script for converting our data files
to the format required for computing metrics
'''
import json
import sys
import os
filename = sys.argv[-1]
nodes = {}
edges = []
n = 1
# need to change the node names to just regular ints
with open(filename) as json_file:
data = json.load(json_file)
# creating list of nodes
for node in data['nodes']:
# map where nodes[name] = node_index
id = node['id']
nodes[id] = n
n = n+1
# creating list of edges
for edge in data['links']:
source = edge['source']
target = edge['target']
edges.append((source, target))
#### done with json loading
outFile = os.path.splitext(filename)[0] + "_gv.txt"
file = open(outFile, "w")
file.write("digraph G {\n")
for edge in edges:
source = edge[0] # returns a string for node name
target = edge[1]
if source != target:
file.write(" " + str(nodes[source]) + " -> " + str(nodes[target]) + ";\n")
file.write("}")
file.close()
##