-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_graph_tool.py
259 lines (217 loc) · 6.55 KB
/
test_graph_tool.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from time import time
from itertools import chain
from collections import defaultdict
from numpy import nan_to_num
from graph_tool import Graph
from graph_tool.centrality import closeness
from graph_tool.draw import (
prop_to_size,
graph_draw,
sfdp_layout,
fruchterman_reingold_layout,
arf_layout,
)
DEBUG = True
def debug(s, *args, **kargs):
if not DEBUG:
return
print >> sys.stderr, '[DEBUG]', str(s).format(*args, **kargs)
def compose_graph(uid_pid_pairs):
# set up graph
g = Graph()
g.vp['pid'] = v_pid_p = g.new_vertex_property('string')
g.vp['count'] = v_count_p = g.new_vertex_property('int')
g.ep['count'] = e_count_p = g.new_edge_property('int')
pid_v_map = {}
uid_last_v_map = {}
vv_e_map = {}
for uid, pid in uid_pid_pairs:
# vertex
v = pid_v_map.get(pid)
if v is None:
v = g.add_vertex()
v_pid_p[v] = pid
v_count_p[v] = 0
pid_v_map[pid] = v
v_count_p[v] += 1
# edge
last_v = uid_last_v_map.get(uid)
uid_last_v_map[uid] = v
if last_v is None:
continue
vv = (last_v, v)
e = vv_e_map.get(vv)
if e is None:
e = g.add_edge(*vv)
e_count_p[e] = 0
vv_e_map[vv] = e
e_count_p[e] += 1
# calculate closeness
g.vp['closeness'] = v_closeness_p = g.new_vertex_property('float')
e_inverse_count_p = g.new_edge_property('int')
e_inverse_count_p.a = e_count_p.a.max()-e_count_p.a
debug('e_inverse_count_p.a: {}', e_inverse_count_p.a)
closeness(g, weight=e_inverse_count_p, vprop=v_closeness_p)
debug('v_closeness_p.a : {}', v_closeness_p.a)
v_closeness_p.a = nan_to_num(v_closeness_p.a)
debug('v_closeness_p.a : {}', v_closeness_p.a)
# fillter
g.vp['picked'] = v_picked_p = g.new_vertex_property('bool')
debug('v_count_p.a.mean() : {}', v_count_p.a.mean())
v_picked_p.a = v_count_p.a > v_count_p.a.mean()
debug('v_picked_p.a : {}', v_picked_p.a)
g.set_vertex_filter(v_picked_p)
g.set_vertex_filter(None)
return g
SIZE = 400
MA_V_SIZE = SIZE / 20.
MI_V_SIZE = MA_V_SIZE / 2.
MA_E_PWIDTH = MA_V_SIZE / 4.
MI_E_PWIDTH = MA_E_PWIDTH / 2.
def render_graph(g, path='graph/{}.pdf'):
# the simplest way
arg_map = dict(
g = g,
output = path.format('1-1-random-simplest'),
)
graph_draw(**arg_map)
# use constants
arg_map.update(dict(
output = path.format('1-2-random-constant'),
output_size = (SIZE, SIZE),
vertex_size = MA_V_SIZE,
edge_pen_width = MA_E_PWIDTH,
))
graph_draw(**arg_map)
# use prop_to_size
v_count_p = g.vp['count']
e_count_p = g.ep['count']
v_size_by_count_p = prop_to_size(v_count_p, MI_V_SIZE, MA_V_SIZE)
e_pwidth_by_count_p = prop_to_size(e_count_p, MI_E_PWIDTH, MA_E_PWIDTH)
arg_map.update(dict(
output = path.format('1-3-random-size'),
vertex_size = v_size_by_count_p,
edge_pen_width = e_pwidth_by_count_p,
))
graph_draw(**arg_map)
# use fill_color
debug('v_count_p.a : {}', v_count_p.a)
v_color_by_count_p = prop_to_size(v_count_p, 0, 1)
debug('v_color_by_count_p.a: {}', v_color_by_count_p.a)
arg_map.update(dict(
output = path.format('1-4-random-color'),
vertex_fill_color = v_color_by_count_p,
))
graph_draw(**arg_map)
# use closeness
v_closeness_p = g.vp['closeness']
v_color_by_closeness_p = prop_to_size(v_closeness_p, 0, 1)
#closeness_arg_map = arg_map.copy()
#closeness_arg_map.update(dict(
# output = path.format('1-5-random-closeness'),
# vertex_fill_color = v_color_by_closeness_p,
#))
arg_map.update(dict(
output = path.format('1-5-random-closeness'),
vertex_fill_color = v_color_by_closeness_p,
))
graph_draw(**arg_map)
# sfdp_layout
arg_map.update(dict(
output = path.format('2-1-sfdp'),
pos = sfdp_layout(g),
))
graph_draw(**arg_map)
# sfdp_layout with only edge's weight
arg_map.update(dict(
output = path.format('2-2-sfdp-edge-weight'),
pos = sfdp_layout(g, eweight=e_count_p),
))
graph_draw(**arg_map)
# sfdp_layout with both edge and vertex's weight
arg_map.update(dict(
output = path.format('2-3-sfdp-both-weight'),
pos = sfdp_layout(g, eweight=e_count_p, vweight=v_count_p),
))
graph_draw(**arg_map)
# fruchterman_reingold_layout
arg_map.update(dict(
output = path.format('3-1-fr'),
pos = fruchterman_reingold_layout(g),
))
graph_draw(**arg_map)
# fruchterman_reingold_layout with edge's weight
arg_map.update(dict(
output = path.format('3-2-fp-edge-weight'),
pos = fruchterman_reingold_layout(g, weight=e_count_p),
))
graph_draw(**arg_map)
# arf_layout
arg_map.update(dict(
output = path.format('4-1-arf'),
pos = arf_layout(g),
))
graph_draw(**arg_map)
# arf_layout with edge's weight
arg_map.update(dict(
output = path.format('4-2-arf-edge-weight'),
pos = arf_layout(g, weight=e_count_p),
))
graph_draw(**arg_map)
def analyze_graph(g):
print 'The graph: {}'.format(g)
print
v_pid_p = g.vp['pid']
v_count_p = g.vp['count']
print 'Top 10 Vertices by Count:'
print
for no, vidx in enumerate(v_count_p.a.argsort()[-10:][::-1], 1):
v = g.vertex(vidx)
print ' {:2}. {:2} {:2}'.format(
no,
v_pid_p[v],
v_count_p[v],
)
print
v_closeness_p = g.vp['closeness']
print 'Top 10 Vertices by Closeness:'
print
for no, vidx in enumerate(v_closeness_p.a.argsort()[-10:][::-1], 1):
v = g.vertex(vidx)
print ' {:2}. {:2} {:f}'.format(
no,
v_pid_p[v],
v_closeness_p[v],
)
print
if __name__ == '__main__':
uid_pid_pairs = [
('U1', 'A1'),
('U1', 'A2'),
('U1', 'A3'),
('U1', 'B1'),
('U1', 'B2'),
('U1', 'C2'),
('U1', 'A2'),
('U1', 'A4'),
('U2', 'A1'),
('U2', 'A2'),
('U2', 'B1'),
('U2', 'C1'),
('U2', 'A1'),
('U3', 'A1'),
('U3', 'B1'),
('U3', 'C1'),
('U3', 'A1'),
('U4', 'A1'),
('U4', 'B1'),
('U4', 'C1'),
('U5', 'A1'),
('U5', 'B1'),
]
g = compose_graph(uid_pid_pairs)
render_graph(g)
analyze_graph(g)