-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
792 lines (730 loc) · 31 KB
/
Copy pathmain.py
File metadata and controls
792 lines (730 loc) · 31 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
import os
import shutil
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pipeline_gen
from app import app
from imblearn.over_sampling import SMOTE
from imblearn.pipeline import Pipeline # smote pipeline
from sklearn.impute import SimpleImputer
from flask import Flask, flash, request, redirect, render_template, url_for, session, send_from_directory
from werkzeug.utils import secure_filename
from multi import run_task, process_data
from extras import *
from extract import get_meta
from predict_meta import predict_meta, predict_time
from utils_local import *
from joblib import dump, load
from pdpbox import pdp
tmp_folder = 'tmp/autosk_tmp'
output_folder = 'tmp/autosk_out'
ALLOWED_EXTENSIONS = set(["npy", "csv"])
def url_mod(fnc):
pre = ""
return pre + url_for(fnc)
def allowed_file(filename):
return '.' in filename and filename.rsplit(
'.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/about')
def about():
return render_template("about.html")
@app.route('/')
def start():
if not os.path.exists("data/hash_list.txt"):
os.mknod("data/hash_list.txt")
return render_template("index.html", url_mod=url_mod)
@app.route('/', methods=['POST'])
def start_p():
if request.method == 'POST':
values = {}
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
#data_type = request.form['data_type']
data_type = "csv"
task = request.form['task']
sep = request.form['sep']
#sep= None
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file :
filename = secure_filename(file.filename)
if data_type == "numpy" and filename[-3:] != "npy":
return "Wrong file extension (expected .npy)"
if data_type == "csv" and (filename[-3:] != "csv" and filename[-3:] != "CSV"):
err = "Unsupported file extension (expected .csv)"
return render_template("error.html", err=err)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
values['task'] = task
values["sep"] = sep
session["filename"] = filename
session["values"] = values
session["data_type"] = data_type
session["task"] = task
return redirect(url_mod("featur_pg"))
else:
flash('Allowed file types are: {}'.format(str(ALLOWED_EXTENSIONS)))
return redirect(request.url)
@app.route('/features')
def featur_pg():
values = session.get('values', 'not set')
path = os.path.join(app.config['UPLOAD_FOLDER'],
session.get("filename", "not set"))
data = load_initial(path,sep=values["sep"])
empty_cols = [col for col in data.columns if data[col].isnull().all()]
data.drop(empty_cols,
axis=1,
inplace=True)
dropped_msg=""
if empty_cols:
dropped_msg = "Empty columns detected, dropped columns : "+str(empty_cols)
features= data.columns
for i in range(len(features)):
plt.clf()
data[features[i]].hist()
plt.savefig("static/images/figs/" + str(i),
bbox_inches="tight", transparent=True)
return render_template("features.html", FEATURES=features, dropped_msg=dropped_msg)
@app.route('/features', methods=['POST'])
def feature_pgr():
if request.method == 'POST':
# check if the post request has the file part
task = session.get("task", "not set")
target_ft = request.form['target_ft']
session["target_ft"] = target_ft
features = request.form.getlist("features_ls")
if target_ft not in features:
return "You can't discard the target class"
features.remove(target_ft)
session["features"] = features
filename = session.get("filename", "not set")
data_type = "csv"
rec = []
path = os.path.join(
app.config['UPLOAD_FOLDER'], session.get("filename", "not set"))
new_data = select_cols(path, list(features) + [target_ft])
new_data = new_data[new_data[target_ft].notna()]
new_data.to_csv(path, index=False)
if task == "classification":
meta = get_meta(path, data_type,target_col=target_ft)
rec = predict_meta(meta)
session["rec"] = rec
return redirect(url_mod('target_class'))
@app.route('/target_class')
def target_class():
task = session.get("task", "not set")
# Configure for Task
if task == "classification":
METRICS = METRICS_CL_DISP
else:
METRICS = METRICS_RG_DISP
values = session.get('values', 'not set')
target_ft = session.get('target_ft', 'not set')
path = os.path.join(app.config['UPLOAD_FOLDER'],
session.get("filename", "not set"))
data = pd.read_csv(path)
unique, counts = np.unique(data[target_ft], return_counts=True)
classes = dict(zip(unique, counts))
mx_key = max(classes, key=classes.get)
plt.clf()
data[target_ft].hist()
plt.savefig("static/images/figs/target",
bbox_inches="tight", transparent=True)
ratio = [True if (min(data[target_ft].value_counts()) /
max(data[target_ft].value_counts())) < 0.6 else False][0]
pre_metric = ["F1" if ratio else "Accuracy"][0]
return render_template(
"target.html",
TASK=values["task"],
ratio=ratio,
METRICS=METRICS,
pre_metric=pre_metric,
classes=classes,
mx_key=mx_key)
@app.route('/target_class', methods=['POST'])
def target_class_r():
if request.method == 'POST':
SMOTE_N = 5
# check if the post request has the file part
values = session.get('values', 'not set')
metric = request.form['metric']
if values["task"] == "classification":
smote = request.form['smote']
else:
smote = "no"
values["metric"] = metric
session["values"] = values
target_ft = session.get('target_ft', 'not set')
features = session.get('features', 'not set')
# feature dropping can be brought here for better perforamnce
session["smote"] = smote
if smote == "yes":
smote_dic = {}
path = os.path.join(
app.config['UPLOAD_FOLDER'], session.get(
"filename", "not set"))
X, y, _ = process_data(path, "csv", target_ft)
unique, counts = np.unique(y, return_counts=True)
if min(counts) <= SMOTE_N:
SMOTE_N = min(counts) - 1
smote_ratios = [int(float(x) * max(counts))
for x in request.form.getlist("smote_ratio[]")]
print(smote_ratios)
for i in range(len(smote_ratios)):
smote_dic[unique[i]] = smote_ratios[i]
print(smote_dic)
sm = SMOTE(random_state=42, sampling_strategy=smote_dic,
k_neighbors=SMOTE_N)
X_res, y_res = sm.fit_resample(X, y)
new_data = pd.DataFrame(np.column_stack(
(X_res, y_res)), columns=list(features) + [target_ft])
new_data.to_csv(path, index=False)
print(smote)
return redirect(url_mod('params'))
@app.route('/params')
def params():
rec = session.get("rec", "not set")
task = session.get("task", "not set")
column_names = ["Classifier", "Score"]
bolds = []
# Configure for Task
if task == "classification":
# remove predicions with 0 score from results
rec = [x for x in rec if x[1] != 0]
# get bold indexes for recomended classifiers
rec_t = list(map(list, zip(*rec)))
for cl in CLASSIFIERS_DISP:
if cl in rec_t[0]:
bolds.append(CLASSIFIERS_DISP.index(cl))
elif cl[-3:] == "SVC":
if "SVC" in rec_t[0]:
bolds.append(CLASSIFIERS_DISP.index(cl))
# Get corect lists for this task
ESTIMATORS = [CLASSIFIERS, CLASSIFIERS_DISP]
PREPROCESSORS = [PREPROCESSORS_CL, PREPROCESSORS_CL_DISP]
else:
ESTIMATORS = [REGRESSORS, REGRESSORS_DISP]
PREPROCESSORS = [PREPROCESSORS_RG, PREPROCESSORS_RG_DISP]
return render_template(
'upload.html',
ESTIMATORS=ESTIMATORS,
PREPROCESSORS=PREPROCESSORS,
column_names=column_names,
row_data=rec,
zip=zip,
TASK=task,
BOLD_CL=bolds)
@app.route('/params', methods=['POST'])
def params_p():
if request.method == 'POST':
values = session.get('values', 'not set')
data_type = session.get('data_type', 'not set')
filename = session.get("filename", "not set")
task = session.get("task", "not set")
search_space = request.form.getlist("estim_ls")
prep_space = request.form.getlist("prep_ls")
if not search_space:
return "You must select at least 1 estimator"
if not prep_space:
return "You must select at least 1 preprocessor"
values['data_type'] = data_type
values["search_space"] = search_space
values["prep_space"] = prep_space
session["values"] = values
return redirect(url_mod('budget'))
@app.route('/budget')
def budget():
task = session.get("task", "not set")
values = session.get("values", "not set")
total_pred_time = 0
filename = session.get("filename", "not set")
meta = get_meta(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'csv')
time_pred = predict_time(meta)
for each in values["search_space"]:
if each in ESTIMATOR_TIMES.keys():
tm = ESTIMATOR_TIMES[each]
total_pred_time += 0.2 * (time_pred)
if total_pred_time<30:
total_pred_time=30
return render_template('budget.html', zip=zip,
TASK=task, PRED_TIME=int(total_pred_time))
@app.route('/budget', methods=['POST'])
def budget_p():
if request.method == 'POST':
values = session.get('values', 'not set')
time = request.form['time']
period = request.form['period']
data_type = session.get('data_type', 'not set')
filename = session.get("filename", "not set")
task = session.get("task", "not set")
reuse = request.form['reuse']
if int(time) < 30:
return "Time budget must be at least 30 seconds"
if int(period) < 30:
return "Update period must be at least 30 seconds"
if int(period) > int(time):
return "Update period can't be larger than total time budget"
values['time'] = int(time)
values['period'] = int(period)
session["values"] = values
session["reuse"] = reuse
return redirect(url_mod('running'))
@app.route('/running')
def running():
values = session.get('values', 'not set')
target_ft = session.get('target_ft', 'not set')
iters = values["time"] // values["period"]
extra = values["time"] % values["period"]
format_period = format_time(values["period"])
reuse = session.get('reuse', 'not set')
# check dataset checksum and lookup
path = os.path.join(app.config['UPLOAD_FOLDER'],
session.get("filename", "not set"))
checksum = hash_file(path) + "_" + target_ft + "_" + \
values["task"] + "_" + values["metric"]
session["checksum"] = checksum
with open("data/hash_list.txt", "r") as f:
lines = f.readlines()
if checksum + "\n" not in lines:
with open("data/hash_list.txt", "a") as f:
f.write(checksum + "\n")
for dir_ in [tmp_folder, output_folder]:
try:
shutil.rmtree(dir_)
except OSError:
pass
# copy tmp to run on it
if reuse == "yes":
if os.path.exists("tmp_runs/{}".format(checksum)):
shutil.copytree("tmp_runs/{}".format(checksum), "tmp/autosk_tmp")
# modify space.pcs
olds = []
old_pres = []
path = "tmp/autosk_tmp/space.pcs"
with open(path, "r") as f:
lines = f.readlines()
"classifier:__choice__ {decision_tree, gradient_boosting, random_forest} [random_forest]"
pre = "classifier:__choice__ {"
for line in lines:
if "classifier:__choice__ {" in line:
olds = [ar.strip()
for ar in line[len(pre):].split("}")[0].split(",")]
elif "preprocessor:__choice__ {" in line:
old_pres = [ar.strip() for ar in line[len(
"preprocessor:__choice__ {"):].split("}")[0].split(",")]
for param in values["search_space"]:
if param not in olds:
olds.append(param)
for param in values["prep_space"]:
if param not in old_pres:
old_pres.append(param)
values["search_space"] = olds
values["prep_space"] = old_pres
session["values"] = values
return render_template(
'running.html',
url_mod=url_mod,
turn=0,
task=values["task"],
time=values["time"],
iters=iters,
PERIOD=format_period,
RAW_PERIOD=values["period"])
@app.route('/progress')
def progress():
turn = request.args.get('iter', default=0, type=int)
print("turn", turn)
values = session.get('values', 'not set')
target_ft = session.get('target_ft', 'not set')
checksum = session.get('checksum', 'not set')
iters = values["time"] // values["period"]
extra = values["time"] % values["period"]
format_period = format_time(values["period"])
metric = gen_metric(values["task"], values["metric"])
path = os.path.join(app.config['UPLOAD_FOLDER'],
session.get("filename", "not set"))
#TODO: features can be passed from previous calls for optimization
features = return_cols(path)
estimator = run_task(path, values["task"], values["data_type"], target_ft)
results = estimator(
turn,
values["period"],
values["search_space"],
values["prep_space"],
metric)
df = pd.DataFrame(data=results).sort_values(by="rank_test_scores")
col_names = ["Classifier", "{} Max Score".format(
values["metric"]), "Models Trained", "Show Models"]
if values["task"] != "classification":
col_names[0] = "Regressor"
# Sort list by scores
res_list = [[a, b]for a, b in zip(
df["mean_test_score"].values.tolist(), df["params"].values.tolist())]
# divide list in dictionaries and dump to drive
grouped_results = {}
if values["task"] == 'classification':
ESTIMATORS = CLASSIFIERS
ESTIMATORS_DISP = CLASSIFIERS_DISP
for each in CLASSIFIERS:
grouped_results[each] = []
for each in res_list:
grouped_results[each[1]['classifier:__choice__']].append(each)
else:
ESTIMATORS = REGRESSORS
ESTIMATORS_DISP = REGRESSORS_DISP
for each in REGRESSORS:
grouped_results[each] = []
for each in res_list:
grouped_results[each[1]['regressor:__choice__']].append(each)
with open("tmp/results.p", 'wb') as filehandler:
pickle.dump(grouped_results, filehandler)
res_list = []
for each in grouped_results.keys():
if grouped_results[each]:
res_list.append((ESTIMATORS_DISP[ESTIMATORS.index(each)], round(
grouped_results[each][0][0], 3), len(grouped_results[each]), "View"))
res_list.sort(key=lambda x: x[0], reverse=True)
turn += 1
# copy tmp files to save for later
if os.path.exists("tmp_runs/{}".format(checksum)):
shutil.rmtree("tmp_runs/{}".format(checksum))
shutil.copytree("tmp/autosk_tmp", "tmp_runs/{}".format(checksum))
with open("tmp/results.p", 'rb') as filehandler:
or_list = pickle.load(filehandler)
estim_dict = {"col_names": [], "disp_index": [],
"index": [], "fig_names": [], "res_list": []}
res_list.sort(key=lambda x: x[1], reverse=True)
for each in res_list:
index = ESTIMATORS[ESTIMATORS_DISP.index(each[0])]
fres_list = or_list[index]
if values["task"] == 'classification':
slc = len("classifier:{}:".format(index))
col_names_e = [x for x in list(fres_list[0][1].keys(
)) if x[:10] == "classifier" and x[-21:] != "min_impurity_decrease"][1:]
else:
slc = len("regressor:{}:".format(index))
col_names_e = [x for x in list(fres_list[0][1].keys(
)) if x[:10] == "regressor" and x[-21:] != "min_impurity_decrease"][1:]
# TODO: 0 if k not in x[1] sets default argumetn to 0, 0 should be
# replaced with default argument
fres_list = [
[
round(
x[0], 3), x[1]["preprocessor:__choice__"].replace(
"_", " ").title()] + [
0 if k not in x[1] else x[1][k] if not isinstance(
x[1][k], float) and not isinstance(
x[1][k], str) else round(
x[1][k], 3) if isinstance(
x[1][k], float) else x[1][k].replace(
"_", " ").title() for k in col_names_e] + ["Interpret"] for x in fres_list]
col_names_e = [("{} Score".format(values["metric"])),
"Preprocessor"] + [x[slc:].replace("_",
" ").title() for x in col_names_e] + ["Details"]
disp_index = index.replace("_", " ").title()
# plotting
fig_names = []
for i in range(1, len(fres_list[0])):
if isinstance(fres_list[0][i], float) or isinstance(
fres_list[0][i], int):
plt.clf()
plt.xlabel(col_names_e[i])
plt.ylabel("{} Score".format(values["metric"]))
plt.scatter([x[i] for x in fres_list], [x[0]
for x in fres_list])
plt.savefig("static/images/figs/" + index + str(i),
bbox_inches="tight", transparent=True)
fig_names.append(index + str(i))
estim_dict["col_names"].append(col_names_e)
estim_dict["disp_index"].append(disp_index)
estim_dict["index"].append(index)
estim_dict["fig_names"].append(fig_names)
estim_dict["res_list"].append(fres_list)
if(turn >= iters):
return render_template(
"results.html",
url_mod=url_mod,
column_names=col_names,
row_data=res_list,
zip=zip,
len=len,
CLASSIFIERS=ESTIMATORS,
CLASSIFIERS_DISP=ESTIMATORS_DISP,
estim_dict=estim_dict,
task=values['task'])
else:
return render_template(
"progress.html",
url_mod=url_mod,
turn=turn,
iters=iters,
PERIOD=format_period,
RAW_PERIOD=values["period"],
time=values["time"],
column_names=col_names,
row_data=res_list,
zip=zip,
CLASSIFIERS=ESTIMATORS,
CLASSIFIERS_DISP=ESTIMATORS_DISP,
estim_dict=estim_dict,
task=values['task'])
@app.route('/stop')
def stop():
values = session.get('values', 'not set')
with open("tmp/results.p", 'rb') as filehandler:
grouped_results = pickle.load(filehandler)
col_names = ["{} Max Score".format(
values["metric"]), "Classifier", "Show Models"]
res_list = []
for each in grouped_results.keys():
if grouped_results[each]:
res_list.append((CLASSIFIERS_DISP[CLASSIFIERS.index(each)], round(
grouped_results[each][0][0], 3), len(grouped_results[each]), "View"))
res_list.sort(key=lambda x: x[0], reverse=True)
estim_dict = {"col_names": [], "disp_index": [],
"index": [], "fig_names": [], "res_list": []}
for each in res_list:
index = CLASSIFIERS[CLASSIFIERS_DISP.index(each[0])]
fres_list = grouped_results[index]
slc = len("classifier:{}:".format(index))
col_names_e = [x for x in list(fres_list[0][1].keys(
)) if x[:10] == "classifier" and x[-21:] != "min_impurity_decrease"][1:]
fres_list = [
[
round(
x[0], 3), x[1]["preprocessor:__choice__"].replace(
"_", " ").title()] + [
x[1][k] if not isinstance(
x[1][k], float) and not isinstance(
x[1][k], str) else round(
x[1][k], 3) if isinstance(
x[1][k], float) else x[1][k].replace(
"_", " ").title() for k in col_names_e] + ["Interpret"] for x in fres_list]
col_names_e = [("{} Score".format(values["metric"])),
"Preprocessor"] + [x[slc:].replace("_",
" ").title() for x in col_names_e] + ["Details"]
disp_index = index.replace("_", " ").title()
# plotting
fig_names = []
for i in range(1, len(fres_list[0])):
if isinstance(fres_list[0][i], float) or isinstance(
fres_list[0][i], int):
fig_names.append(index + str(i))
estim_dict["col_names"].append(col_names_e)
estim_dict["disp_index"].append(disp_index)
estim_dict["index"].append(index)
estim_dict["fig_names"].append(fig_names)
estim_dict["res_list"].append(fres_list)
return render_template("results.html", column_names=col_names,
estim_dict=estim_dict, row_data=res_list, zip=zip)
@app.route('/estimator')
def view_estimator():
values = session.get('values', 'not set')
with open("tmp/results.p", 'rb') as filehandler:
or_list = pickle.load(filehandler)
index = request.args.get('model', default=None, type=str)
res_list = or_list[index]
slc = len("classifier:{}:".format(index))
col_names = [x for x in list(res_list[0][1].keys(
)) if x[:10] == "classifier" and x[-21:] != "min_impurity_decrease"][1:]
res_list = [
[
round(
x[0], 3), x[1]["preprocessor:__choice__"].replace(
"_", " ").title()] + [
x[1][k] if not isinstance(
x[1][k], float) and not isinstance(
x[1][k], str) else round(
x[1][k], 3) if isinstance(
x[1][k], float) else x[1][k].replace(
"_", " ").title() for k in col_names] + ["Interpret"] for x in res_list]
col_names = [("{} Score".format(values["metric"])), "Preprocessor"] + \
[x[slc:].replace("_", " ").title() for x in col_names] + ["Details"]
disp_index = index.replace("_", " ").title()
# plotting
fig_names = []
for i in range(1, len(res_list[0])):
if isinstance(res_list[0][i], float) or isinstance(
res_list[0][i], int):
plt.clf()
plt.xlabel(col_names[i])
plt.ylabel("{} Score".format(values["metric"]))
plt.scatter([x[i] for x in res_list], [x[0] for x in res_list])
plt.savefig("static/images/figs/" + index + str(i),
bbox_inches="tight", transparent=True)
fig_names.append(index + str(i))
return render_template(
"estimator_results.html",
column_names=col_names,
disp_index=disp_index,
estimator=index,
fig_names=fig_names,
row_data=res_list,
zip=zip)
@app.route('/model')
def view_model():
with open("tmp/results.p", 'rb') as filehandler:
res_list = pickle.load(filehandler)
index = request.args.get('model', default=0, type=int)
estim = request.args.get('estimator', default=None, type=str)
model = res_list[estim][index]
return render_template("model.html", model=model,
estimator=estim, model_index=index)
@app.route("/generate_model")
def generate_model():
# generates model from the parameters and trains the model on the train set
# Load parameters
values = session.get('values', 'not set')
smote = session.get('smote', 'not set')
smote = "no" # dont include smote in the pipeline
target_ft = session.get('target_ft', 'not set')
features = session.get('features', 'not set')
index = request.args.get('model', default=0, type=int)
estim = request.args.get('estimator', default=None, type=str)
filehandler = open("tmp/results.p", 'rb')
res_list = pickle.load(filehandler)
arg_dict = res_list[estim][index][1]
# constuct and fit pipeline
param_dict = pipeline_gen.process_dict(arg_dict)
pipeline_params = [("imputation",SimpleImputer(missing_values=np.nan, strategy='mean')), ("preprocessor", pipeline_gen.build_preprocessor_cl(
param_dict)), ("classifeir", pipeline_gen.build_classifier(param_dict))]
if smote == "yes":
pipeline_params.insert(0, ("smote", SMOTE(random_state=42)))
pipe = Pipeline(pipeline_params)
path = os.path.join(app.config['UPLOAD_FOLDER'],
session.get("filename", "not set"))
X, y, data = process_data(path, "csv", target_ft)
pipe.fit(X, y)
dump(pipe, 'tmp_files/model_{}_{}.joblib'.format(estim, str(index)))
with open("tmp_files/model_{}_{}.pickle".format(estim, str(index)), 'wb') as filehandler:
pickle.dump(pipe, filehandler)
cl = param_dict["classifier:__choice__"]
# feature importances
importance = (pipeline_gen.get_importance(pipe, cl, smote))
metric_res = pipeline_gen.get_matrix(pipe, X, y, smote)
if len(importance) > 0:
imps = [[features[i], round(importance[i], 2)]
for i in range(len(features))]
imps = sorted(imps, key=lambda l: l[1], reverse=True)
plt_features = [x[0] for x in reversed(imps)]
plt_imps = [x[1] for x in reversed(imps)]
plt.clf()
plt.title("Feature Importance")
plt.ylabel("Feature Name")
plt.xlabel("Importance")
plt.barh(plt_features, plt_imps, align='center', height=0.2, color='c')
plt.savefig("static/images/figs/model_imp",
bbox_inches="tight", transparent=True)
else:
imps = []
column_names = ["Metric", "Score"]
metric_names = ["Accuracy", "Recall", "Precision", "F1"]
metric_res = [[metric_names[i], round(
metric_res[i], 3)] for i in range(len(metric_res))]
# partial dependancy
partial_fig_names = []
return render_template(
"download.html",
url_mod=url_mod,
features=features,
targets=np.unique(y),
estimator=estim,
index=index,
column_names=column_names,
row_data=metric_res,
CL_Name=cl,
metric_res=metric_res,
zip=zip,
partial_fig_names=partial_fig_names)
@app.route('/download_joblib')
def download_joblib():
index = request.args.get('model', default=0, type=int)
estim = request.args.get('estimator', default=None, type=str)
return send_from_directory("tmp_files", 'model_{}_{}.joblib'.format(
estim, str(index)), as_attachment=True)
@app.route('/download_pickle')
def download_pickle():
index = request.args.get('model', default=0, type=int)
estim = request.args.get('estimator', default=None, type=str)
return send_from_directory("tmp_files", 'model_{}_{}.pickle'.format(
estim, str(index)), as_attachment=True)
@app.route('/plot_pdp')
def plot_pdp():
path = os.path.join(app.config['UPLOAD_FOLDER'],
session.get("filename", "not set"))
index = request.args.get('model', default=0, type=int)
estim = request.args.get('estimator', default=None, type=str)
target_ft = session.get('target_ft', 'not set')
features = session.get('features', 'not set')
f1 = request.args.get('f1', default=None, type=str)
t1 = request.args.get('t1', default=None, type=str)
X, y, data = process_data(path, "csv", target_ft)
#remove nans
data = data.dropna()
chosen_class = list(np.unique(y)).index(int(float(t1)))
with open("tmp_files/model_{}_{}.pickle".format(estim, str(index)), 'rb') as filehandler:
pipe = pickle.load(filehandler)
mod_path = "modal_" + "pdp_" + str(f1.replace('.', '_'))
feat_p = pdp.pdp_isolate(
model=pipe,
dataset=data,
model_features=features,
feature=f1)
fig, axes = pdp.pdp_plot(pdp_isolate_out=feat_p, feature_name=f1, center=True, x_quantile=True, plot_lines=True, frac_to_plot=100,
show_percentile=False, which_classes=[chosen_class], plot_params={"subtitle": "For Class {}, Label: {}".format(chosen_class, t1)})
fig.savefig("static/images/figs/" + mod_path,
bbox_inches="tight", transparent=True)
plt.figure()
return render_template("modal_plot.html", plot_name=mod_path)
@app.route('/plot_modal')
def plot_modal():
path = os.path.join(app.config['UPLOAD_FOLDER'],
session.get("filename", "not set"))
index = request.args.get('model', default=0, type=int)
estim = request.args.get('estimator', default=None, type=str)
target_ft = session.get('target_ft', 'not set')
features = session.get('features', 'not set')
f1 = request.args.get('f1', default=None, type=str)
f2 = request.args.get('f2', default=None, type=str)
t1 = request.args.get('t1', default=None, type=str)
X, y, data = process_data(path, "csv", target_ft)
#remove nans
data = data.dropna()
chosen_class = list(np.unique(y)).index(int(float(t1)))
with open("tmp_files/model_{}_{}.pickle".format(estim, str(index)), 'rb') as filehandler:
pipe = pickle.load(filehandler)
mod_path = "modal_" + str(f1.replace('.', '_')) + \
"_" + str(f2.replace('.', '_'))
pdp_V1_V2 = pdp.pdp_interact(
model=pipe, dataset=data, model_features=features, features=[
f1, f2], num_grid_points=None, percentile_ranges=[
None, None])
fig, axes = pdp.pdp_interact_plot(
pdp_V1_V2, [f1, f2], plot_type='grid', x_quantile=True, ncols=2, plot_pdp=True,
which_classes=[chosen_class], plot_params={
"subtitle": "For Class {}, Label: {}".format(chosen_class, t1)}
)
fig.savefig("static/images/figs/" + mod_path,
bbox_inches="tight", transparent=True)
plt.figure()
return render_template("modal_plot.html", plot_name=mod_path)
@app.route("/loading")
def loading():
return render_template("loading.html")
@app.route("/test")
def test():
return render_template("test.html")
@app.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)