/
viz.py
417 lines (332 loc) · 12.8 KB
/
viz.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
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
from collections import defaultdict
import pickle
import getopt
import sqlite3 as lite
import itertools
from operator import itemgetter
from matplotlib import animation
import matplotlib
import math
import random
import collections
import json
from scipy import sparse
from sklearn.manifold.t_sne import TSNE
from tqdm import *
import numpy as np
import sys
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure, show
from sklearn import datasets
from sklearn.decomposition import PCA
import sklearn
from sklearn.preprocessing import scale
import label_position
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh import palettes
from bokeh.models import HoverTool
# database
database = "/home/olihb/IdeaProjects/cnn_analysis/data/cnn/topics.db"
output_animation_prefix = "/home/olihb/IdeaProjects/cnn_analysis/data/cnn/animation/test_"
output_file_csv = "/home/olihb/IdeaProjects/cnn_analysis/data/cnn/matrix_topic.csv"
output_file_csv_dict = "/home/olihb/IdeaProjects/cnn_analysis/data/cnn/matrix_topic_dict.csv"
topic_json = "/home/olihb/IdeaProjects/cnn_analysis/data/cnn/topics.json"
def load_data_structures(cur, config_name):
# words dictionary
words = dict()
cur.execute("select word_index, word from word_matrix_words where id = ?",(config_name,))
rows = cur.fetchall()
for row in tqdm(rows, leave=True):
words[int(row[0])]=row[1]
# create matrix
row_list = list()
col_list = list()
data_list = list()
max_tuples = dict()
cur.execute("select word_index, topic_id, similarity from word_matrix where id = ?",(config_name,))
rows = cur.fetchall()
for cell in tqdm(rows, leave=True):
row = int(cell[0])
col = int(cell[1])
data = float(cell[2])
if row in max_tuples:
if max_tuples[row][1]<data:
max_tuples[row]=(col, data)
else:
max_tuples[row]=(col, data)
row_list.append(row)
col_list.append(col)
data_list.append(data)
topics = list()
for x in range(len(max_tuples)):
topics.append(max_tuples[x][0])
mrow = np.array(row_list)
mcol = np.array(col_list)
mdata = np.array(data_list)
mtx = sparse.csr_matrix((mdata, (mrow, mcol)))
return words, mtx, topics
def save_transformation(cur, name, algo, matrix, topics):
lst = list()
for i in tqdm(range(len(topics))):
x = matrix[i,0]
y = matrix[i,1]
c = topics[i]
lst.append((name,algo,i,x,y,c))
cur.executemany("insert into computed_viz values (?,?,?,?,?,?)", lst)
def load_and_transform(con, cur, tag):
# erase old data
cur.execute("delete from computed_viz where id = ?",(tag,))
con.commit()
# load/transform data structure
words, mtx, topics = load_data_structures(cur, tag)
# compute
matrix = mtx.toarray()
# PCA
pca = PCA(n_components=2)
X_r = pca.fit(matrix).transform(matrix)
save_transformation(cur, tag, "pca", X_r, topics)
con.commit()
# T-SNE
t_sne = TSNE(n_components=2, random_state=0, verbose=1)
X_r = t_sne.fit_transform(matrix)
save_transformation(cur, tag, "tsne", X_r, topics)
con.commit()
def create_animation(con, cur, tag, algo='tsne'):
sql = """ select strftime('%Y-%m',ws.stamp) fdate, w.word, w.word_id, v.x, v.y, v.topic, m.similarity, sum(nb) n
from word_matrix m
join word_matrix_words w on m.word_index=w.word_index
join computed_viz v on v.word_index=w.word_index and v.topic=m.topic_id
join words_stats ws on ws.word_id=w.word_id
where algo='tsne' and (ws.stamp between '2000-01-01' and '2016-01-01')
group by fdate, w.word, w.word_id, v.x, v.y, v.topic, m.similarity
having sum(nb)>0
order by fdate"""
#strftime('%Y-%m',ws.stamp)
# setup chart
top_nb_label = 50
x_size = 10
fig = plt.figure(figsize=[x_size,x_size*4/5])
label_position.set_renderer(fig)
scatter = plt.scatter([],[],c=[],lw = 0)
plt.xlim([-15,15])
plt.ylim([-15,15])
label=[]
for t in range(top_nb_label):
label.append(plt.text(.5, .5, '', fontsize=9, multialignment='center'))
# get data
data = {}
list_keys = []
cur.execute(sql)
rows = cur.fetchall()
data_by_date = itertools.groupby(rows, key=itemgetter(0))
for key, items in data_by_date:
data[key]=list(items)
list_keys.append(key)
pickle.dump(data, open('pickles/data-s.p','wb'))
pickle.dump(list_keys, open('pickles/list-s.p', 'wb'))
data = pickle.load(open('pickles/data-s.p','rb'))
list_keys = pickle.load(open('pickles/list-s.p','rb'))
def init():
return scatter
def update_chart(i, chart):
key = list_keys[i]
current_data = data[key]
xy = map(lambda x: [x[3],x[4]], current_data)
c_list = map(lambda x: x[5], current_data)
a = 0.225
max_s = max(map(lambda x: x[6]*math.log10(x[7]), current_data))
w_list = map(lambda x: [x[1],x[3],x[4],x[6]*math.log10(x[7])], current_data)
s_list = map(lambda x: math.pow((1.0-math.pow((x[6]*math.log10(x[7]))/max_s,a)),1.0/a)*800.0, current_data)
w_list.sort(key=lambda x: x[3],reverse=True)
label_position.set_positions(w_list, top_nb_label, label, 0.5)
chart.set_array(np.array(c_list))
chart.set_offsets(xy)
chart.set_sizes(np.array(s_list))
plt.title(key)
print i
return chart,
anim = animation.FuncAnimation(fig, update_chart, init_func=init, frames=len(list_keys), fargs=(scatter,))
anim.save('chart-year-month.gif', writer='imagemagick', fps=1)
#plt.show()
def create_chart_scatter_bokeh(con, cur, tag, algo='tsne'):
sql = """ select strftime('%Y-%m',ws.stamp) fdate, w.word, w.word_id, v.x, v.y, v.topic, m.similarity, sum(nb) n
from word_matrix m
join word_matrix_words w on m.word_index=w.word_index
join computed_viz v on v.word_index=w.word_index and v.topic=m.topic_id
join words_stats ws on ws.word_id=w.word_id
where algo='tsne' and (ws.stamp between '2000-01-01' and '2016-01-01')
group by fdate, w.word, w.word_id, v.x, v.y, v.topic, m.similarity
having sum(nb)>0
order by fdate"""
data = {}
list_keys = []
cur.execute(sql)
rows = cur.fetchall()
data_by_date = itertools.groupby(rows, key=itemgetter(0))
for key, items in data_by_date:
data[key]=list(items)
list_keys.append(key)
pickle.dump(data, open('pickles/data-scatter.p','wb'))
pickle.dump(list_keys, open('pickles/list-scatter.p', 'wb'))
data = pickle.load(open('pickles/data-scatter.p','rb'))
list_keys = pickle.load(open('pickles/list-scatter.p','rb'))
key = list_keys[0]
current_data = data[key]
max_s = max(map(lambda x: x[6]*math.log10(x[7]), current_data))
a = 0.5
source = ColumnDataSource (
data = dict(
x= map(lambda c: c[3], current_data),
y= map(lambda c: c[4], current_data),
prob = map(lambda c: int(c[6]*100), current_data),
topic= map(lambda c: c[5], current_data),
desc= map(lambda c: c[1], current_data),
color = map(lambda x: palettes.Spectral11[x[5]%11], current_data),
s=map(lambda x: math.pow((1.0-math.pow((x[6]*math.log10(x[7]))/max_s,a)),1.0/a)*0.3, current_data)
)
)
hover = HoverTool(
tooltips="""
<div>
<span style="font-size: 15px; font-weight: bold;">@desc</span>
<span style="font-size: 12px; color: #966;">[@topic - @prob%]</span>
</div>
"""
)
# output to static HTML file
output_file("viz/charts/scatter.html",)
# create a new plot with a title and axis labels
p = figure()
p.add_tools(hover)
# add a line renderer with legend and line thickness
p.circle('x','y', source=source, radius='s', color='color', fill_alpha=0.4)
# show the results
show(p)
def create_chart_heatmap_bokeh(con, cur, tag, topic_description, algo='tsne'):
# load data for chart
sql = """ select strftime('%Y-%m',ws.stamp) date, v.topic, sum(nb) n
from word_matrix m
join word_matrix_words w on m.word_index=w.word_index
join computed_viz v on v.word_index=w.word_index and v.topic=m.topic_id
join words_stats ws on ws.word_id=w.word_id
where algo='tsne' and (ws.stamp between '2000-01-01' and '2016-01-01') and m.similarity>0.5
group by date, v.topic
having sum(nb)>0
order by date, v.topic"""""
cur.execute(sql)
rows = cur.fetchall()
raw_data = []
for row in rows:
raw_data.append(row)
pickle.dump(raw_data, open('pickles/heatmap-all-chart.p','wb'))
# load data for description
sql = """ select strftime('%Y-%m',ws.stamp) date, v.topic, w.word, sum(nb) n
from word_matrix m
join word_matrix_words w on m.word_index=w.word_index
join computed_viz v on v.word_index=w.word_index and v.topic=m.topic_id
join words_stats ws on ws.word_id=w.word_id
where algo='tsne' and (ws.stamp between '2000-01-01' and '2016-01-01') and m.similarity>0.5
group by date, v.topic, w.word
having sum(nb)>0"""""
cur.execute(sql)
rows = cur.fetchall()
raw_words = []
for row in rows:
raw_words.append(row)
pickle.dump(raw_words, open('pickles/heatmap-all-desc.p','wb'))
# load for faster testing
raw_data = pickle.load(open('pickles/heatmap-all-chart.p','rb'))
raw_words = pickle.load(open('pickles/heatmap-all-desc.p','rb'))
# setup chart
date = []
topic = []
data = []
color = []
max_topic = defaultdict(float)
for row in raw_data:
date.append(row[0])
topic.append(row[1])
data.append(float(row[2]))
if row[2]>max_topic[row[1]]:
max_topic[row[1]]=row[2]
for i in range(len(topic)):
t = topic[i]
data[i] = data[i]/max_topic[t]
color_index=(int((data[i]*9-0.0000001))%9)
color.append(list(reversed(palettes.PuBu9))[color_index])
desc_raw = defaultdict(list)
for row in raw_words:
key = (row[0], row[1])
value = (row[2],row[3])
desc_raw[key].append(value)
desc = defaultdict(str)
for key in desc_raw:
desc_raw[key].sort(reverse=True, key=lambda x: x[1])
word = map(lambda x:x[0], desc_raw[key])
desc[key]=", ".join(word[:5])
descriptions = []
for row in raw_data:
key = (row[0], row[1])
descriptions.append(desc[key])
source = ColumnDataSource(
data=dict(date=date,topic=topic,data=data, color=color, description=descriptions)
)
y_range=list(set(date))
y_range.sort(reverse=True)
x_range=[str(x+1) for x in range(100)]
output_file("viz/charts/heatmap.html",)
p = figure(plot_width=1200, plot_height=1000, x_range=x_range, y_range=y_range)
p.rect("topic","date", 1, 1, source=source,color='color',alpha=0.8, 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"
hover = HoverTool(
tooltips=[
("date", "@date"),
("topic", "@topic"),
("keywords", "@description"),
]
)
p.add_tools(hover)
show(p)
def main(argv):
con = None
config_name = "topic_0.25_1000"
try:
con = lite.connect(database)
cur = con.cursor()
# arguments
try:
opts, args = getopt.getopt(argv, "lach")
except getopt.GetoptError:
sys.exit(2)
for opt, arg in opts:
# load tables
if opt == '-l':
load_and_transform(con, cur, config_name)
# send to dynamodb
elif opt =='-a':
create_animation(con, cur, config_name)
elif opt == '-c':
create_chart_scatter_bokeh(con, cur, config_name)
elif opt == '-h':
create_chart_heatmap_bokeh(con, cur, config_name, topic_json)
#def onpick3(event):
# ind = event.ind
# print "-----"
# for i in ind:
# print words[i]+" : "+str(topics[i])
#fig = figure()
#ax1 = fig.add_subplot(111)
#col = ax1.scatter(X_r[:, 0], X_r[:, 1], c=topics, picker=True)
#fig.canvas.mpl_connect('pick_event', onpick3)
#show()
except lite.Error, e:
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
if con:
con.close()
if __name__ == "__main__":
main(sys.argv[1:])