forked from palmagro/gg2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
421 lines (393 loc) · 14.4 KB
/
visualization.py
File metadata and controls
421 lines (393 loc) · 14.4 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import csv
from matplotlib import rcParams, pyplot as pl
import random
import numpy as np
from bokeh.charts.utils import cycle_colors
from sklearn import manifold
from math import acos
from bokeh.io import output_notebook
from bokeh.plotting import figure, output_file, show, ColumnDataSource, vplot
from bokeh.models import(
GMapPlot, Range1d, ColumnDataSource, LinearAxis,
PanTool, WheelZoomTool, BoxZoomTool, ResetTool, ResizeTool, BoxSelectTool, HoverTool)#printfTickFormatter)
from bokeh.charts import Line
import math
from aux import *
from node2vec import *
import colorsys
colormapdb = ["#0000FF","#FF0000","#00FF00","#682F79","#a6cee3", "#444444", "#1f78b4", "#b2df8a", "#33a02c","#fb9a99","FF6600"]
colormapn = ["#1C75BC","#FCAF17","#EF4136","#682F79","#a6cee3", "#444444", "#1f78b4", "#b2df8a", "#33a02c","#fb9a99","FF6600"]
colormap2 = [
"#fff9d8",
"#ffe8cd",
"#dbc0ae",
"#cccccc",
"#999999",
"#3252b2"]
colormapa = [
"#58dc91","#52daca","#f05574","#e1b560","#6c49da","#ff09d8","#BCF1ED", "#999999", "#ff7f00", "#cab2d6", "#6a3d9a",
"#ffe8cd",
"#dbc0ae",
"#cccccc",
"#999999",
"#3252b2","#FA5CE5","#DEFACE"]
colormapa2 = [
"#58dc95","#52dace","#f05578","#e1b565","#6c49de","#ff09dc","#BCF1ED", "#99999e", "#ff7f05", "#cab2db", "#6a3d9a",
"#ffe8cd",
"#dbc0ae",
"#cccccc",
"#999999",
"#3252b2"]
#e9d9af
colormap = [
"#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a"
]
def pallete(t):
test = {}
for i in range(0,100):
test[str(i)] = np.random.normal(0,1,100)
if t == "nodes":
return cycle_colors(test,palette=colormapn)
else:
if t == "desv":
return cycle_colors(test,palette=colormapa2)
else:
if t == "traversals":
return cycle_colors(test,palette=colormap)
else:
if t == "db":
return cycle_colors(test,palette=colormapdb)
else:
return cycle_colors(test,palette=colormapa)
def links_figure(n2v):
pal = pallete("links")
#Links Plotting
q = figure(title="Link Labels")
for idx,r in enumerate(n2v.r_types):
x = np.linspace(math.pi+math.pi/2,3*math.pi+math.pi/2,2000)
cdf = np.sin(x)*len(n2v.r_types[r])/2
q.line(x*500000*n2v.r_types_d[idx] + idx*1000000, cdf+(len(n2v.r_types[r])/2), alpha=0.9,color=pal[idx],line_width=1)
q.patch(x*500000*n2v.r_types_d[idx] + idx*1000000, cdf+(len(n2v.r_types[r])/2), alpha=0.7, legend=r,color=pal[idx])
return q
def angles_figure(self):
#Links Matrix Plotting
colormap = [
"#444444", "#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99",
"#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a"
]
xname = []
yname = []
color = []
alpha = []
angle = []
names=[]
for i, n1 in enumerate(n2v.r_types):
names.append(n1)
for j, n2 in enumerate(n2v.r_types):
xname.append(n1)
yname.append(n2)
alpha.append(n2v.angle_matrix[n1][n2]/math.pi)
angle.append(np.degrees(n2v.angle_matrix[n1][n2]))
if n1 == n2:
color.append(colormap[2])
else:
color.append('black')
source = ColumnDataSource(
data=dict(
xname=xname,
yname=yname,
colors=color,
alphas=alpha,
angles=angle
)
)
p = figure(title="Angles Between Link labels",
x_axis_location="above", tools="resize,hover,save",
x_range=list(reversed(names)), y_range=names)
p.rect('xname', 'yname', 0.9, 0.9, source=source,
color='colors', alpha='alphas', line_color=None)
p.grid.grid_line_color = None
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_text_font_size = "5pt"
p.axis.major_label_standoff = 0
p.xaxis.major_label_orientation = np.pi/3
hover = p.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([
('names', '@yname, @xname'),
('angle', '@angles'),
])
return p
def all_figure(n2v,tp,ntypes,legend):
pal = pallete("nodes")
X = []
Y = []
C = []
for idx,nt in enumerate(ntypes):
for n in n2v.n_types[nt]:
if random.random() < tp[idx]:
if n in n2v.w2v:
X.append(n2v.w2v[n])
Y.append(n)
C.append(idx)
mds = manifold.MDS(n_components=2, metric=True, n_init=4, max_iter=300, verbose=0, eps=0.001, n_jobs=1, random_state=None, dissimilarity='euclidean')
result = mds.fit_transform(np.asfarray(X,dtype='float'))
x = []
y = []
c = []
label = []
for idx,v in enumerate(Y):
label.append(v)
x.append(result[idx][0])
y.append(result[idx][1])
c.append(pal[C[idx]])
source = ColumnDataSource(data=dict(x=x,y=y, label=label))
#Nodes Plotting
o = figure(plot_height=n2v.ploth,plot_width=n2v.plotw)
for idx,nt in enumerate(ntypes):
o.line([], [], color=pal[idx],legend=nt,line_width=1.5)
if legend:
o.text('x', 'y', label, source=source, )
o.circle('x', 'y', size=10, source=source,color=c,alpha=0.5 )
return o
def all_links_figure(n2v,tp,ltypes,legend):
showed = []
pal = pallete("links")
#mds = manifold.TSNE(n_components=2)
mds = manifold.MDS(n_components=2, metric=True, n_init=4, max_iter=300, verbose=0, eps=0.001, n_jobs=1, random_state=None, dissimilarity='euclidean')
X = []
C = []
for idx,nt in enumerate(ltypes):
for a in n2v.r_types[nt]:
if random.random() < tp[idx]:
X.append(a["v"])
C.append(idx)
result = mds.fit_transform(np.asfarray(X,dtype='float'))
x = []
y = []
c = []
label = []
for idx,v in enumerate(result):
x.append(result[idx][0])
y.append(result[idx][1])
c.append(pal[C[idx]])
source = ColumnDataSource(data=dict(x=x,y=y, label=label))
#Nodes Plotting
o = figure(title="All Links",plot_height=n2v.ploth,plot_width=n2v.plotw)
for idx,rt in enumerate(n2v.r_types):
if rt in ltypes:
o.line([], [], color=pal[idx],legend=rt,line_width=1.5)
o.text('x', 'y', label, source=source, )
o.circle('x', 'y', size=10, source=source,color=c )
return o
def some_figure(n2v,c,legend):
mds = manifold.MDS(n_components=2, metric=True, n_init=4, max_iter=300, verbose=0, eps=0.001, n_jobs=1, random_state=None, dissimilarity='euclidean')
X = []
Y = []
C = []
con = neo4j.CypherQuery(n2v.graph_db, c).execute()
for row in con:
if row[2] in n2v.w2v:
print row[0]
X.append(n2v.w2v[row[2]])
Y.append(row[2])
C.append(ord(row[1][0]) % 10)
pal = pallete("nodes")
#mds = manifold.TSNE(n_components=2)
#print X
result = mds.fit_transform(np.asfarray(X,dtype='float'))
x = []
y = []
c = []
label = []
for idx,v in enumerate(Y):
label.append(v)
x.append(result[idx][0])
y.append(result[idx][1])
c.append(pal[C[idx]])
source = ColumnDataSource(data=dict(x=x,y=y, label=label))
#Nodes Plotting
o = figure(title="All Nodes",plot_height=n2v.ploth,plot_width=n2v.plotw)
if legend:
o.text('x', 'y', label, source=source, )
o.circle('x', 'y', size=10, source=source,color=c )
return o
def nodes_figure(n2v):
pal = pallete("nodes")
palr = pallete("links")
#mds = manifold.TSNE(n_components=2)
mds = manifold.MDS(n_components=2, metric=True, n_init=4, max_iter=300, verbose=0, eps=0.001, n_jobs=1, random_state=None, dissimilarity='euclidean')
X = []
Y = []
for v in n2v.m_points:
X.append(n2v.m_points[v])
Y.append(v)
#print X
result = mds.fit_transform(np.asfarray(X,dtype='float'))
for idx,r in enumerate(Y):
n2v.m_points[r] = result[idx]
x = []
y = []
d = []
d1 = []
d2 = []
c=[]
label = []
nnodes = []
for idx,v in enumerate(n2v.m_points):
label.append(v)
x.append(n2v.m_points[v][0])
y.append(n2v.m_points[v][1])
d.append(n2v.n_types_d[v])
d1.append(n2v.n_types_d[v]*2*1.33333)
d2.append(n2v.n_types_d[v]*2)
nnodes.append(len(n2v.n_types[v]))
c.append(pal[idx])
source = ColumnDataSource(data=dict(x=x,y=y, label=label,d=d,nnodes=nnodes))
#Nodes Plotting
o = figure(title="Node Labels",plot_width=n2v.plotw,plot_height=n2v.ploth,tools="pan,wheel_zoom,box_zoom,reset,resize,hover")
print "d"
print d
print d1
print d2
o.oval('x', 'y', width=d1,height=d2, source=source,alpha=0.7**len(n2v.m_points),height_units="data",width_units="data",color=c )
for idx,r in enumerate(n2v.r_types1):
a1 = rotatePoint(n2v.m_points[n2v.r_types1[r]["s"]],n2v.m_points[n2v.r_types1[r]["t"]],np.degrees(n2v.r_types_d[idx])/2)
a2 = rotatePoint(n2v.m_points[n2v.r_types1[r]["s"]],n2v.m_points[n2v.r_types1[r]["t"]],-np.degrees(n2v.r_types_d[idx])/2)
o.segment(x0=n2v.m_points[n2v.r_types1[r]["s"]][0],x1=n2v.m_points[n2v.r_types1[r]["t"]][0],y0=n2v.m_points[n2v.r_types1[r]["s"]][1],y1=n2v.m_points[n2v.r_types1[r]["t"]][1],color=palr[idx],line_width=2.5)
o.segment(x0=n2v.m_points[n2v.r_types1[r]["t"]][0],x1=a1[0],y0=n2v.m_points[n2v.r_types1[r]["t"]][1],y1=a1[1],color=palr[idx],line_width=2.5)
o.segment(x0=n2v.m_points[n2v.r_types1[r]["t"]][0],x1=a2[0],y0=n2v.m_points[n2v.r_types1[r]["t"]][1],y1=a2[1],color=palr[idx],line_width=2.5)
#o.segment(x0=a1[0],x1=a2[0],y0=a1[1],y1=a2[1])
x = [n2v.m_points[n2v.r_types1[r]["s"]][0],a1[0],n2v.m_points[n2v.r_types1[r]["t"]][0],a2[0],n2v.m_points[n2v.r_types1[r]["s"]][0]]
y = [n2v.m_points[n2v.r_types1[r]["s"]][1],a1[1],n2v.m_points[n2v.r_types1[r]["t"]][1],a2[1],n2v.m_points[n2v.r_types1[r]["s"]][1]]
o.patch(x,y , alpha=0.3,color=palr[idx])
o.text('x', 'y', label, source=source, )
o.circle('x', 'y', size=10, source=source,fill_color=c)
hover = o.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([
('Node Label', '@label'),
('Standard Deviation', '@d'),
('Number of Nodes', '@nnodes'),
])
return o
def vis(model,t,x,y,label):
if t =="bekoh":
# output to static HTML file
output_file("lines.html", title="line plot example")
source = ColumnDataSource(data=dict(x=x,y=y,label=label))
hover = HoverTool(
tooltips=[
("(x,y)", "($x, $y)"),
("label", "@label"),
]
)
p = figure(plot_width=600, plot_height=800, tools=[hover,BoxZoomTool(),PanTool(), WheelZoomTool(), BoxZoomTool(),ResetTool(), ResizeTool(), BoxSelectTool()],title="Mouse over the dots")
p.circle('x', 'y', size=5, source=source)
show(p)
else:
fig = plt.figure()
ax = fig.add_subplot(111)
for idx,v in enumerate(model.vocab):
ax.annotate(v, xy=((x[idx],(y[idx]))))
plt.scatter(x,y)
plt.grid()
plt.show()
def show2D(model,t):
#mds = manifold.TSNE(n_components=2)
mds = manifold.MDS(n_components=2, metric=True, n_init=4, max_iter=300, verbose=0, eps=0.001, n_jobs=1, random_state=None, dissimilarity='euclidean')
X = []
for v in model.vocab:
X.append(model[v])
if ndim > 2:
result = mds.fit_transform(np.array(X))
else:
result = np.array(X)
x = []
y = []
label = []
for idx,v in enumerate(model.vocab):
label.append(v)
x.append(result[idx][0])
y.append(result[idx][1])
vis(model,t,x,y,label)
def visual_matrix(matriz,colorFlag):
print matriz
newmatrix = []
for idx1,a in enumerate(matriz):
if idx1 > 0:
newmatrix.append([])
for idx2,b in enumerate(matriz[idx1]):
if idx2 > 0:
print idx1
newmatrix[idx1-1].append(b)
names = []
xname = []
yname = []
color = []
alpha = []
confusion = []
for idx,n in enumerate(matriz):
if idx != 0:
names.append(matriz[idx][0])
for idx1,n1 in enumerate(matriz):
for idx2,n2 in enumerate(matriz):
if idx1 != 0 and idx2 != 0:
xname.append(matriz[idx1][0])
yname.append(matriz[0][idx2])
if not colorFlag:
alpha.append(matriz[idx1][idx2]/100)
confusion.append(matriz[idx1][idx2])
color.append('black')
else:
temp = colorsys.hls_to_rgb(matriz[idx1][idx2]/200, 0.5, 0.7)
color.append('#%02x%02x%02x' % (float(temp[0])*255,float(temp[1])*255,float(temp[2]*255)))
alpha.append(1)
confusion.append(matriz[idx1][idx2])
print xname
print yname
print alpha
source = ColumnDataSource(
data=dict(
xname=xname,
yname=yname,
colors=color,
alphas=alpha,
angles=confusion
)
)
p = figure(x_axis_location="above", tools="resize,hover,save",
x_range=list(reversed(names)), y_range=names)
p.rect('xname', 'yname', 0.9, 0.9, source=source,
color='colors', alpha='alphas', line_color=None)
p.grid.grid_line_color = None
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_text_font_size = "15pt"
p.axis.major_label_standoff = 0
p.xaxis.major_label_orientation = np.pi/3
hover = p.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([
('names', '@yname, @xname'),
('angle', '@angles'),
])
print newmatrix
pl.matshow(newmatrix)
pl.colorbar()
pl.xticks(np.arange(0,len(newmatrix)), names, rotation=45, rotation_mode="anchor", ha='left')
pl.yticks(np.arange(0,len(newmatrix)), names)
pl.xlabel('Predicted',fontsize=17)
pl.ylabel('True',fontsize=17)
pl.tick_params(axis='both', which='major', labelsize=13)
pl.savefig("image.png",bbox_inches='tight',dpi=100)
pl.show()
print matriz
return p
def latex_matrix(matriz):
#Poniendo negritas
for i in range(1,len(matriz)):
matriz[i][0] = "\meg{ "+str(matriz[i][0])+"}"
matriz[0][i] = "\meg{ "+matriz[0][i]+"}"
for i in range(1,len(matriz)):
matriz[i][i] = "\meg{ "+matriz[i][i]+"}"
matriz[0][0] = ""
return matriz