-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_graph_encode_affinity.py
191 lines (166 loc) · 7.41 KB
/
test_graph_encode_affinity.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
import cPickle as pickle
import sys
import numpy as np
import torch
import torch.cuda
from rdkit import Chem
from sklearn import metrics
from sklearn.model_selection import train_test_split
from torch import nn
from torch import optim
from torch.utils.data import DataLoader
from models.normed_encoded_basic_model import BasicModel
from models.graph_model_wrapper import GraphWrapper
from mol_graph import *
from mol_graph import GraphEncoder
from pre_process.data_loader import GraphDataSet, collate_2d_graphs, collate_2d_tensors
from pre_process.load_dataset import load_classification_dataset, load_affinity_dataset
from mpnn_functions.encoders.bond_autoencoder import BondAutoEncoder
from mpnn_functions.encoders.atom_autoencoder import AtomAutoEncoder
import tqdm
def filter_dataset(data, labels, lower_cutoff=None, upper_cutoff=None, count_cutoff=None):
uniq, count = np.unique(labels, return_counts=True)
if lower_cutoff is not None:
uniq_mask = count > lower_cutoff
if upper_cutoff is not None:
uniq_mask = np.logical_and(uniq_mask, count < upper_cutoff)
if count_cutoff is not None:
positive = np.argwhere(uniq_mask).reshape(-1)[:4]
uniq_mask = np.zeros_like(uniq, dtype=np.bool)
uniq_mask[positive] = True
label_mask = np.isin(labels, uniq[uniq_mask])
new_label_dict = dict(zip(uniq[uniq_mask], range(uniq_mask.sum())))
filtered_dataset = []
new_labels = []
for graph, cond, label in zip(data, label_mask, labels):
if cond:
new_label = new_label_dict[label]
new_labels.append(new_label)
graph.label = new_label
filtered_dataset.append(graph)
return filtered_dataset, new_labels, uniq_mask.sum()
def count_model_params(model):
model_parameters = filter(lambda p: p.requires_grad, model.parameters())
return sum([np.prod(p.size()) for p in model_parameters])
def save_model(model, model_name, model_att, model_metrics):
# type: (nn.Module, dict) -> None
torch.save(model.state_dict(), 'basic_model' + str(model_name) + '.state_dict')
with open('basic_model_attributes.pickle', 'wb') as out_file:
pickle.dump(model_att, out_file)
with open('basic_model_' + str(model_name) + '_stats.pickle', 'wb') as out_file:
pickle.dump(model_metrics, out_file)
def test_model(model, dataset):
model.eval()
labels = []
true_labels = []
with torch.no_grad():
for batch in tqdm.tqdm(dataset):
labels = labels + model(batch).max(dim=-1)[1].cpu().data.numpy().tolist()
true_labels = true_labels + batch['labels'].cpu().data.numpy().tolist()
return (
metrics.accuracy_score(true_labels, labels),
metrics.precision_score(true_labels, labels, average='micro'),
metrics.recall_score(true_labels, labels, average='micro')
)
seed = 317
torch.manual_seed(seed)
data_file = sys.argv[1]
mgf = MolGraphFactory(Mol2DGraph.TYPE, AtomFeatures(), BondFeatures())
try:
file_data = np.load(data_file+'.npz')
data = file_data['data']
for graph in data:
graph.mask = np.ones(graph.afm.shape[0], dtype=np.float32).reshape(graph.afm.shape[0], 1)
graph.afm = graph.afm.astype(np.float32)
graph.bfm = graph.bfm.astype(np.float32)
graph.adj = graph.adj.astype(np.float32)
graph.label = long(graph.label)
graph.affinity = long(graph.affinity)
no_labels = int(file_data['no_labels'])
all_labels = file_data['all_labels']
file_data.close()
except IOError:
data, no_labels, all_labels = load_affinity_dataset(data_file+'.csv',
'InChI', Chem.MolFromInchi, mgf, 'target', 'pxc50')
graph_encoder = GraphEncoder()
with open('basic_model_graph_encoder.pickle', 'wb') as out:
pickle.dump(graph_encoder, out)
np.savez_compressed(data_file, data=data, no_labels=no_labels, all_labels=all_labels)
data, all_labels, no_labels = filter_dataset(data, all_labels, lower_cutoff=49, upper_cutoff=100)
model_attributes = {
'afm': 8,
'bfm': 2,
'mfm': 8,
'adj': data[0].adj.shape[-1],
'out': 2*8,
'classification_output': 1
}
ae = AtomAutoEncoder()
# ae.load_state_dict(torch.load('./atom_autoencoder.state_dict', map_location=lambda storage, loc: storage))
be = BondAutoEncoder()
# be.load_state_dict(torch.load('./bond_autoencoder.state_dict', map_location=lambda storage, loc: storage))
model = nn.Sequential(
GraphWrapper(BasicModel(model_attributes['afm'], model_attributes['bfm'], model_attributes['mfm'],
model_attributes['adj'], model_attributes['out'], atom_encoder=ae.encoder,
bond_encoder=be.encoder)),
nn.BatchNorm1d(model_attributes['out']),
nn.Linear(model_attributes['out'], model_attributes['classification_output'])
)
selected_label = 243
for graph in data:
graph.label = graph.affinity if selected_label == graph.label else long(4)
all_labels = (selected_label == all_labels)
model.float() # convert to half precision
# for layer in model.modules():
# if isinstance(layer, nn.BatchNorm1d):
# layer.float()
model.apply(BasicModel.init_weights)
ae.load_state_dict(torch.load('./atom_autoencoder.state_dict', map_location=lambda storage, loc: storage))
be.load_state_dict(torch.load('./bond_autoencoder.state_dict', map_location=lambda storage, loc: storage))
print "Model has: {} parameters".format(count_model_params(model))
if torch.cuda.is_available():
model.cuda()
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5)
model.train()
train, test, train_labels, test_labels = train_test_split(data, all_labels, test_size=0.1,
random_state=seed, stratify=all_labels)
del data
del all_labels
del test_labels
train, val = train_test_split(train, test_size=0.1, random_state=seed, stratify=train_labels)
del train_labels
train = GraphDataSet(train)
val = GraphDataSet(val)
test = GraphDataSet(test)
train = DataLoader(train, 128, shuffle=True, collate_fn=collate_2d_graphs)
val = DataLoader(val, 128, shuffle=False, collate_fn=collate_2d_graphs)
test = DataLoader(test, 128, shuffle=False, collate_fn=collate_2d_graphs)
epoch_losses = []
break_con = False
for epoch in tqdm.trange(1000):
model.train()
epoch_loss = 0
for batch in tqdm.tqdm(train):
model.zero_grad()
loss = criterion(model(batch), batch['labels'])
epoch_loss += loss.item()
loss.backward()
optimizer.step()
epoch_losses.append(epoch_loss)
acc, pre, rec = test_model(model, train)
f1 = 2 * (pre * rec) / (pre + rec)
tqdm.tqdm.write(
"epoch {} training loss: {}, acc: {}, pre: {}, rec: {}, F1: {}".format(epoch, epoch_loss, acc,
pre, rec, f1))
acc, pre, rec = test_model(model, val)
f1 = 2 * (pre * rec) / (pre + rec)
tqdm.tqdm.write(
"epoch {} loss: {} validation acc: {}, pre: {}, rec: {}, F1: {}".format(epoch, epoch_loss, acc,
pre, rec, f1))
if not np.isnan(f1) and f1 > 0.8:
save_model(model, 'epoch_'+str(epoch), model_attributes, {'acc': acc, 'pre': pre, 'rec': rec, 'f1': f1})
acc, pre, rec = test_model(model, test)
f1 = 2 * (pre * rec) / (pre + rec)
tqdm.tqdm.write(
"Testing acc: {}, pre: {}, rec: {}, F1: {}".format(acc, pre, rec, f1))