-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.py
497 lines (374 loc) · 15 KB
/
tasks.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
import os
import numpy as np
import pandas as pd
from shutil import copy
from net import Net, TimeSeriesNet
class Task:
def __init__(self, net, years_loop=True, epochs_step=5, epochs_steps=2,
subsets_evaluations=[]):
self.net = net
self.years_loop = years_loop
self.epochs_step = epochs_step
self.epochs_steps = epochs_steps
self.subsets_evaluations = subsets_evaluations
# Create dir for net files if it doesn't exist
self.should_save = False
self.data_dir = os.path.join(self.net.evaluate_dir, 'task-results')
os.makedirs(self.data_dir, exist_ok=True)
def __enter__(self):
self.results = {}
self.orig_suffix = self.net.suffix
self.orig_epochs = self.net.epochs
self.orig_predict_after_years = self.net.predict_after_years
return self
def get_results_filename(self):
return os.path.join(
self.data_dir,
self.__class__.__name__ + self.orig_suffix + '.npy')
def save_results(self):
np.save(self.get_results_filename(), self.results)
self.save_results_nice()
def load_results(self):
return np.load(self.get_results_filename()).item()
def save_results_nice(self):
pass
def __exit__(self, *args, **kwargs):
# Write results
if self.should_save:
self.save_results()
self.net.suffix = self.orig_suffix
self.net.epochs = self.orig_epochs
self.net.predict_after_years = self.orig_predict_after_years
def get_years_range(self):
if isinstance(self.net, TimeSeriesNet) or not self.years_loop:
return [self.net.predict_after_years]
else:
return range(1, self.net.predict_after_years+1)
def get_suffix(self, additional_epochs, **kwargs):
suffix = self.orig_suffix
if not isinstance(self.net, TimeSeriesNet) and self.years_loop:
suffix += '-years' + str(self.net.predict_after_years)
if additional_epochs > 0:
suffix += '-add'+str(additional_epochs)
return suffix
def generator(self):
for years in self.get_years_range():
print("Years:", years)
# Set predict_after_years
self.net.predict_after_years = years
for i in range(0, self.epochs_steps+1):
if i > 0:
# Continue training for a few epochs to get to variance of
# individual epochs
self.net.epochs = self.epochs_step
else:
# Reset from additional epochs training
self.net.epochs = self.epochs = self.orig_epochs
yield (i*self.epochs_step,)
def train(self, params):
additional_epochs, *args = params
if additional_epochs > 0:
load = True
args = tuple(args)
self.net.suffix = self.get_suffix(
additional_epochs - self.epochs_step, *args)
prev_net_file = self.net.get_net_filename()
self.net.suffix = self.get_suffix(additional_epochs, *args)
net_file = self.net.get_net_filename()
copy(prev_net_file, net_file)
else:
load = False
self.net.train(live_validate=False, load=load)
def update_evaluate_result(self, params, data):
additional_epochs, *_ = params
if additional_epochs not in self.results:
self.results[additional_epochs] = []
self.results[additional_epochs].append(data)
def evaluate(self, params):
net_result = self.net.evaluate()
if isinstance(self.net, TimeSeriesNet):
for years, row in net_result.items():
if self.years_loop or years == self.net.predict_after_years:
self.update_evaluate_result(params, row)
else:
loss, r, r2, mae = net_result
years = self.net.predict_after_years
self.update_evaluate_result(params, [years, loss, r, r2, mae])
def train_all(self):
for params in self.generator():
self.train(params)
def evaluate_all(self):
self.results = {}
self.should_save = True
for params in self.generator():
self.evaluate(params)
def run(self):
for params in self.generator():
self.train(params)
self.evaluate(params)
def subsets_evaluate(self):
for params in self.generator():
# Only additional epochs 0
additional_epochs, *_ = params
if additional_epochs != 0:
continue
for subsetsclass, runs in self.subsets_evaluations:
subsets_evaluate = subsetsclass(self.net, runs=runs)
subsets_evaluate.run()
class PredictivityOverTime(Task):
def save_results_nice(self):
# Write results
columns = ['years', 'loss', 'r', 'R^2', 'MAPE', 'MAPEmin5']
for additional_epochs, data in self.results.items():
suffix = self.orig_suffix
if additional_epochs > 0:
suffix += '-add'+str(additional_epochs)
filename = os.path.join(
self.data_dir,
'predictivity-over-time-' + suffix + '.csv')
pd.DataFrame(data, columns=columns).to_csv(filename)
def generator(self):
for params in super().generator():
# For file names
self.net.suffix = self.get_suffix(*params)
yield params
def __enter__(self):
ret = super().__enter__()
# We want to get MAPE >= 5 here
self.orig_metric_mapemin5 = self.net.metric_mapemin5
self.net.metric_mapemin5 = True
return ret
def __exit__(self, *args, **kwargs):
# Reset MAPE >= 5
self.net.metric_mapemin5 = self.orig_metric_mapemin5
super().__exit__(*args, **kwargs)
class PredictivityOverTimeNoHindex0(PredictivityOverTime):
def get_nonzero_author_ids(self):
filename = os.path.join(self.net.data_dir, 'author_ids-%s.npy' %
self.orig_suffix)
try:
nonzero_author_ids = np.load(filename)
except FileNotFoundError as e:
from db import db
sql = """
SELECT a.author_id
FROM analysis{0}_authors AS a
INNER JOIN analysis{0}_hindex_data AS h
ON h.author_id = a.author_id
WHERE
h.predict_after_years = 1 AND
h.hindex_cumulative = 0
""".format(self.net.suffix_cuts)
c = db().cursor()
numauthors = c.execute(sql)
nonzero_author_ids = np.fromiter(
c, count=numauthors, dtype=[('author_id', 'i4')])['author_id']
np.save(filename, nonzero_author_ids)
return nonzero_author_ids
def __enter__(self):
ret = super().__enter__()
# Change suffix
self.__orig_suffix_no_hindex0 = self.orig_suffix
self.orig_suffix += '-nohindex0'
self.net.suffix = self.orig_suffix
# Change get_train_authors method
self.orig_get_train_authors = self.net.__class__.get_train_authors
def fake_get_train_authors(net, *args, **kwargs):
authors = self.orig_get_train_authors(net, *args, **kwargs)
print('authors train incl 0 hindex',
len(np.where(authors['train'] == 1)[0]))
nonzero_author_ids = self.get_nonzero_author_ids()
indices = np.where(np.isin(authors['author_id'], nonzero_author_ids))
authors['train'][indices] = 0
print('authors train excl 0 hindex',
len(np.where(authors['train'] == 1)[0]))
return authors
self.net.__class__.get_train_authors = fake_get_train_authors
return ret
def __exit__(self, *args, **kwargs):
super().__exit__(*args, **kwargs)
# Reset suffix
self.net.suffix = self.__orig_suffix_no_hindex0
# Restore get_train_authors method
self.net.__class__.get_train_authors = self.orig_get_train_authors
class DataImportance(Task):
allow_includes = False
default_excludes = [
['broadness_lda'],
['num_citations'],
['months'],
['pagerank'],
['paper_topics'],
['length'],
['jif',
'published'],
['num_coauthors'],
['avg_coauthor_pagerank',
'max_coauthor_pagerank',
'min_coauthor_pagerank'],
['categories']
]
def __init__(self, net, excludes=None, includes=None, **kwargs):
super().__init__(net, years_loop=False, **kwargs)
if includes is not None and excludes is not None:
raise Exception("Can only specify one of excludes and includes")
if not self.allow_includes and includes is not None:
raise Exception("Please use dedicated classes for includes")
if includes:
self.includes = includes
self.excludes = [self.__invert(include) for include in includes]
elif excludes:
self.includes = None
self.excludes = excludes
else:
self.includes = None
self.excludes = self.default_excludes
def __invert(self, fields):
# 'padding' should not be inverted!
return [field for field in sorted(Net.data_positions.keys())
if field not in fields and field != 'padding'] + \
[field for field in sorted(Net.data_positions_aux.keys())
if field not in fields]
def __enter__(self):
ret = super().__enter__()
# Put include in suffix
if self.includes is not None:
self.__orig_suffix_no_include = self.orig_suffix
self.orig_suffix += '-include'
self.net.suffix = self.orig_suffix
return ret
def __exit__(self, *args, **kwargs):
super().__exit__(*args, **kwargs)
# Reset exclude
self.net.set_exclude_data([])
# Remove include in suffix
if self.includes is not None:
self.net.suffix = self.__orig_suffix_no_include
def save_results_nice(self):
columns = ['exclude', 'years', 'loss', 'r', 'R^2', 'MAPE', 'MAPEmin5']
for additional_epochs, data in self.results.items():
suffix = self.orig_suffix
if additional_epochs > 0:
suffix += '-add'+str(additional_epochs)
filename = os.path.join(self.data_dir,
'data-importance-' + suffix + '.csv')
pd.DataFrame(data, columns=columns).to_csv(filename)
def get_suffix(self, additional_epochs, exclude):
suffix = super().get_suffix(additional_epochs)
if self.includes is not None:
suffix += '-' + '-'.join(self.__invert(exclude))
else:
suffix += '-' + '-'.join(exclude)
return suffix
def generator(self):
for exclude in self.excludes:
for params in super().generator():
params = params + (exclude,)
# For file names
self.net.suffix = self.get_suffix(*params)
# Set exclude
self.net.set_exclude_data(exclude)
yield params
def update_evaluate_result(self, params, data):
additional_epochs, exclude = params
if additional_epochs not in self.results:
self.results[additional_epochs] = []
self.results[additional_epochs].append(['-'.join(exclude)] + data)
class DataImportanceInclude(DataImportance):
allow_includes = True
class DataImportanceOverTime(DataImportance):
def __init__(self, net, **kwargs):
super().__init__(net, **kwargs)
self.years_loop = True
def save_results_nice(self):
if not len(self.results):
return
base_columns = ['loss', 'r', 'R^2', 'MAPE', 'MAPEmin5']
if self.includes is not None:
columns = ["%s_%s" % (col, include[0])
for include in self.includes for col in base_columns]
else:
columns = ["%s_%s" % (col, exclude[0])
for exclude in self.excludes for col in base_columns]
columns = ['years'] + columns
for i in range(0, self.epochs_steps + 1):
additional_epochs = i * self.epochs_step
data = []
for years, year_results in self.results[additional_epochs].items():
row = [year_results['-'.join(exclude)][i]
for exclude in self.excludes
for i, _ in enumerate(base_columns)]
data.append([years] + row)
# Save data
suffix = self.orig_suffix
if additional_epochs > 0:
suffix += '-add' + str(additional_epochs)
filename = os.path.join(
self.data_dir,
'data-importance-over-time-' + suffix + '.csv')
pd.DataFrame(data, columns=columns).to_csv(filename)
def get_years_range(self):
if isinstance(self.net, TimeSeriesNet):
return super().get_years_range()
else:
# To save time only 1, 5, 10
return [1, 5, 10]
def update_evaluate_result(self, params, data):
additional_epochs, exclude = params
exclude = '-'.join(exclude)
years = data[0]
data = data[1:]
if additional_epochs not in self.results:
self.results[additional_epochs] = {}
if years not in self.results[additional_epochs]:
self.results[additional_epochs][years] = {}
self.results[additional_epochs][years][exclude] = data
class DataImportanceOverTimeInclude(DataImportanceOverTime):
allow_includes = True
if __name__ == '__main__':
"""
net = Net(cutoff=Net.CUTOFF_SINGLE, target='hindex_cumulative')
net.epochs = 1
net.predict_after_years = 1
with PredictivityOverTime(net) as task:
task.train_all()
task.evaluate_all()
"""
"""
net = Net(cutoff=Net.CUTOFF_SINGLE, target='sqrt_nc_after')
net.epochs = 1
excludes = DataImportance.default_excludes + [
[],
['broadness_lda',
'months',
'pagerank',
'length',
'jif',
'published',
'num_coauthors',
'avg_coauthor_pagerank',
'max_coauthor_pagerank',
'min_coauthor_pagerank',
'categories',
'paper_topics']
]
with DataImportance(net, excludes) as task:
task.epochs_steps = 0
task.train_all()
task.evaluate_all()
"""
"""
net = Net(cutoff=Net.CUTOFF_SINGLE, target='hindex_cumulative')
net.epochs = 1
with DataImportanceOverTime(net) as task:
# task.train_all()
task.evaluate_all()
"""
"""
net = TimeSeriesNet(cutoff=Net.CUTOFF_SINGLE, target='hindex_cumulative',
force_monotonic=True)
net.epochs = 1
with DataImportanceOverTime(net, epochs_steps=0) as task:
# task.train_all()
task.evaluate_all()
"""