-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildersOpenAlex.py
More file actions
564 lines (474 loc) · 22.8 KB
/
buildersOpenAlex.py
File metadata and controls
564 lines (474 loc) · 22.8 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
import pandas as pd
import traceback
def buildPaperMetadata(data, debug_mode = False):
"""
Extracts metadata from a JSON (transformed in dict) data object containing paper information and constructs a DataFrame.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- dfMeta (DataFrame): A pandas DataFrame containing the extracted metadata.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts various metadata attributes such as paper ID, DOI, publication date, journal ID,
number of citations, language, fees (in USD), and whether the paper is open access.
If a metadata attribute is missing for a paper, the corresponding field in the DataFrame will be filled with an empty string ('').
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'doi': '10.1234/example', ...}, {...}, ...]}
>>> metadata_df = buildPaperMetadata(data)
"""
pId=[]
pDate=[]
pNCit=[]
pJId=[]
pDoi=[]
pLang=[]
pFees=[]
pIsOA=[]
for paper in data['results']:
pId.append(paper['id'].replace('https://openalex.org/',''))
try:
pDoi.append(paper['doi'])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pDoi.append('')
try:
pDate.append(paper['publication_date'])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pDate.append('')
try:
pNCit.append(paper['cited_by_count'])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pNCit.append('')
try:
pJId.append(paper['primary_location']['source']['id'].replace('https://openalex.org/',''))
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pJId.append('')
try:
pLang.append(paper['language'])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pLang.append('')
try:
pFees.append(paper['apc_paid']["value_usd"])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pFees.append('')
try:
pIsOA.append(paper['open_access']["is_oa"])
except Exception as e:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pIsOA.append('')
dfMeta=pd.DataFrame()
dfMeta['paper_id']=pId
dfMeta['paper_doi']=pDoi
dfMeta['pulication_date']=pDate
dfMeta['journal_id']=pJId
dfMeta['n_cit']=pNCit
dfMeta['language']=pLang
dfMeta['fees_usd']=pFees
dfMeta['is_oa']=pIsOA
return dfMeta
def rebuild_abstract(word_dict, debug_mode = False):
"""
Rebuilds abstract text from an inverted index of words to positions, using the 'abstract_inverted_index' key per paper.
Parameters:
- word_dict (dict): A dictionary containing word positions as keys and words as values.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- abstract (str): The reconstructed abstract text.
The function iterates through the keys (word positions) in the input dictionary and reconstructs
the abstract text by placing the corresponding word at its position in the abstract. The reconstructed abstract is
then returned as a single string.
Example usage:
>>> word_dict = {'This':[0], 'is':[1], 'an':[2], 'abstract'[3]}
>>> abstract = rebuild_abstract(word_dict)
>>> print(abstract)
This is an abstract
"""
# find max index in inverted abstract (word_dict)
max_index = 0
for word, list_indexes in word_dict.items():
max_index = max([max_index]+list_indexes)
# create a list of elements long as the number of total words used
abstract = [0]*(max_index+1)
for i in word_dict.keys():
for j in word_dict[i]:
abstract[j] = i
abstract = ' '.join(abstract)
return abstract
def buildTitleAbstractDataframe(data, debug_mode = False):
"""
Constructs a DataFrame containing paper title and abstract information from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- p2Ab (DataFrame): A pandas DataFrame containing paper titles and abstracts.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts the paper ID, title, and abstract information. If any attribute is missing,
it fills the corresponding field in the DataFrame with an empty string ('').
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'title': 'Example Title', ...}, {...}, ...]}
>>> title_abstract_df = buildTitleAbstractDataframe(data)
"""
pTitle=[]
p_abstr=[]
pId=[]
for paper in data['results']:
pId.append(paper['id'].replace('https://openalex.org/',''))
try:
pTitle.append(paper['title'].replace(';','.'))
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pTitle.append('')
try:
# every ";" is substituted with a "." so that ";" can be used as a separator in the DataFrame.
p_abstr.append(rebuild_abstract(paper['abstract_inverted_index'], debug_mode = debug_mode).replace(';','.'))
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
p_abstr.append('')
p2Ab=pd.DataFrame()
p2Ab['paper_id']=pId
p2Ab['title']=pTitle
p2Ab['abstract']=p_abstr
return p2Ab
def buildPaper2Author(data, debug_mode = False):
"""
Constructs a DataFrame containing paper-to-author relationships from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- p2Au (DataFrame): A pandas DataFrame containing paper-to-author relationships.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts the paper ID and author ID information and constructs a DataFrame representing
the relationship between papers and their authors.
For each paper, if multiple authors are present, then multiple lines are created for that paper, one for each author.
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'authorships': [{'author': {...}}, ...]}, {...}, ...]}
>>> paper_author_df = buildPaper2Author(data)
"""
pp=[]
aa=[]
for paper in data['results']:
try:
authors=paper['authorships']
for au in authors:
try:
aa.append(au['author']['id'].replace('https://openalex.org/',''))
pp.append(paper['id'].replace('https://openalex.org/',''))
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
continue
except Exception as e:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
continue
p2Au=pd.DataFrame()
p2Au['paper_id']=pp
p2Au['author_id']=aa
return p2Au
def buildAuthorInformation(data, debug_mode = False):
"""
Constructs a DataFrame containing author information from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- auMD (DataFrame): A pandas DataFrame containing author information.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts author-related attributes such as ID, name, ORCID, institution ID, institution name,
and country.
For each paper, if multiple authors are present, then multiple lines are created for that paper, one for each author.
Example usage:
>>> data = {'results': [{'authorships': [{'author': {'id': '123', ...}}, ...]}, ...]}
>>> author_info_df = buildAuthorInformation(data)
"""
authorId=[]
authorName=[]
authorOrcid=[]
authorInstitutionId=[]
authorInstitutionName=[]
authorInstitutionCountry=[]
for paper in data['results']:
authors=paper['authorships']
for au in authors:
try:
authorId.append(au['author']['id'].replace('https://openalex.org/',''))
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
authorId.append('')
try:
authorName.append(au['author']['display_name'])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
authorName.append('')
try:
authorOrcid.append(au['author']['orcid'])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
authorOrcid.append('')
tmp_authorInstitutionId = ""
tmp_authorInstitutionName = ""
tmp_authorInstitutionCountry = ""
for institution in au['institutions']:
try:
tmp_authorInstitutionId += institution['id'].replace('https://openalex.org/','') + "_"
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
tmp_authorInstitutionId += '' + "_"
try:
tmp_authorInstitutionName += institution['display_name'] + "_"
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
tmp_authorInstitutionName += '' + "_"
try:
tmp_authorInstitutionCountry += institution['country_code'] + "_"
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
tmp_authorInstitutionId += '' + "_"
# remove last /
if len(au['institutions']) > 0:
tmp_authorInstitutionId = tmp_authorInstitutionId[:-1]
tmp_authorInstitutionName = tmp_authorInstitutionName[:-1]
tmp_authorInstitutionCountry = tmp_authorInstitutionCountry[:-1]
authorInstitutionId.append(tmp_authorInstitutionId)
authorInstitutionName.append(tmp_authorInstitutionName)
authorInstitutionCountry.append(tmp_authorInstitutionCountry)
auMD=pd.DataFrame()
auMD['author_id']=authorId
auMD['author_name']=authorName
auMD['author_orcid']=authorOrcid
auMD['institution_id']=authorInstitutionId
auMD['institution_name']=authorInstitutionName
auMD['country']=authorInstitutionCountry
return auMD
def buildPaperConcepts(data, debug_mode = False):
"""
Constructs a DataFrame containing paper concepts from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- concDf (DataFrame): A pandas DataFrame containing paper concepts.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts concept-related attributes such as paper ID, concept ID, and concept score.
For each paper, if multiple concepts are present, then multiple lines are created for that paper, one for each concept.
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'concepts': [{'id': '123', ...}}, ...], ...}
>>> paper_concepts_df = buildPaperConcepts(data)
"""
pList=[]
pConcepts=[]
pScore=[]
for paper in data['results']:
try:
klist=[(paper['id'].replace('https://openalex.org/',''),i['id'].replace('https://openalex.org/',''),i['score']) for i in paper['concepts']]
except KeyError as e: # some recent papers do not yet have topics field in the json
klist=[(paper['id'].replace('https://openalex.org/',''),"","")]
for (u,v,w) in klist:
pList.append(u)
pConcepts.append(v)
pScore.append(w)
concDf=pd.DataFrame()
concDf['paper_id']=pList
concDf['concept_id']=pConcepts
concDf['concept_score']=pScore
return concDf
def buildPaperKeywords(data, debug_mode = False):
"""
Constructs a DataFrame containing paper keywords from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- keywordDf (DataFrame): A pandas DataFrame containing paper keywords.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts keyword-related attributes such as paper ID, keyword, and keyword score.
For each paper, if multiple keywords are present, then multiple lines are created for that paper, one for each keyword.
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'keywords': [{'keyword': 'machine learning', 'score': 0.8}, ...]}, ...]}
>>> paper_keywords_df = buildPaperKeywords(data)
"""
pList=[]
pKeyword=[]
pScore=[]
for paper in data['results']:
try:
klist=[(paper['id'].replace('https://openalex.org/',''),i['display_name'],i['score']) for i in paper['keywords']]
except KeyError as e: # some recent papers do not yet have topics field in the json
klist=[(paper['id'].replace('https://openalex.org/',''),"","")]
for (u,v,w) in klist:
pList.append(u)
pKeyword.append(v)
pScore.append(w)
keywordDf=pd.DataFrame()
keywordDf['paper_id']=pList
keywordDf['keyword']=pKeyword
keywordDf['keyword_score']=pScore
return keywordDf
def buildPaperTopics(data, debug_mode = False):
"""
Constructs a DataFrame containing paper topics from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- topicDf (DataFrame): A pandas DataFrame containing paper topics.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts topic-related attributes such as paper ID, topic ID, and topic score.
For each paper, if multiple topics are present, then multiple lines are created for that paper, one for each topic.
For each paper, there should always be three topics
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'topics': [{'id': '123', 'score': 0.8}, ...]}, ...]}
>>> paper_topics_df = buildPaperTopics(data)
"""
pList=[]
pTopic=[]
pScore=[]
for paper in data['results']:
try:
klist=[(paper['id'].replace('https://openalex.org/',''),i['id'].replace('https://openalex.org/',''),i['score']) for i in paper['topics']]
except KeyError as e: # some recent papers do not yet have topics field in the json
klist=[(paper['id'].replace('https://openalex.org/',''),"","")]
for _ in klist:
for i,l in enumerate([pList,pTopic,pScore]):
l.append(_[i])
topicDf=pd.DataFrame()
topicDf['paper_id']=pList
topicDf['topic_id']=pTopic
topicDf['topic_score']=pScore
return topicDf
def buildPaperReferences(data, debug_mode = False):
"""
Constructs a DataFrame containing paper references from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
Returns:
- referenceDf (DataFrame): A pandas DataFrame containing paper references.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts reference-related attributes such as paper ID and referenced work IDs.
For each paper, if multiple references are present, then multiple lines are created for that paper, one for each reference.
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'referenced_works': ['https://openalex.org/5678', ...]}, ...]}
>>> paper_references_df = buildPaperReferences(data)
"""
pList=[]
pReferences=[]
for paper in data['results']:
pList.append(paper['id'].replace('https://openalex.org/',''))
tmp_references = ""
for reference_url in paper['referenced_works']:
try:
tmp_references += reference_url.replace('https://openalex.org/','') + "_"
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
tmp_references += '' + "_"
# remove last /
if len(paper['referenced_works']) > 0:
tmp_references = tmp_references[:-1]
pReferences.append(tmp_references)
referenceDf=pd.DataFrame()
referenceDf['paper_id']=pList
referenceDf['reference_ids']=pReferences
return referenceDf
def buildPaperGrants(data, debug_mode = False):
"""
Constructs a DataFrame containing paper grants from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- grantDf (DataFrame): A pandas DataFrame containing paper grants.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts grant-related attributes such as paper ID, funder ID, funder name, and award ID.
For each paper, if multiple grants are present, then multiple lines are created for that paper, one for each grant.
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'grants': [{'funder': 'https://openalex.org/funder1', 'funder_display_name': 'Funder 1', 'pAwardID': '12345'}, ...]}, ...]}
>>> paper_grants_df = buildPaperGrants(data)
"""
pList=[]
pFunder=[]
pFname=[]
pAwardID=[]
for paper in data['results']:
for grant in paper['grants']:
try:
pList.append(paper['id'].replace('https://openalex.org/',''))
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pList.append('')
try:
pFunder.append(grant['funder'].replace('https://openalex.org/',''))
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pFunder.append('')
try:
pFname.append(grant['funder_display_name'])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pFname.append('')
try:
pAwardID.append(grant['award_id'])
except Exception as e:
if debug_mode:
print(paper['id'].replace('https://openalex.org/',''), e, "\n\n", traceback.format_exc(), flush=True)
pAwardID.append('')
grantDf=pd.DataFrame()
grantDf['paper_id']=pList
grantDf['funder_id']=pFunder
grantDf['funder_name']=pFname
grantDf['award_id']=pAwardID
return grantDf
def buildPaperSustainableDevelopmentGoals(data, debug_mode = False):
"""
Constructs a DataFrame containing paper sustainable development goals (SDGs) from a JSON data object.
Parameters:
- data (dict): A dictionary containing paper information.
- debug_mode (bool): if True it prints errors caught with try and except.
Returns:
- sdgDf (DataFrame): A pandas DataFrame containing paper SDGs.
The function iterates through each paper in the 'results' list of the input data dictionary.
It extracts SDG-related attributes such as paper ID, SDG ID, and SDG score.
For each paper, if multiple SDGs are present, then multiple lines are created for that paper, one for each SDG.
Example usage:
>>> data = {'results': [{'id': 'https://openalex.org/1234', 'sustainable_development_goals': [{'id': '1', 'score': 0.8}, ...]}, ...]}
>>> paper_sdgs_df = buildPaperSustainableDevelopmentGoals(data)
"""
pList=[]
pSdgIdList=[]
pSdgIdScore=[]
for paper in data['results']:
try:
klist=[(paper['id'].replace('https://openalex.org/',''),i['id'].replace('https://metadata.un.org/sdg/',''),i['score']) for i in paper['sustainable_development_goals']]
except KeyError as e: # some recent papers do not yet have topics field in the json
klist = [(paper['id'].replace('https://openalex.org/',''),"","")]
for _ in klist:
for i,l in enumerate([pList,pSdgIdList,pSdgIdScore]):
l.append(_[i])
sdgDf=pd.DataFrame()
sdgDf['paper_id']=pList
sdgDf['sdg_id']=pSdgIdList
sdgDf['sdg_score']=pSdgIdScore
return sdgDf