-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric_data_converter.py
70 lines (53 loc) · 1.31 KB
/
metric_data_converter.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
'''
converts a regular json file to the file format required by:
https://github.com/VIDILabs/glam
in order to calculate graph aesthetic metrics
'''
import json
import sys
import os
filename = sys.argv[-1]
nodes = {}
nodeCoords = []
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
# list where nodes[node_index] = (x, y)
x = node['x']
y = node['y']
nodeCoords.append((x, y))
# creating list of edges
for edge in data['links']:
source = edge['source']
target = edge['target']
edges.append((source, target))
#### done with json loading
#### to write to json
metricFormat = {}
metricFormat['nodes'] = []
metricFormat['links'] = []
# this list should be in order
for node in nodeCoords:
metricFormat['nodes'].append({
'x': float(node[0]),
'y': float(node[1])
})
for edge in edges:
source = edge[0] # returns a string for node name
target = edge[1]
metricFormat['links'].append({
'source': nodes[source],
'target': nodes[target]
})
outFile = os.path.splitext(filename)[0] + "_metricFormat.json"
with open( outFile, 'w') as outfile:
json.dump(metricFormat, outfile, indent=4)
##