-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyModule.py
More file actions
823 lines (701 loc) · 37.7 KB
/
Copy pathmyModule.py
File metadata and controls
823 lines (701 loc) · 37.7 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import datetime
import numpy as np
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sip
import pandas as pd
from sklearn.manifold import MDS
import matplotlib.pyplot as plt
import math
import copy
from PIL import Image, ImageDraw, ImageFont
import requests
import os
import MeCab
from scipy.spatial import Delaunay, delaunay_plot_2d, Voronoi, voronoi_plot_2d
import math
import random
class Watch:
def __init__(self, video_title, video_id, channel_name, channel_id, watch_datetime):
self.video_title = video_title
self.video_id = video_id
self.channel_name = channel_name
self.channel_id = channel_id
self.watch_datetime = datetime.datetime.strptime(str(watch_datetime), '%Y-%m-%d %H:%M:%S')
self.tags = []
class Watchs:
def __init__(self, path):
print(path)
self.df = pd.read_csv(path)
self.df = self.df.dropna(how ="any")
self.df = self.df[~self.df['watch_date'].str.contains('Wa')]
self.df = self.df[~self.df['watch_date'].str.contains('Wt')]
self.watch_list_all = []
self.watch_list_selected = []
self.watch_start_datetime_all = 0
self.watch_end_datetime_all = 0
def construct_watch_list_all(self):
row_no = len(self.df)
i = 0
while i < row_no:
# たまにゴミデータがあるから、それはスキップ
try :
#hoge = datetime.datetime.strptime(str(self.df.iat[i, 4]), '%b %d, %Y, %I:%M:%S %p JST')
text = str(self.df.iat[i, 4]).replace(" を視聴しました", '')
print( text )
self.df.iat[i, 4] = datetime.datetime.strptime(text, '%Y/%m/%d %H:%M:%S JST')
#print( str(self.df.iat[i, 4]) + "test")
#hoge = datetime.datetime.strptime(str(self.df.iat[i, 4]), '%Y/%m/%d %H:%M:%S JST')
except ValueError:
i += 1
continue
watch = Watch(self.df.iat[i, 0], self.df.iat[i, 1], self.df.iat[i, 2], self.df.iat[i, 3], self.df.iat[i, 4])
self.watch_list_all.append(watch)
i += 1
print(i)
# 3アカウントのデータをつなげたから、ここで時系列順にソート
self.watch_list_all = sorted(self.watch_list_all, key=lambda x: x.watch_datetime, reverse=True)
for el in self.watch_list_all:
print(el.watch_datetime)
# watchsの start_datetime と end_datetime を抽出
self.watch_start_datetime_all = self.watch_list_all[0].watch_datetime
self.watch_end_datetime_all = self.watch_list_all[-1].watch_datetime
def set_watch_list_selected(self, start_datetime, end_datetime):
self.watch_list_selected.clear()
for watch in self.watch_list_all:
if watch.watch_datetime <= start_datetime and watch.watch_datetime >= end_datetime:
self.watch_list_selected.append(watch)
def tag_each_watch(self):
for watch in self.watch_list_all:
mecab = MeCab.Tagger('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd').parse(watch.video_title)
lines = mecab.split('\n')
nounAndVerb = []
for line in lines:
feature = line.split('\t')
if len(feature) == 2:#'EOS'と''を省く
info = feature[1].split(',')
hinshi = info[0]
# if hinshi in ('名詞', '動詞'):
if hinshi in ('名詞'):
nounAndVerb.append(info[6])
watch.tags = nounAndVerb
while '*' in watch.tags:
watch.tags.remove('*')
'''
-------------------------------------------------------------------------------
'''
class TelementWInfo:
def __init__(self):
self.frequency = 0
self.position = np.zeros(2)
self.watch_video_dict = {} # {video_id : [watch_1, watch_2, watch_3], video_id : [watch_1, watch_2], ....}
self.color = "NO"
self.histgram_position = [0, 0, 0, 0]
def set_histgram_position(self, which, interval_num=0, max=0, min=0):
if which == 0: #frequency
interval = (max - min) / interval_num
i = 0
while self.frequency >= min + interval * (i + 1):
i += 1
self.histgram_position[which] = i
if which == 1: #x position
interval = (max - min) / interval_num
i = 0
while self.position[0] >= min + interval * (i + 1):
i += 1
self.histgram_position[which] = i
if which == 2: #x position
interval = (max - min) / interval_num
i = 0
while self.position[1] >= min + interval * (i + 1):
i += 1
self.histgram_position[which] = i
if which == 3: #color
if self.color == "RED":
self.histgram_position[which] = 0
if self.color == "BLUE":
self.histgram_position[which] = 1
if self.color == "PURPLE":
self.histgram_position[which] = 2
if self.color == "NO":
self.histgram_position[which] = 3
class TelementWatchInfo:
def __init__(self):
self.view_num = 0
self.position = np.zeros(2)
class Telement:
def __init__(self,start_datetime, end_datetime, i):
self.start_datetime = start_datetime
self.end_datetime = end_datetime
self.word_count_dict = {} # {word : frequency, word : frequency, ...} ソートされて、上位の選ばれたもの
self.extracted_w_info_dict = {} # {word : w_info, word : w_info, ...}
self.extracted_watch_info_dict = {} # {video_id: watch_info, video_id: watch_info , ....}
self.index = i
self.all_histgram_position_dict = {} # {[1,2,4,0]:1, [3,6,7,8]:5, ...}
def max_frequency(self):
max = 0
i = 0
for w_info in self.extracted_w_info_dict.values():
if i == 0:
max = w_info.frequency
elif max < w_info.frequency:
max = w_info.frequency
i += 1
return max
def min_frequency(self):
min = 0
i = 0
for w_info in self.extracted_w_info_dict.values():
if i == 0:
min = w_info.frequency
elif min > w_info.frequency:
min = w_info.frequency
i += 1
return min
def max_x(self):
max = 0
for w_info in self.extracted_w_info_dict.values():
if w_info.position[0] > max:
max = w_info.position[0]
return max
def min_x(self):
min = 0
for (i, w_info) in zip( range(len(self.extracted_w_info_dict.values())), self.extracted_w_info_dict.values() ):
if i == 0:
min = w_info.position[0]
elif w_info.position[0] < min:
min = w_info.position[0]
return min
def max_y(self):
max = 0
for w_info in self.extracted_w_info_dict.values():
if w_info.position[1] > max:
max = w_info.position[1]
return max
def min_y(self):
min = 0
for (i, w_info) in zip( range(len(self.extracted_w_info_dict.values())), self.extracted_w_info_dict.values()):
if i == 0:
min = w_info.position[1]
elif w_info.position[1] < min:
min = w_info.position[1]
return min
class Tset:
def __init__(self, start_datetime, end_datetime):
self.interval_num = 0
self.start_datetime = start_datetime
self.end_datetime = end_datetime
self.interval_time = 0
self.elements_list = []
self.x_max = 0
self.y_max = 0
self.x_min = 0
self.y_min = 0
def set_interval_num(self, interval_num):
self.interval_num = interval_num
self.elements_list.clear()
self.interval_time = (self.start_datetime - self.end_datetime) / self.interval_num
# t_setを、t_elementに、start_datetime, end_datetime, index(0オリジン)を登録して生成。
def construct_element_list(self):
tmp = self.start_datetime
for i in range(self.interval_num):
if i != self.interval_num - 1: #最後以外。境界条件の関係で
t_element = Telement(tmp, tmp - self.interval_time, i)
self.elements_list.append(t_element)
tmp -= self.interval_time
i += 1
if i == self.interval_num -1: #最後だったら。境界条件的にこれいる。
t_element = Telement(tmp, tmp - self.interval_time*1.1, i) #なんとなく1.1
self.elements_list.append(t_element)
def construct_t_element_word_count_dict_2nd(self, watchs, WORDS_NUM_IN_A_CLOUD):
# t_elemnt.word_dictを作る。一つの期間で、出現回数まとめる。
for t_element in self.elements_list:
for watch in watchs.watch_list_all:
if watch.watch_datetime <= t_element.start_datetime and watch.watch_datetime >= t_element.end_datetime:
for tag in watch.tags:
if tag not in t_element.word_count_dict:
t_element.word_count_dict[tag] = 1
else:
t_element.word_count_dict[tag] += 1
tmp_taple_list = sorted(t_element.word_count_dict.items(), key=lambda x:x[1], reverse=True) #多い順に並び替え。返り値がタプルのリストになっているため、辞書に置き換える必要がある。
tmp_taple_list = tmp_taple_list[0:WORDS_NUM_IN_A_CLOUD] #出現回数が閾値以降のものを残す.
#タプルを辞書に直して格納
t_element.word_count_dict.clear()
for item in tmp_taple_list:
t_element.word_count_dict[item[0]] = item[1]
#print(t_element.word_count_dict)
def construct_t_element_word_count_dict(self, watchs, WORDS_NUM_IN_A_CLOUD):
# t_elemnt.word_dictを作る。一つの期間で、出現回数まとめる。
for t_element in self.elements_list:
for watch in watchs.watch_list_all:
if watch.watch_datetime <= t_element.start_datetime and watch.watch_datetime >= t_element.end_datetime:
if watch.channel_id not in t_element.word_count_dict: #channel_idで作る。
t_element.word_count_dict[watch.channel_id] = 1
else:
t_element.word_count_dict[watch.channel_id] += 1
tmp_taple_list = sorted(t_element.word_count_dict.items(), key=lambda x:x[1], reverse=True) #多い順に並び替え。返り値がタプルのリストになっているため、辞書に置き換える必要がある。
tmp_taple_list = tmp_taple_list[0:WORDS_NUM_IN_A_CLOUD] #出現回数が閾値以降のものを残す.
#タプルを辞書に直して格納
t_element.word_count_dict.clear()
for item in tmp_taple_list:
t_element.word_count_dict[item[0]] = item[1]
#print(t_element.word_count_dict)
def set_t_element_extracted_w_info_dic(self, watchs, w_set):
# t_elementの extracted_w_info_dictに情報を挿入
for (i, t_element) in zip(range(len(self.elements_list)), self.elements_list):
# watch_video_dictを作るために、
watchs.set_watch_list_selected(t_element.start_datetime, t_element.end_datetime)
for word in t_element.word_count_dict.keys():
t_element.extracted_w_info_dict[word] = TelementWInfo()
# frequency ポインタじゃないから、実体を直接変える必要がある。
self.elements_list[i].extracted_w_info_dict[word].frequency = t_element.word_count_dict[word]
# position
t_element.extracted_w_info_dict[word].position = w_set.elements_dict[word].position
# watch_video_dict
for watch in watchs.watch_list_selected:
if word in watch.tags:
if watch.video_id not in t_element.extracted_w_info_dict[word].watch_video_dict:
t_element.extracted_w_info_dict[word].watch_video_dict[watch.video_id] = [watch]
else:
t_element.extracted_w_info_dict[word].watch_video_dict[watch.video_id].append(watch)
# color
vec = w_set.elements_dict[word].importance_vec # 使い回すから、代入
index = t_element.index # 使い回すから、代入
if t_element.index == 0: #一番端のT_elementの時
if vec[index + 1] > 0:
t_element.extracted_w_info_dict[word].color = "RED"
elif np.count_nonzero(vec[index + 1:] > 0) == 0:
t_element.extracted_w_info_dict[word].color = "PURPLE"
elif t_element.index == len(vec) -1:#一番端のT_elementの時
if vec[index - 1] > 0:
t_element.extracted_w_info_dict[word].color = "BLUE"
elif np.count_nonzero(vec[:index] > 0) == 0:
t_element.extracted_w_info_dict[word].color = "PURPLE"
else:
if vec[index + 1] > 0 and vec[index - 1] == 0:
t_element.extracted_w_info_dict[word].color = "RED"
elif vec[index - 1] > 0 and vec[index + 1] == 0:
t_element.extracted_w_info_dict[word].color = "BLUE"
elif np.count_nonzero(vec[index + 1:] > 0) == 0 and np.count_nonzero(vec[:index] > 0) == 0:
t_element.extracted_w_info_dict[word].color = "PURPLE"
#print(channel_id, ": color = ", t_element.extracted_w_info_dict[channel_id].color)
#print("---------------------------")
def set_t_element_extracted_watch_list(self, watchs):
# t_elementの extracted_watch_info_dictに情報を挿入
for (i, t_element) in zip(range(len(self.elements_list)), self.elements_list):
# 指定時間範囲のwatchを抽出
watchs.set_watch_list_selected(t_element.start_datetime, t_element.end_datetime)
for watch in watchs.watch_list_selected:
video_id = watch.video_id
# extracted wordを含むwatchかをチェック。含むならflag=1に変更。
flag = 0
for tag in watch.tags:
for dictionary in t_element.extracted_w_info_dict:
if tag in dictionary:
flag = 1
if flag == 1: #flag = 1しか相手にしない。
# 初回登録
if video_id not in t_element.extracted_watch_info_dict:
t_element.extracted_watch_info_dict[video_id] = TelementWatchInfo()
t_element.extracted_watch_info_dict[video_id].view_num = 1
# ↓↓ ここから前処理の計算部分
sum_frequency = 0
for tag in watch.tags:
if tag in t_element.extracted_w_info_dict:
sum_frequency += t_element.extracted_w_info_dict[tag].frequency
x_position = 0
y_position = 0
for tag in watch.tags:
if tag in t_element.word_count_dict:
x_position += t_element.extracted_w_info_dict[tag].frequency * t_element.extracted_w_info_dict[tag].position[0] / sum_frequency
y_position += t_element.extracted_w_info_dict[tag].frequency * t_element.extracted_w_info_dict[tag].position[1] / sum_frequency
# ↑↑ 計算終了
t_element.extracted_watch_info_dict[video_id].position[0] = x_position
t_element.extracted_watch_info_dict[video_id].position[1] = y_position
# 2回目以降登録
else:
t_element.extracted_watch_info_dict[video_id].view_num += 1
#print("x : " + str(t_element.extracted_watch_info_dict[video_id].position[0]))
#print("y : " + str(t_element.extracted_watch_info_dict[video_id].position[1]))
#print( "frequency: " + str(t_element.extracted_watch_info_dict[video_id].view_num))
def draw_word_crowd(self, DRAW_INDEX, X_SIZE, Y_SIZE):
campus = Image.new('RGB', (X_SIZE, Y_SIZE), (128, 128, 128))
position_scale_rate = X_SIZE / 2 * (self.x_max + 0.5)
draw = ImageDraw.Draw(campus)
# 一回 DRAW_INDEX のt_elementに対して、描画してみる。
for i, word in zip(range(len(self.elements_list[DRAW_INDEX].extracted_w_info_dict)), \
self.elements_list[DRAW_INDEX].extracted_w_info_dict\
):
x = self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].position[0]
y = self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].position[1]
size = self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].frequency
size = int(math.log(size, 1.5) * 5) ### ここ考えよ
ttfontname = "./logotypejp_mp_m_1.1.ttf"
fontsize = size
text = word
textRGB = (0, 0, 0)
font = ImageFont.truetype(ttfontname, fontsize)
textWidth, textHeight = draw.textsize(text,font=font)
X_center = X_SIZE / 2 + position_scale_rate * x
Y_center = Y_SIZE / 2 + position_scale_rate * y
if self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].color == "RED":
draw.rectangle((X_center - (textWidth / 2), \
Y_center - (textHeight / 2), \
X_center + (textWidth / 2), \
Y_center + (textHeight / 2)),\
fill=(240, 0, 0), outline=(255, 255, 255)
)
if self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].color == "BLUE":
draw.rectangle((X_center - (textWidth / 2), \
Y_center - (textHeight / 2), \
X_center + (textWidth / 2), \
Y_center + (textHeight / 2)),\
fill=(0, 0, 240), outline=(255, 255, 255))
if self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].color == "PURPLE":
draw.rectangle((X_center - (textWidth / 2), \
Y_center - (textHeight / 2), \
X_center + (textWidth / 2), \
Y_center + (textHeight / 2)),\
fill=(150, 0, 150), outline=(255, 255, 255))
if self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].color == "NO":
draw.rectangle((X_center - (textWidth / 2), \
Y_center - (textHeight / 2), \
X_center + (textWidth / 2), \
Y_center + (textHeight / 2)),\
fill=(50, 50, 50), outline=(255, 255, 255))
text_position_x = X_center - (textWidth / 2)
text_position_y = Y_center - (textHeight / 2)
font = ImageFont.truetype(ttfontname, fontsize)
textWidth, textHeight = draw.textsize(text,font=font)
draw.text((text_position_x, text_position_y), text, fill=textRGB, font=font)
if i == 0:
word_positions_in_pic = np.array([[X_center, Y_center]])
# 別作業するためのDFを作らせてください。↓↓
# tmp_dfの一行 = [word, p_c_x, p_c_y, p_tl_x, p_tl_y, p_tr_x, p_tl_y, p_bl_x, p_bl_y, p_br_x, p_br_y, size, color]
'''
p_tl --------- p_tr
| |
| p_c | <- size (= frequency = importance)
| |
p_bl ----------p_br
'''
tmp_df = pd.DataFrame(\
[[word,\
X_center,\
Y_center,\
X_center - (textWidth / 2),\
Y_center - (textHeight / 2),\
X_center + (textWidth / 2),\
Y_center - (textHeight / 2),\
X_center - (textWidth / 2),\
Y_center + (textHeight / 2),\
X_center + (textWidth / 2),\
Y_center + (textHeight / 2),\
size,\
self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].color\
]]\
)
tmp_df.columns = ['word', 'p_c_x', 'p_c_y', 'p_tl_x', 'p_tl_y', 'p_tr_x', 'p_tr_y', 'p_bl_x', 'p_bl_y', 'p_br_x', 'p_br_y', 'size', 'color']
if i > 0:
a_2d_ex = np.array([[X_center, Y_center]])
word_positions_in_pic = np.append(word_positions_in_pic, a_2d_ex, axis=0)
# 別作業するためのDFを作らせてください。↓↓
tmp_df = tmp_df.append({'word' : word,\
'p_c_x' : X_center,\
'p_c_y' : Y_center,\
'p_tl_x' : X_center - (textWidth / 2),\
'p_tl_y' : Y_center - (textHeight / 2),\
'p_tr_x' : X_center + (textWidth / 2),\
'p_tr_y' : Y_center - (textHeight / 2),\
'p_bl_x' : X_center - (textWidth / 2),\
'p_bl_y' : Y_center + (textHeight / 2),\
'p_br_x' : X_center + (textWidth / 2),\
'p_br_y' : Y_center + (textHeight / 2),\
'size' : size,\
'color' : self.elements_list[DRAW_INDEX].extracted_w_info_dict[word].color\
} , ignore_index=True)
print(tmp_df)
SAVE_PATH = './CSVs/positions_corners_size_csv_out' + '_' + str(DRAW_INDEX) + '.csv'
tmp_df.to_csv(SAVE_PATH, index=False)
#campus.save('./Images/pillow_imagedraw.jpg', quality=95) ← もう画像を残す必要が無くなったからここはコメントアウト
#print(self.elements_list[DRAW_INDEX].word_count_dict)
#print(word_positions_in_pic)
#ドロネー三角分割
tri = Delaunay(word_positions_in_pic)
fig = delaunay_plot_2d(tri)
#fig.savefig('./Images/scipy_matplotlib_delaunay.png') ← 力学モデルを使う前のドロネー三角化 → 表示する必要がないので削除
def draw_thumbnail_crowd_with_word(self, DRAW_INDEX, X_SIZE, Y_SIZE):
# csvファイルオープンする
OPEN_PATH = './CSVs/positions_corners_size_csv_out' + '_' + str(DRAW_INDEX) + '.csv'
df = pd.read_csv(OPEN_PATH)
position_scale_rate = X_SIZE / (self.x_max + 0.5)
for item in self.elements_list[DRAW_INDEX].extracted_watch_info_dict.items():
size = item[1].view_num
if int(math.log(size, 1.5) * 5) > 0:
size = int(math.log(size, 1.5) * 5) ### ここ考えよ
elif int(math.log(size, 1.5) * 5) == 0:
size = 1
video_id = item[0]
X_center = X_SIZE / 2 + position_scale_rate * item[1].position[0]
Y_center = Y_SIZE / 2 + position_scale_rate * item[1].position[1]
picWidth = 32 * item[1].view_num
picHeight = 24 * item[1].view_num
df = df.append({'word' : video_id,\
'p_c_x' : X_center,\
'p_c_y' : Y_center,\
'p_tl_x' : X_center - (picWidth / 2),\
'p_tl_y' : Y_center - (picHeight / 2),\
'p_tr_x' : X_center + (picWidth / 2),\
'p_tr_y' : Y_center - (picHeight / 2),\
'p_bl_x' : X_center - (picWidth / 2),\
'p_bl_y' : Y_center + (picHeight / 2),\
'p_br_x' : X_center + (picWidth / 2),\
'p_br_y' : Y_center + (picHeight / 2),\
'size' : size,\
'color' : "Thumbnail"\
} , ignore_index=True)
print("hoge")
print(df)
SAVE_PATH = './CSVs/withThumb_positions_corners_size_csv_out' + '_' + str(DRAW_INDEX) + '.csv'
df.to_csv(SAVE_PATH, index=False)
''''
campus = Image.open('./Images/pillow_imagedraw.jpg')
#画像の貼り付け
for item in self.elements_list[DRAW_INDEX].extracted_watch_info_dict.items():
url = 'http://i.ytimg.com/vi/' + item[0] + "/mqdefault.jpg"
response = requests.get(url)
image = response.content
file_name = "Thumbnail/" + item[0] + ".jpeg"
with open(file_name, "wb") as aaa:
aaa.write(image)
img = Image.open("Thumbnail/" + item[0] + ".jpeg")
img_resize = img.resize((40*item[1].view_num, 30*item[1].view_num))
campus.paste(img_resize, (int(X_SIZE / 2 + position_scale_rate * item[1].position[0] -2), \
int(Y_SIZE / 2 + position_scale_rate * item[1].position[1] -2) )\
)
campus.save('./Images/pillow_imagedraw.jpg', quality=95)
'''
def draw_significance_curve(self, TSET_INTERVAL_NUM):
# significance curve 書く
left = np.array( range(TSET_INTERVAL_NUM-1) )
height = np.empty(0)
for i in range(TSET_INTERVAL_NUM-1):
# H(X)を求めるために、t[5]に対して、ヒストグラムを作成する。
S_INDEX = i
INTERVAL_NUM = 5 #論文だと64になっていたやつ
for w_info in self.elements_list[S_INDEX].extracted_w_info_dict.values():
# frequency
w_info.set_histgram_position(0, INTERVAL_NUM, self.elements_list[S_INDEX].max_frequency(), self.elements_list[S_INDEX].min_frequency())
# x position
w_info.set_histgram_position(1, INTERVAL_NUM, self.elements_list[S_INDEX].max_x() , self.elements_list[S_INDEX].min_x() )
# y position
w_info.set_histgram_position(2, INTERVAL_NUM, self.elements_list[S_INDEX].max_y() , self.elements_list[S_INDEX].min_y() )
# color
w_info.set_histgram_position(3, INTERVAL_NUM)
if str(w_info.histgram_position) not in self.elements_list[S_INDEX].all_histgram_position_dict:
self.elements_list[S_INDEX].all_histgram_position_dict[str(w_info.histgram_position)] = 1
else:
self.elements_list[S_INDEX].all_histgram_position_dict[str(w_info.histgram_position)] += 1
# H(X)を計算
all_sum = sum(self.elements_list[S_INDEX].all_histgram_position_dict.values())
H_X = 0
for cnt in self.elements_list[S_INDEX].all_histgram_position_dict.values():
p = cnt / all_sum
H_X += p * math.log( 1 / p)
# H(X;Y)を計算
n = 1
for w_info in self.elements_list[S_INDEX + n].extracted_w_info_dict.values():
# frequency
w_info.set_histgram_position(0, INTERVAL_NUM, self.elements_list[S_INDEX + n].max_frequency(), self.elements_list[S_INDEX + n].min_frequency())
# x position
w_info.set_histgram_position(1, INTERVAL_NUM, self.elements_list[S_INDEX + n].max_x() , self.elements_list[S_INDEX + n].min_x() )
# y position
w_info.set_histgram_position(2, INTERVAL_NUM, self.elements_list[S_INDEX + n].max_y() , self.elements_list[S_INDEX + n].min_y() )
# color
w_info.set_histgram_position(3, INTERVAL_NUM)
# joint histgramを作る
# S_INDEXに対して
j_hist_dict = {} # {channel_id: [[1,3,4,2][0,1,3,4]], channel_id: [[-1][0,1,3,4]] }
for item in self.elements_list[S_INDEX].extracted_w_info_dict.items():
myid = item[0]
histgram_position = item[1].histgram_position
if myid not in j_hist_dict:
j_hist_dict[myid] = [histgram_position, [-1]]
# S_INDEX + n に対して
for item in self.elements_list[S_INDEX + n].extracted_w_info_dict.items():
myid = item[0]
histgram_position = item[1].histgram_position
if myid not in j_hist_dict:
j_hist_dict[myid] = [[-1], histgram_position]
else:
j_hist_dict[myid][1] = histgram_position
j_hist_count_dict = {}
for position in j_hist_dict.values():
if str(position) not in j_hist_count_dict:
j_hist_count_dict[str(position)] = 1
else:
j_hist_count_dict[str(position)] += 1
#print(j_hist_count_dict)
all_sum = sum(j_hist_count_dict.values())
H_X_semicolon_Y = 0
for item in j_hist_count_dict.items():
position_str = item[0]
cnt_xy = item[1]
p_xy = cnt_xy / all_sum
#p_x計算する
x_str = '[' + position_str.split(", [")[0][1:]
cnt_x = 0
for key in j_hist_count_dict.keys():
if x_str in key:
cnt_x += 1
p_x = cnt_x / all_sum
#p_y計算する
y_str = '[' + position_str.split(", [")[1][:-1]
cnt_y = 0
for key in j_hist_count_dict.keys():
if y_str in key:
cnt_y += 1
p_y = cnt_y / all_sum
H_X_semicolon_Y += p_xy * math.log( p_xy / (p_x * p_y))
S_X = H_X - H_X_semicolon_Y + 1.4
height = np.append(height, S_X)
print(len(height))
#Bokeh用にデータをcsvで吐き出し
i = 0
for sx_value in height:
if i == 0:
tmp_df = pd.DataFrame(\
[[i,\
sx_value,\
self.elements_list[i].start_datetime,\
self.elements_list[i].end_datetime\
]]\
)
tmp_df.columns = ['i', 'sx_value', 'start_datetime', 'end_datetime']
if i > 0:
tmp_df = tmp_df.append({'i' : i,\
'sx_value' : sx_value,\
'start_datetime' : self.elements_list[i].start_datetime,\
'end_datetime' : self.elements_list[i].end_datetime\
} , ignore_index=True)
i += 1
tmp_df.to_csv('./CSVs/S_X_output.csv', index=False)
# plot S_X.png の作成
plt.style.use("seaborn-dark")
for param in ['figure.facecolor', 'axes.facecolor', 'savefig.facecolor']:
plt.rcParams[param] = '#212946' # bluish dark grey
for param in ['text.color', 'axes.labelcolor', 'xtick.color', 'ytick.color']:
plt.rcParams[param] = '0.9' # very light grey
fig, ax = plt.subplots()
# 折れ線のメイン=水色
ax.plot(left, height, marker='o', color='#08F7FE')
n_shades = 10
diff_linewidth = 1.05
alpha_value = 0.3 / n_shades
# 折れ線の影=水色
for n in range(1, n_shades+1):
ax.plot(marker='o',linewidth=2+(diff_linewidth * n), alpha=alpha_value, legend=False, color='#08F7FE')
ax.fill_between(left,height, color='#08F7FE', alpha=0.1)
# Persistencyに応じて、色を変える
for value, i in zip(height, range(len(height))):
if i == 0:
prevalue = value
continue
if abs(value - prevalue) > 0.7:
ax.plot(np.array([i-1, i]), np.array([prevalue, value]), marker='o', color='#FE53BB')
ax.fill_between(np.array([i-1, i]), np.array([prevalue, value]), color='#FE53BB', alpha=0.11)
prevalue = value
ax.grid(color='#2A3459')
fig.savefig("./Images/S_X.png")
'''
-------------------------------------------------------------------------------
'''
class Welement:
def __init__(self, interval_num):
self.importance_vec = np.zeros(interval_num)
self.position = np.zeros(2)
class Wset: #選び抜かれた、Wordクラウドに出てくるchannel idの集まり
def __init__(self):
self.elements_dict = {} #{channelid: w_element, channelid: w_element, } w_elementには、importance_vec と positionが含まれる。
def set_elements_dict(self, t_set):
self.elements_dict.clear()
for t_element in t_set.elements_list:
for channel_id in t_element.word_count_dict.keys():
if not channel_id in self.elements_dict:
self.elements_dict[channel_id] = Welement(t_set.interval_num)
def set_element_dict_importance_vec(self, t_set):
# w_elementのimportance Vecを設定
for i, t_element in zip(range(t_set.interval_num), t_set.elements_list):
for item in t_element.word_count_dict.items():
self.elements_dict[item[0]].importance_vec[i] = item[1]
def set_words_initital_position(self,t_set):
# Initial Positionを決めるために、 マトリックスを作成
size = len(self.elements_dict)
matrix = np.empty((size, size))
for i, w_element_1 in zip(range(size), self.elements_dict.values()):
for j, w_element_2 in zip(range(size), self.elements_dict.values()):
vec_1 = w_element_1.importance_vec
vec_2 = w_element_2.importance_vec
vec_1 = vec_1 / np.linalg.norm(vec_1, ord=2)
vec_2 = vec_2 / np.linalg.norm(vec_2, ord=2)
value = np.dot(vec_1, vec_2)
matrix[i][j] = 1 - value
mds = MDS(n_components=2, dissimilarity="precomputed")
X_2d = mds.fit_transform(matrix)
t_set.x_max = np.max(X_2d, axis = 0)[0]
t_set.y_max = np.max(X_2d, axis = 0)[1]
t_set.x_min = np.min(X_2d, axis = 0)[0]
t_set.y_min = np.min(X_2d, axis = 0)[1]
# w element に position を入れる。この時に、x, yの最大値、最小値も調べておく。
for (x, y), w_element in zip(X_2d, self.elements_dict.values()):
w_element.position = np.array([x, y])
def print_w_set(self):
for w_element in self.elements_dict.items():
print(w_element)
print("****")
class MainWindow(QWidget):
def __init__(self, image1, image2, start_datetime, end_datetime, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle('Trend View')
self.vbox = QVBoxLayout()
self.vbox2 = QVBoxLayout()
self.parent_hbox = QHBoxLayout()
# QPixmapオブジェクト作成
self.pixmap1 = QPixmap(image1)
self.pixmap2 = QPixmap(image2)
# ラベルを作ってその中に画像を置く
self.lbl1 = QLabel()
self.lbl1.setPixmap(self.pixmap1)
self.lbl_time = QLabel()
self.lbl_time.setText(" " + end_datetime.strftime('%Y/%m/%d') + " ~ " + start_datetime.strftime('%Y/%m/%d'))
self.lbl2 = QLabel()
self.lbl2.setPixmap(self.pixmap2)
self.vbox.addWidget(self.lbl1)
self.vbox.addWidget(self.lbl_time)
self.button = QPushButton('change')
self.button.clicked.connect(self.change)
self.inputText = QLineEdit()
self.inputText.setText("")
self.vbox2.addWidget(self.lbl2)
self.vbox2.addWidget(self.inputText)
self.vbox2.addWidget(self.button)
self.parent_hbox.addLayout(self.vbox)
self.parent_hbox.addLayout(self.vbox2)
self.setLayout(self.parent_hbox)
self.move(300, 200)
self.show()
def change(self):
draw_index = int(self.inputText.text())
start_datetime, end_datetime = make_figure(DRAW_INDEX=draw_index)
self.pixmap1 = QPixmap("pillow_imagedraw.jpg")
self.lbl1.setPixmap(self.pixmap1)
self.pixmap2 = QPixmap("S_X.png")
self.lbl2.setPixmap(self.pixmap2)
self.lbl_time.setText(" " + end_datetime.strftime('%Y/%m/%d') + " ~ " + start_datetime.strftime('%Y/%m/%d'))
'''
'''
'''
-------------------------------------------------------------------------------
'''