-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphpainter.py
318 lines (294 loc) · 11.8 KB
/
graphpainter.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
import warnings
warnings.filterwarnings("ignore")
from matplotlib.pyplot import figure, subplot, show
import matplotlib.pyplot as plt
from networkx import Graph, DiGraph, draw
import networkx as nx
import os
import random
import sys
from networkx.drawing.nx_pydot import graphviz_layout
def binary_tree_layout(G, root=None, width=1., vert_gap = 0.2, vert_loc = 0., xcenter = 0.5,
pos = None, parent = None):
'''If there is a cycle that is reachable from root, then this will see infinite recursion.
G: the graph
root: the root node of current branch
width: horizontal space allocated for this branch - avoids overlap with other branches
vert_gap: gap between levels of hierarchy
vert_loc: vertical location of root
xcenter: horizontal location of root
pos: a dict saying where all nodes go if they have been assigned
parent: parent of this branch.
each node has an attribute "left: or "right"'''
if root is None:
root = T.nodes.__iter__().__next__()
if pos is None:
pos = {root:(xcenter,vert_loc)}
else:
pos[root] = (xcenter, vert_loc)
neighbors = list(G.neighbors(root))
if parent != None:
neighbors.remove(parent)
if len(list(neighbors))!=0:
dx = width/2.
leftx = xcenter - dx/2
rightx = xcenter + dx/2
for neighbor in neighbors:
if G.nodes[neighbor]['child_status'] == 'left':
pos = binary_tree_layout(G,neighbor, width = dx, vert_gap = vert_gap,
vert_loc = vert_loc-vert_gap, xcenter=leftx, pos=pos,
parent = root)
elif G.nodes[neighbor]['child_status'] == 'right':
pos = binary_tree_layout(G,neighbor, width = dx, vert_gap = vert_gap,
vert_loc = vert_loc-vert_gap, xcenter=rightx, pos=pos,
parent = root)
return pos
def hierarchy_pos(G, root=None, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5):
'''
From Joel's answer at https://stackoverflow.com/a/29597209/2966723.
Licensed under Creative Commons Attribution-Share Alike
If the graph is a tree this will return the positions to plot this in a
hierarchical layout.
G: the graph (must be a tree)
root: the root node of current branch
- if the tree is directed and this is not given,
the root will be found and used
- if the tree is directed and this is given, then
the positions will be just for the descendants of this node.
- if the tree is undirected and not given,
then a random choice will be used.
width: horizontal space allocated for this branch - avoids overlap with other branches
vert_gap: gap between levels of hierarchy
vert_loc: vertical location of root
xcenter: horizontal location of root
'''
if not nx.is_tree(G):
raise TypeError('cannot use hierarchy_pos on a graph that is not a tree')
if root is None:
if isinstance(G, nx.DiGraph):
root = next(iter(nx.topological_sort(G))) #allows back compatibility with nx version 1.11
else:
root = random.choice(list(G.nodes))
def _hierarchy_pos(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5, pos = None, parent = None):
'''
see hierarchy_pos docstring for most arguments
pos: a dict saying where all nodes go if they have been assigned
parent: parent of this branch. - only affects it if non-directed
'''
if root is None:
if isinstance(G, nx.DiGraph):
root = next(iter(nx.topological_sort(G))) # allows back compatibility with nx version 1.11
else:
root = random.choice(list(G.nodes))
if pos is None:
pos = {root:(xcenter,vert_loc)}
else:
pos[root] = (xcenter, vert_loc)
children = list(G.neighbors(root))
if not isinstance(G, nx.DiGraph) and parent is not None:
children.remove(parent)
if len(children)!=0:
dx = width/len(children)
nextx = xcenter - width/2 - dx/2
for child in children:
nextx += dx
pos = _hierarchy_pos(G,child, width = dx, vert_gap = vert_gap,
vert_loc = vert_loc-vert_gap, xcenter=nextx,
pos=pos, parent = root)
return pos
return _hierarchy_pos(G, root, width, vert_gap, vert_loc, xcenter)
def hierarchy_pos2(G, root=None, levels=None, width=1., height=1.):
'''If there is a cycle that is reachable from root, then this will see infinite recursion.
G: the graph
root: the root node
levels: a dictionary
key: level number (starting from 0)
value: number of nodes in this level
width: horizontal space allocated for drawing
height: vertical space allocated for drawing'''
TOTAL = "total"
CURRENT = "current"
def make_levels(levels, node=root, currentLevel=0, parent=None):
"""Compute the number of nodes for each level
"""
if not currentLevel in levels:
levels[currentLevel] = {TOTAL : 0, CURRENT : 0}
levels[currentLevel][TOTAL] += 1
neighbors = G.neighbors(node)
for neighbor in neighbors:
if not neighbor == parent:
levels = make_levels(levels, neighbor, currentLevel + 1, node)
return levels
def make_pos(pos, node=root, currentLevel=0, parent=None, vert_loc=0):
dx = 1/levels[currentLevel][TOTAL]
left = dx/2
pos[node] = ((left + dx*levels[currentLevel][CURRENT])*width, vert_loc)
levels[currentLevel][CURRENT] += 1
neighbors = G.neighbors(node)
for neighbor in neighbors:
if not neighbor == parent:
pos = make_pos(pos, neighbor, currentLevel + 1, node, vert_loc-vert_gap)
return pos
if levels is None:
levels = make_levels({})
else:
levels = {l:{TOTAL: levels[l], CURRENT:0} for l in levels}
vert_gap = height / (max([l for l in levels])+1)
return make_pos({})
if __name__ == '__main__':
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
## Read Info
if len(sys.argv)<=1:
filename = "data.txt"
else:
filename = sys.argv[1]
if len(sys.argv)<=2:
dcd="GBK"
else:
dcd=sys.argv[2]
#filename="data.txt"
mode = 0
graphname = 'Graph'
figureinfo = {}
layoutinfo = {}
drawinfo = {'with_labels': True, 'node_color': 'w'}
nodeinfo = {}
uselayout="graphviz"
graphinfo = {}
edgeinfo = []
onenodeinfo={}
curinfo = None
curkey = None
curvalue = None
def Error(text, ecode=1):
print(text)
os.system('pause')
sys.exit(ecode)
def TryEval(str, text, ecode=1):
try:
return eval(str)
except:
Error(text, ecode)
with (open(filename, 'r',encoding=dcd)) as f:
for ss in f:
s = ss.strip()
if s == '%Graph%':
graphname = 'Graph'
curinfo = graphinfo
uselayout = "graphviz"
layoutinfo = {'prog': 'dot'}
mode = 1
graphinfo = {}
elif s == '%DiGraph%':
graphname = 'DiGraph'
curinfo = graphinfo
uselayout = "graphviz"
layoutinfo = {'prog': 'dot'}
mode = 1
graphinfo = {}
elif s == '%BT%':
graphname = 'BT'
curinfo = graphinfo
uselayout = "bt"
mode = 1
graphinfo = {}
elif s == "%none layout%":
layoutinfo = {}
uselayout = "none"
elif s == "%bt layout%":
layoutinfo = {}
uselayout = "bt"
elif s=="%graphviz layout%":
layoutinfo = {'prog': 'dot'}
uselayout = "graphviz"
elif s=='%joel layout%':
layoutinfo = {}
uselayout = "joel"
elif s=='%joel2 layout%':
layoutinfo = {}
uselayout = "joel2"
elif s == '%figure%':
mode = 1
curinfo = figureinfo
elif s == '%layout%':
mode = 1
curinfo = layoutinfo
elif s == '%draw%':
mode = 1
curinfo = drawinfo
elif s == '%node%':
mode = 4
elif s == '%edge%':
mode = 5
elif mode == 0:
Error("Error Command: %s" % s, 1)
elif mode == 1:
if s[0] == '#' and s[-1] == '#':
curkey = s[1:-1]
mode = 2
else:
Error("Error Key Format: %s" % s, 3)
elif mode == 2:
curvalue = TryEval(s, "Value Parse Error: %s" % s, 4)
mode = 1
curinfo[curkey] = curvalue
elif mode == 4:
if graphname != 'BT':
pos = s.find(':')
if pos != -1:
curkey = TryEval(s[:pos], 'Key Parse Error: %s' % s[:pos], 2)
curvalue = TryEval(s[pos + 1:], 'Value Parse Error: %s' % s[pos + 1:], 4)
nodeinfo[curkey] = curvalue
else:
curkey = TryEval(s, 'Key Parse Error: %s' % s, 2)
curvalue = str(curkey)
nodeinfo[curkey] = curvalue
else:
bpos=s[0].upper()
if bpos!='L' and bpos!='R':
Error("BT Graph Node should be started with L or R.",5)
s=s[1:]
pos = s.find(':')
if pos != -1:
curkey = TryEval(s[:pos], 'Key Parse Error: %s' % s[:pos], 2)
curvalue = TryEval(s[pos + 1:], 'Value Parse Error: %s' % s[pos + 1:], 4)
nodeinfo[curkey] = (curvalue,'left' if bpos=='L' else 'right')
else:
curkey = TryEval(s, 'Key Parse Error: %s' % s, 2)
curvalue = str(curkey)
nodeinfo[curkey] = (curvalue,'left' if bpos=='L' else 'right')
elif mode == 5:
curvalue = TryEval(s, 'Value Parse Error: %s' % s, 4)
edgeinfo += [curvalue]
## Generate Pic
figure(**figureinfo)
subplot(111)
if graphname == 'Graph' or graphname == 'BST':
T = Graph(**graphinfo)
elif graphname == 'DiGraph':
T = DiGraph(**graphinfo)
else:
T = Graph(**graphinfo)
if graphname=='BT':
for i in nodeinfo.keys():
T.add_node(i,child_status=nodeinfo[i][1])
nodeinfo[i]=nodeinfo[i][0]
else:
for i in nodeinfo.keys():
T.add_node(i)
for i in edgeinfo:
T.add_edge(*i)
if uselayout=='graphviz':
pos = graphviz_layout(T, **layoutinfo)
elif uselayout=='bt':
pos = binary_tree_layout(T,**layoutinfo)
elif uselayout=='joel':
pos = hierarchy_pos(T, **layoutinfo)
elif uselayout=='joel2':
pos = hierarchy_pos2(T, **layoutinfo)
else:
pos=None
draw(T, pos, labels=nodeinfo, **drawinfo)
show()
sys.exit(0)