-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransPro_Ic50_ft.py
executable file
·187 lines (154 loc) · 8.31 KB
/
transPro_Ic50_ft.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
from ast import parse
import os
import sys
from xmlrpc.client import boolean
import torch
import argparse
import pandas as pd
import datareader
import numpy as np
import wandb
from transPro_config import get_config
import transPro_model
from collections import defaultdict
from torch import save
import random
metrics_summary = defaultdict(
pearson_list_dev = [],
pearson_list_test = [],
spearman_list_dev = [],
spearman_list_test = [],
rmse_list_dev = [],
rmse_list_test = [])
# check cuda
def setup_dataloader(dataloader):
dataloader.setup()
print('#Train: %d' % len(dataloader.train_data))
print('#Dev: %d' % len(dataloader.dev_data))
print('#Test: %d' % len(dataloader.test_data))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = 'transPro')
parser.add_argument('--exp_id', type=str, default='test')
parser.add_argument('--Ic_50_train_dir',
type = str,
default= 'data/Ic50/rand_0_train_ic50_wo_na.csv',
help= 'Ic50 training file')
parser.add_argument('--Ic_50_dev_dir',
type = str,
default= 'data/Ic50/rand_0_dev_ic50_wo_na.csv',
help= 'Ic50 dev file')
parser.add_argument('--Ic_50_test_dir',
type = str,
default= 'data/Ic50/rand_0123_test_ic50_wo_na.csv',
help= 'Ic50 test file')
parser.add_argument('--drug_file_dir',
type = str,
default='data/a_gdsc_drugs_smiles_pro.csv',
help = 'the drug file directory (# broad_id # smiles #)')
parser.add_argument('--trans_basal_dir',
type = str,
default='data/CCLE_x1305_978genes.csv',
help = 'basal transcriptome data (cell feature)')
parser.add_argument('--pretrained_model_dir',
type = str,
default='data/trained_model/final_transPro_model.pt',
help = 'saved pretrained pretraining model')
# '/raid/home/yoyowu/PertPro/models_inventory/0422_get_pertTrans_w_transmitter_model.pt'
parser.add_argument('--saved_model_path',
type = str,
default = None)
parser.add_argument('--max_epochs',
type = int,
default=4,
help = 'Total number of epochs')
parser.add_argument('--lr',type=float,default=0.0001)
parser.add_argument('--batch_size',type=int, default=64)
parser.add_argument('--wd',type=float, default=0.01)
parser.add_argument('--include_trans', type=int, default=1,help='whether to include the pert trans data')
parser.add_argument('--device', type=int, default=3)
parser.add_argument('--dop',type=float,default=0.1)
parser.add_argument('--seed',type=int, default=343)
parser.add_argument('--use_transmitter',type=int, default=1)
parser.add_argument('--infer_mode',type=int, default=0,
help=' infer mode 0: infer mode is turned off, \
infer mode 1 : output the hidden representation, \
infer mode 2: output the final prediction')
parser.add_argument('--task_spec', type = int, default=1)
parser.add_argument('--job', type = str, default='perturbed_pros', help='which embedding to use, perturbed_pros or perturbed_trans')
parser.add_argument('--freeze_pretrained_modules',type = int, default=0)
parser.add_argument('--predicted_result_for_testset',type=str,default='/raid/home/yoyowu/CODE-AE/data/0608_Transpro_embeddiings_cle_gdsc_pred.csv')
# /raid/home/yoyowu/PertPro/chemblFiltered_and_supervise_pretrained_model_with_contextPred.pth
args = parser.parse_args()
seed=args.seed
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
device = torch.device("cuda:"+str(args.device)) if torch.cuda.is_available() else torch.device("cpu")
print("Use GPU: %s" % args.device)
### prepare for the two dataloaders
data_config = get_config('data')
if args.infer_mode ==2 or args.infer_mode ==1:
data_config.data_filter = None
Ic_50_data = datareader.Ic_50_DataLoader(
args.drug_file_dir, args.Ic_50_train_dir, args.Ic_50_dev_dir,
args.Ic_50_test_dir, data_config.data_filter, device,
args.trans_basal_dir, batch_size = args.batch_size)
Ic_50_data.setup()
print('#Train: %d' % len(Ic_50_data.train_data))
print('#Dev: %d' % len(Ic_50_data.dev_data))
print('#Test: %d' % len(Ic_50_data.test_data))
### prepare for models
model_config = get_config('model')
model = transPro_model.Ic50_task(
device,
model_config,
args).double().to(device)
if args.pretrained_model_dir:
model.transPro.load_state_dict(torch.load(args.pretrained_model_dir))
print("successfully loaded pretrained model from {}".format(args.pretrained_model_dir))
model.config_optimizer( lr = args.lr)
wandb.init(project="transPro_Ic50",config=args)
wandb.watch(model, log="all")
for epoch in range(args.max_epochs):
print("Iteration %d:" % (epoch+1))
for step, (features,labels,_) in enumerate(Ic_50_data.train_dataloader()):
model.train_step(
features['drug'].to(device),
features['cell_id'],
labels,job =args.job,epoch=epoch,
freeze_pretrained_modules=args.freeze_pretrained_modules
)
model.train_epoch_end(epoch)
for step, (features,labels,_) in enumerate(Ic_50_data.val_dataloader()):
model.val_test_step(
features['drug'].to(device),
features['cell_id'],
labels,job = args.job,epoch=epoch
)
model.validation_test_epoch_end(epoch=epoch,
validation_test_flag ='Perturbed_Pros_Validation',
metrics_summary=metrics_summary )
for step, (features,labels,_) in enumerate(Ic_50_data.test_dataloader()):
model.val_test_step(
features['drug'].to(device),
features['cell_id'],
labels,job =args.job,epoch=epoch
)
model.validation_test_epoch_end(epoch=epoch,
validation_test_flag ='Perturbed_Pros_Test',
metrics_summary=metrics_summary )
if args.saved_model_path:
save(model.state_dict(),args.saved_model_path)
print("the trained model is successfully saved at {}".format(args.saved_model_path))
best_dev_epoch = np.argmax(metrics_summary['pearson_list_dev'])
print("Epoch %d got best Pearson's correlation on dev set: %.4f" % (best_dev_epoch + 1, metrics_summary['pearson_list_dev'][best_dev_epoch]))
print("Epoch %d got Spearman's correlation on dev set: %.4f" % (best_dev_epoch + 1, metrics_summary['spearman_list_dev'][best_dev_epoch]))
print("Epoch %d got RMSE on dev set: %.4f" % (best_dev_epoch + 1, metrics_summary['rmse_list_dev'][best_dev_epoch]))
print("Epoch %d got Pearson's correlation on test set w.r.t dev set: %.4f" % (best_dev_epoch + 1, metrics_summary['pearson_list_test'][best_dev_epoch]))
print("Epoch %d got Spearman's correlation on test set w.r.t dev set: %.4f" % (best_dev_epoch + 1, metrics_summary['spearman_list_test'][best_dev_epoch]))
print("Epoch %d got RMSE on test set w.r.t dev set: %.4f" % (best_dev_epoch + 1, metrics_summary['rmse_list_test'][best_dev_epoch]))
best_test_epoch = np.argmax(metrics_summary['pearson_list_test'])
print("Epoch %d got best Pearson's correlation on test set: %.4f" % (best_test_epoch + 1, metrics_summary['pearson_list_test'][best_test_epoch]))
print("Epoch %d got Spearman's correlation on test set: %.4f" % (best_test_epoch + 1, metrics_summary['spearman_list_test'][best_test_epoch]))
print("Epoch %d got RMSE on test set: %.4f" % (best_test_epoch + 1, metrics_summary['rmse_list_test'][best_test_epoch]))