-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
215 lines (165 loc) · 11.6 KB
/
main.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
import parser
import data_preprocess as dp
import feature_selection as feat
import validation as val
import pandas as pd
import numpy as np
import classification
import itertools
import sys
from sklearn.model_selection import train_test_split
from datetime import datetime
# Handles the verbosity of the debug mode
def debug_mode(debug):
if debug == 0:
dict = {'dataset_split':0,'feature_selection':0,'classification':0, 'validation':0, 'feature_counter':0}
elif debug == 1:
dict = {'dataset_split':1,'feature_selection':0,'classification':0, 'validation':1, 'feature_counter':0}
elif debug == 2:
dict = {'dataset_split':1,'feature_selection':1,'classification':0, 'validation':1, 'feature_counter':0}
elif debug == 3:
dict = {'dataset_split':1,'feature_selection':1,'classification':1, 'validation':1, 'feature_counter':0}
elif debug == 4:
dict = {'dataset_split':1,'feature_selection':1,'classification':1, 'validation':1, 'feature_counter':1}
else:
sys.exit("ERROR: Unrecognized debugging level. Debug levels available: no debug - 0, validation_mode - 1, +feature_selection - 2, +classification - 3, +feature_counter - 4")
return dict
def save_feature_counter(result_path, feature_selection, classifier, date, validation, feature_counter, feature_size, debug_mode):
feature_counter_path = result_path + date + "/" + validation + "/" + feature_selection + "/" + classifier
# DEBUG MODE
if debug_mode:
print("FEATURE COUNTER STARTS")
# Sorts and selects the top (feature_size) most frequent features, isn't used in the code right now but keeping this here just in case
#selected_features = dict(itertools.islice(feature_counter.items(), len(feature_counter) - (1 + feature_size), len(feature_counter) - 1))
#selected_features = selected_features.keys()
dp.make_result_dir(feature_counter_path)
# Converts dictionary into dataframe
feature_counter_df = pd.DataFrame(feature_counter.items(), columns=["FEATURE", "FREQUENCY"])
#DEBUG MODE
if debug_mode:
debug_path = feature_counter_path + "/debug/"
# Saves unsorted feature counter
feature_counter_df.to_csv(debug_path + "/feature_counter.tsv", index = False, sep="\t")
# sorts the features of feature counter in ascending order
feature_counter_df = feature_counter_df.sort_values(by=['FREQUENCY'], ascending=False)
feature_counter_df.to_csv(feature_counter_path + "/feature_counter.tsv", index = False, sep="\t")
print("FEATURE COUNTER SAVED")
return
def main():
parameters = parser.get_parser()
date = datetime.today().strftime('%m-%d-%Y')
result_path = parameters['result_path']
dataset_path = parameters['dataset_path']
run_name = parameters['run_name']
project = parameters['project']
hyper_opt = parameters['hyper_opt']
feature_selection = parameters['feature_selection'].split(',')
feature_size = int(parameters['feature_size'])
classifiers = parameters['classifiers'].split(',')
train_set_split = float(parameters['train_test_split'])
train_test_seed = 200
validation = parameters['validation']
iterations = int(parameters['validation_iterations'])
normalization = parameters['normalization']
fs_keys = ['project','dataset_path', 'dge_path', 'swapped_label', 'drug_feature_path', 'swapped_path']
feature_selection_parameters = {key: parameters[key] for key in fs_keys}
save_fc = int(parameters['feature_counter'])
debug = int(parameters['debug'])
debug = debug_mode(debug)
drug_name = parameters['drug_name']
result_path = result_path + project + "/" + run_name + "/" + drug_name + "/"
gpu = int(parameters['gpu'])
if gpu == 1:
import gpu.data_preprocess as dp
import gpu.feature_selection as feat
import gpu.validation as val
import gpu.classification as classification
else:
import data_preprocess as dp
import feature_selection as feat
import validation as val
import classification as classification
print("PIPELINE STARTS")
dataset, dataset_samples = dp.load_dataset(dataset_path, project, normalization)
print("FINISHED LOADING DATASET")
labels = dp.load_labels(dataset_path, project, drug_name)
print("FINISHED LOADING LABELS")
dataset, labels, samples = dp.sample_match(dataset, labels, dataset_samples)
if parameters['simulation_size'] != 'none':
simulation_size = int(parameters['simulation_size'])
dataset, labels, samples = dp.simulate_data(dataset, labels, simulation_size)
print("DATSET BEFORE SIZE:", dataset.shape)
print("LABELS BEFORE SIZE:", labels.shape)
if int(parameters['balance']):
dataset, labels, samples = dp.balance_dataset(dataset, labels)
print("DATSET AFTER SIZE:", dataset.shape)
print("LABELS AFTER SIZE:", labels.shape)
feature_counter = feat.build_feature_counter(dataset)
print("SPLITTING DATASET BASED ON VALIDATION STYLE: " + validation)
datasets, iterations = val.split_dataset(validation, dataset, labels, train_set_split, iterations)
if validation == "cv_and_test":
# CV HAPPENS FIRST
for fs in feature_selection:
for classifier in classifiers:
for iteration in range(0, iterations):
dp.make_result_dir(result_path + date + "/" + validation + "/" + fs + "/" + classifier + "/" + str(iteration) + "/")
print("CV - PERFORMING FEATURE SELECTION: ")
datasets_cv = {fs:[feat.feature_selection(result_path + date + "/" + validation + "/", fs, iteration, datasets, labels, feature_size, classifiers, feature_counter, debug['feature_selection'], feature_selection_parameters, drug_name) for iteration in range(0, iterations)] for fs in feature_selection}
print("CV - PERFORMING MODEL TRAINING: ")
best_parameters = {}
models = {fs: {classifier: [classification.model_train(result_path + date + "/" + validation + "/" + fs + "/", datasets_cv[fs][iteration]['x_train'], datasets_cv[fs][iteration]['y_train'], classifier, debug['classification'], iteration, hyper_opt, best_parameters) for iteration in range(0, iterations)] for classifier in classifiers} if fs != "random" else {classifier: ["no random cv" for iteration in range(0, iterations)] for classifier in classifiers} for fs in feature_selection}
print("CV - FINISHED TRAINING MODELS")
print("CV - GATHERING RESULTS")
cv_results = {fs: {classifier: [val.validate_model(models[fs][classifier][iteration][0], datasets_cv[fs][iteration]['x_test'], datasets_cv[fs][iteration]['y_test'], 0.50, "cv") for iteration in range(0, iterations)] for classifier in classifiers} if fs != "random" else {classifier: ["no random cv" for iteration in range(0, iterations)] for classifier in classifiers} for fs in feature_selection}
#print("CV - FINISHED GATHERING")
#val.save_results(result_path + date, "cv", feature_selection, classifiers, iterations, results, models, datasets, labels, feature_selection_parameters, drug_name)
if save_fc:
for fs in feature_selection:
for classifier in classifiers:
save_feature_counter(result_path, fs, classifier, date, validation, feature_counter, feature_size, debug['feature_counter'])
print("CV - SAVED FEATURE COUNTER")
# INDEPENDENT TEST SET
for fs in feature_selection:
for classifier in classifiers:
# for k in range(0, iterations):
dp.make_result_dir(result_path + date + "/" + validation + "/" + fs + "/" + classifier + "/hold_out/")
print("HOLD-OUT - PERFORMING FEATURE SELECTION: ")
# print(datasets)
datasets = {fs:feat.feature_selection(result_path + date + "/" + validation + "/", fs, "hold_out", datasets['hold_out'], labels, feature_size, classifiers, feature_counter, debug['feature_selection'], feature_selection_parameters, drug_name) for fs in feature_selection}
print("HOLD-OUT - PERFORMING MODEL TRAINING: ")
models = {j: {classifier: [classification.model_train(result_path + date + "/" + validation + "/" + j + "/", datasets[j]['x_train'], datasets[j]['y_train'], classifier, debug['classification'], "hold_out", "best", models[j][classifier][i]) for i in range(0, iterations)] for classifier in classifiers} if j != "random" else {classifier: [classification.model_train(result_path + date + "/" + validation + "/" + j + "/", datasets[j]['x_train'], datasets[j]['y_train'], classifier, debug['classification'], "hold_out", "none", models[j][classifier][i]) for i in range(0, iterations)] for classifier in classifiers} for j in feature_selection}
holdout_results = {j: {classifier: [val.validate_model(models[j][classifier][i][0], datasets[j]['x_test'], datasets[j]['y_test'], 0.50, validation) for i in range(0, iterations)] for classifier in classifiers} for j in feature_selection}
models, holdout_results = val.pick_top_performer(models, cv_results, holdout_results, classifiers, feature_selection, iterations)
print("FINISHED TRAINING MODELS")
print("HOLD-OUT - FINISHED GATHERING RESULTS")
results = {"cv": cv_results, "holdout":holdout_results}
val.save_results(result_path + date, validation, feature_selection, classifiers, iterations, results, models, datasets, labels, feature_selection_parameters, drug_name)
if save_fc:
for fs in feature_selection:
for classifier in classifiers:
save_feature_counter(result_path, fs, classifier, date, validation, feature_counter, feature_size, debug['feature_counter'])
print("HOLD-OUT - SAVED FEATURE COUNTER")
else:
for fs in feature_selection:
for classifier in classifiers:
for iteration in range(0, iterations):
dp.make_result_dir(result_path + date + "/" + validation + "/" + fs + "/" + classifier + "/" + str(iteration) + "/")
print("PERFORMING FEATURE SELECTION: ")
datasets = {fs:[feat.feature_selection(result_path + date + "/" + validation + "/", fs, iteration, datasets, labels, feature_size, classifiers, feature_counter, debug['feature_selection'], feature_selection_parameters, drug_name) for iteration in range(0, iterations)] for fs in feature_selection}
print("PERFORMING MODEL TRAINING: ")
best_parameters = {}
models, best_parameters = {fs: {classifier: [classification.model_train(result_path + date + "/" + validation + "/" + fs + "/", datasets[fs][iteration]['x_train'], datasets[fs][iteration]['y_train'], classifier, debug['classification'], iteration, hyper_opt, best_parameters) for iteration in range(0, iterations)] for classifier in classifiers} for fs in feature_selection}
print("FINISHED TRAINING MODELS")
print("GATHERING RESULTS")
results = {j: {classifier: [val.validate_model(models[fs][classifier][iteration][0], datasets[fs][iteration]['x_test'], datasets[fs][iterationo]['y_test'], 0.50, validation) for i in range(0, iterations)] for classifier in classifiers} for fs in feature_selection}
print("FINISHED GATHERING")
val.save_results(result_path + date, validation, feature_selection, classifiers, iterations, results, models, datasets, labels, feature_selection_parameters, drug_name)
if save_fc:
for fs in feature_selection:
for classifier in classifiers:
save_feature_counter(result_path, fs, classifier, date, validation, feature_counter, feature_size, debug['feature_counter'])
print("SAVED FEATURE COUNTER")
print("PIPELINE ENDS")
return 0
if __name__ == "__main__":
main()