-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_table_1.py
192 lines (155 loc) · 5.62 KB
/
make_table_1.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
from functools import partial
from time import time
import sys
import yaml
from rdkit import Chem
from rdkit import RDLogger
from rdkit.Chem import AllChem
from rdkit.DataStructs import TanimotoSimilarity
from scipy.stats import pearsonr, kendalltau
import numpy as np
import torch
from lib.dataset.chem import standardize_smiles
from lib.dataset.pair_dataset import PairedDataset
from lib.model.model import LitMolformer
from lib.model.sample import sample
RDLogger.DisableLog("rdApp.*")
def load_model(config_path, checkpoint_path, vocabulary_path, device="cuda"):
hparams = yaml.load(open(config_path), Loader=yaml.FullLoader)
hparams["vocabulary"] = vocabulary_path
model = LitMolformer(**hparams)
state_dict = torch.load(checkpoint_path, map_location="cpu")["state_dict"]
model.load_state_dict(state_dict)
model = model.eval().to(device)
if "with_counts" in config_path:
model.mol_to_fingerprints = partial(AllChem.GetMorganFingerprint, radius=2)
else:
model.mol_to_fingerprints = partial(
AllChem.GetMorganFingerprintAsBitVect, radius=2, nBits=1024
)
return model
def generate_samples(model, smiles, beam_size=1000):
smiles = standardize_smiles(smiles)
src = model.vocabulary.encode(model.tokenizer.tokenize(smiles))
src = torch.from_numpy(src.astype(np.int64))
src, src_mask, _, _, _ = PairedDataset.collate_fn([(src, src, torch.ones((1, 1)))])
# tin = time()
samples = sample(
model,
src[:1],
src_mask[:1],
decode_type="beamsearch",
beam_size=beam_size,
beam_search_bs=512,
)
# tout = time()
# print(f"Generated {beam_size:d} samples in {tout-tin:.3f} seconds.")
return samples
def aggregate_same_similarity(sim, ll):
sim_ll = {}
for s, l in zip(sim, ll):
if s not in sim_ll:
sim_ll[s] = []
sim_ll[s].append(l)
for s in sim_ll:
sim_ll[s] = np.mean(sim_ll[s])
sim = np.array([x[0] for x in list(sim_ll.items())])
ll = np.array([x[1] for x in list(sim_ll.items())])
idx = np.argsort(sim)[::-1]
sim = sim[idx]
ll = ll[idx]
return sim, ll
def compute_stats(model, samples):
fp = None
valid = 0
unique = set()
unique_canonicalized = set()
unique_no_stereo = set()
top = 0
top_canonicalized = 0
top_no_stereo = 0
nlls = []
tanimotos = []
first = True
for input_smi, generated_smi, nll in zip(
samples[0][:1000], samples[1][:1000], samples[2][:1000]
):
if fp is None:
mol = Chem.MolFromSmiles(input_smi)
fp = model.mol_to_fingerprints(mol)
std_input_smi = standardize_smiles(input_smi)
Chem.RemoveStereochemistry(mol)
input_smi_no_stereo = Chem.MolToSmiles(mol)
input_smi_no_stereo = standardize_smiles(input_smi_no_stereo)
try:
g_mol = Chem.MolFromSmiles(generated_smi)
g_fp = model.mol_to_fingerprints(g_mol)
t = TanimotoSimilarity(fp, g_fp)
Chem.RemoveStereochemistry(g_mol)
no_stero_smi = Chem.MolToSmiles(g_mol)
tanimotos.append(t)
nlls.append(nll)
unique.add(generated_smi)
std_generated_smi = standardize_smiles(generated_smi)
unique_canonicalized.add(std_generated_smi)
no_stero_smi = standardize_smiles(no_stero_smi)
unique_no_stereo.add(no_stero_smi)
if first:
top += input_smi == generated_smi
top_canonicalized += std_input_smi == std_generated_smi
top_no_stereo += input_smi_no_stereo == no_stero_smi
first = False
valid += 1
except BaseException:
pass
nlls = np.array(nlls)
tans = np.array(tanimotos)
lls = -nlls
idx = np.argsort(lls)[::-1]
rank_lls = lls[idx[:10]]
rank_sims = tans[idx[:10]]
rank_10 = kendalltau(rank_sims, rank_lls).statistic
tans, lls = aggregate_same_similarity(tans, lls)
correlation = pearsonr(tans, lls).statistic
return {
"valid": valid,
"unique": len(unique),
"unique_canonicalized": len(unique_canonicalized),
"unique_no_stereo": len(unique_no_stereo),
"top": top,
"top_canonicalized": top_canonicalized,
"top_no_stereo": top_no_stereo,
"rank_10": rank_10,
"correlation": correlation,
}
if __name__ == "__main__":
config_path = sys.argv[1]
checkpoint_path = sys.argv[2]
vocabulary_path = sys.argv[3]
device = "cuda"
all_res = {}
model = load_model(config_path, checkpoint_path, vocabulary_path, device)
with open("test_data/TTD/ttd.smi") as smiles:
for smi_id, smi in enumerate(smiles):
smi = smi.strip()
smi = standardize_smiles(smi)
samples = generate_samples(model, smi, beam_size=1000)
results = compute_stats(model, samples)
for k in results:
if k not in all_res:
all_res[k] = []
all_res[k].append(results[k])
valid_den = len(all_res[k]) * 1000
unique_den = sum(all_res["valid"])
top_den = len(all_res[k])
for k in all_res:
if k == "valid":
print(k, sum(all_res[k]) / valid_den)
elif "unique" in k:
print(k, sum(all_res[k]) / unique_den)
elif "top" in k:
print(k, sum(all_res[k]) / top_den)
elif "rank" in k:
print(k, np.nanmean(all_res[k]), "+/-", np.nanstd(all_res[k]))
elif k == "correlation":
print(k, np.nanmean(all_res[k]), "+/-", np.nanstd(all_res[k]))