forked from DavidMChan/hog2
-
Notifications
You must be signed in to change notification settings - Fork 8
/
tree.py
executable file
·67 lines (61 loc) · 2 KB
/
tree.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
#!/usr/bin/python3
import sys
import subprocess
import igraph as ig
def plotTree(edges,labels,highlight,name):
mx=0
for e in edges:
mx=max(mx,e[1])
print(mx,len(labels))
g=ig.Graph(n=len(labels),directed=True)
g.add_edges(edges)
g.vs['label']=labels #["{}\n{}".format(r,labels[r]) for r in range(len(labels))]
for i in range(len(labels)):
if 'X' in labels[i]:
g.vs[i]['color']='#DDD'
elif '*' in labels[i] or '_' in labels[i]:
g.vs[i]['color']='#F99'
g.vs["label_size"]=[12]*len(labels)
g.vs["shape"]=['rectangle']*len(labels)
g.vs["size"]=[80]*len(labels)
for i in highlight:
g.vs[i]['color']='gold'
#g.write_svg('searchtree%s.svg'%name,
#layout=g.layout_reingold_tilford(root=[0]),
#rescale=False,bbox=(32000,2000))
ig.plot(g,
margin=50,
layout=g.layout_reingold_tilford(root=[0]),
rescale=False,bbox=(4400,1600))
labels=[]
prunedlabels = []
process = subprocess.run('grep label %s'%sys.argv[1], shell=True, check=True, stdout=subprocess.PIPE, universal_newlines=True)
for line in process.stdout.split('\n'):
try:
if 'X' in line:
prunedlabels.append(line.split('"')[1])
else:
labels.append(line.split('"')[1].replace(',\\n','\\n').replace('1)',')').replace("0\\n","\n").replace("\\n","\n").replace('4294967295','inf').replace('0,',','))
except:
pass
prunededges=[]
edges=[]
process = subprocess.run('grep edge %s'%sys.argv[1], shell=True, check=True, stdout=subprocess.PIPE, universal_newlines=True)
for line in process.stdout.split('\n'):
try:
if 'X' in line:
prunededges.append(eval(line.split(' ')[1]))
else:
edges.append(eval(line.split(' ')[1]))
except:
pass
path=[]
try:
process = subprocess.run('grep -e "path [0-9]" %s'%sys.argv[1], shell=True, check=True, stdout=subprocess.PIPE, universal_newlines=True)
path=[int(p) for p in process.stdout.split(' ')[1].split(',')]
except:
pass
for p in prunededges:
edges.append((p[0],len(edges)+1))
labels += prunedlabels
plotTree(edges,labels,path,sys.argv[1])