-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCF7_targeted_optuna_input_optimizer.py
256 lines (141 loc) · 11.8 KB
/
MCF7_targeted_optuna_input_optimizer.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import optuna
from keras.models import load_model
# In[2]:
import numpy as np
# In[3]:
up_model = load_model("trained_models/MCF7_multi_task_model_up.h5")
dn_model = load_model("trained_models/MCF7_multi_task_model_dn.h5")
# In[4]:
from sklearn.metrics import make_scorer
from imblearn.metrics import geometric_mean_score
gm_scorer = make_scorer(geometric_mean_score, greater_is_better=True, average='binary')
# In[53]:
def objective(trial):
test_pred = []
for i in range(56):
name = 'jtvae_' + str(i)
test_pred.append(trial.suggest_uniform(name, 0, 1))
# print(test_pred)
up_pred = up_model.predict(np.asarray(test_pred).reshape(1,-1))
dn_pred = dn_model.predict(np.asarray(test_pred).reshape(1,-1))
# print(up_pred)
up_pred_bin = []
dn_pred_bin = []
for i in range(len(up_pred)):
up_pred_bin.append(round(up_pred[i][0][0]))
for i in range(len(dn_pred)):
dn_pred_bin.append(round(dn_pred[i][0][0]))
# print(len(up_pred_bin))
'''
Magic happens
'''
up_score = geometric_mean_score(up_genes_228, up_pred_bin)
dn_score = geometric_mean_score(dn_genes_228, dn_pred_bin)
# print(up_score)
return((up_score + dn_score) / 2)
# In[41]:
import pandas as pd
from sklearn.metrics import f1_score, accuracy_score
# In[42]:
up_harmonizome = pd.read_csv('harmonizome_diseases/harmonizome_dn_binarized_use_for_up_model.csv')
dn_harmonizome = pd.read_csv('harmonizome_diseases/harmonizome_up_binarized_use_for_dn_model.csv')
# In[43]:
gene_names = pd.read_csv('100_gene_names/meta_Probes_info.csv',index_col='probe')
# gene_names.loc[up_harmonizome.iloc[228].index[3]][0]
# In[44]:
f = open("100_gene_names/MCF7_multi_task_gene_list_up.txt", "rt")
mcf7_up_genes = f.read()
mcf7_up_genes = mcf7_up_genes[:-1]
f.close()
print(mcf7_up_genes.split('\n'))
# In[45]:
f = open("100_gene_names/MCF7_multi_task_gene_list_dn.txt", "rt")
mcf7_dn_genes = f.read()
mcf7_dn_genes = mcf7_dn_genes[:-1]
f.close()
print(mcf7_dn_genes.split('\n'))
# In[46]:
up_genes_228 = []
for gene in up_harmonizome.iloc[228].index[2:]:
if(gene_names.loc[gene][0] in mcf7_up_genes.split('\n')):
up_genes_228.append(up_harmonizome.iloc[228][gene])
# In[47]:
dn_genes_228 = []
for gene in dn_harmonizome.iloc[228].index[2:]:
if(gene_names.loc[gene][0] in mcf7_dn_genes.split('\n')):
dn_genes_228.append(dn_harmonizome.iloc[228][gene])
# In[48]:
len(mcf7_dn_genes)
# In[54]:
if __name__ == '__main__':
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=100000)
print(study.best_trial)
# In[14]:
import os
os.environ['USE_CPU']
# In[25]:
# from keras.layers import Dense, Dropout, Activation, BatchNormalization, Input
# from keras.models import Model
# from keras.optimizers import SGD
# from keras.models import load_model
# from keras import backend as K
from sklearn.utils import class_weight
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import log_loss
import json
import pandas as pd
import numpy as np
import torch
import sys
import copy
# import tensorflow as tf
from jtnn import *
sys.path.append('./jtnn')
vocab = [x.strip("\r\n ") for x in open("unique_canonical_train_vocab.txt")]
vocab = Vocab(vocab)
hidden_size = 450
latent_size = 56
depth = 3
stereo = True
model_jtvae = JTNNVAE(vocab, hidden_size, latent_size, depth, stereo=stereo)
model_jtvae.cuda()
model_jtvae.load_state_dict(torch.load("Models/model.iter-9-6000", map_location=torch.device('cuda'))) # opts.model_path
optimize_edilen_smi = model_jtvae.reconstruct2(torch.from_numpy(np.asarray([acc_opt[0:28]])).float().cuda(),
torch.from_numpy(np.asarray([acc_opt[28:56]])).float().cuda())
optimize_edilen_smi
# In[10]:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# In[2]:
acc_opt = [0.9804356273053599, 0.9610935922951785, 0.05127288323007048, 0.8177196753122299, 0.6837313862591637, 0.7381689245386361, 0.9978219162802908, 0.31223249326205404, 0.47164596753620364, 0.6587842097358985, 0.9821190533175232, 0.07323873235520775, 0.22895716100218955, 0.3151992136137758, 0.9666066382800963, 0.9165516525000329, 0.6144619598597236, 0.9271859269377859, 0.9847945508220569, 0.15774337715234832, 0.6381545648811106, 0.07934051928978003, 0.0025256746975840273, 0.5093227808791668, 0.37305360414758715, 0.1994836835293278, 0.9521265517140703, 0.14129542622050825, 0.5555574684419486, 0.33547602721695924, 0.16638181164354612, 0.07522793652588872, 0.09634419715095802, 0.15365720080331485, 0.477949940451241, 0.811129927565073, 0.6708712022766021, 0.40593081107887163, 0.2531098670326919, 0.8093048505088234, 0.7158852520557983, 0.6767502844608871, 0.8283655038611972, 0.7486927373320671, 0.05773727687662405, 0.08673514367145627, 0.8994114685614923, 0.921080896569702, 0.7326272685476385, 0.871908263473597, 0.9845467491270194, 0.3345094272640142, 0.0333432011146262, 0.3182025361053061, 0.0925238638895128, 0.19442030628019263]
# 'CC1COC2COCC2(C(=O)NCc2ccc(S(C)=O)cc2)N1'
# In[15]:
acc_opt = [0.7574552589954106, 0.30700816860974384, 0.1387814872169683, 0.7387946142983973, 0.18818262571440603, 0.5005838882723711, 0.9981191653430485, 0.48534156679726326, 0.4715467637649426, 0.1471025678619839, 0.5136339295779269, 0.7698737521864689, 0.2141428434723781, 0.6580194398837078, 0.1256870453925412, 0.7141851022512122, 0.18698617716939706, 0.7402713262446405, 0.642855496759388, 0.24718042678972696, 0.6939146661009131, 0.6225164894428715, 0.06296009636489236, 0.6902210053494402, 0.9435527115622447, 0.05478966114974697, 0.7809712272566657, 0.15128176384716358, 0.4640970752306958, 0.6167947616871894, 0.13988518717774867, 0.0029416480831944945, 0.7347131010227024, 0.7141493497947312, 0.3238290502933372, 0.09445229539457486, 0.47639050920517223, 0.0315833643332638, 0.11785332750683082, 0.8483182701239439, 0.08565528286884445, 0.8491844687229194, 0.748975722166217, 0.18350292575646876, 0.5098531319288114, 0.25848680338985347, 0.3513993369546173, 0.8173269964432355, 0.45573195156719976, 0.9504017754493265, 0.7606423385977175, 0.7280260030912289, 0.7018860155956247, 0.08237258870978736, 0.7876465178297136, 0.7327773540979894]
# 'Cc1ccccc1OCC(=O)N1CCCC1c1nnc[nH]1'
# In[18]:
acc_opt = [0.9688114504909912, 0.6830225466420283, 0.13150181512860107, 0.24133750208231305, 0.15362346722524703, 0.9983615719616445, 0.20517788987498314, 0.7593408921169336, 0.016414704305768947, 0.3885659869623006, 0.6176526206022203, 0.8170877145419247, 0.7598670444076829, 0.1147461368604251, 0.8438562683707928, 0.25993000231565583, 0.11497208475602519, 0.9191123215590766, 0.07389693522629255, 0.07908587550369675, 0.11898419177390525, 0.10665960222890458, 0.8273884198738979, 0.4732374799140186, 0.6205333405977497, 0.5381245852444215, 0.3630185920178866, 0.033768881982648366, 0.8365047722767632, 0.2054290698431954, 0.20169597947675785, 0.28011173716079735, 0.8234659721671418, 0.056188155813208754, 0.3580749644391623, 0.4580187519070481, 0.8570760913606774, 0.42587734744931605, 0.00010590139823894902, 0.28295262899264617, 0.23377099291752018, 0.06293015757464308, 0.821461648625098, 0.006266626855761067, 0.8834018107780569, 0.41686081968007355, 0.0983819740980368, 0.9118186869833879, 0.15594020051029978, 0.5696976450025227, 0.11115248675404395, 0.26196812841079853, 0.46941949912042935, 0.14950218644508184, 0.5580011453191948, 0.3675996888794041]
# 'CS(=O)(=O)c1ccc(C2CC2NC(=O)CO)cc1'
# In[20]:
acc_opt =[ 0.42168457861406267, -0.35157913933572943, -0.9963217755203908, -0.8443350287973989, 0.5364313828782596, -0.8327697600975509, -0.0851309252839466, 0.6521145522819276, -0.645793235698485, -0.5274371211363784, -0.5531145756045901, 0.8472281766806458, -0.028798822166682836, -0.2536717799098069, -0.6277264462209896, 0.11772597701422366, 0.2967669658900175, 0.043223508603611, 0.6124282525369573, -0.8940651455591146, -0.25340943169752167, 0.734705140870945, -0.8054959147932397, -0.4727696142041638, -0.5823293128812315, -0.7933266753914707, -0.9819736045262673, 0.2697120597001528, 0.9995728841502498, -0.23760088792251133, -0.013312932960684282, -0.7526494705118258, -0.3296390399988286, -0.44549021601971295, -0.9897744558981729, -0.5484010871139485, 0.8465748126161237, -0.5101583911942417, -0.8695501299701602, -0.10831730451082103, 0.7669859941370328, 0.8042745050902914, 0.13809997291740184, 0.37065302828508573, -0.588741769102698, 0.20685430314845168, 0.2638333747062656, -0.18359292132297492, -0.5027423878303925, -0.42856882363793997, -0.33085981033620476, -0.7417935166301123, 0.3098242692767708, 0.07744049199024415, 0.4007945914297265, 0.2610590536878075]
# 'CC(C)Oc1cccc(CNC(=O)C2C=CCSC2)c1'
# In[22]:
acc_opt = [ 0.9999240173746481, 0.6251303773908249, 0.03774960474303733, 0.9058439907473931, 0.17660977484651674, 0.31160707240287255, 0.7522758167058459, 0.05154535232484111, 0.7343903650621229, 0.0801747716448448, 0.7337467060059051, 0.2960819412988333, 0.6558604119179112, 0.22495521827041848, 0.028321877149525165, 0.7271940587853871, 0.9868761910979877, 0.9884875506025453, 0.052887459414015264, 0.19424340579702282, 0.25122479475288584, 0.33672979879621384, 0.6701935795894086, 0.6895904498901455, 0.680496777766601, 0.20920572675998872, 0.8648724125796091, 0.5009907060946867, 0.31435791070128255, 0.01927159577639652, 0.09323380027673837, 0.7287471686425366, 0.0004079846930550371, 0.13957365143290665, 0.4040562430988304, 0.48841130424403806, 0.6920181477545444, 0.35662680934929697, 0.3310720397074543, 0.8670382833191691, 0.9124649516195307, 0.8993954011968196, 0.7735614908193859, 0.38489738535449325, 0.9580948291269855, 0.9987224075570658, 0.9982962267486802, 0.9367584629166569, 0.34570550253530924, 0.8433107640163088, 0.06449881599256471, 0.885640896573954, 0.9837468222698316, 0.003606279136564937, 0.7122113062584974, 0.34837321400522386]
# 'CC(=O)N1CCOCC1CS(=O)(=O)C1CCCCC1'
# In[24]:
acc_opt = [ 0.6467070982069009, 0.9803128614494161, 0.08090352748603347, 0.05071208596080625, 0.00039495662872494203, 0.33306636983109494, 0.546607027802012, 0.11778123087286665, 0.03291124497499527, 0.736329292449917, 0.39387729325744525, 0.3153132029895923, 0.923265156108104, 0.22220487521001256, 0.47313618236224564, 0.6358775849385104, 0.06094984664945835, 0.6853725256884233, 0.017747118883407534, 0.11520769872823663, 0.19506872729854305, 0.0019258216937553807, 0.37366786413209363, 0.48373224745185595, 0.5120629325415776, 0.1659500127301293, 0.9012956124166867, 0.05641396882331536, 0.2529741634842772, 0.3854474379898493, 0.9502105904489864, 0.5081520140204306, 0.9638214193508015, 0.24761712580400866, 0.025361720344199312, 0.8162440444212102, 0.8039580616306922, 0.6055955806448012, 0.0010884342802737479, 0.47062204270997976, 0.8087490986300102, 0.018174971096524793, 0.7636470466468805, 0.3187189472422826, 0.24807384353046835, 0.026228428778428736, 0.9488416982386606, 0.9799510605584996, 0.36360121580948784, 0.9449639775484892, 0.17412675167355668, 0.16403213536221747, 0.9350643514713851, 0.15514478615182858, 0.2037401978067503, 0.4601662450751146]
# 'CC(=O)Nc1ccccc1COC(c1ccsc1)C1CCCCN1'
# In[21]:
up_pred = up_model.predict(np.asarray(acc_opt).reshape(1,-1))
dn_pred = dn_model.predict(np.asarray(acc_opt).reshape(1,-1))
# print(up_pred)
up_pred_bin = []
dn_pred_bin = []
for i in range(len(up_pred)):
up_pred_bin.append(round(up_pred[i][0][0]))
for i in range(len(dn_pred)):
dn_pred_bin.append(round(dn_pred[i][0][0]))