-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtr_examples.py
204 lines (169 loc) · 8.73 KB
/
tr_examples.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
"""
This code performs TR with various configurations
"""
import pandas as pd
import json
import numpy as np
from sklearn.model_selection import train_test_split
from extendtr.TR.topoReg import TopoReg
from extendtr.utils.args import TopoRegArgs
from extendtr.utils.utils import set_seed, metric_calc, stack_models
if __name__ == "__main__":
# load the descriptor - the indices of desc will be used later
desc = pd.read_parquet(f'./SampleDatasets/CHEMBL278/data_ECFP4.parquet', engine='fastparquet').astype('bool')
# load targets
data = pd.read_csv(f'./SampleDatasets/CHEMBL278/data_cp.csv', index_col=0)
target = data["pChEMBL Value"]
# make sure that the indices of desc and target match
desc = desc.loc[target.index]
target = target.loc[desc.index]
# load indicies for scaffold split
with open(f'./SampleDatasets/CHEMBL278/scaffold_split_index.json', 'r') as f:
index = json.load(f)
train_idx = index['train_idx']
test_idx = index['test_idx']
# make sure that train and test indices are included in target.index
train_idx = [idx for idx in train_idx if idx in target.index]
test_idx = [idx for idx in test_idx if idx in target.index]
##### alternatively, you can ranomly split train and test idx
# dataset_idx = target.index.tolist()
# train_idx, test_idx = train_test_split(dataset_idx, test_size=0.2, random_state=args.seed)
# set validation index if necessary
val_set = 0.2 # a fraction number to use [val_set] percent samples from the train set as val set, or None for no validation
if val_set is not None: # if we want to test on the validation set
train_idx, val_idx = train_test_split(train_idx, test_size=val_set, random_state=2021)
else: # no validation
val_idx = None
print('----------------- Running different descriptors -----------------')
# define arg strs for different anchor selection methods
args = TopoRegArgs(f'-ensemble 1')
descriptors = ['ECFP4', 'ECFP6', 'Mordred', 'RDKdesc', 'tcnn_zpad']
data_path = './SampleDatasets/CHEMBL278/'
########################### Train and get the prediction ###############################
preds_test = []
preds_val = []
for descriptor in descriptors:
# Load descriptors
if descriptor in ['ECFP4', 'ECFP6']:
desc = pd.read_parquet(f'{data_path}/data_{descriptor}.parquet', engine='fastparquet').astype('bool')
elif descriptor in ['Mordred', 'RDKdesc']:
desc = pd.read_parquet(f'{data_path}/data_{descriptor}.parquet', engine='fastparquet')
elif descriptor == 'tcnn_zpad':
desc = pd.read_csv(f"{data_path}/tcnn_embeddings_zpad.csv", index_col=0)
if descriptor in ['ECFP4', 'ECFP6']:
args.distance = 'jaccard'
args.desc_norm = False
else:
args.distance = 'euclidean'
args.desc_norm = True
# set random seed
set_seed(args.seed)
mdl, pred_test, pred_val, train_time, test_time = TopoReg(desc, target, train_idx, test_idx, val_idx, args)
# Stack the predicted responses for test and validation sets
preds_test.append(pred_test)
preds_val.append(pred_val)
########################### Combine the results ###############################
# ensemble
pred_test = np.array(preds_test).mean(axis=0)
# evaluation
print('Performance of ensemble predictions:')
scorr, r2, rmse, nrmse = metric_calc(pred_test, target.loc[test_idx], True)
# stacking the results
pred_test, train_time, test_time = stack_models(preds_val, preds_test, target, val_idx)
# evaluation
print('Performance of stacking predictions:')
scorr, r2, rmse, nrmse = metric_calc(pred_test, target.loc[test_idx], True)
print('----------------- Running different anchor selections -----------------')
# load the descriptor - the indices of desc will be used later
desc = pd.read_parquet(f'./SampleDatasets/CHEMBL278/data_ECFP4.parquet', engine='fastparquet').astype('bool')
# define arg strs for different anchor selection methods
arg_strs_anchor = [f'-ensemble 1',
f'-anchorselection maximin -ensemble 1 -mean_anchor_percentage 0.4 -min_anchor_percentage 0.2 -max_anchor_percentage 0.6',
f'-refine_anchors_lasso 1 -anchor_percentage 0.8',
f'-anchorselection maximin_density -weight_density 0.5 -check_duplicates 1'
]
########################### Train and get the prediction ###############################
preds_test = []
preds_val = []
for arg_str in arg_strs_anchor:
args = TopoRegArgs(f'{arg_str}')
# set random seed
set_seed(args.seed)
mdl, pred_test, pred_val, train_time, test_time = TopoReg(desc, target, train_idx, test_idx, val_idx, args)
# Stack the predicted responses for test and validation sets
preds_test.append(pred_test)
preds_val.append(pred_val)
########################### Combine the results ###############################
# ensemble
pred_test = np.array(preds_test).mean(axis=0)
# evaluation
print('Performance of ensemble predictions:')
scorr, r2, rmse, nrmse = metric_calc(pred_test, target.loc[test_idx], True)
# stacking the results
pred_test, train_time, test_time = stack_models(preds_val, preds_test, target, val_idx)
# evaluation
print('Performance of stacking predictions:')
scorr, r2, rmse, nrmse = metric_calc(pred_test, target.loc[test_idx], True)
print('----------------- Running different distance calculations -----------------')
# load the descriptor - the indices of desc will be used later
desc = pd.read_parquet(f'./SampleDatasets/CHEMBL278/data_ECFP4.parquet', engine='fastparquet').astype('bool')
# define arg strs for different anchor selection methods
arg_strs_dist = [f'-ensemble 1',
f'-distance tversky -ensemble 1',
f'-distance euclidean -ensemble 1',
f'-distance cosine -ensemble 1'
]
########################### Train and get the prediction ###############################
preds_test = []
preds_val = []
for arg_str in arg_strs_dist:
args = TopoRegArgs(f'{arg_str}')
# set random seed
set_seed(args.seed)
mdl, pred_test, pred_val, train_time, test_time = TopoReg(desc, target, train_idx, test_idx, val_idx, args)
# Stack the predicted responses for test and validation sets
preds_test.append(pred_test)
preds_val.append(pred_val)
########################### Combine the results ###############################
# ensemble
pred_test = np.array(preds_test).mean(axis=0)
# evaluation
print('Performance of ensemble predictions:')
scorr, r2, rmse, nrmse = metric_calc(pred_test, target.loc[test_idx], True)
# stacking the results
pred_test, train_time, test_time = stack_models(preds_val, preds_test, target, val_idx)
# evaluation
print('Performance of stacking predictions:')
scorr, r2, rmse, nrmse = metric_calc(pred_test, target.loc[test_idx], True)
print('----------------- Running different models -----------------')
# load the descriptor - the indices of desc will be used later
desc = pd.read_parquet(f'./SampleDatasets/CHEMBL278/data_ECFP4.parquet', engine='fastparquet').astype('bool')
# define arg strs for different anchor selection methods
arg_strs_mdls = [f'-ensemble 1',
f'-model LR_L1 -ensemble 1',
f'-model RF -ensemble 1',
f'-model ANN -ensemble 1'
]
########################### Train and get the prediction ###############################
preds_test = []
preds_val = []
for arg_str in arg_strs_dist:
args = TopoRegArgs(f'{arg_str}')
# set random seed
set_seed(args.seed)
mdl, pred_test, pred_val, train_time, test_time = TopoReg(desc, target, train_idx, test_idx, val_idx, args)
# Stack the predicted responses for test and validation sets
preds_test.append(pred_test)
preds_val.append(pred_val)
########################### Combine the results ###############################
# ensemble
pred_test = np.array(preds_test).mean(axis=0)
# evaluation
print('Performance of ensemble predictions:')
scorr, r2, rmse, nrmse = metric_calc(pred_test, target.loc[test_idx], True)
# stacking the results
pred_test, train_time, test_time = stack_models(preds_val, preds_test, target, val_idx)
# evaluation
print('Performance of stacking predictions:')
scorr, r2, rmse, nrmse = metric_calc(pred_test, target.loc[test_idx], True)
print('----------------- All configurations run successfully! -----------------')