-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplorge_graph.py
More file actions
152 lines (125 loc) · 4.81 KB
/
splorge_graph.py
File metadata and controls
152 lines (125 loc) · 4.81 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import networkx as nx
import plotly.offline
import plotly.graph_objs as go
import json
import warnings
warnings.filterwarnings("ignore")
filename = 'logger/PythonShootGameOut.json'
with open(filename) as log_json:
data_dict = json.load(log_json)
start_size = 90
node_lists = [[]]
edge_lists = [[]]
current_node_list = []
current_edge_list = []
name_list = []
depth_list = []
size_list = []
maxX = []
maxY = []
minX = []
minY = []
color_list = ['rgb(200,0,0)', 'rgb(200,200,0)', 'rgb(200,0,200)', 'rgb(0,200,0)', 'rgb(0,200,200)',
'rgb(100,0,0)', 'rgb(100,100,0)', 'rgb(100,0,100)', 'rgb(0,100,0)', 'rgb(0,100,100)',
'rgb(50,60,70)', 'rgb(80,70,60)', 'rgb(255,150,100)', 'rgb(100,150,255)', 'rgb(150,255,100)']
def parse_nodes(node_data, depth):
depth_list.append(depth)
node = (node_data['name'], node_data['time'])
time_span = 0
if current_node_list:
start_time = current_node_list[-1][1]
time_span = int(node_data['time']) - int(start_time)
size_list.append(time_span + 30)
if time_span > 0:
# name_list.append(node_data['name'] + ': ' + str(time_span) + ' ms')
name_list.append(node_data['name'])
else:
name_list.append(node_data['name'])
current_node_list.append(node)
node_lists.append(current_node_list.copy())
edge_lists.append(current_edge_list.copy())
for child in node_data['children']:
current_edge_list.append((node, (child['name'], child['time'])))
parse_nodes(child, depth+1)
return node
parse_nodes(data_dict,1)
def create_digraph(nodes, edges):
G = nx.DiGraph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
return G
def build_data(digraph):
if digraph.nodes():
first_node = list(digraph.nodes)[0]
node_pos = nx.spring_layout(digraph, pos={first_node:(0,0)}, fixed=[first_node], seed=1)
maxX.append(max([pos[0] for pos in node_pos.values()]))
maxY.append(max([pos[1] for pos in node_pos.values()]))
minX.append(min([pos[1] for pos in node_pos.values()]))
minY.append(min([pos[1] for pos in node_pos.values()]))
else:
node_pos = nx.spring_layout(digraph, seed=1)
node_trace = go.Scatter(
x=[pos[0] for pos in node_pos.values()],
y=[pos[1] for pos in node_pos.values()],
text=name_list,
mode='markers+text',
textposition='top center',
textfont=dict(
family='arial',
size=18,
color='rgb(0,0,0)'
),
hoverinfo='none',
marker=go.Marker(
showscale=False,
color=[color_list[depth-1 % len(color_list)] for depth in depth_list],
opacity=1,
#size= [(start_size * 1/(depth)) for depth in depth_list],
size= size_list,
line=go.Line(width=1, color='rgb(0,0,0)')))
edgesX = []
edgesY = []
for edge in digraph.edges:
try:
x0, y0 = node_pos[edge[0]]
x1, y1 = node_pos[edge[1]]
edgesX.extend([x0, x1, None])
edgesY.extend([y0, y1, None])
except KeyError:
continue
edge_trace = go.Scatter(
x=edgesX,
y=edgesY,
line=go.Line(width=1, color='rgb(150,150,150)'),
hoverinfo='none',
mode='lines')
return go.Data([edge_trace, node_trace])
data_graph = create_digraph(current_node_list, current_edge_list)
data_vals = build_data(data_graph)
frame_list = []
for i in range(len(node_lists)):
node_list = node_lists[i]
edge_list = edge_lists[i]
frame_graph = create_digraph(node_list, edge_list)
frame_data = build_data(frame_graph)
frame_list.append({'data':frame_data, 'name':'frame{}'.format(i)})
trueMaxX = max(maxX) + 0.3
trueMaxY = max(maxY) + 0.3
trueMinX = min(minX) - 0.3
trueMinY = min(minY) - 0.3
fig = go.Figure(data = data_vals,
layout = go.Layout(
title = '{}: Dynamic Call Graph'.format(filename),
titlefont = dict(size=16),
updatemenus = [{'type': 'buttons',
'buttons': [{'label': 'Play',
'method': 'animate',
'args': [None, {'frame': {'duration': 500, 'redraw': False},
'fromcurrent': True, 'transition': {'duration': 300, 'easing': 'quadratic-in-out'}}]}]}],
showlegend = False,
hovermode = 'closest',
margin = dict(b=20,l=20,r=20,t=40),
xaxis = go.XAxis(range=[-1*trueMaxX, trueMaxX], showgrid=False, zeroline=False, showticklabels=False),
yaxis = go.YAxis(range=[-1*trueMaxY, trueMaxY], showgrid=False, zeroline=False, showticklabels=False)),
frames=frame_list)
plotly.offline.plot(fig)