forked from susheel/papers
-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprint-extractor.py
227 lines (197 loc) · 8.31 KB
/
preprint-extractor.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
#!/usr/bin/env python
# usage: preprint-extractor.py
__author__ = "Susheel Varma"
__copyright__ = "Copyright (c) 2019-2020 Susheel Varma All Rights Reserved."
__email__ = "[email protected]"
__license__ = "MIT"
import sys
import time
import csv
import json
import urllib
from datetime import datetime
from operator import itemgetter
import psutil
import requests
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
import ray
# Based on https://api.biorxiv.org/covid19/help
BIORXIV_COVID_API_URL = "https://api.biorxiv.org/covid19/{}"
HDRUK_MEMBERS_CSV = "/home/runner/secrets/contacts.csv"
# HDRUK_MEMBERS_CSV = "../contacts.csv"
num_cpus = psutil.cpu_count(logical=False)
ray.init(num_cpus=num_cpus)
def request_url(URL):
"""HTTP GET request and load into json"""
r = requests.get(URL)
if r.status_code != requests.codes.ok:
r.raise_for_status()
return json.loads(r.text)
def read_json(filename):
with open(filename, 'r') as file:
return json.load(file)
def write_json(data, filename, indent=2):
with open(filename, 'w') as jsonfile:
json.dump(data, jsonfile, indent=indent)
def write_csv(data, header, outputFilename):
with open(outputFilename, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=header)
writer.writeheader()
writer.writerows(data)
def read_csv(filename):
header = []
data = []
with open(filename, mode='r', encoding='utf-8-sig', newline='') as csvfile:
reader = csv.DictReader(csvfile)
header = reader.fieldnames
for row in reader:
data.append(row)
return data, header
def retrieve_preprints(BASE_URL, data=None, page=0):
DATA = data or []
URL = BASE_URL.format(page)
print("Retrieving preprints from", URL)
d = request_url(URL)
cursor = int(d['messages'][0]['cursor'])
count = int(d['messages'][0]['count'])
total = int(d['messages'][0]['total'])
page = (cursor+count)+1
print("cursor:{} count:{} total:{}".format(cursor, count, total))
DATA.extend(d['collection'])
if page < total:
time.sleep(1)
retrieve_preprints(BIORXIV_COVID_API_URL, DATA, page)
return DATA
def fuzzy_match_lists(value_list, match_list):
max_match_value = 0
for value in value_list:
matches = process.extract(value, match_list, scorer=fuzz.token_set_ratio)
match_value = max(matches, key = itemgetter(1))[1]
if int(match_value) > int(max_match_value): max_match_value = match_value
return max_match_value
def match_lists(value_list, match_list):
for v in value_list:
if v.lower() in (match.lower() for match in match_list):
return 100
return 0
@ray.remote
def filter_preprint(i, p, num_preprints, authors, affiliations):
fuzzy_row = None
exact_row = None
doi = p.get('rel_doi', "")
if p.get('rel_authors', None) is not None:
preprint_authors = [a['author_name'] for a in p['rel_authors']]
preprint_affiliations = [a['author_inst'] for a in p['rel_authors']]
doi_max_author_match = 0
doi_max_affiliation_match = 0
print("{}/{} Processing authors and affiliations for doi:{}".format(i+1, num_preprints, doi))
# Fuzzy match author
doi_max_author_match = fuzzy_match_lists(preprint_authors, authors)
# Fuzzy match affilaition
doi_max_affiliation_match = fuzzy_match_lists(preprint_affiliations, affiliations)
print("Author Fuzzy Match: {} | Affiliation Fuzzy Match: {}".format(doi_max_author_match, doi_max_affiliation_match))
if doi_max_author_match >= 90 and doi_max_affiliation_match >= 90:
fuzzy_row = {
'site': p.get('rel_site', ""),
'doi': doi,
'date': p.get('rel_date',""),
'link': p.get('rel_link', ""),
'title': p.get('rel_title', ""),
'authors': "; ".join(preprint_authors),
'affiliations': "; ".join(preprint_affiliations),
'abstract': p.get('rel_abs', ""),
'category': p.get('category', ""),
'match_type': "fuzzy",
'author_similarity': doi_max_author_match,
'affiliation_similarity': doi_max_affiliation_match
}
print(fuzzy_row)
doi_max_author_match = 0
doi_max_affiliation_match = 0
# Exact match author
doi_max_author_match = match_lists(preprint_authors, authors)
# Exact match affilaition
doi_max_affiliation_match = match_lists(preprint_affiliations, affiliations)
print("Author Exact Match: {} | Affiliation Exact Match: {}".format(doi_max_author_match, doi_max_affiliation_match))
if doi_max_author_match >= 90 and doi_max_affiliation_match >= 90:
exact_row = {
'site': p.get('rel_site', ""),
'doi': doi,
'date': p.get('rel_date',""),
'link': p.get('rel_link', ""),
'title': p.get('rel_title', ""),
'authors': "; ".join(preprint_authors),
'affiliations': "; ".join(preprint_affiliations),
'abstract': p.get('rel_abs', ""),
'category': p.get('category', ""),
'match_type': "exact",
'author_similarity': doi_max_author_match,
'affiliation_similarity': doi_max_affiliation_match
}
print(exact_row)
return (fuzzy_row, exact_row)
def filter_preprints(preprints):
found = 0
authors = []
affiliations = []
members, header = read_csv(HDRUK_MEMBERS_CSV)
for a in members:
authors.append(a['Full Name'])
affiliations.append(a['Affiliation'])
authors = list(set(authors))
affiliations = list(set(affiliations))
affiliations.extend(["HDRUK", "HDR UK", "HDR-UK", "HEALTH DATA RESEARCH UK", "HEALTH DATA RESEARCH UK LTD"])
ray_authors_id = ray.put(authors)
ray_affiliations_id = ray.put(affiliations)
num_preprints = len(preprints)
ray_num_preprints_id = ray.put(num_preprints)
futures = []
for i, p in enumerate(preprints):
futures.append(filter_preprint.remote(i, p, ray_num_preprints_id, ray_authors_id, ray_affiliations_id))
# Get match results
results = ray.get(futures)
fuzzy_data = [r[0] for r in results if r[0] is not None]
exact_data = [r[1] for r in results if r[1] is not None]
return fuzzy_data, exact_data
def generate_summary(preprints):
data = []
headers = []
summaries = {}
for p in preprints:
date_obj = datetime.strptime(p['date'], "%Y-%m-%d")
month_year = datetime.strftime(date_obj, "%b-%y")
if month_year not in summaries.keys(): summaries[month_year] = {"Total": 0}
summaries[month_year]['Total'] += 1
if p['category'] not in summaries[month_year].keys(): summaries[month_year][p['category']] = 0
summaries[month_year][p['category']] += 1
for month, summary in summaries.items():
row = { 'month': month}
row.update(summary)
headers.extend(row.keys())
data.append(row)
headers = list(set(headers))
return data, headers
def main():
# read old preprint extract
old_data = read_json('data/covid/raw-preprints.json')
# retrieve new preprint extract
data = retrieve_preprints(BIORXIV_COVID_API_URL)
write_json(data, 'data/covid/raw-preprints.json')
# check if length of new extract is more than old extract. fail if not.
if len(old_data) >= len(data):
print("Error: New extract ({}) smaller than previous extract ({})".format(len(data), len(old_data)))
sys.exit(1)
# data = read_json('data/covid/raw-preprints.json')
# filter preprints for HDR UK authors and affilaitions
fuzzy_data, exact_data = filter_preprints(data)
headers = ['site', 'doi', 'date', 'link', 'title', 'authors', 'affiliations', 'abstract', 'category', 'match_type', 'author_similarity', 'affiliation_similarity']
write_json(fuzzy_data, 'data/covid/preprints.json')
write_csv(fuzzy_data, headers, "data/covid/preprints.csv")
write_json(exact_data, 'data/covid/preprints.exact.json')
write_csv(exact_data, headers, "data/covid/preprints.exact.csv")
# generate preprint summary
summary, headers = generate_summary(fuzzy_data)
write_csv(summary, headers, "data/covid/preprints-summary.csv")
if __name__ == "__main__":
main()