/
network_citations.py
559 lines (434 loc) · 30.7 KB
/
network_citations.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
## Imports
from pyspark import SparkConf, SparkContext
from pyspark.mllib.regression import LabeledPoint, LinearRegressionWithSGD, LinearRegressionModel
from pyspark.mllib.feature import Normalizer
import time
## CONSTANTS
APP_NAME = "Network Citations"
def histogram():
citations = sc.textFile("/corpora/corpus-microsoft-academic-graph/data//PaperReferences.tsv");
papers_citations = citations.map(lambda l : (l.split('\t')[1], 1)).reduceByKey(lambda a,b: a+b)
papers_citations.saveAsTextFile('/corpora/corpus-microsoft-academic-graph/data/PaperReferencesCounts.tsv')
citations_stats = papers_citations.map(lambda c: c[1] )
#histogram = citations_stats.histogram(list(range(0,200000,100)))
#print(citations.stats())
def extract_cs_papers(sc):
#field of study
fos = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/FieldsOfStudy.tsv.bz2")
#onley computer science
fos = fos.map(lambda x : x.split("\t")).filter(lambda x: "Computer Science" in x[1])
#[['21286E67', 'Information and Computer Science'], ['0186E68F', 'AP Computer Science'], ['0271BC14', 'Computer Science'], ['08063736', 'On the Cruelty of Really Teaching Computer Science']]
keywords = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperKeywords.tsv.bz2")
#onley keywords related to computer science field
fkws = keywords.map(lambda k: k.split("\t")).filter(lambda l: l[2] == "0271BC14")
#onley papers that talks about computer science field with number of keywords as value
cs_papers_ids = fkws.map(lambda kw: (kw[0], 1)).reduceByKey(lambda a,b : a+b)
cs_papers_ids.saveAsHadoopFile('/user/bd-ss16-g3/data/cs_papers_ids', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
def get_papers_of(sc, year):
papers = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/Papers.tsv.bz2")
papers = papers.map(lambda line : line.split("\t"))
papers = papers.map(lambda line : [line[0], line[1], line[2], int(line[3]), line[4], line[5], line[6], line[7], line[8], line[9], line[10]]).filter(lambda l: l[3] == year)
return papers
def get_citations_on_papers_of(sc, year):
#papers and number of citations per year
citations = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperReferences.tsv.bz2").map(lambda line : line.split("\t")).map(lambda c: (c[0], c[1]))
papers = get_papers_of(sc, year)
papers = papers.map(lambda p: (p[0], p[3]))
#join
papersMap = sc.broadcast(papers.collectAsMap());
rowFunc1 = lambda x: (x[0], x[1], papersMap.value.get(x[1], -1))
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
result = citations.mapPartitions(mapFunc1, preservesPartitioning=True)
result = result.filter(lambda c: c[2] != -1).map(lambda x: (x[0], x[1]))
return result
def nb_citations_per_paper(sc):
papers = sc.textFile("/user/bd-ss16-g3/data/papers")
papers = papers.map(lambda p : p.split("\t"))
citations = sc.textFile("/user/bd-ss16-g3/data/citations")
cited_papers = citations.map(lambda x: x.split("\t")).map(lambda x: (x[1], 1)).reduceByKey(lambda a, b: a+b)
#join
cited_papers_bc = sc.broadcast(cited_papers.collectAsMap());
rowFunc = lambda line: (line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7], line[8], line[9], line[10], cited_papers_bc.value.get(line[0], 0))
def mapFunc(partition):
for row in partition:
yield rowFunc(row)
result = papers.mapPartitions(mapFunc, preservesPartitioning=True)
return result
def authors_weights(sc):
papers = sc.textFile("/user/bd-ss16-g3/data/papers_with_nb_citations")
papers = papers.map(lambda p : p.split("\t"))
#paper_id and number of citations
papers = papers.map(lambda p: (p[0], p[11]))
papers = papers.filter(lambda p: p[1] != '0')
paa = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperAuthorAffiliations.tsv.bz2").map(lambda l : l.split("\t")).filter(lambda a : a[1] != '')
#author_id & 1 for the paper he/she is in
paa1 = paa.map(lambda l : (l[0], l[1], 1/float(l[5])));
#join
papersMap = sc.broadcast(papers.collectAsMap());
rowFunc1 = lambda x: (x[1], float(papersMap.value.get(x[0], 0)) * x[2])
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
stage1 = paa1.mapPartitions(mapFunc1, preservesPartitioning=True)
stage1 = stage1.reduceByKey(lambda a, b : a+b)
result = stage1.combineByKey(lambda value: (value, 1),lambda x, value: (x[0] + value, x[1] + 1),lambda x, y: (x[0] + y[0], x[1] + y[1]))
result = result.map(lambda item: (item[0], item[1][0]/item[1][1]))
return result
def affiliations_weights(sc):
papers = sc.textFile("/user/bd-ss16-g3/data/papers_with_nb_citations")
papers = papers.map(lambda p : p.split("\t"))
#paper_id and number of citations
papers = papers.map(lambda p: (p[0], p[11]))
papers = papers.filter(lambda p: p[1] != '0')
paa = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperAuthorAffiliations.tsv.bz2").map(lambda l : l.split("\t")).filter(lambda a : a[2] != '')
#author_id & 1 for the paper he/she is in
paa = paa.map(lambda l : (l[0], l[2], 1));
#join
papersMap = sc.broadcast(papers.collectAsMap());
rowFunc1 = lambda x: (x[1], int(papersMap.value.get(x[0], 0)))
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
result = paa.mapPartitions(mapFunc1, preservesPartitioning=True)
result = result.reduceByKey(lambda a, b : a+b)
return result
def conf_weights(sc):
papers = sc.textFile("/user/bd-ss16-g3/data/papers_with_nb_citations")
papers = papers.map(lambda p : p.split("\t"))
#conference_id and number of citations
confs = papers.map(lambda p: (p[8].lower(), int(p[11])))
confs = confs.filter(lambda p: p[1] != 0 and p[0] != '')
confs = confs.reduceByKey(lambda a, b : a+b)
return confs
def fos_weights(sc):
papers = sc.textFile("/user/bd-ss16-g3/data/papers_with_nb_citations")
papers = papers.map(lambda p : p.split("\t"))
#paper_id and number of citations
papers = papers.map(lambda p: (p[0], p[11]))
papers = papers.filter(lambda p: p[1] != '0')
keywords = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperKeywords.tsv.bz2")
keywords = keywords.map(lambda k: k.split("\t"))
#join
papersMap = sc.broadcast(papers.collectAsMap());
rowFunc1 = lambda x: (x[2], int(papersMap.value.get(x[0], 0)))
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
fos = keywords.mapPartitions(mapFunc1, preservesPartitioning=True)
fos = fos.reduceByKey(lambda a, b : a+b)
return fos
def get_paa_of(sc, year):
#papers and number of citations per year
paas = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperAuthorAffiliations.tsv.bz2")
paas = paas.map(lambda line : line.split("\t")).map(lambda c: (c[0], c[1], c[2], c[3], c[4]))
papers = get_papers_of(sc, year)
papers = papers.map(lambda p: (p[0], 1))
#join
papersMap = sc.broadcast(papers.collectAsMap())
rowFunc1 = lambda x: (x[0], x[1], x[2], x[3], x[4], papersMap.value.get(x[0], -1))
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
result = paas.mapPartitions(mapFunc1, preservesPartitioning=True)
result = result.filter(lambda c: c[5] != -1)
return result
def convert_papers_to_feature_file(sc):
#step1 conference weight
conferences = sc.textFile("/user/bd-ss16-g3/data/confs_citations")
conferences = conferences.map(lambda a : a.split("\t")).filter(lambda a: float(a[1]) > 0).map(lambda a: (a[0], a[1]))
conferences_bc = sc.broadcast(conferences.collectAsMap())
papers = sc.textFile("/user/bd-ss16-g3/data/papers").map(lambda l : l.split("\t"))
#paper_id, conf_id
papers = papers.map(lambda l : (l[0], l[8].lower()));
rowFunc1 = lambda x: (x[0], float(conferences_bc.value.get(x[1], 0)))
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
papers_with_conf_weights = papers.mapPartitions(mapFunc1, preservesPartitioning=True)
#by now we have for each paper the weight of its authors
papers_with_conf_weights.saveAsHadoopFile('/user/bd-ss16-g3/data/papers_conferences_weight', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step2 adding author weight
authors = sc.textFile("/user/bd-ss16-g3/data/authors_citations")
authors = authors.map(lambda a : a.split("\t")).filter(lambda a: float(a[1]) > 0).map(lambda a: (a[0], a[1]))
authors_bc = sc.broadcast(authors.collectAsMap())
paa = sc.textFile("/user/bd-ss16-g3/data/paper_author_affiliation").map(lambda l : l.split("\t")).filter(lambda a : a[1] != '')
#paper_id, author_id
paa = paa.map(lambda l : (l[0], l[1]));
rowFunc1 = lambda x: (x[0], float(authors_bc.value.get(x[1], 0)))
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
papers_with_author_weights = paa.mapPartitions(mapFunc1, preservesPartitioning=True)
papers_with_author_weights = papers_with_author_weights.combineByKey(lambda value: (value, 1),lambda x, value: (x[0] + value, x[1] + 1),lambda x, y: (x[0] + y[0], x[1] + y[1]))
papers_with_author_weights = papers_with_author_weights.map(lambda item: (item[0], item[1][0]/item[1][1]))
#by now we have for each paper the weight of its authors
papers_with_author_weights.saveAsHadoopFile('/user/bd-ss16-g3/data/papers_authors_weight', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step3 affiliation weight
affiliations = sc.textFile("/user/bd-ss16-g3/data/affiliation_citations")
affiliations = affiliations.map(lambda a : a.split("\t")).filter(lambda a: float(a[1]) > 0).map(lambda a: (a[0], a[1]))
affiliations_bc = sc.broadcast(affiliations.collectAsMap())
paa = sc.textFile("/user/bd-ss16-g3/data/paper_author_affiliation").map(lambda l : l.split("\t")).filter(lambda a : a[2] != '')
#paper_id, affiliation_id
paa = paa.map(lambda l : (l[0], l[2]));
rowFunc1 = lambda x: (x[0], float(affiliations_bc.value.get(x[1], 0)))
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
papers_with_affiliation_weights = paa.mapPartitions(mapFunc1, preservesPartitioning=True)
papers_with_affiliation_weights = papers_with_affiliation_weights.combineByKey(lambda value: (value, 1),lambda x, value: (x[0] + value, x[1] + 1),lambda x, y: (x[0] + y[0], x[1] + y[1]))
papers_with_affiliation_weights = papers_with_affiliation_weights.map(lambda item: (item[0], item[1][0]/item[1][1]))
#by now we have for each paper the weight of its authors
papers_with_affiliation_weights.saveAsHadoopFile('/user/bd-ss16-g3/data/papers_affiliation_weight', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step4 fieldofstudy weight
fos = sc.textFile("/user/bd-ss16-g3/data/fos_citations")
fos = fos.map(lambda a : a.split("\t")).filter(lambda a: float(a[1]) > 0).map(lambda a: (a[0], a[1]))
fos_bc = sc.broadcast(fos.collectAsMap())
keywords = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperKeywords.tsv.bz2").map(lambda l : l.split("\t"))
#paper_id, field_of_study
keywords = keywords.map(lambda l : (l[0], l[2]));
rowFunc1 = lambda x: (x[0], float(fos_bc.value.get(x[1], 0)))
def mapFunc1(partition):
for row in partition:
yield rowFunc1(row)
papers_with_fos_weights = keywords.mapPartitions(mapFunc1, preservesPartitioning=True)
papers_with_fos_weights = papers_with_fos_weights.combineByKey(lambda value: (value, 1),lambda x, value: (x[0] + value, x[1] + 1),lambda x, y: (x[0] + y[0], x[1] + y[1]))
papers_with_fos_weights = papers_with_fos_weights.map(lambda item: (item[0], item[1][0]/item[1][1]))
#by now we have for each paper the weight of its authors
papers_with_fos_weights.saveAsHadoopFile('/user/bd-ss16-g3/data/papers_fosn_weight', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
def merge_features_files(sc):
papers = sc.textFile("/user/bd-ss16-g3/data/papers_with_nb_citations").map(lambda line: line.split("\t")).map(lambda i: (i[0], float(i[11])))
papers = papers.filter(lambda p: p[1] > 0).map(lambda p: (p[0],p[1]))
authors = sc.textFile("/user/bd-ss16-g3/data/papers_authors_weight").map(lambda line: line.split("\t")).map(lambda i: (i[0], i[1]))
affiliations = sc.textFile("/user/bd-ss16-g3/data/papers_affiliation_weight").map(lambda line: line.split("\t")).map(lambda i: (i[0], i[1]))
fos = sc.textFile("/user/bd-ss16-g3/data/papers_fosn_weight").map(lambda line: line.split("\t")).map(lambda i: (i[0], i[1]))
conferences = sc.textFile("/user/bd-ss16-g3/data/papers_conferences_weight").map(lambda line: line.split("\t")).map(lambda i: (i[0], i[1]))
stage1 = papers.leftOuterJoin(authors)
stage2 = stage1.leftOuterJoin(affiliations)
stage3 = stage2.leftOuterJoin(conferences)
stage4 = stage3.leftOuterJoin(fos)
#('808B8671', ((((3.0, '2.25'), '19267.0'), '32.0'), '13052.0'))
#paper_id, nb_citations, fos, conf, aff, author
stage4 = stage4.map(lambda p: (p[0],p[1][1], p[1][0][1], p[1][0][0][1], p[1][0][0][0][1], p[1][0][0][0][0]))
stage4 = stage4.map(lambda p: (p[0], '\t'.join([str(p[1]),str(p[2]), str(p[3]), str(p[4]), str(p[5])])))
stage4.saveAsHadoopFile('/user/bd-ss16-g3/data/features_file', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
def extract_features(sc, year):
#step1 extract papers of year year
papers = get_papers_of(sc, year)
papers = papers.map(lambda line: (line[0], '\t'.join([line[0], line[1], line[2], str(line[3]), line[4], line[5], line[6], line[7], line[8], line[9], line[10]])))
papers.saveAsHadoopFile('/user/bd-ss16-g3/data/papers', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step2 extract citations on papers of year year
citations = get_citations_on_papers_of(sc, year)
citations.saveAsHadoopFile('/user/bd-ss16-g3/data/citations', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step3 extract paper author affiliation
paas = get_paa_of(sc, year)
paas = paas.map(lambda line: (line[0], '\t'.join([line[1], line[2], line[3], line[4]])))
paas.saveAsHadoopFile('/user/bd-ss16-g3/data/paper_author_affiliation', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step4 extract number of citations for each paper in the subset
cited_papers = nb_citations_per_paper(sc)
cited_papers = cited_papers.map(lambda p: (p[0],'\t'.join([p[1], p[2], str(p[3]), p[4], p[5], p[6], p[7], p[8], p[9], p[10], str(p[11])])))
cited_papers.saveAsHadoopFile('/user/bd-ss16-g3/data/papers_with_nb_citations', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step5 give authors weight based on number of citations they got on each paper
author_feature = authors_weights(sc)
author_feature.saveAsHadoopFile('/user/bd-ss16-g3/data/authors_citations', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step6 give affiliations weight based on number of citations
affiliation_feature = affiliations_weights(sc)
affiliation_feature.saveAsHadoopFile('/user/bd-ss16-g3/data/affiliation_citations', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step7 give conferences weight based on number of citations
confs = conf_weights(sc)
confs.saveAsHadoopFile('/user/bd-ss16-g3/data/confs_citations', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#step8 give fos weight based on number of citations
fos = fos_weights(sc)
fos.saveAsHadoopFile('/user/bd-ss16-g3/data/fos_citations', "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
def learn_model(sc, file_path, normalize):
feature_file = sc.textFile(file_path).map(lambda l:l.split("\t"))
points = feature_file.map(lambda f: LabeledPoint(f[1], f[2:]))
#normalizing
if normalize:
nor = Normalizer()
labels = points.map(lambda x: x.label)
features = points.map(lambda x: x.features)
points = labels.zip(nor.transform(features))
points = points.map(lambda i: LabeledPoint(i[0], i[1]))
training, testing = points.randomSplit([0.7,0.3],11)
index = 0
iterations = 100
p_mse = -1
converge = False
result = {}
while(not converge):
x = time.clock()
model = LinearRegressionWithSGD.train(training, iterations=iterations, step=0.00001,intercept=True,regType="l1")
y = time.clock()
print("========== time = " + str(y - x))
preds = testing.map(lambda p: (p.label, model.predict(p.features)))
MSE = preds.map(lambda r: (r[1] - r[0])**2).reduce(lambda x, y: x + y) / preds.count()
print("========== MSE = " + str(MSE))
if p_mse == MSE:
converge = True
iterations = iterations +100
result[iterations] = MSE
p_mse = MSE
print(result)
return model
def test(sc):
#papers_c = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c").map(lambda line : line.split("\t")).map(lambda c: (c[0], c[1]))
#papers_i = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/Papers.tsv.bz2").map(lambda line : line.split("\t")).map(lambda c: (c[0], c[3]))
#result = papers_c.join(papers_i)
#result = result.map(lambda r: (r[0], '\t'.join([r[1][0], r[1][1]])))
#result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_year", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
# citations = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperReferences.tsv.bz2").map(lambda line : line.split("\t")).map(lambda c: (c[0], c[1]))
# papers = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_year").map(lambda line : line.split("\t")).map(lambda c: (c[0], c[2]))
# result = papers.join(citations)
# result = result.map(lambda r: (r[0], '\t'.join([r[1][0], r[1][1]])))
# result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/citations_1", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
# citations_1 = sc.textFile("/user/bd-ss16-g3/data_all/citations_1").map(lambda line : line.split("\t")).map(lambda c: (c[2], (c[0], c[1])))
# papers = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_year").map(lambda line : line.split("\t")).map(lambda c: (c[0], c[2]))
# result = papers.join(citations_1)
# result = result.map(lambda r: (r[0], '\t'.join([r[1][0], r[1][1][0], r[1][1][1]])))
# result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/citations_2", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
# citations_2 = sc.textFile("/user/bd-ss16-g3/data_all/citations_2").map(lambda line : line.split("\t"))
# c3years = citations_2.filter(lambda c: (int(c[3]) - int(c[1])) == 3).map(lambda c: (c[0], c[2]))
# c3years.saveAsHadoopFile("/user/bd-ss16-g3/data_all/citations_3years_old", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
# citations3years = sc.textFile("/user/bd-ss16-g3/data_all/citations_3years_old").map(lambda line : line.split("\t"))
# cited_papers = citations3years.map(lambda l: (l[1], 1)).reduceByKey(lambda a,b: a+b)
# all_papers = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c").map(lambda line : line.split("\t")).map(lambda c: (c[0], c[1]))
# all_papers = all_papers.leftOuterJoin(cited_papers)
# #update all papers nb citations from cited_papers
# all_papers = all_papers.map(lambda p: (p[0], p[1][1])).map(lambda p: (p[0], 0 if p[1] == None else p[1]))
# all_papers.saveAsHadoopFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_3years_citations", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#author feature
# all_papers = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_3years_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[1])))
# paa = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperAuthorAffiliations.tsv.bz2").map(lambda l : l.split("\t")).filter(lambda a : a[1] != '')
# paa = paa.map(lambda p: (p[0], (p[1], 1/float(p[5]))))
# result = paa.join(all_papers)
# result = result.map(lambda i: (i[1][0][0], 0 if i[1][1] == None else (i[1][0][1] * i[1][1]) ))
# #reduce by combining
# result = result.combineByKey(lambda value: (value, 1),lambda x, value: (x[0] + value, x[1] + 1),lambda x, y: (x[0] + y[0], x[1] + y[1]))
# result = result.map(lambda item: (item[0], item[1][0]/item[1][1]))
# result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/authors_weights", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#affiliation feature
# all_papers = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_3years_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[1])))
# paa = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperAuthorAffiliations.tsv.bz2").map(lambda l : l.split("\t")).filter(lambda a : a[2] != '')
# paa = paa.map(lambda p: (p[0], (p[2], 1/float(p[5]))))
# result = paa.join(all_papers)
# result = result.map(lambda i: (i[1][0][0], 0 if i[1][1] == None else (i[1][0][1] * i[1][1]) ))
# #reduce by combining
# result = result.combineByKey(lambda value: (value, 1),lambda x, value: (x[0] + value, x[1] + 1),lambda x, y: (x[0] + y[0], x[1] + y[1]))
# result = result.map(lambda item: (item[0], item[1][0]/item[1][1]))
# result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/affiliations_weights", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#fos feature
# all_papers = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_3years_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[1])))
# keywords = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperKeywords.tsv.bz2").map(lambda k: k.split("\t")).map(lambda f: (f[0], f[2]))
# result = keywords.join(all_papers)
# result = result.map(lambda i: (i[1][0], 0 if i[1][1] == None else i[1][1]))
# #reduce by combining
# result = result.combineByKey(lambda value: (value, 1),lambda x, value: (x[0] + value, x[1] + 1),lambda x, y: (x[0] + y[0], x[1] + y[1]))
# result = result.map(lambda item: (item[0], item[1][0]/item[1][1]))
# result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/fos_weights", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#conf feature
# papers_citations = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_3years_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[1])))
# papers = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/Papers.tsv.bz2").map(lambda p: p.split("\t")).map(lambda p: (p[0], p[5]))
# result = papers_citations.join(papers)
# result = result.map(lambda i: (i[1][1], float(i[1][0])))
# #reduce by combining
# result = result.combineByKey(lambda value: (value, 1),lambda x, value: (x[0] + value, x[1] + 1),lambda x, y: (x[0] + y[0], x[1] + y[1]))
# result = result.map(lambda item: (item[0], item[1][0]/item[1][1]))
# result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/confs_weights", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#Learning ============= papers + authors ================
# paa = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperAuthorAffiliations.tsv.bz2").map(lambda l : l.split("\t")).filter(lambda a : a[1] != '')
# paa = paa.map(lambda p: (p[1], p[0]))
# #join with authors
# authors_f = sc.textFile("/user/bd-ss16-g3/data_all/author_weight").map(lambda a: a.split("\t")).map(lambda a: (a[0], float(a[1])))
# result = paa.join(authors_f).map(lambda p: (p[1][0], 0 if p[1][1] == None else p[1][1]))
# #sum up weights
# result = result.reduceByKey(lambda a,b: a+b)
# #join with papers
# papers = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_year").map(lambda a: a.split("\t")).map(lambda a: (a[0], float(a[1])))
# result2 = papers.join(result).map(lambda p: (p[0], p[1][0], 0 if p[1][1] == None else p[1][1]))
# result2 = result2.map(lambda x: (x[0], '\t'.join([str(x[1]), str(x[2])])))
# result2.saveAsHadoopFile("/user/bd-ss16-g3/data_all/paper_author_weight_citations_3", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#Learning ============= papers + affiliations ================
# paa = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperAuthorAffiliations.tsv.bz2").map(lambda l : l.split("\t")).filter(lambda a : a[2] != '')
# paa = paa.map(lambda p: (p[2], p[0]))
# #join with authors
# affs_f = sc.textFile("/user/bd-ss16-g3/data_all/affiliations_weights").map(lambda a: a.split("\t")).map(lambda a: (a[0], float(a[1])))
# result = paa.join(affs_f).map(lambda p: (p[1][0], 0 if p[1][1] == None else p[1][1]))
# #sum up weights
# result = result.reduceByKey(lambda a,b: a+b)
# #join with papers
# papers = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_year").map(lambda a: a.split("\t")).map(lambda a: (a[0], float(a[1])))
# result2 = papers.leftOuterJoin(result).map(lambda p: (p[0], p[1][0], 0 if p[1][1] == None else p[1][1]))
# result2 = result2.map(lambda x: (x[0], '\t'.join([str(x[1]), str(x[2])])))
# result2.saveAsHadoopFile("/user/bd-ss16-g3/data_all/paper_affiliations_weight_citations", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#Learning ============= papers + fos ================
# fos_papers = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/PaperKeywords.tsv.bz2").map(lambda k: k.split("\t")).map(lambda f: (f[2], f[0]))
# papers_citations = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_year").map(lambda a: a.split("\t")).map(lambda a: (a[0], float(a[1])))
# fos_weights = sc.textFile("/user/bd-ss16-g3/data_all/fos_weights").map(lambda a: a.split("\t")).map(lambda a: (a[0], float(a[1])))
# #join with
# result = fos_papers.leftOuterJoin(fos_weights).map(lambda p: (p[1][0], 0 if p[1][1] == None else p[1][1]))
# #sum up weights
# result = result.reduceByKey(lambda a,b: a+b)
# #join with papers
# result2 = papers_citations.leftOuterJoin(result).map(lambda p: (p[0], p[1][0], 0 if p[1][1] == None else p[1][1]))
# result2 = result2.map(lambda x: (x[0], '\t'.join([str(x[1]), str(x[2])])))
# result2.saveAsHadoopFile("/user/bd-ss16-g3/data_all/paper_fos_weight_citations", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#Learning ============= papers + confs ================
# papers_citations = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_3years_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[1])))
# confs_papers = sc.textFile("/corpora/corpus-microsoft-academic-graph/data/Papers.tsv.bz2").map(lambda p: p.split("\t")).map(lambda p: (p[5], p[0]))
# confs_weights = sc.textFile("/user/bd-ss16-g3/data_all/confs_weights").map(lambda a: a.split("\t")).map(lambda a: (a[0], float(a[1])))
# #join with authors
# result = confs_papers.join(confs_weights).map(lambda p: (p[1][0], 0 if p[1][1] == None else p[1][1]))
# #sum up weights
# result = result.reduceByKey(lambda a,b: a+b)
# #join with papers
# result2 = papers_citations.join(result).map(lambda p: (p[0], p[1][0], 0 if p[1][1] == None else p[1][1]))
# result2 = result2.map(lambda x: (x[0], '\t'.join([str(x[1]), str(x[2])])))
# result2.saveAsHadoopFile("/user/bd-ss16-g3/data_all/paper_conf_weight_citations", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
#merge all features together
papers_citations = sc.textFile("/user/bd-ss16-g3/data_all/papers_citations_less_200c_3years_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[1])))
author_feature = sc.textFile("/user/bd-ss16-g3/data_all/paper_author_weight_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], (float(p[1]), float(p[2]))))
affiliation_feature = sc.textFile("/user/bd-ss16-g3/data_all/paper_affiliations_weight_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[2])))
fos_feature = sc.textFile("/user/bd-ss16-g3/data_all/paper_fos_weight_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[2])))
conf_feature = sc.textFile("/user/bd-ss16-g3/data_all/paper_conf_weight_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], float(p[2])))
result = author_feature.join(affiliation_feature)
result.cache()
result = result.join(fos_feature)
result.cache()
result = result.join(conf_feature)
#paper_id, nb_citations, author_weight, affiliation_w, fos_weight, conf_weight
result = result.map(lambda x: (x[0], str(x[1][0][0][0][0]), str(x[1][0][0][0][1]), str(x[1][0][0][1]), str(x[1][0][1]), str(x[1][1])))
result = result.map(lambda x: (x[0], '\t'.join(x[1:])))
result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/paper_all_weights", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
# print(result.take(1))
# author_feature_1 = sc.textFile("/user/bd-ss16-g3/data_all/paper_author_weight_citations").map(lambda p: p.split("\t")).map(lambda p: (p[0], (float(p[1]), float(p[2]))))
# author_feature_2 = sc.textFile("/user/bd-ss16-g3/data_all/paper_author_weight_citations_1").map(lambda p: p.split("\t")).map(lambda p: (p[0], (float(p[1]), float(p[2]))))
# result = author_feature_1.join(author_feature_2)
# result = result.map(lambda x: (x[0], str(x[1][0][0]), str(x[1][0][1]), str(x[1][1][1])))
# result = result.map(lambda x: (x[0], '\t'.join(x[1:])))
# result.saveAsHadoopFile("/user/bd-ss16-g3/data_all/author_feature_3", "org.apache.hadoop.mapred.TextOutputFormat", compressionCodecClass="org.apache.hadoop.io.compress.GzipCodec")
if __name__ == "__main__":
# Configure OPTIONS
conf = SparkConf().setAppName(APP_NAME)
conf = conf.setMaster("yarn-client")
conf = conf.set("spark.executor.memory", "25g").set("spark.driver.memory", "25g").set("spark.mesos.executor.memoryOverhead", "10000")
sc = SparkContext(conf=conf)
test(sc)
# model = learn_model(sc, "/user/bd-ss16-g3/data_all/paper_author_weight_citations", False)
# model.save(sc,'/user/bd-ss16-g3/data_all/author_model')
#model = learn_model(sc, "/user/bd-ss16-g3/data_all/paper_fos_weight_citations", False)
#model.save(sc,'/user/bd-ss16-g3/data_all/paper_all_weights')
#step1
#Extract weights for the features
#extract_features(sc, 2012)
#step2
#build feature file
#convert_papers_to_feature_file(sc)
#step3
#merge featurs files into one
#merge_features_files(sc)
#step4
#learn a linear model from the feature file
#model = learn_model(sc)
#model.save(sc,'/user/bd-ss16-g3/data/my_model')