-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomboLTR_CV.py
442 lines (380 loc) · 16.7 KB
/
comboLTR_CV.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
import sys, os
import time
import pickle
import sqlite3
import numpy as np
from sklearn.preprocessing import scale
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
from scipy.stats import spearmanr
import scipy.sparse as sp
import ltr_tensor_solver_actxu_v_cls_010 as tensor_cls
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## ncreg parameter is included for the regularization constant
def main(CV_indices_list, n_order, n_rank, n_repeat, lossdegree=0, \
n_sigma=0.01, ncreg=0.0000001):
time00 = time.time()
nfold = 5 ## number of folds in the cross validation
## (((((((((((((((((((((((((((((((((((((((((((
## Parameters to ltr learner
# n_d=5 ## maximum power(order)
n_d = n_order
order = n_d
# rank=20 ## number of ranks, n_t in the paper
rank = n_rank
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## projection dimension to reduce the total size of the parameters
rankuv=20 ## projection dimension
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
# sigma=0.002 ## learning step size
sigma = n_sigma
learning_rate = sigma
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
sigmadscale=4 ## convergence control
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
nsigma = 1 ## speed correction interval
gamma = 0.999999 ## discount factor of the parameter update
gammanag = 0.95 ## discount for the ADAM gradient update
gammanag2 = 0.95 ## discount for the ADAM norm update
mblock = 1000 ## minibatch size, number of examples
mblock_gap = mblock ## shift of blocks
batch_size = mblock
# nrepeat = 10 ## number of epochs, repetition of the full online run
nrepeat = n_repeat
n_epochs = nrepeat
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
cregular = ncreg ## regularization penaly constant for P and Q as well
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ikolmogorov = 0
kolm_t = 1.5
## nrankclass=1 ## number of steps in multilayer algorithm
nsigmamax = 1 ## gradient scaling to avoid too long gradient, default=1
ihomogen = 1 ## homegeous data vectors, default=1
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## rankuv parameter for projection dimension is included
cmodel = tensor_cls.tensor_latent_vector_cls(norder=n_d, rank=rank, rankuv=rankuv)
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## dscale is included
## set optimization parameters
cmodel.update_parameters(nsigma=nsigma, \
mblock=mblock, \
mblock_gap=mblock_gap, \
sigma0=sigma, \
dscale=sigmadscale, \
gamma=gamma, \
gammanag=gammanag, \
gammanag2=gammanag2, \
cregular=cregular, \
sigmamax=nsigmamax, \
ikolmogorov=ikolmogorov, \
kolm_t=kolm_t)
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## nrankuv and dscale are printed
print('n-t:', cmodel.norder)
print('Rank:', cmodel.nrank)
print('Rankuv:',cmodel.nrankuv)
print('Step size:', cmodel.sigma0)
print('Step freq:', cmodel.nsigma)
print('Epoch:', nrepeat)
print('Block size:', mblock)
print('Discount:', cmodel.gamma)
print('Discount for NAG:', cmodel.gammanag)
print('Discount for NAG norm:', cmodel.gammanag2)
print('Bag size:', cmodel.mblock)
print('Bag step:', cmodel.mblock_gap)
print('Regularization:', cmodel.cregular)
print('Regularization degree:',cmodel.regdegree)
print('Gradient max ratio:', cmodel.sigmamax)
print('Kolmogorov mean:', cmodel.ikolmogorov)
print('Kolmogorov mean param:', cmodel.kolm_t)
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#####################################
## loss function type slection
## activation function
cmodel.iactfunc = 0 ## =0 identity, =1 arcsinh =2 2*sigmoid-1 =3 tanh
## loss degree
# cmodel.lossdegree = 0 ## =0 L_2^2, =1 L^2, =0.5 L_2^{0.5}, ...L_2^{z}
cmodel.lossdegree = lossdegree
print('Activation:', cmodel.iactfunc)
print('Loss degree:', cmodel.lossdegree)
##)))))))))))))))))))))))))))))))))))))))))))))
## prediction collector for all folds
yprediction = np.zeros(m)
xcorrpear = np.zeros(nfold)
xcorrspear = np.zeros(nfold)
xrmse = np.zeros(nfold)
ifold = 0
for itrain, itest in CV_indices_list:
Xtrain = X[itrain]
ytrain = y[itrain]
Xtest = X[itest]
ytest = y[itest]
print('Training:', len(Xtrain))
print('Test:', len(Xtest))
mtrain = len(itrain)
mtest = len(itest)
time0 = time.time()
## training
cmodel.fit(Xtrain, ytrain, nepoch=nrepeat)
time1 = time.time()
print('Training time:', time1 - time0)
with open(outdir + 'Weights_%s_%s.Pickle'%(job_id, ifold), 'wb') as f:
pickle.dump(cmodel.xlambdaU, f)
## Perform predictions
ypred_test = cmodel.predict(Xtest)
ypred_test = ypred_test.ravel()
yprediction[itest] = ypred_test
# Measure performance
RMSE_test = np.sqrt(mean_squared_error(ytest*ymax+ymean, ypred_test*ymax+ymean))
Rpears_test = np.corrcoef(ytest, ypred_test)[0, 1]
Rspear_test, _ = spearmanr(ytest, ypred_test)
xcorrpear[ifold] = Rpears_test
xcorrspear[ifold] = Rspear_test
xrmse[ifold] = RMSE_test
np.savetxt((outdir + "ytestst_scaled_" \
+ "order-" \
+ str(order) + "_rank-" + str(rank) + "_learnR-" \
+ str(learning_rate) + "_nepochs-" + str(n_epochs) \
+ str(nrepeat) + "_nrepeat" + str(cmodel.lossdegree) + "_lossdegree" \
+ "_batchSize-" + str(batch_size) \
+ "_fold_%s" % ifold + "_CV_%s" % job_id + ".txt"), \
ytest)
np.savetxt((outdir + "ypred_scaled_" \
+ "order-" \
+ str(order) + "_rank-" + str(rank) + "_learnR-" \
+ str(learning_rate) + "_nepochs-" + str(n_epochs)
+ str(nrepeat) + "_nrepeat" + str(cmodel.lossdegree) + "_lossdegree" \
+ "_batchSize-" + str(batch_size) \
+ "_fold_%s" % ifold + "_CV_%s" % job_id + ".txt"), \
ypred_test)
np.savetxt((outdir + "ytest_" \
+ "order-" \
+ str(order) + "_rank-" + str(rank) + "_learnR-" \
+ str(learning_rate) + "_nepochs-" + str(n_epochs) \
+ str(nrepeat) + "_nrepeat" + str(cmodel.lossdegree) + "_lossdegree" \
+ "_batchSize-" + str(batch_size) \
+ "_fold_%s" % ifold + "_CV_%s" % job_id + ".txt"), \
ytest*ymax+ymean)
np.savetxt((outdir + "ypred_" \
+ "order-" \
+ str(order) + "_rank-" + str(rank) + "_learnR-" \
+ str(learning_rate) + "_nepochs-" + str(n_epochs)
+ str(nrepeat) + "_nrepeat" + str(cmodel.lossdegree) + "_lossdegree" \
+ "_batchSize-" + str(batch_size) \
+ "_fold_%s" % ifold + "_CV_%s" % job_id + ".txt"), \
ypred_test*ymax+ymean)
# Write results into a file
f = open(outdir + "results" + "_fold_" + str(ifold) + "_order_" + str(order) + "_rank_" + str(rank) \
+ "_repeat_" + str(nrepeat) + "_lossdegree_" + str(cmodel.lossdegree) + "_CV_%s" % job_id + ".txt",
'w')
f.write("rank = %d\n" % (rank))
f.write("n_epochs = %f\n" % (n_epochs))
f.write("n_repeat = %f\n" % (nrepeat))
f.write("lossdegree = %f\n" % (cmodel.lossdegree))
f.write("TEST:\n")
f.write("RMSE = %f\n" % (RMSE_test))
f.write("R = %f\n" % (Rpears_test))
f.write("R_spear = %f\n" % (Rspear_test))
f.close()
# Write results on the screen
print('fold:', ifold)
print("rank = %d" % (rank))
print("n_epochs = %f" % (n_epochs))
print("TEST:")
print("RMSE = %f" % (RMSE_test))
print("R = %f" % (Rpears_test))
print("R_spear = %f" % (Rspear_test))
# Save parameters
cmodel.save_parameters(outdir + 'Params_%s_%s.Pickle'%(job_id, ifold))
ifold += 1
if ifold == nfold:
break
time2 = time.time()
print('Total training time(s): ', time2 - time00)
pcorr = np.corrcoef(y, yprediction)[0, 1]
rmse = np.sqrt(np.mean((y*ymax+ymean - yprediction*ymax+ymean) ** 2))
scorr, _ = spearmanr(y, yprediction)
np.savetxt((outdir + "y_full_" \
+ "order-" \
+ str(order) + "_rank-" + str(rank) + "_learnR-" \
+ str(learning_rate) + "_nepochs-" + str(n_epochs) \
+ str(nrepeat) + "_nrepeat" + str(cmodel.lossdegree) + "_lossdegree" \
+ "_batchSize-" + str(batch_size) + "_CV_%s" % job_id \
+ ".txt"), \
y*ymax+ymean)
np.savetxt((outdir + "ypred_full_" \
+ "order-" \
+ str(order) + "_rank-" + str(rank) + "_learnR-" \
+ str(learning_rate) + "_nepochs-" + str(n_epochs)
+ str(nrepeat) + "_nrepeat" + str(cmodel.lossdegree) + "_lossdegree" \
+ "_batchSize-" + str(batch_size) + "_CV_%s" % job_id \
+ ".txt"), \
yprediction*ymax+ymean)
np.savetxt((outdir + "y_full_scaled_" \
+ "order-" \
+ str(order) + "_rank-" + str(rank) + "_learnR-" \
+ str(learning_rate) + "_nepochs-" + str(n_epochs) \
+ str(nrepeat) + "_nrepeat" + str(cmodel.lossdegree) + "_lossdegree" \
+ "_batchSize-" + str(batch_size) + "_CV_%s" % job_id \
+ ".txt"), \
y)
np.savetxt((outdir + "ypred_full_scaled_" \
+ "order-" \
+ str(order) + "_rank-" + str(rank) + "_learnR-" \
+ str(learning_rate) + "_nepochs-" + str(n_epochs)
+ str(nrepeat) + "_nrepeat" + str(cmodel.lossdegree) + "_lossdegree" \
+ "_batchSize-" + str(batch_size) + "_CV_%s" % job_id \
+ ".txt"), \
yprediction)
#cmodel.save_parameters('Weights_%s.Pickle'%job_id)
print('Full result ---------------')
print('P-corr:', '%6.4f' % pcorr)
print('S-corr:', '%6.4f' % scorr)
print('RMSE:', '%6.4f' % rmse)
print('average on folds')
print('P-corr:', '%6.4f' % np.mean(xcorrpear))
print('S-corr:', '%6.4f' % np.mean(xcorrspear))
print('RMSE:', '%6.4f' % np.mean(xrmse))
## return the Pearson, Spearman correlations and the rmse
return(xcorrpear,xcorrspear,xrmse)
## ###################################################################
def get_data_from_SQLite(sql_db, fp='', per='', dataset='Small'):
'''
Retrieve data in np.ndarray format by specifying fingerprint and omics percent:
fignerprints: '', maccs, circular, estate, extended, graph, hybrid, pubchem, shortp, standard.
('' : no fingerprint; shortp : shortest path; circular : ECFP6)
omics percent: '', 05, 1, 2, 5 (no omics, 0.5%, 1%, 2%, 5%)
dataset: 'Full' or 'Small'
'''
if dataset == 'Small':
combo = 'Combo_Sub'
drug = 'Drugs_Sub'
conc = 'Concs_Sub'
elif dataset == 'Full':
combo = 'Combo'
drug = 'Drugs'
conc = 'Concs'
else:
print(get_data_from_SQLite.__doc__)
raise ValueError('Wrong dataset!')
if fp != '':
fp = 'd1.%s, d2.%s,'%(fp, fp)
if per != '':
per = 'c.gene_expression_%s, c.gene_cnv_%s, c.crispr_ko_%s, c.proteomics_%s,'\
%(per, per, per, per)
print(fp, per)
def adapt_array(arr):
return arr.tobytes()
def convert_array(text):
return np.frombuffer(text)
sqlite3.register_adapter(np.ndarray, adapt_array)
sqlite3.register_converter("array", convert_array)
conn = sqlite3.connect(sql_db, detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.cursor()
cursor.execute('''
SELECT d1.drug_code, d2.drug_code, conc1.conc_code, conc2.conc_code, c.cell_code,
%s %s
combo.response FROM %s combo
INNER JOIN %s d1 ON d1.NSC = combo.drug1
INNER JOIN %s d2 ON d2.NSC = combo.drug2
INNER JOIN Cells c ON c.cell_name = combo.cell
INNER JOIN %s conc1 ON conc1.conc_value = combo.conc1
INNER JOIN %s conc2 ON conc2.conc_value = combo.conc2
ORDER BY combo.order_id
'''%(fp, per, combo, drug, drug, conc, conc)
)
data_array = np.array([np.concatenate(i) for i in cursor.fetchall()])
print('Data loaded from SQLite! Shape: ', data_array.shape)
conn.close()
return data_array
try:
idx_run = int(sys.argv[1])
except:
print('Wrong array id!')
exit()
list_fp = ['', 'maccs']
list_data = ['', '1']
list_scene = ['S1', 'S2', 'S3', 'S4']
dataset = 'Full'
idx_tuple = np.unravel_index(idx_run, (len(list_fp), len(list_data), len(list_scene)))
i_fp = list_fp[idx_tuple[0]]
i_data = list_data[idx_tuple[1]]
i_scene = list_scene[idx_tuple[2]]
job_id = '_'.join([dataset, i_fp, i_data, i_scene])
print(job_id)
### Load training data with single drug cell data
## data directory
cwd = os.getcwd()
sdir = '.'
outdir = cwd + "/comboLTR_results/" + job_id + '/'
print(outdir)
if not os.path.exists(outdir):
os.makedirs(outdir)
with open(sdir + '/CV_Folds_%s_%s.List.Pickle' %(i_scene, dataset), 'rb') as f:
CV_indices_list = pickle.load(f)
print('CV splits: ', [len(j) for i in CV_indices_list for j in i])
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
inorminf = 1 ## normalization by infinite norm ->[-1,+1]
dnormscale=10 ## scaling the norm
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Data = get_data_from_SQLite(sdir + '/DrugCombo.db', i_fp, i_data, dataset)
print('SLQ db used: ', sdir + '/DrugCombo.db')
X = Data[:, :-1]
y = Data[:, -1]
m = len(y)
ndim = X.shape[1]
ymean = np.mean(y)
print('Ymean: ', ymean)
##((((((((((((((((((((((((((((((
## output normalization
y -= np.mean(y)
ymax = np.max(np.abs(y))
print('Ymax:', np.max(np.abs(y)))
y /= np.max(np.abs(y))
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
if inorminf==1:
X = X / np.outer(np.ones(m), dnormscale*np.max(np.abs(X), 0))
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
print('Dataset shape: {}'.format(X.shape))
print('Non-zeros rate: {:.05f}'.format(np.mean(X != 0)))
del Data
dstat={}
lstepsize=[ 0.01 ]
lcregular=[ 0.0001 ]
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
orders = range(5, 6)
ranks = range(20,25,5)
repeats = range(20, 21)
for n_order in orders:
for n_rank in ranks:
for n_repeat in repeats:
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
for n_step in lstepsize:
for nc in lcregular:
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
print('*' * 50)
print('Stepsize:',n_step,',','Cregular:',nc)
print('Order: ', n_order, 'Rank: ', n_rank, 'Epoch: ', n_repeat)
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
xcorrpear,xcorrspear,xrmse = \
main(CV_indices_list, n_order, n_rank, n_repeat, \
n_sigma = n_step, ncreg = nc)
## collect all results for all parameter combinations
dstat[(n_order, n_rank, n_repeat, n_step, nc)] = \
(np.mean(xcorrpear), np.mean(xcorrspear), np.mean(xrmse))
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
## summary result on all parameter combination
for tkey in dstat.keys():
for keyitem in tkey:
print(str('%6.4f'%keyitem)+',', end='')
print(',', end='')
xvalue=dstat[tkey]
for value in xvalue:
print(str('%6.4f'%value)+',', end='')
print()
## @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@