-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute-posteriors-factored-model.py
657 lines (407 loc) · 13.1 KB
/
compute-posteriors-factored-model.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
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
import codecs
import os
import math
import numpy as np
import scipy.special
import operator
import datetime
import argparse
def load_phi_w(phi_w_path):
PHI_W = []
file = codecs.open(phi_w_path, 'r', 'utf-8')
for row in file:
s = row.strip().split(' ')
curr_li = []
for elem in s:
prob = float(elem)
curr_li.append(prob)
PHI_W.append(curr_li)
file.close()
return PHI_W
def load_phi_b(phi_b_path):
PHI_B = []
file = codecs.open(phi_b_path, 'r', 'utf-8')
for row in file:
s = row.strip().split(' ')
curr_li = []
for elem in s:
prob = float(elem)
curr_li.append(prob)
PHI_B.append(curr_li)
file.close()
return PHI_B
def load_time_map(mapping_path):
TIME_MAP = {}
mapping_file = codecs.open(mapping_path, 'r', 'utf-8')
idx = 0
for row in mapping_file:
# UserId, PostId, Behav, TimeStamp
s = row.strip().split('\t')
struct_time = datetime.datetime.strptime(s[3], "%Y-%m-%d %H:%M:%S")
# Id = int(s[1])
# actual_ts = int(s[3].strip())
TIME_MAP[idx] = struct_time
idx+=1
return TIME_MAP
def load_link_probs(link_prob_path):
LINK_PROB = []
link_prob_file = codecs.open(link_prob_path, 'r', 'utf-8')
for row in link_prob_file:
s = row.strip().split(' ')
curr_li = []
for elem in s:
prob = float(elem)
curr_li.append(prob)
LINK_PROB.append(curr_li)
link_prob_file.close()
return LINK_PROB
def load_alpha_beta_k():
ALPHA_K = []
alpha_file = codecs.open(alpha_k_path, 'r', 'utf-8')
for row in alpha_file:
ALPHA_K.append(float(row.strip()))
alpha_file.close()
BETA_K = []
beta_file = codecs.open(beta_k_path, 'r', 'utf-8')
for row in beta_file:
BETA_K.append(float(row.strip()))
beta_file.close()
return ALPHA_K, BETA_K
def load_alpha_beta_g(alpha_g_path, beta_g_path):
ALPHA_G = []
alpha_file = codecs.open(alpha_g_path, 'r', 'utf-8')
for row in alpha_file:
curr_alpha = []
s = row.strip().split(' ')
for elem in s:
curr_alpha.append(float(elem))
ALPHA_G.append(curr_alpha)
alpha_file.close()
BETA_G = []
beta_file = codecs.open(beta_g_path, 'r', 'utf-8')
for row in beta_file:
curr_beta = []
s = row.strip().split(' ')
for elem in s:
curr_beta.append(float(elem))
BETA_G.append(curr_beta)
beta_file.close()
return ALPHA_G, BETA_G
def load_group_user_distr(group_user_distr_path):
GROUP_USER = []
group_user_distr_file = codecs.open(group_user_distr_path, 'r', 'utf-8')
for row in group_user_distr_file:
s = row.strip().split(' ')
curr_li = []
for elem in s:
prob = float(elem)
curr_li.append(prob)
GROUP_USER.append(curr_li)
group_user_distr_file.close()
return GROUP_USER
def load_group_prior(group_prior_path):
GROUP_PRIOR = []
group_prior_file = codecs.open(group_prior_path, 'r', 'utf-8')
for row in group_prior_file:
GROUP_PRIOR.append(float(row.strip()))
group_prior_file.close()
return GROUP_PRIOR
def load_group_topic_distr(group_b_topic_distr_path, group_w_topic_distr_path):
GROUP_B_TOPIC = []
GROUP_W_TOPIC = []
group_topic_distr_file = codecs.open(group_b_topic_distr_path, 'r', 'utf-8')
for row in group_topic_distr_file:
s = row.strip().split(' ')
curr_li = []
for elem in s:
prob = float(elem)
curr_li.append(prob)
GROUP_B_TOPIC.append(curr_li)
group_topic_distr_file.close()
group_topic_distr_file = codecs.open(group_w_topic_distr_path, 'r', 'utf-8')
for row in group_topic_distr_file:
s = row.strip().split(' ')
curr_li = []
for elem in s:
prob = float(elem)
curr_li.append(prob)
GROUP_W_TOPIC.append(curr_li)
group_topic_distr_file.close()
return GROUP_B_TOPIC, GROUP_W_TOPIC
def load_word_idx(word_idx_path):
WORD_IDX = {}
file = codecs.open(word_idx_path, 'r', 'utf-8')
idx = 0
for row in file:
# idx+=1
# print(idx)
try:
s = row.strip().split('\t')
WORD_IDX[s[1]] = int(s[0])
except:
print("Error: ",row.strip())
file.close()
return WORD_IDX
def load_behav_idx(behav_idx_path):
BEHAV_IDX = {}
file = codecs.open(behav_idx_path, 'r', 'utf-8')
for row in file:
s = row.strip().split('\t')
BEHAV_IDX[s[1]] = int(s[0])
file.close()
return BEHAV_IDX
def load_user_map(user_map_path):
USER_MAP = {}
file = codecs.open(user_map_path, 'r', 'utf-8')
for row in file:
s = row.strip().split('\t')
USER_MAP[int(s[1])] = s[0]
file.close()
return USER_MAP
def load_user_group_topic(table_assign_path, discount):
USER_GROUP = {}
USER_TOPIC = {}
USER_TABLE = {}
PY_TERM = [[0.0]*K_b]*G
# N_g = [0]*G
# N_k = [0]*K
C = 0
table_idx = 0
table_assign_file = codecs.open(table_assign_path, 'r', 'utf-8')
for row in table_assign_file:
# Num_intr, Group, Topic, (Interactions separated by ,)
s = row.strip().split('\t')
if(int(s[0]) == 0):
table_idx+=1
continue
group = int(s[1])
topic = int(s[2])
intr_list = s[3].strip().split(",")
for elem in intr_list:
if elem.strip() == '':
continue
user = int(elem.strip())
USER_GROUP[user] = group
USER_TOPIC[user] = topic
USER_TABLE[user] = table_idx
# N_g[group]+=1
# N_k[topic]+=1
C+=1
PY_TERM[group][topic] += (int(s[0]) - discount)
table_idx+=1
table_assign_file.close()
return USER_GROUP, USER_TOPIC, USER_TABLE, PY_TERM
def load_post_ids(mapping_path):
POST_IDs = {}
mapping_file = codecs.open(mapping_path, 'r', 'utf-8')
idx = 0
for row in mapping_file:
# UserId, Postid, Behav, CreationDate
s = row.strip().split('\t')
Id = int(s[1])
POST_IDs[idx] = Id
idx+=1
return POST_IDs
def compute_time_prob(alpha, beta, t):
prob = (1.0*(math.pow(t, alpha - 1))*(math.pow(1-t, beta - 1)))/(scipy.special.beta(alpha, beta))
return prob
def compute_posteriors(INTR_GROUP, INTR_TOPIC, USER_MAP, WORD_IDX, BEHAV_IDX, PHI_W, PHI_B, GROUP_USER, ALPHA_G, BETA_G, ALPHA_K, BETA_K, GROUP_PRIOR, GROUP_B_TOPIC_PRIOR, GROUP_W_TOPIC_PRIOR, POST_IDs, LINKS_i_j, LINK_PROB, USER_TABLE, PY_TERM, TIME_MAP, model, dataset, discount, intr_path):
DICT_USER_POSTERIORS = {}
group_posteriors = []
intr_file = codecs.open(intr_path, 'r', 'utf-8')
idx = 0
for row in intr_file:
# Text, u, b, ts
s = row.split('\t')
# if s[1].startswith("TEMP_USER"):
# continue
text = s[0].strip()
u = int(s[1].strip())
b = s[2].strip()
ts = float(s[3].strip())
b = BEHAV_IDX[b]
text_li = text.strip().split(' ')
curr_posterior_g = []
curr_posterior_k = []
for g in range(G):
prob_g = 0.0
for k_w in range(K_w):
prob_k = 1.0
for word in text_li:
if word == '':
continue
w = WORD_IDX[word]
prob_k = prob_k * PHI_W[k_w][w]
prob_k = prob_k * compute_time_prob(ALPHA_G[g][k_w], BETA_G[g][k_w], ts)
prob_k *= GROUP_W_TOPIC_PRIOR[g][k_w]
prob_g+=prob_k
curr_posterior_g.append(prob_g)
group_posteriors.append(curr_posterior_g)
idx+=1
if idx%1000 == 0:
print(model, dataset, discount, idx)
intr_file.close()
print("Without link prob loaded")
intr_file = codecs.open(intr_path, 'r', 'utf-8')
idx = 0
for row in intr_file:
# Text, u, b, ts
s = row.split('\t')
# if s[1].startswith("TEMP_USER"):
# continue
text = s[0].strip()
u = int(s[1].strip())
if u == -1:
idx+=1
continue
b = s[2].strip()
ts = float(s[3].strip())
b = BEHAV_IDX[b]
text_li = text.strip().split(' ')
curr_posterior_g_k_b = []
for g in range(G):
for k_b in range(K_b):
prob = 1.0
prob *= GROUP_PRIOR[g]
prob *= PY_TERM[g][k_b]
prob *= PHI_B[k_b][b]
prob_k_w = 0.0
for k_w in range(K_w):
prob_k = 1.0
for word in text_li:
if word == '':
continue
w = WORD_IDX[word]
prob_k = prob_k * PHI_W[k_w][w]
prob_k = prob_k * compute_time_prob(ALPHA_G[g][k_w], BETA_G[g][k_w], ts)
prob_k *= GROUP_W_TOPIC_PRIOR[g][k_w]
prob_k_w+=prob_k
prob *= prob_k_w
if idx in LINKS_i_j:
curr_links = LINKS_i_j[idx]
for j in curr_links:
posterior_j = group_posteriors[j]
max_index, max_value = max(enumerate(posterior_j), key=operator.itemgetter(1))
prob = prob * LINK_PROB[g][max_index]
curr_posterior_g_k_b.append(prob)
curr_posterior_g = []
li_idx = 0
for g in range(G):
prob_g = 0.0
for k in range(K_b):
prob_g += curr_posterior_g_k_b[li_idx]
li_idx+=1
curr_posterior_g.append(prob_g)
curr_posterior_k = [0.0]*K_b
li_idx = 0
for g in range(G):
for k in range(K_b):
curr_posterior_k[k] += curr_posterior_g_k_b[li_idx]
li_idx+=1
sum_li = np.sum(curr_posterior_g)
if sum_li == 0.0:
sum_li = np.sum([1.0])
curr_posterior_g = curr_posterior_g/sum_li
curr_posterior_g = curr_posterior_g.tolist()
sum_li = np.sum(curr_posterior_k)
if sum_li == 0.0:
sum_li = np.sum([1.0])
curr_posterior_k = curr_posterior_k/sum_li
curr_posterior_k = curr_posterior_k.tolist()
b = s[2].strip()
# actual_ts = TIME_MAP[idx]
# actual_user = USER_MAP[u]
actual_user = u
if actual_user not in DICT_USER_POSTERIORS:
DICT_USER_POSTERIORS[actual_user] = []
# g = INTR_GROUP[idx]
DICT_USER_POSTERIORS[actual_user].append(curr_posterior_g+curr_posterior_k)
idx+=1
if idx%1000==0:
print(model, dataset, discount, idx)
intr_file.close()
return DICT_USER_POSTERIORS
def load_links(links_path):
LINKS_i_j = {}
links_file = codecs.open(links_path, 'r', 'utf-8')
for row in links_file:
# Interaction i -> Interaction j
s = row.strip().split('\t')
i = int(s[0])
j = int(s[1])
if i not in LINKS_i_j:
LINKS_i_j[i] = []
LINKS_i_j[i].append(j)
links_file.close()
return LINKS_i_j
def generate_posteriors(basepath, intr_path, links_path, discount):
# INTR_GROUP, INTR_TOPIC, USER_MAP, WORD_IDX, BEHAV_IDX, PHI_W, PHI_B, GROUP_USER, ALPHA_G, BETA_G, ALPHA_K, BETA_K, GROUP_PRIOR, GROUP_TOPIC_PRIOR
# basepath = "../Output/" +model+"_"+str(K_b)+"_"+str(G)+"_"+dataset+"_"+str(discount)+"00000/"
phi_w_path = basepath+"topic-word-distribution.txt"
phi_b_path = basepath+"topic-behavior-distribution.txt"
alpha_k_path = basepath+"topic-time-alpha.txt"
beta_k_path = basepath+"topic-time-beta.txt"
alpha_g_path = basepath+"group-time-alpha.txt"
beta_g_path = basepath+"group-time-beta.txt"
group_user_distr_path = basepath+"group-user-distribution.txt"
group_prior_path = basepath+"group-priors.txt"
group_b_topic_distr_path = basepath+"group-b-topic-distribution.txt"
group_w_topic_distr_path = basepath+"group-w-topic-distribution.txt"
word_idx_path = basepath+"vocab-mapping.txt"
behav_idx_path = basepath+"behavior-mapping.txt"
table_assign_path = basepath+"table-assignment-status.txt"
# user_map_path = "../Data/"+dataset+"-user-map.txt"
# intr_path = "../Data/"+dataset+"_pre_processed.txt"
# mapping_path = "../Data/"+dataset+"_map.txt"
# links_path = "../Data/"+dataset+"_links.txt"
posterior_path = basepath+"posteriors-user-interactions.txt"
link_prob_path = basepath+"link-prob.txt"
USER_MAP = load_user_map(user_map_path)
WORD_IDX = load_word_idx(word_idx_path)
BEHAV_IDX = load_behav_idx(behav_idx_path)
PHI_W = load_phi_w(phi_w_path)
PHI_B = load_phi_b(phi_b_path)
GROUP_USER = load_group_user_distr(group_user_distr_path)
ALPHA_G, BETA_G = load_alpha_beta_g(alpha_g_path, beta_g_path)
# ALPHA_K, BETA_K = load_alpha_beta_k()
ALPHA_K = []
BETA_K = []
GROUP_PRIOR = load_group_prior(group_prior_path)
GROUP_B_TOPIC_PRIOR, GROUP_W_TOPIC_PRIOR = load_group_topic_distr(group_b_topic_distr_path, group_w_topic_distr_path)
# INTR_GROUP, INTR_TOPIC = load_intr_group_topic()
USER_GROUP, USER_TOPIC, USER_TABLE, PY_TERM = load_user_group_topic(table_assign_path, discount)
# USER_GROUP = {}
# USER_TOPIC = {}
# POST_IDs = load_post_ids(mapping_path)
POST_IDs = {}
LINKS_i_j = load_links(links_path)
LINK_PROB = load_link_probs(link_prob_path)
# LINKS_i_j = {}
# LINK_PROB = []
# TIME_MAP = load_time_map(mapping_path)
TIME_MAP = {}
print("Loading Done")
DICT_USER_POSTERIORS = compute_posteriors(USER_GROUP, USER_TOPIC, USER_MAP, WORD_IDX, BEHAV_IDX, PHI_W, PHI_B, GROUP_USER, ALPHA_G, BETA_G, ALPHA_K, BETA_K, GROUP_PRIOR, GROUP_B_TOPIC_PRIOR, GROUP_W_TOPIC_PRIOR, POST_IDs, LINKS_i_j, LINK_PROB, USER_TABLE, PY_TERM, TIME_MAP, model, dataset, discount, intr_path)
print("User Posterior Dict: ", len(DICT_USER_POSTERIORS))
posterior_file = codecs.open(posterior_path, 'w', 'utf-8')
for user in DICT_USER_POSTERIORS:
print(str(user)+'\t'+str(DICT_USER_POSTERIORS[user]), file = posterior_file)
posterior_file.close()
print('Posteriors Saved to '+ posterior_path)
parser = argparse.ArgumentParser("Posterior_Factored")
parser.add_argument("--output_path", help="Path to CMAP output files")
parser.add_argument("--corpus_path", help="Path to pre_processed file")
parser.add_argument("--links_path", help="Path to links file")
parser.add_argument("--discount", help="Value of Discount Parameter", default = "0.5")
parser.add_argument("--K_b", help="Number of Behavior Topics", default = "5")
parser.add_argument("--K_w", help="Number of Text Topics", default = "20")
parser.add_argument("--G", help="Number of Profiles", default = "20")
args = parser.parse_args()
K_b = int(args.K_b)
K_w = int(args.K_w)
G = int(args.G)
discount = float(args.discount)
basepath = args.output_path
intr_path = args.corpus_path
links_path = args.links_path
generate_posteriors(basepath, intr_path, links_path, discount)