-
Notifications
You must be signed in to change notification settings - Fork 0
/
rem_graphics.py
408 lines (360 loc) · 15.1 KB
/
rem_graphics.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'''
Plotly graph rendering supports
Zhe Chen (zkchen@uvic.ca)
'''
from utils import is_valid_key
import plotly.graph_objects as go # create figure
import plotly.express as px # colorscale
import math # ceil
import networkx as nx
def dict_to_text(node: tuple, key: str) -> str:
out_list = ['final', 'quality', 'popularity', 'maintenance', 'type', 'version', 'deprecated']
name, d = node
s = '<b>'+name+'</b>'
for k,v in sorted(d.items()):
if k == key:
s += '<br><b><i>{}</i></b>: {}'.format(k, v)
elif k in out_list:
s += '<br><b>{}</b>: {}'.format(k, v)
return s
def set_scalecolor(val) -> str:
scale = px.colors.diverging.RdYlGn
return scale[val]
def set_node_color(node: tuple, key: str) -> str:
'''
#6959CD - software
green - active :)
red - deprecared :(
'''
name, meta = node
if is_valid_key(data=meta, key='type') and meta['type'] == 'GITHUB':
return '#6959CD'
elif is_valid_key(data=meta, key='non_problematic') and meta['non_problematic'] == True:
# return set_scalecolor(math.ceil(meta[key]*10)) if (meta and key in meta and meta[key] is not None) else 'black'
return set_scalecolor(math.ceil(meta[key]*10)) # node with None metric will be marked problematic and should not enter here
else:
return 'red' if is_valid_key(data=meta, key='deprecated') and meta['deprecated'] else 'green'
def set_node_color_by_scores(node: tuple, key: str) -> str:
'''
node: full node information
key: final, popularity, quality, maintenance
'''
name, meta = node
if is_valid_key(data=meta, key='type') and meta['type'] == 'GITHUB':
return '#6959CD'
elif is_valid_key(data=meta, key='non_problematic') and meta['non_problematic'] == True:
return 'white'
else:
return set_scalecolor(math.ceil(meta[key]*10)) if (meta and key in meta and meta[key] is not None) else 'black'
def set_plain_node_color(node: tuple, dir_list: list) -> str:
name, meta = node
if is_valid_key(data=meta, key='type') and meta['type'] == 'GITHUB':
return '#6959CD'
elif name in dir_list:
return '#6495ED'
else:
return 'grey'
def set_node_marker_size(node: tuple) -> int:
'''
regular size: 10
deprecated size: 15
'''
name, meta = node
return 15 if is_valid_key(meta, 'deprecated') and meta['deprecated'] else 10
def set_node_line_width(node: tuple) -> int:
'''
regular size: 1
deprecated size: 3
'''
name, meta = node
return 3 if is_valid_key(meta, 'deprecated') and meta['deprecated'] else 1
def plain_plotly_graph_to_html(G: nx.Graph, pname: str, pos: dict, title: str = '', outfile: str = 'plain_temp.html'):
rt_sub_G = nx.DiGraph()
# DEVELOPMENT
dev_sub_G = nx.DiGraph()
for u,v,m in G.edges(data=True):
if 'runtime' in m and m['runtime'] is True:
rt_sub_G.add_node(u, **G.nodes()[u])
rt_sub_G.add_node(v, **G.nodes()[v])
rt_sub_G.add_edge(u,v, **m)
if 'development' in m and m['development'] is True:
dev_sub_G.add_node(u, **G.nodes()[u])
dev_sub_G.add_node(v, **G.nodes()[v])
dev_sub_G.add_edge(u,v, **m)
Xv_gh_rt=[pos[n][0] for n in list(rt_sub_G.nodes())]
Yv_gh_rt=[pos[n][1] for n in list(rt_sub_G.nodes())]
Xv_gh_dev=[pos[n][0] for n in list(dev_sub_G.nodes())]
Yv_gh_dev=[pos[n][1] for n in list(dev_sub_G.nodes())]
# edges
Xed_github_rt=[] # RUNTIME
Yed_github_rt=[]
Xed_github_dev=[] # DEVELOPMENT
Yed_github_dev=[]
# GITHUB RUNTIME
for u, v, m in list(rt_sub_G.edges(data=True)):
Xed_github_rt+=[pos[u][0],pos[v][0]]
Yed_github_rt+=[pos[u][1],pos[v][1]]
# GITHUB DEVELOPMENT
for u, v, m in list(dev_sub_G.edges(data=True)):
Xed_github_dev+=[pos[u][0],pos[v][0]]
Yed_github_dev+=[pos[u][1],pos[v][1]]
# vertice node color based on dependency type (filling)
dir_list = list(G.neighbors(pname))
v_scores_gh_rt=[set_plain_node_color(n, dir_list) for n in list(rt_sub_G.nodes(data=True))]
v_scores_gh_dev=[set_plain_node_color(n, dir_list) for n in list(dev_sub_G.nodes(data=True))]
# edge traces
data=[]
for i in range(0, len(Xed_github_rt)-2, 2):
data+=[go.Scatter(
x=Xed_github_rt[i:i+2],
y=Yed_github_rt[i:i+2],
mode='lines',
legendgroup="gh_rt",
showlegend=False,
line=dict(color='lightgrey', width=0.8)
)]
if len(Xed_github_rt) > 0:
data+=[go.Scatter(
x=Xed_github_rt[len(Xed_github_rt)-2:],
y=Yed_github_rt[len(Yed_github_rt)-2:],
mode='lines',
legendgroup="gh_rt",
name="runtime dependency relationships",
line=dict(color='lightgrey', width=0.8)
)]
for i in range(0, len(Xed_github_dev)-2, 2):
data+=[go.Scatter(
x=Xed_github_dev[i:i+2],
y=Yed_github_dev[i:i+2],
mode='lines',
legendgroup="gh_dev",
showlegend=False,
line=dict(color='lightgrey', width=3.2)
)]
if len(Xed_github_dev) > 0:
data+=[go.Scatter(
x=Xed_github_dev[len(Xed_github_dev)-2:],
y=Yed_github_dev[len(Yed_github_dev)-2:],
mode='lines',
legendgroup="gh_dev",
name="development dependency relationships",
line=dict(color='lightgrey', width=3.2)
)]
# node traces
if len(Xv_gh_rt) > 0:
data+=[go.Scatter(x=Xv_gh_rt,
y=Yv_gh_rt,
mode='markers',
legendgroup="gh_rt",
name='runtime dependencies',
marker=dict(symbol='circle',
size=10,
opacity=1,
color=v_scores_gh_rt,
line=dict(color='black', width=1),
),
)]
if len(Xv_gh_dev) > 0:
data+=[go.Scatter(x=Xv_gh_dev,
y=Yv_gh_dev,
mode='markers',
legendgroup="gh_dev",
name='development dependencies (red outline means deprecation)',
marker=dict(symbol='circle',
size=10,
opacity=1,
color=v_scores_gh_dev,
line=dict(color='black', width=1),
),
)]
fig=go.Figure(data=data)
fig.update_layout(
title=title,
title_x=0.5,
legend=dict(orientation="h",
font=dict(size=12)),
xaxis=go.layout.XAxis(showticklabels=False),
yaxis=go.layout.YAxis(showticklabels=False),
paper_bgcolor='white',
plot_bgcolor='white'
)
x_pos_list = [v[0] for v in pos.values()]
y_pos_list = [v[1] for v in pos.values()]
xoffset = (max(x_pos_list) - min(x_pos_list)) * 0.05
yoffset = (max(y_pos_list) - min(y_pos_list)) * 0.05
fig.update_xaxes(range=[min(x_pos_list)-xoffset, max(x_pos_list)+xoffset])
fig.update_yaxes(range=[min(y_pos_list)-yoffset, max(y_pos_list)+yoffset])
return fig.write_html(outfile)
def plotly_graph_to_html(G: nx.Graph, pos: dict, title: str = '', key: str = 'final', outfile: str = 'temp.html'):
'''
G : networkx graph
pos : positions of graph node
title : graph title
key : display of nodes based on scores (final(default)|popularity|quality|maintenance)
outfile : output path for html file
'''
# separating runtime and development dependency networks
# github software subgraph
# RUNTIME
rt_sub_G = nx.DiGraph()
# DEVELOPMENT
dev_sub_G = nx.DiGraph()
for u,v,m in G.edges(data=True):
if 'runtime' in m and m['runtime'] is True:
rt_sub_G.add_node(u, **G.nodes()[u])
rt_sub_G.add_node(v, **G.nodes()[v])
rt_sub_G.add_edge(u,v, **m)
if 'development' in m and m['development'] is True:
dev_sub_G.add_node(u, **G.nodes()[u])
dev_sub_G.add_node(v, **G.nodes()[v])
dev_sub_G.add_edge(u,v, **m)
Xv_gh_rt=[pos[n][0] for n in list(rt_sub_G.nodes())]
Yv_gh_rt=[pos[n][1] for n in list(rt_sub_G.nodes())]
Xv_gh_dev=[pos[n][0] for n in list(dev_sub_G.nodes())]
Yv_gh_dev=[pos[n][1] for n in list(dev_sub_G.nodes())]
# vertice color lists (line)
v_color_gh_rt=[set_node_color(n, key) for n in list(rt_sub_G.nodes(data=True))]
v_color_gh_dev=[set_node_color(n, key) for n in list(dev_sub_G.nodes(data=True))]
# vertice node color based on scores system (fill)
v_scores_gh_rt=[set_node_color_by_scores(n, key) for n in list(rt_sub_G.nodes(data=True))]
v_scores_gh_dev=[set_node_color_by_scores(n, key) for n in list(dev_sub_G.nodes(data=True))]
# vertice size
v_size_gh_rt=[set_node_marker_size(n) for n in list(rt_sub_G.nodes(data=True))]
v_size_gh_dev=[set_node_marker_size(n) for n in list(dev_sub_G.nodes(data=True))]
# vertice line width
v_width_gh_rt=[set_node_line_width(n) for n in list(rt_sub_G.nodes(data=True))]
v_width_gh_dev=[set_node_line_width(n) for n in list(dev_sub_G.nodes(data=True))]
# vertice text lists
v_text_gh_rt=[dict_to_text(n, key) for n in list(rt_sub_G.nodes(data=True))]
v_text_gh_dev=[dict_to_text(n, key) for n in list(dev_sub_G.nodes(data=True))]
# edges
Xed_github_rt=[] # RUNTIME
Yed_github_rt=[]
Xed_github_dev=[] # DEVELOPMENT
Yed_github_dev=[]
# edge color lists
ed_color_github_rt=[]
ed_color_github_dev=[]
# GITHUB RUNTIME
for u, v, m in list(rt_sub_G.edges(data=True)):
Xed_github_rt+=[pos[u][0],pos[v][0]]
Yed_github_rt+=[pos[u][1],pos[v][1]]
ed_color_github_rt+=[m['color']]*2
# GITHUB DEVELOPMENT
for u, v, m in list(dev_sub_G.edges(data=True)):
Xed_github_dev+=[pos[u][0],pos[v][0]]
Yed_github_dev+=[pos[u][1],pos[v][1]]
ed_color_github_dev+=[m['color']]*2
# edge traces
data=[]
for i in range(0, len(Xed_github_rt)-2, 2):
data+=[go.Scatter(
x=Xed_github_rt[i:i+2],
y=Yed_github_rt[i:i+2],
mode='lines',
legendgroup="gh_rt",
showlegend=False,
line=dict(color=ed_color_github_rt[i], width=0.8)
)]
if len(Xed_github_rt) > 0:
data+=[go.Scatter(
x=Xed_github_rt[len(Xed_github_rt)-2:],
y=Yed_github_rt[len(Yed_github_rt)-2:],
mode='lines',
legendgroup="gh_rt",
name="runtime dependency relationships (dark-red means is affected by package deprecation)",
line=dict(color=ed_color_github_rt[len(ed_color_github_rt)-1], width=0.8)
)]
for i in range(0, len(Xed_github_dev)-2, 2):
data+=[go.Scatter(
x=Xed_github_dev[i:i+2],
y=Yed_github_dev[i:i+2],
mode='lines',
legendgroup="gh_dev",
showlegend=False,
line=dict(color=ed_color_github_dev[i], width=3.2)
)]
if len(Xed_github_dev) > 0:
data+=[go.Scatter(
x=Xed_github_dev[len(Xed_github_dev)-2:],
y=Yed_github_dev[len(Yed_github_dev)-2:],
mode='lines',
legendgroup="gh_dev",
name="development dependency relationships (dark-red means is affected by library deprecation)",
line=dict(color=ed_color_github_dev[len(ed_color_github_dev)-1], width=3.2)
)]
# node traces
if len(Xv_gh_rt) > 0:
data+=[go.Scatter(x=Xv_gh_rt,
y=Yv_gh_rt,
mode='markers',
legendgroup="gh_rt",
name='runtime dependencies (red outline means deprecation)',
marker=dict(symbol=[m['symbol'] for x,m in rt_sub_G.nodes(data=True)],
size=v_size_gh_rt,
opacity=1,
color=v_scores_gh_rt,
line=dict(color=v_color_gh_rt, width=v_width_gh_rt),
colorscale="RdYlGn",
showscale=True,
cmin=0.0,
cmid=0.5,
cmax=1.0,
colorbar=dict(
title=dict(text='metric of health: '+key, side='right'),
thickness=10
)),
text=v_text_gh_rt,
hovertemplate = '%{text}'
)]
if len(Xv_gh_dev) > 0:
data+=[go.Scatter(x=Xv_gh_dev,
y=Yv_gh_dev,
mode='markers',
legendgroup="gh_dev",
name='development dependencies (red outline means deprecation)',
marker=dict(symbol=[m['symbol'] for x,m in dev_sub_G.nodes(data=True)],
size=v_size_gh_dev,
opacity=1,
color=v_scores_gh_dev,
line=dict(color=v_color_gh_dev, width=v_width_gh_dev),
colorscale="RdYlGn",
showscale=True,
cmin=0.0,
cmid=0.5,
cmax=1.0,
colorbar=dict(
title=dict(text='metric of health: '+key, side='right'),
thickness=10
)),
text=v_text_gh_dev,
hovertemplate = '%{text}'
)]
fig=go.Figure(data=data)
fig.update_layout(
title=title,
title_x=0.5,
legend=dict(orientation="h",
font=dict(size=12)),
xaxis=go.layout.XAxis(showticklabels=False),
yaxis=go.layout.YAxis(showticklabels=False),
paper_bgcolor='white',
plot_bgcolor='white'
)
x_pos_list = [v[0] for v in pos.values()]
y_pos_list = [v[1] for v in pos.values()]
xoffset = (max(x_pos_list) - min(x_pos_list)) * 0.05
yoffset = (max(y_pos_list) - min(y_pos_list)) * 0.05
fig.update_xaxes(range=[min(x_pos_list)-xoffset, max(x_pos_list)+xoffset])
fig.update_yaxes(range=[min(y_pos_list)-yoffset, max(y_pos_list)+yoffset])
print(f'exporting REM dependency graph to {outfile}.')
return fig.write_html(outfile)
def assign_graph_node_symbol(full_G: nx.Graph, filtered_G: nx.Graph):
for node in full_G:
full_G.nodes()[node]['symbol'] = 'circle'
if node in filtered_G:
full_size = len(list(full_G.neighbors(node)))
filtered_size = len(list(filtered_G.neighbors(node)))
# if a node in a filtered graph hasless children, then mark it 'circle-cross'
filtered_G.nodes()[node]['symbol'] = 'circle-cross' if filtered_size < full_size else 'circle'
return