-
Notifications
You must be signed in to change notification settings - Fork 0
/
runbench.py
392 lines (295 loc) · 15.4 KB
/
runbench.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
""" The main runner script for the Clairvoya benchmark code
Config should be specified in a python config file (see voya_config_example.py) and ideally stored in config i.e
config/config_name.py
This would then be ran as
runbench.py config.config_name
runbench.py config/config_name.py
Usage:
runbench.py [<config>] [-v <level>]
Options:
config config file location
-v level verbosity level 0 = quiet, 1 = info, 2 = debug
Notes:
Currently the config files are stored in python, these aren't very portable and are not the best end solution
but are much faster for prototyping new classifiers. If these become cumbersome we should rethink as they arent a
great solution.
Syntax:
We use the sklearn syntax of
clf = classifier
clf_name = classifier name
X = features from dataset
y = labels from dataset
y_pred = predicted labels on test set
X and y are further defined
X_<test/train>_<split_num>
We split the dataset into a test and training set and we do
this multiple times, each time has a different split number
eg X_train_0 and y_test_4
"""
import os
import importlib
import logging
import csv
from sklearn.grid_search import GridSearchCV
import sklearn.metrics
import sklearn.cross_validation
import sklearn.ensemble
import docopt
import matplotlib.pyplot as plt
import datasetup
import benchmarks
import voya_plotter
voya_logger = logging.getLogger('clairvoya')
def run_benchmark(config, classifiers, classifiers_gridparameters):
""" Runs the benchmark code, see voya_config_example for argument explanation
"""
default_config = {
"data_file": None, # input data
"test_df": None, # instead of data_file, give split data
"train_df": None,
"out_path": None,
"num_folds": 5,
"test_size": 0.2,
"num_cores": 1,
"pu_learning": False,
"pu_rand_samp_frac": False,
"verbosity": 0,
"random_forest_tree_plot": False,
"auc_folds": 1,
'u_to_p_ratio': False,
'ranking_Frac': None,
'include_neg_inTrain': True,
'grisearch_metric': 'AUC',
}
default_config.update(config)
config = default_config
set_verbosity_level(config["verbosity"])
voya_logger.info("Starting Benchmark")
out_path = config['out_path']
if out_path is not None:
if not os.path.isdir(out_path):
os.makedirs(out_path)
# If we are given the test / train sets explicitly
test_df = config["test_df"]
train_df = config["train_df"]
if test_df is not None and train_df is not None:
y_test, X_test = datasetup.split_df_labels_features(test_df)
y_train, X_train = datasetup.split_df_labels_features(train_df)
elif config["data_file"] is not None: # or load all the data and auto split
voya_logger.info('loading data from: {}'.format(config['data_file']))
try:
df = datasetup.load_data(config['data_file'])
except IOError: # file doesnt exist, try seeing is its a df instead
df = config['data_file']
voya_logger.info("Input data labels \n{}".format(df.label.value_counts()))
try:
datasetup.scale_dataframe_features(df)
except TypeError: # Got a string as the DF (after IOError)
raise VoyaConfigError('data_file is not a valid path to a file or a Pandas DF, got {}'.format(df))
if config["pu_learning"]: # input of positive, negative and unlabeled labels (1, -1, 0)
voya_logger.info("PU Learning Mode On")
if config["u_to_p_ratio"]:
df = datasetup.downsample_pu_df(df, config["u_to_p_ratio"])
df_test, df_train = datasetup.split_test_train_df_pu(df, config['test_size'],)
assert set(df_test['label'].unique()) == set((1, 0))
y_test, X_test = datasetup.split_df_labels_features(df_test)
y_train, X_train = datasetup.split_df_labels_features(df_train)
else: # input of positive and negative (i.e 1, 0)
X, y = datasetup.split_df_labels_features(df)
X_train, y_train, X_test, y_test = datasetup.get_stratifed_data(y, X, config['test_size'])
else:
raise ValueError("You must give either `test_df` and `train_df` OR `data_file` in config")
results_table_rows = {} # each row is a dict with column_name: value
for clf_name, clf_notoptimized in classifiers.iteritems():
voya_logger.info("Running {}".format(clf_name))
clf_results = {'clf_name': clf_name}
param_grid = classifiers_gridparameters[clf_name]
if param_grid is None:
voya_logger.info('Skipping grid search for {}'.format(clf_name))
voya_logger.debug("clf_notoptimized {}".format(clf_notoptimized))
clf_fitted = clf_notoptimized.fit(X_train, y_train)
else:
voya_logger.info('Performing grid search for {}'.format(clf_name))
skf = sklearn.cross_validation.StratifiedKFold(y_train, n_folds=config['num_folds'])
ranking = voya_plotter.PrInRanking(config['ranking_Frac'], config['desired_retention'])
if (config['gridsearch_metric'] == 'PosRate'):
clf = GridSearchCV(estimator=clf_notoptimized, param_grid=param_grid, cv=skf, scoring=ranking.pr_in_ranking,
n_jobs=config['num_cores'])
elif (config['gridsearch_metric'] == 'Frac'):
clf = GridSearchCV(estimator=clf_notoptimized, param_grid=param_grid, cv=skf, scoring=ranking.frac_to_Xpercent,
n_jobs=config['num_cores'])
else:
clf = GridSearchCV(estimator=clf_notoptimized, param_grid=param_grid, cv=skf, scoring='roc_auc',
n_jobs=config['num_cores'])
clf_fitted = clf.fit(X_train, y_train).best_estimator_
clf_optimal_parameters = clf.best_params_
clf_results['clf_optimal_parameters'] = clf_optimal_parameters
voya_logger.info('Optimal parameters are {}'.format(clf_optimal_parameters))
voya_logger.debug('X = {}'.format(clf_fitted))
y_pred = clf_fitted.predict_proba(X_test)[:, 1]
y_pred_label = clf_fitted.predict(X_test)
clf_results.update({
'y_pred': y_pred,
'y_pred_label': y_pred_label,
'clf': clf_fitted,
'clf_notoptimized': clf_notoptimized,
'X_train': X_train,
'y_train': y_train,
'X_test': X_test,
'y_test': y_test,
'param_grid': param_grid,
})
voya_logger.info("Benchmarking {}".format(clf_name))
benchmarks.all_benchmarks(clf_results, out_path, config["auc_folds"], # TODO (ryan) split this up now into benchmarks and plots?
config['ranking_Frac'])
if out_path is not None: # TODO (ryan) non conforming plots, move to benchmarks
if config["random_forest_tree_plot"] and isinstance(clf_fitted, sklearn.ensemble.RandomForestClassifier):
voya_logger.debug('Generating random forrest plot')
# TODO (ryan) weve hardcoded '2' where the feature start several times, export to var?
feature_names = [colname.replace('url/tech/', '').replace('cid/tech/', '') for colname in
df.columns[2:]]
voya_plotter.plot_trees(clf_results['clf'], feature_names)
results_table_rows[clf_name] = clf_results
voya_logger.info("\n#######\nResults\n#######")
num_positives_y_train = y_train.sum()
voya_logger.info("Training: positives = {}, negatives/unlabelled={}".format(num_positives_y_train,
len(y_train) - num_positives_y_train))
num_positives_y_test = y_test.sum()
voya_logger.info(
"Testing: positives = {}, negatives={}".format(num_positives_y_test, len(y_test) - num_positives_y_test))
results_table = benchmarks.results_dict_to_data_frame(results_table_rows)
voya_logger.info('\n{}'.format(results_table))
return results_table_rows
def set_verbosity_level(level):
""" Set the console verbosity level, 0 - silent, 1 - info, 2 - debug
"""
levels = (logging.CRITICAL, logging.INFO, logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(levels[level])
voya_logger.setLevel(levels[level])
voya_logger.addHandler(console_handler)
fh = logging.FileHandler('clairvoya.log')
fh.setLevel(levels[level])
voya_logger.addHandler(fh)
if level == 2:
formatter = logging.Formatter('%(asctime)s:%(levelname)s: %(message)s', "%H:%M:%S")
else:
formatter = logging.Formatter('%(message)s', "%H:%M:%S")
console_handler.setFormatter(formatter)
def run_search_benchmark(config, classifiers, classifiers_gridparameters):
""" This works like run_bench except it calls runbench multiple times varying the fraction of unlabelled in the
sample.
Currently PU learning only, varies the fraction of unlabelled in the classifier as a function of positive
Search parameters are set in the config dictionary. See the code for required config (in addition to that in runbench).
:param config:
:param classifiers:
:param classifiers_gridparameters:
:return:
"""
default_config = {
"data_file": None, # input data
"test_df": None, # instead of data_file, give split data
"train_df": None,
"out_path": None,
"num_folds": 5,
"test_size": 0.2,
"num_cores": 3,
"pu_learning": False,
"pu_rand_samp_frac": False,
"verbosity": 0,
"random_forest_tree_plot": False,
"auc_folds": 1,
'u_to_p_ratio': False,
'voya_mode': 'pusearch',
'search_results_file': '', # csv file that records the results of each run
'soft_search_run': True, # if True builds on the previous results, if false overwrites the results file
'search_range': (0.5, 1, 2), # range of values to run over
'runs_per_search': 3, # number of times to run the search per parameter per classifier
'search_live_plot': False,
'constant_test_train': True, # otherwise will resplit every run_per_search
'test_neg_to_pos_ratio': None,
'includes_neg_inTrain': False,
}
default_config.update(config)
config = default_config
out_path = config['out_path']
if out_path is not None:
if not os.path.isdir(out_path):
os.makedirs(out_path)
if config['constant_test_train']: # Split test / train so we have a constant testing set
try:
df = datasetup.load_data(config['data_file'])
except IOError: # file doesnt exist, try seeing is its a df instead
df = config['data_file']
df_test, df_train = datasetup.split_test_train_df_pu(df, config['test_size'],
test_neg_to_pos_ratio=config['test_neg_to_pos_ratio'],
includes_neg_inTrain=config['includes_neg_inTrain'])
config["test_df"] = df_test
config["train_df"] = df_train
config["data_file"] = None
if not config['runs_per_search'] == 1: # no point doing more if we have a constant test/train
voya_logger.warning('Setting runs_per_search to 1 as constant_test_train is True, change auc_folds instead')
config['runs_per_search'] = 1
save_file = config['search_results_file']
search_range = config['search_range']
voya_logger.info('Starting search benchmark')
if not os.path.exists(save_file) or not config['soft_search_run']:
with open(save_file, 'wb') as f:
if config['constant_test_train']:
f.write('gamma, folds, clf, auc, auc_std, auc_stderr, ranking_Frac, local_auc, local_auc_std, local_auc_stderr, local_pr, frac_to_desRet, frac_to_desRet_stderr\n')
else:
f.write('gamma, clf, auc, local_pr, frac_to_desRet\n')
fig = None
for gamma_num, gamma in enumerate(search_range): # gamma is a single value in the search range
voya_logger.info('Running classifiers for gamma={} ({}/{})'.format(gamma, gamma_num + 1, len(search_range)))
config.update({"u_to_p_ratio": gamma})
if config['constant_test_train']:
config["train_df"] = datasetup.downsample_pu_df(df_train, config["u_to_p_ratio"])
results_dict = run_benchmark(config, classifiers, classifiers_gridparameters)
# Output
csv_output = []
for clf_name in classifiers.keys():
if config['auc_folds']>1:
csv_row = (gamma, results_dict[clf_name]['auc_folds'], clf_name, results_dict[clf_name]['auc_score'],
results_dict[clf_name]['auc_std'], results_dict[clf_name]['auc_std_err'], results_dict[clf_name]['ranking_Frac'],
results_dict[clf_name]['local_auc_score'], results_dict[clf_name]['local_auc_std'],
results_dict[clf_name]['local_auc_std_err'], results_dict[clf_name]['local_pr'],
results_dict[clf_name]['frac_to_ret'], results_dict[clf_name]['frac_to_ret_stderr'])
else:
csv_row = (gamma, clf_name, results_dict[clf_name]['auc_score'], results_dict[clf_name]['local_pr'], results_dict[clf_name]['frac_to_ret'])
csv_output.append(csv_row)
with open(save_file, 'ab') as f:
csv_f = csv.writer(f)
csv_f.writerows(csv_output)
if config['search_live_plot']:
plt.clf()
fig = voya_plotter.pu_search_result(save_file, fig)
plt.draw()
if out_path is not None:
voya_logger.info('Generating prVSranking all methods plot')
voya_plotter.prVSranking_methodComparison(results_dict)
plt.savefig(os.path.join(out_path, 'prVsRankComparison__Gamma__{}.png'.format(gamma)), bbox_inches='tight')
class VoyaConfigError(Exception):
pass
if __name__ == '__main__':
arguments = docopt.docopt(__doc__)
config_module_name = arguments['<config>']
if config_module_name is None: # use default
config_module_name = 'voya_config_example_pu'
else:
config_module_name = datasetup.parse_config_module_name(config_module_name)
# This config loading as a module may not be entirely sensible, but is very quick for prototyping
voya_config = importlib.import_module(config_module_name)
if arguments["-v"] is not None: # overwrite config verbosity
voya_config.config["verbosity"] = int(arguments["-v"])
try:
voya_mode = voya_config.config['voya_mode']
except KeyError:
raise VoyaConfigError('voya_mode undefined in config, must be "bench" or "pusearch"')
if voya_mode == 'bench':
results = run_benchmark(voya_config.config, voya_config.classifiers, voya_config.classifiers_gridparameters)
elif voya_mode == 'pusearch':
results = run_search_benchmark(voya_config.config, voya_config.classifiers, voya_config.classifiers_gridparameters)
else:
raise VoyaConfigError('config must define voya_mode as "bench" or "pusearch" got {}'
''.format(voya_config.config['voya_mode']))