/
compete.py
325 lines (268 loc) · 10.2 KB
/
compete.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
import os.path
import pdb
import pickle
from collections import OrderedDict
import IPython.display as d
import numpy as np
import pandas as pd
from bs4 import BeautifulSoup
from SComplexity.crawl import collect_pubs
from SComplexity.get_bmark_corpus import process
from SComplexity.t_analysis import text_proc
# Put these results, in a data frame, then in Markdown, using RGerkin's code.
# https://gist.github.com/rgerkin/af5b27a0e30531c30f2bf628aa41a553
# !pip install --user tabulate # Install the tabulate package
from tabulate import tabulate
from SComplexity.t_analysis import text_proc, perplexity, unigram
def metricss(rg):
if isinstance(rg,list):
pub_count = len(rg)
standard = np.mean([ r['standard'] for r in rg ])
return standard
else:
return None
def metricsp(rg):
if isinstance(rg,list):
pub_count = len(rg)
penalty = np.mean([ r['penalty'] for r in rg ])
penalty = np.mean([ r['perplexity'] for r in rg ])
return penalty
else:
return None
def filter_empty(the_list):
the_list = [ tl for tl in the_list if tl is not None ]
return [ tl for tl in the_list if 'standard' in tl.keys() ]
try:
summary_data = pickle.load(open('competition_data.p','rb'))
except:
pass
# good standards:
xkcd_self_sufficient = str('http://splasho.com/upgoer5/library.php')
high_standard = str('https://elifesciences.org/download/aHR0cHM6Ly9jZG4uZWxpZmVzY2llbmNlcy5vcmcvYXJ0aWNsZXMvMjc3MjUvZWxpZmUtMjc3MjUtdjIucGRm/elife-27725-v2.pdf?_hash=WA%2Fey48HnQ4FpVd6bc0xCTZPXjE5ralhFP2TaMBMp1c%3D')
# competitors (3):
RGERKIN = str('https://scholar.google.com/citations?user=GzG5kRAAAAAJ&hl=en&oi=sra')
SCROOK = str('https://scholar.google.com/citations?user=xnsDhO4AAAAJ&hl=en&oe=ASCII&oi=sra')
EMARDER = str('https://scholar.google.com/citations?user=WUWmiBcAAAAJ&hl=en&oi=sra')
GRAYDEN = str('https://scholar.google.com/citations?user=X7aP2LIAAAAJ&hl=en')
SMBAER = str('https://scholar.google.com/scholar?hl=en&as_sdt=0%2C3&q=SM+baer+&btnG=')
RICK_FAVORED = str('https://elifesciences.org/articles/08127')
MARKRAM = str('https://scholar.google.com/citations?user=W3lyJF8AAAAJ&hl=en&oi=sra')
BHENDERSON = str('https://scholar.google.com/citations?user=o_aMfnoAAAAJ&hl=en&oi=ao')
PMCGURRIN = str('https://www.pmcgurrin.com/publications')
try:
assert os.path.isfile('authors.p')
authors = pickle.load(open('authors.p','rb'))
except:
RGERKIN = collect_pubs(RGERKIN)
SCROOK = collect_pubs(SCROOK)
GRAYDEN = collect_pubs(GRAYDEN)
MARKRAM = collect_pubs(MARKRAM)
EMARDER = collect_pubs(EMARDER)
BHENDERSON = collect_pubs(BHENDERSON)
authors = {}
authors['rgerkin'] = RGERKIN
authors['scrook'] = SCROOK
authors['grayden'] = GRAYDEN
authors['smbaer'] = SMBAER
authors['markram'] = MARKRAM
authors['emarder'] = EMARDER
authors['bhen'] = BHENDERSON
with open('authors.p','wb') as f:
pickle.dump(authors,f)
PMCGURRIN = collect_pubs(PMCGURRIN)
BHENDERSON = collect_pubs(BHENDERSON)
#authors['markram'] = MARKRAM
#authors['emarder'] = EMARDER
authors['bhen'] = BHENDERSON
authors['pg'] = PMCGURRIN
with open('authors.p','wb') as f:
pickle.dump(authors,f)
try:
assert os.path.isfile('other_standards.p')
other_s = pickle.load(open('other_standards.p','rb'))
except:
hs = process(high_standard)
urlDat = {'link':high_standard}
hss = text_proc(hs,urlDat)
benchmark = process(xkcd_self_sufficient)
urlDat = {'link':xkcd_self_sufficient}
bench = text_proc(benchmark,urlDat)
other_s = pickle.dump([hss,benchmark,bench],open('other_standards.p','wb'))
def get_ind_author(author_link_scholar_link_list):
more = [author_results['markram'],author_results['emarder'],authors['bhen']]
names = [str('bhen'),str('pg')]
latest = []
latest.extend(authors['bhen'])
latest.extend(authors['pg'])
for i,s in enumerate(latest):
follow_links = collect_pubs(s)
#names[i] = str('bhen')
#import pdb; pdb.set_trace()
for r in follow_links[0:10]:
urlDat = process(r)
if not isinstance(urlDat,type(None)):
if str(r) not in author_results.keys():
author_results[names[i]] = {}
author_results[names[i]][str(r)] = urlDat
else:
author_results[names[i]][str(r)] = urlDat
print(author_results)
with open('new.p','wb') as f:
pickle.dump(author_results,f)
try:
assert os.path.isfile('author_results.p')
author_results = pickle.load(open('author_results.p','rb'))
#import pdb
#pdb.set_trace()
#sb_results = pickle.load(open('sb_results.p','rb'))
new = pickle.load(open('new.p','rb'))
#author_results['sbaer'] = sb_results
author_results['markram'] = new['markram']
author_results['emarder'] = new['emarder']
author_results['bhen'] = new['bhen']
except:
author_results['bhen'] = authors['bhen']
more = [author_results['markram'],author_results['emarder'],author_results['bhen']]
names = [ str('bhen')]
local = []
local.extend(authors['bhen'][0:10])
local.extend(authors['pg'][0:10])
for i,s in enumerate(local):
follow_links = collect_pubs(s)
#names[i] = str('bhen')
#import pdb; pdb.set_trace()
for r in follow_links[0:15]:
urlDat = process(r)
if not isinstance(urlDat,type(None)):
if str(r) not in author_results.keys():
author_results[str('bhen')] = {}
author_results[str('bhen')][str(r)] = urlDat
else:
author_results[str('bhen')][str(r)] = urlDat
print(author_results)
with open('new.p','wb') as f:
pickle.dump(author_results,f)
# with open('new.p','wb') as f:
# pickle.dump(author_results,f)
author_results = {'rgerkin':{}, 'scrook':{}, 'grayden':{}, 'emarder':{}, 'markram':{},'bhen':{}}
for author,links in authors.items():
for r in links:
urlDat = process(r)
if str(r) not in author_results.keys():
author_results[author][str(r)] = urlDat
else:
author_results[author][str(r)] = urlDat
print(author_results)
with open('intermediate_author_results.p','wb') as f:
pickle.dump(author_results,f)
with open('author_results.p','wb') as f:
pickle.dump(author_results,f)
try:
assert 1==2
big_model = pickle.load(open('big_model_science.p','rb'))
author_results = OrderedDict(pickle.load(open('author_results_processed.p','rb')))
except:
author_results_r = OrderedDict(author_results)
terms = []
for k,v in author_results.items():
try:
#author_results_r[k] = list(s for s in v.values() )
author_results[k]['files'] = list(s for s in v.values() )
words = [ ws['tokens'] for ws in author_results[k]['files'] if ws is not None ]
author_results[k]['words'] = words
terms.extend(words)# if isinstance(terms,dict) ]
except:
print(terms[-1])
big_model = unigram(terms)
with open('author_results_processed.p','wb') as file:
pickle.dump(author_results,file)
with open('big_model_science.p','wb') as file:
pickle.dump(list(big_model),file)
compete_results = {}
for k,v in author_results.items():
per_dpc = []
try:
for doc in author_results[k]['words']:
per_doc.append(perplexity(doc, big_model))
except:
pass
compete_results[k] = np.mean(per_doc)
author_results['rgerkin']['perplexity'] = compete_results[k]
bh = list(author_results['bhen'].values())
rg = list(author_results['rgerkin'].values())
sc = list(author_results['scrook'].values())
gn = list(author_results['grayden'].values())
em = list(author_results['emarder'].values())
mk = list(author_results['markram'].values())
import pdb
pdb.set_trace()
bh = filter_empty(bh)
rg = filter_empty(rg)
sc = filter_empty(sc)
gn = filter_empty(gn)
#sb = filter_empty(sb)
mk = filter_empty(mk)
em = filter_empty(em)
all_authors = []
all_authors.extend(rg)
all_authors.extend(sc)
all_authors.extend(gn)
#all_authors.extend(sb)
all_authors.extend(em)
all_authors.extend(markram)
pickle.dump([rg,sc,gn,sb,all_authors],open('competition_data.p','wb'))
rick = metricss(rg)
scrook = metricss(sc)
grayden = metricss(gn)
bhenderson = metricss(bh)
emarder = metricss(em)
markram = metricss(mk)
rank = [(rick,str('rick')),(scrook,str('sharon')),(bhenderson,str('bryan henderson'))
(grayden,str('grayden')),(emarder,str('emarder')),(markram,str('markram'))]
print('the winner of the science clarity competition is: ', sorted(rank)[0])
pdb.set_trace()
print(rank)
print(rick,scrook,grayden,smbaer)
rick = metricsp(rg)
scrook = metricsp(sc)
scrook = metricsp(gn)
smbaer = metricsp(sb)
emarder = metricsp(em)
markram = metricsp(mk)
print('penalties: rick,scrook,grayden,bryan hen,emarder,markram')
print(rick,scrook,grayden,bhen,emarder,markram)
# Some random data
#data = np.random.rand(10,4)
# Columns A, B, C, D
#columns = [chr(x) for x in range(65,69)]
# Create the dataframe
data = [rick,scrook,scrook,smbaer,emarder,markram]
df = pd.DataFrame(data=data,
columns=columns)
# Optionally give the dataframe's index a name
#df.index.name = "my_index"
# Create the markdown string
md = tabulate(df, headers='keys', tablefmt='pipe')
# Fix the markdown string; it will not render with an empty first table cell,
# so if the dataframe's index has no name, just place an 'x' there.
md = md.replace('| |','| %s |' % (df.index.name if df.index.name else 'x'))
# Create the Markdown object
result = d.Markdown(md)
# Display the markdown object (in a Jupyter code cell)
result
#print('the scores are:',np.min(rick,sharon))
'''
bench = metrics(bench)
pm = metrics(pm)
hss = metrics(hss)
winners = [('rgerkin',rick),('scrook',scrook),('upgoer5_corpus',bench),('the readability of science decr over time', hss), ('peter',pm)]
with open('results.p','wb') as f:
pickle.dump(winners,f)
winners = sorted([(w[1],w[0]) for w in winners])
print(winners)
try:
win = pickle.load(open('winners.p','rb'))
winners = sorted([(w['standard'],list(w.items())) for w in both_authors])
except:
pass
'''