-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
206 lines (182 loc) · 6.5 KB
/
Copy pathtrain.py
File metadata and controls
206 lines (182 loc) · 6.5 KB
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
from itertools import product
import numpy as np
import pytorch_lightning as pl
import torch
from pytorch_lightning.loggers import WandbLogger
# from src.datamodules.ligand_activity_and_binding_datamodule import (
# LigandActivityAndBindingDataModule,
# )
from src.datamodules import LigandTargetActivityAndBindingDataModule
from src.featurizers import RMatFeaturizer, SchnetFeaturizer
from src.huggingmolecules import RMatConfig
from src.models.common import CrossAttentionType
from src.models.rmat_rmat import RmatRmatModel
from src.models.rmat import RmatModel
from src.models.schnet_rmat import RMatSchNetModel
if __name__ == "__main__":
np.random.seed(0)
torch.random.manual_seed(0)
# batch[].target[].name
# configs = {
# "lr": [1e-5],
# "batch_size": [26],
# "model": ["RMatSchnet"],# ["RMatRMat"],#
# # "model": ["RMatRMat", "RMatSchnet", "RMat"],
# # "target": ["5HT1A"], # "CYP2C8", "5HT1A", "D2"
# "cross_attention_type": [CrossAttentionType.NONE],
# "targets": [
# ["binding_score", "IC50", "Ki"]
# ], # in ['Ki','IC50','binding_score']
# "activity_importance": [1.0],
# "thresholds": [
# {
# "binding_score": (-torch.inf, -9.0),
# "IC50": (-torch.inf, np.log10(900.0)),
# "Ki": (-torch.inf, np.log10(3000.0)),
# }
# ], # in ['Ki','IC50','binding_score']
# # "cross_attention_type": [CrossAttentionType.NONE, CrossAttentionType.LIGAND, CrossAttentionType.TARGET, CrossAttentionType.BOTH],
# }
"""
# Mateusz #1
configs = {
"lr": [1e-5],
"batch_size": [26],
"model": ["RMatRMat"],
"cross_attention_type": [CrossAttentionType.NONE],
"targets": [
["binding_score", "IC50", "Ki"]
],
"activity_importance": [1.0],
"thresholds": [
{
"binding_score": (-torch.inf, -9.0),
"IC50": (-torch.inf, np.log10(900.0)),
"Ki": (-torch.inf, np.log10(3000.0)),
}]
}
"""
# Mateusz #2
configs = {
"lr": [1e-5],
"batch_size": [26],
"model": ["RMatRMat"],
"cross_attention_type": [CrossAttentionType.BOTH],
"targets": [
["binding_score", "IC50", "Ki"]
],
"activity_importance": [1.0],
"thresholds": [
{
"binding_score": (-torch.inf, -9.0),
"IC50": (-torch.inf, np.log10(900.0)),
"Ki": (-torch.inf, np.log10(3000.0)),
}]
}
"""
# Adam/Michal #1
configs = {
"lr": [1e-5],
"batch_size": [26],
"model": ["RMatSchNet"],
"cross_attention_type": [CrossAttentionType.NONE],
"targets": [
["binding_score", "IC50", "Ki"]
],
"activity_importance": [1.0],
"thresholds": [
{
"binding_score": (-torch.inf, -9.0),
"IC50": (-torch.inf, np.log10(900.0)),
"Ki": (-torch.inf, np.log10(3000.0)),
}]
}
# Adam/Michal #2
configs = {
"lr": [1e-5],
"batch_size": [26],
"model": ["RMatSchNet"],
"cross_attention_type": [CrossAttentionType.BOTH],
"targets": [
["binding_score", "IC50", "Ki"]
],
"activity_importance": [1.0],
"thresholds": [
{
"binding_score": (-torch.inf, -9.0),
"IC50": (-torch.inf, np.log10(900.0)),
"Ki": (-torch.inf, np.log10(3000.0)),
}]
}
"""
# docking score that is related to the free energy of binding of a ligand to a receptor.
# For this type of docking score, the more negative the score, the better.
# Generalnie przyjmuje się, że Ki poniżej 1000 (jednostką są nM) determinuje aktywność
# consider IC50 of <100 nM to be active, 101 nM to 300 nM to be moderately active, and >300 nM to be inactive
configs = product(
*[zip([name] * len(values), values) for name, values in configs.items()]
)
for hyperparams in configs:
hyperparams = dict(hyperparams)
batch_size = hyperparams["batch_size"]
ligand_featurizer = RMatFeaturizer(use_bonds=False, cutout=False)
if hyperparams["model"] in ["RMatSchNet"]:
target_featurizer = SchnetFeaturizer(
# Michal
use_bonds=False, cutout=True, cutout_radius=10.0
# Adam
# use_bonds = False, cutout = False
)
elif hyperparams["model"] in ["RMatRMat"]:
target_featurizer = RMatFeaturizer(
use_bonds=False, cutout=True, cutout_radius=10.0
)
# TODO: report avg, min, max number of atoms after cutouts
wandb_logger = WandbLogger(
project="Drug Repositioning",
entity="drug_repositioning",
save_dir="logs",
tags=[],
reinit=True,
)
if hyperparams["model"] == "RMat":
pass
# datamodule = LigandActivityAndBindingDataModule(
# ligand_featurizer,
# target=hyperparams["target"],
# num_workers=0,
# batch_size=batch_size,
# )
else:
datamodule = LigandTargetActivityAndBindingDataModule(
ligand_featurizer,
target_featurizer,
num_workers=0,
batch_size=batch_size,
)
if hyperparams["model"] == "RMatRMat":
model = RmatRmatModel(
rmat_config=RMatConfig.get_default(use_bonds=False), **hyperparams
)
elif hyperparams["model"] == "RMatSchNet":
model = RMatSchNetModel(
rmat_config=RMatConfig.get_default(use_bonds=False), **hyperparams
)
elif hyperparams["model"] == "RMat":
model = RmatModel(
rmat_config=RMatConfig.get_default(use_bonds=False), **hyperparams
)
trainer = pl.Trainer(
max_epochs=16,
log_every_n_steps=1,
devices=1,
accelerator="auto",
precision=32,
logger=wandb_logger,
fast_dev_run=False,
# limit_train_batches=1,
# profiler="advanced",
)
trainer.fit(model=model, datamodule=datamodule)
wandb_logger.experiment.finish()
wandb_logger.finalize("success")