-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotUtils.py
157 lines (118 loc) · 4.54 KB
/
plotUtils.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
# Import packages for data cleaning
import numpy as np
import re # For finding specific strings in the text
# Import packages for data visualization
import plotly.offline as py
import plotly.graph_objects as go
import networkx as nx
import itertools
import graph_utils as gUtils
import copy
import utils
from networkx.drawing.nx_agraph import graphviz_layout
# Custom function to create an edge between node x and node y, with a given text and width
def make_edge(x, y, color, width):
return go.Scatter(x = x,
y = y,
line = dict(width = width,
color = color),
mode = 'lines')
def make_edge_trace_list(G,width,pos):
# For each edge, make an edge_trace, append to list
edge_trace = []
for edge in G.edges():
char_1 = edge[0]
char_2 = edge[1]
x0, y0 = pos[char_1]
x1, y1 = pos[char_2]
if G.edges[edge]["feasibility"] == True:
trace = make_edge([x0, x1, None], [y0, y1, None],color = 'cornflowerblue',width = width)
else:
trace = make_edge([x0, x1, None], [y0, y1, None],color = 'crimson',width = width)
edge_trace.append(trace)
return edge_trace
# Make a node trace
def make_node_trace(G,pos,marker_size = 10,text_size = 25):
node_trace = go.Scatter(x = [],
y = [],
text = [],
textposition = "middle center",
textfont_size = text_size,
mode = 'markers+text',
hoverinfo = 'none',
marker = dict(color = [],
size = [],
line = None))
# For each node in midsummer, get the position and size and add to the node_trace
for node in G.nodes():
x, y = pos[node]
node_trace['x'] += tuple([x])
node_trace['y'] += tuple([y])
if(G.nodes[node]["feasibility"] == True):
node_trace['marker']['color'] += tuple(['cornflowerblue'])
else:
node_trace['marker']['color'] += tuple(['crimson'])
node_trace['marker']['size'] += tuple([marker_size])
node_trace['text'] += tuple(['<b>' + node + '</b>'])
return node_trace
def new_pos_from_old_pos(G,pos):
height = 1000
for node in G.nodes():
if G.nodes[node]["level"] == 7:
top_node = node
break
top = pos[top_node]
width = 400
# delta = 0.1 * width
new_pos = {}
new_pos[top_node] = top
diffy = height/6
for i,level_num in enumerate(reversed(range(7))):
level_nodes = [node for node in G.nodes if G.nodes[node]["level"] == level_num]
num_nodes = len(level_nodes)
# width = width + delta
difference = width/(num_nodes-1)
x0 = top[0]
init_x = x0-(width/2)
y_coord = top[1]-i*diffy
for id,node in enumerate(level_nodes):
coord = list(pos[node])
coord[0] = init_x + id*difference
# coord[1] = top[1] - y_coord
new_pos[node] = tuple(coord)
return new_pos
def get_fig(G,marker_size,text_size,width):
pos = graphviz_layout(G, prog='dot')
# print(pos)
# scale = 10
# center = pos['BLMRSSbSf']
# pos = {}
# for key in pos:
# pos[key] = np.asarray(pos[key]) - np.asarray(center)
# pos[key][0] = scale*pos[key][0]
# pos[key] = tuple(pos[key])
# print(pos)
pos = new_pos_from_old_pos(G,pos)
node_trace = make_node_trace(G,pos,marker_size=marker_size,text_size=text_size)
edge_trace = make_edge_trace_list(G,width=width,pos=pos)
# print(node_trace)
# Customize layout
layout = go.Layout(
paper_bgcolor='rgba(0,0,0,0)', # transparent background
plot_bgcolor='rgba(0,0,0,0)', # transparent 2nd background
xaxis = {'showgrid': False, 'zeroline': False}, # no gridlines
yaxis = {'showgrid': False, 'zeroline': False}, # no gridlines
)
# Create figure
fig = go.Figure(layout = layout)
# Add all edge traces
for trace in edge_trace:
fig.add_trace(trace)
# Add node trace
fig.add_trace(node_trace)
# Remove legend
fig.update_layout(showlegend = False)
# Remove tick labels
fig.update_xaxes(showticklabels = False)
fig.update_yaxes(showticklabels = False)
return fig