-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_graph.py
95 lines (81 loc) · 2.54 KB
/
visualize_graph.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
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
from networkx.drawing.nx_agraph import to_agraph
import sys
import graphviz
import json
use_graphviz = False
if not use_graphviz:
file = open(sys.argv[1],'r')
a1 = int(sys.argv[2])
a2 = int(sys.argv[3])
bit = 1
json_string = ""
graph_deserial = []
reads = dict()
color = False
labels = True
if len(sys.argv) > 4:
color = True
read_to_check = sys.argv[7]
json_file = open(sys.argv[4],'r')
read_anchor_file = open(sys.argv[6],'r')
json_string = json_file.readline()
graph_deserial = json.loads(json_string)
bit = int(sys.argv[5])
for read in read_anchor_file:
splitted = read.split(':')
spl = splitted[1].split(',')
spl = spl[:-1]
int_spl = [int(x) for x in spl];
for node in spl:
reads[splitted[0]] = int_spl
G = nx.Graph()
for line in file:
sp = line.split(',')
n1 = int(sp[0].split('-')[0])
n2 = int(sp[1].split('-')[0])
#w = int(sp[2])
if a1 < n1 < a2 and a1< n2 < a2:
#print(n1,n2,w)
#G.add_edge(n1,n2,weight=w)
G.add_edge(n1,n2)
if color:
order_to_id = dict()
for node in graph_deserial:
order_to_id[node['order']] = int(node['id'])
cmap = []
node_s = []
for n_order in G:
inside = False
if read_to_check in reads and order_to_id[n_order] in reads[read_to_check]:
inside = True
#print(n_order)
node_s.append(105)
else:
node_s.append(5)
node_color = graph_deserial[order_to_id[n_order]]['color']
if node_color & bit== bit:
cmap.append('red')
elif node_color & 2 == 2:
cmap.append('green')
else:
cmap.append('blue')
nx.draw(G, node_size=node_s, pos = nx.nx_pydot.graphviz_layout(G), node_color = cmap, with_labels=labels)
else:
#nx.draw(G, node_size=7, pos = nx.nx_pydot.graphviz_layout(G), with_labels=True)
nx.draw(G, node_size=7, pos = nx.nx_pydot.graphviz_layout(G))
plt.plot()
plt.show()
else:
file = open(sys.argv[1],'r')
G = graphviz.Digraph(format='png', strict=False)
for line in file:
sp = line.split(',')
n1 = sp[0]
n2 = sp[1]
w = sp[2]
print(n1,n2,w)
G.edge(n1,n2, label=w)
G.render()