This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_models_args.py
executable file
·348 lines (252 loc) · 12.9 KB
/
train_models_args.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
#!/usr/bin/env python
#SBATCH -N 1 # nodes requested
#SBATCH -n 1 # tasks requested
#SBATCH -c 8 # cores requested
#SBATCH --mem=24000 # memory in Mb
#SBATCH -t 140:00:00 # time requested in hour:minute:second
import warnings
def fxn():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
import pandas as pd
import sklearn as sk
from sklearn import svm
from sklearn.calibration import CalibratedClassifierCV
from sklearn.kernel_approximation import RBFSampler
from sklearn.ensemble import BaggingClassifier
from sklearn.model_selection import ShuffleSplit
import os
import numpy as np
from sklearn.metrics import balanced_accuracy_score, roc_auc_score, recall_score, precision_score, matthews_corrcoef, f1_score
from joblib.parallel import Parallel
import itertools
import joblib
from joblib.parallel import Parallel, delayed
import argparse
import numpy as np
import scipy.sparse
def linearkernel(data_1, data_2):
return np.dot(data_1, data_2.T)
def tanimotokernel(data_1, data_2):
if isinstance(data_1, scipy.sparse.csr_matrix) and isinstance(data_2, scipy.sparse.csr_matrix):
return _sparse_tanimotokernel(data_1, data_2)
elif isinstance(data_1, scipy.sparse.csr_matrix) or isinstance(data_2, scipy.sparse.csr_matrix):
# try to sparsify the input
return _sparse_tanimotokernel(scipy.sparse.csr_matrix(data_1), scipy.sparse.csr_matrix(data_2))
else: # both are dense
return _dense_tanimotokernel(data_1, data_2)
def _dense_tanimotokernel(data_1, data_2):
"""
Tanimoto kernel
K(x, y) = <x, y> / (||x||^2 + ||y||^2 - <x, y>)
as defined in:
"Graph Kernels for Chemical Informatics"
Liva Ralaivola, Sanjay J. Swamidass, Hiroto Saigo and Pierre Baldi
Neural Networks
https://www.sciencedirect.com/science/article/pii/S0893608005001693
http://members.cbio.mines-paristech.fr/~jvert/svn/bibli/local/Ralaivola2005Graph.pdf
"""
norm_1 = (data_1 ** 2).sum(axis=1).reshape(data_1.shape[0], 1)
norm_2 = (data_2 ** 2).sum(axis=1).reshape(data_2.shape[0], 1)
prod = data_1.dot(data_2.T)
divisor = (norm_1 + norm_2.T - prod) + np.finfo(data_1.dtype).eps
return prod / divisor
def _sparse_tanimotokernel(data_1, data_2):
"""
Tanimoto kernel
K(x, y) = <x, y> / (||x||^2 + ||y||^2 - <x, y>)
as defined in:
"Graph Kernels for Chemical Informatics"
Liva Ralaivola, Sanjay J. Swamidass, Hiroto Saigo and Pierre Baldi
Neural Networks
https://www.sciencedirect.com/science/article/pii/S0893608005001693
http://members.cbio.mines-paristech.fr/~jvert/svn/bibli/local/Ralaivola2005Graph.pdf
"""
norm_1 = np.array(data_1.power(2).sum(axis=1).reshape(data_1.shape[0], 1))
norm_2 = np.array(data_2.power(2).sum(axis=1).reshape(data_2.shape[0], 1))
prod = data_1.dot(data_2.T).A
divisor = (norm_1 + norm_2.T - prod) + np.finfo(data_1.dtype).eps
result = prod / divisor
return result
def _minmaxkernel_numpy(data_1, data_2):
"""
MinMax kernel
K(x, y) = SUM_i min(x_i, y_i) / SUM_i max(x_i, y_i)
bounded by [0,1] as defined in:
"Graph Kernels for Chemical Informatics"
Liva Ralaivola, Sanjay J. Swamidass, Hiroto Saigo and Pierre Baldi
Neural Networks
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.92.483&rep=rep1&type=pdf
"""
return np.stack([(np.minimum(data_1, data_2[cpd,:]).sum(axis=1) / np.maximum(data_1, data_2[cpd,:]).sum(axis=1)) for cpd in range(data_2.shape[0])],axis=1)
try:
import numba
from numba import njit, prange
@njit(parallel=True,fastmath=True)
def _minmaxkernel_numba(data_1, data_2):
"""
MinMax kernel
K(x, y) = SUM_i min(x_i, y_i) / SUM_i max(x_i, y_i)
bounded by [0,1] as defined in:
"Graph Kernels for Chemical Informatics"
Liva Ralaivola, Sanjay J. Swamidass, Hiroto Saigo and Pierre Baldi
Neural Networks
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.92.483&rep=rep1&type=pdf
"""
result = np.zeros((data_1.shape[0], data_2.shape[0]), dtype=np.float64)
for i in prange(data_1.shape[0]):
for j in prange(data_2.shape[0]):
result[i,j] = _minmax_two_fp(data_1[i], data_2[j])
return result
@njit(fastmath=True)
def _minmax_two_fp(fp1, fp2):
common = numba.int32(0)
maxnum = numba.int32(0)
i = 0
while i < len(fp1):
min_ = fp1[i]
max_ = fp2[i]
if min_ > max_:
min_ = fp2[i]
max_ = fp1[i]
common += min_
maxnum += max_
i += 1
return numba.float64(common) / numba.float64(maxnum)
except:
print("Couldn't find numba. I suggest to install numba to compute the minmax kernel much much faster")
minmaxkernel = _minmaxkernel_numpy
def precompute_gram(target, kernel):
df = pd.read_pickle("{}_df.pkl.gz".format(target))
training = df[df.trainingset_class == "training"]
test = df[df.trainingset_class == "test"]
validation = df[df.trainingset_class == "validation"]
dtype = np.int32 if kernel == "minmax" else np.float64
training_X = np.array([np.array(e) for e in training.cfp.values],dtype=dtype)
training_Y = np.array(training.activity_label, dtype=np.float64)
if kernel =="tanimoto":
gram_matrix_training = tanimotokernel(training_X,training_X)
elif kernel =='minmax':
gram_matrix_training = minmaxkernel(training_X,training_X)
else:
gram_matrix_training = linearkernel(training_X,training_X)
np.save("{}_{}_training_X.npy".format(target, kernel),gram_matrix_training)
np.save("{}_{}_training_Y.npy".format(target, kernel),training_Y)
del gram_matrix_training
test_X = np.array([np.array(e) for e in test.cfp.values],dtype=dtype)
test_Y = np.array(test.activity_label, dtype=np.float64)
if kernel =="tanimoto":
gram_matrix_test = tanimotokernel(test_X, training_X)
elif kernel =='minmax':
gram_matrix_test = minmaxkernel(test_X,training_X)
else:
gram_matrix_test = linearkernel(test_X, training_X)
np.save("{}_{}_test_X.npy".format(target, kernel),gram_matrix_test)
np.save("{}_{}_test_Y.npy".format(target, kernel),test_Y)
del gram_matrix_test
validation_X = np.array([np.array(e) for e in validation.cfp.values],dtype=dtype)
validation_Y = np.array(validation.activity_label, dtype=np.float64)
if kernel =="tanimoto":
gram_matrix_validation = tanimotokernel(validation_X, training_X)
elif kernel =='minmax':
gram_matrix_validation = minmaxkernel(validation_X,training_X)
else:
gram_matrix_validation = linearkernel(validation_X, training_X)
np.save("{}_{}_validation_X.npy".format(target, kernel),gram_matrix_validation)
np.save("{}_{}_validation_Y.npy".format(target, kernel),validation_Y)
del gram_matrix_validation
def train_and_evaluate(target,c,kernel, bal, mmap_mode=None):
if mmap_mode == "None":
mmap_mode = None
print("Start {} {} {} {}".format(target,c,kernel, bal))
if os.path.exists('models/{}_c_{}_kernel_{}_{}_proba.pkl'.format(target, c,kernel,bal)):
return
if not os.path.exists("{}_{}_validation_Y.npy".format(target, kernel)):
precompute_gram(target, kernel)
training_X = np.load("{}_{}_training_X.npy".format(target, kernel), mmap_mode=mmap_mode)
training_Y = np.ascontiguousarray(np.load("{}_{}_training_Y.npy".format(target, kernel)), dtype=np.float64)
test_X = np.load("{}_{}_test_X.npy".format(target, kernel), mmap_mode=mmap_mode)
test_Y = np.ascontiguousarray(np.load("{}_{}_test_Y.npy".format(target, kernel)), dtype=np.float64)
validation_X = np.load("{}_{}_validation_X.npy".format(target, kernel), mmap_mode=mmap_mode)
validation_Y = np.ascontiguousarray(np.load("{}_{}_validation_Y.npy".format(target, kernel)), dtype=np.float64)
def advertize_mmap(arr, advise):
#MADV_NORMAL 0
#MADV_RANDOM 1
#MADV_WILLNEED 3
#MADV_DONTNEED 4
if mmap_mode:
#numpy is quite slow with random access mmap. lets give it some hints for random access
import platform
if platform.system() == "Linux" or "Darwin":
import ctypes
libc = "libc.so.6" if platform.system() == "Linux" else "libc.dylib"
madvise = ctypes.CDLL(libc).madvise
madvise.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int]
madvise.restype = ctypes.c_int
madvise(arr.ctypes.data, arr.size * arr.dtype.itemsize, advise)
#assert madvise(arr.ctypes.data, arr.size * arr.dtype.itemsize, advise) == 0, "MADVISE FAILED" # 1 means MADV_RANDOM
def score_clf(clf):
sets = {"Training set": (training_X, training_Y) ,"Test set": (test_X, test_Y), "Validation set": (validation_X, validation_Y) }
scores_binary = {"Balanced Accuracy": lambda x,y: balanced_accuracy_score(x,y, adjusted=False), "Recall": recall_score, "Precision": precision_score, "MCC": matthews_corrcoef, "F1": f1_score }
scores_proba = {"ROC AUC": roc_auc_score }
scores = {}
for setname, data in sets.items():
data_X = data[0]
data_Y = data[1]
advertize_mmap(data_X, 0)
predicted_Y = clf.predict(data_X)
predicted_Y_proba = clf.predict_proba(data_X)[:,1]
advertize_mmap(data_X, 4)
for scorename, score_fn in scores_binary.items():
scores["{}\t{}".format(setname, scorename)] = score_fn(data_Y, predicted_Y)
for threshold in [0.5, 0.6, 0.7, 0.8, 0.85, 0.9]:
for scorename, score_fn in scores_binary.items():
scores["{}\t{}\tthreshold_{}".format(setname, scorename, threshold)] = score_fn(data_Y, np.array(predicted_Y_proba >= threshold, dtype=np.int))
for scorename, score_fn in scores_proba.items():
scores["{}\t{}".format(setname, scorename)] = score_fn(data_Y, predicted_Y_proba)
return scores
def build_test_svm(c, kernel, bal, training_X, training_Y, test_X, test_Y, validation_X, validation_Y):
advertize_mmap(training_X, 0)
if bal == "unbalanced":
class_weight = None
else:
class_weight = 'balanced'
clf = svm.SVC(C=c, random_state=1234, kernel='precomputed', cache_size=1900, probability=True, class_weight=class_weight)
clf.fit(training_X, training_Y)
scores = score_clf(clf)
return clf, scores
if not os.path.exists('models/{}_c_{}_kernel_{}_{}_proba.pkl'.format(target, c,kernel,bal)):
clf, scores = build_test_svm(c,kernel, bal, training_X, training_Y,test_X, test_Y, validation_X,validation_Y)
joblib.dump(clf, 'models/{}_c_{}_kernel_{}_{}_proba.pkl'.format(target, c,kernel,bal))
with open('models/{}_c_{}_kernel_{}_{}_proba.stats'.format(target, c,kernel,bal), "w") as fd:
for scorename, score in scores.items():
line = "{}:\t{}\n".format(scorename, score)
fd.write(line)
else:
clf = joblib.load('models/{}_c_{}_kernel_{}_{}_proba.pkl'.format(target, c,kernel,bal))
scores = score_clf(clf)
with open('models/{}_c_{}_kernel_{}_{}_proba.stats'.format(target, c,kernel,bal), "w") as fd:
for scorename, score in scores.items():
line = "{}:\t{}\n".format(scorename, score)
fd.write(line)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Precompute gram matrix')
parser.add_argument('target', type=str, help='Target dataset to lead')
parser.add_argument('c', type=float, help='C parameter of SVM')
parser.add_argument('k', type=str, help='Kernel of SVM')
parser.add_argument('bal', type=str, help='Balance Classes')
parser.add_argument('mmap', type=str, help='MMAP mode for loading the Gram Matrix')
args = parser.parse_args()
if not os.path.exists("{}_{}_validation_Y.npy".format(args.target, args.k)):
precompute_gram(target, kernel)
train_and_evaluate(args.target, args.c, args.k, args.bal, mmap_mode=args.mmap)
#targets = ["DRD2"]
#cs = [0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10 ,100, 1000, 10000]
#gs = [0.00001,0.0001,0.001,0.01,0.1,1,10,100, 1000, 10000]
#combinations = list(itertools.product(targets, cs, gs))[::-1]
#results_training = {}
#results_test = {}
#results = Parallel()(delayed(train_and_evaluate)(target, c, g) for target,c,g in combinations)
# DRD2_nm_c_5000.0_kernel_1e-05_k_rbf.stats:Test set adjusted balanced accuracy: 0.4300765325077398