-
Notifications
You must be signed in to change notification settings - Fork 4
/
inference.py
229 lines (187 loc) · 6.66 KB
/
inference.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import argparse
import os
import sys
from collections import defaultdict
from typing import List, Optional
import molgrid
import numpy as np
import pandas as pd
import torch
from ignite.engine import Events
from ignite.handlers import Checkpoint
from gnina import metrics, models, setup, training, utils
from gnina.dataloaders import GriddedExamplesLoader
def options(args: Optional[List[str]] = None):
"""
Define options and parse arguments.
Parameters
----------
args: Optional[List[str]]
List of command line arguments
"""
parser = argparse.ArgumentParser(
description="Inference with GNINA scoring function",
)
parser.add_argument("input", type=str, help="Input file for inference")
parser.add_argument("model", type=str, help="Model")
parser.add_argument("checkpoint", type=str, help="Checkpoint file")
parser.add_argument(
"-d",
"--data_root",
type=str,
default="",
help="Root folder for relative paths in train files",
)
parser.add_argument(
"--rotations",
type=int,
default=1,
help="Number of rotations to average on",
)
parser.add_argument(
"-o", "--out_dir", type=str, default=os.getcwd(), help="Output directory"
)
parser.add_argument("-g", "--gpu", type=str, default="cuda:0", help="Device name")
parser.add_argument("-s", "--seed", type=int, default=None, help="Random seed")
# TODO: Retrieve the following parameters from the chekpoint file!
parser.add_argument(
"--label_pos", type=int, default=0, help="Pose label position in training file"
)
parser.add_argument(
"--affinity_pos",
type=int,
default=None,
help="Affinity value position in training file",
)
parser.add_argument(
"--ligmolcache",
type=str,
default="",
help=".molcache2 file for ligands",
)
parser.add_argument(
"--recmolcache",
type=str,
default="",
help=".molcache2 file for receptors",
)
parser.add_argument("--dimension", type=float, default=23.5, help="Grid dimension")
parser.add_argument("--resolution", type=float, default=0.5, help="Grid resolution")
parser.add_argument("--batch_size", type=int, default=64, help="Batch size")
# Misc
parser.add_argument("--silent", action="store_true", help="No console output")
parser.add_argument(
"--no_roc_auc",
action="store_false",
help="Disable ROC AUC (useful for crystal poses)",
dest="roc_auc",
)
parser.add_argument(
"--no_csv",
action="store_false",
help="Disable CSV output",
dest="csv",
)
return parser.parse_args(args)
def inference(args):
"""
Main function for inference with GNINA scoring function.
Parameters
----------
args:
"""
# Create necessary directories if not already present
os.makedirs(args.out_dir, exist_ok=True)
# Define output streams for logging
logfile = open(os.path.join(args.out_dir, "inference.log"), "w")
if not args.silent:
outstreams = [sys.stdout, logfile]
else:
outstreams = [logfile]
# Print command line arguments
for outstream in outstreams:
utils.print_args(args, "--- GNINA INFERENCE ---", stream=outstream)
# Set random seed for reproducibility
if args.seed is not None:
molgrid.set_random_seed(args.seed)
torch.manual_seed(args.seed)
np.random.seed(args.seed)
# Set device
device = utils.set_device(args.gpu)
# Create example providers
test_example_provider = setup.setup_example_provider(
args.input, args, training=False
)
# Create grid maker
grid_maker = setup.setup_grid_maker(args)
test_loader = GriddedExamplesLoader(
example_provider=test_example_provider,
grid_maker=grid_maker,
label_pos=args.label_pos,
affinity_pos=args.affinity_pos,
random_translation=0.0, # No random translations for inference
random_rotation=False, # No random rotations for inference
device=device,
)
affinity: bool = True if args.affinity_pos is not None else False
# Create model
model = models.models_dict[(args.model, affinity)](test_loader.dims).to(device)
# Compile model with TorchScript
model = torch.jit.script(model)
# Load checkpoint
checkpoint = torch.load(args.checkpoint, map_location=device)
Checkpoint.load_objects(to_load={"model": model}, checkpoint=checkpoint)
# TODO: Allow prediction for systems without known pose or affinity
# Setup metrics but do not compute losses
allmetrics = metrics.setup_metrics(
affinity,
pose_loss=None,
affinity_loss=None,
roc_auc=args.roc_auc,
device=device,
)
evaluator = training._setup_evaluator(model, allmetrics, affinity=affinity)
results = defaultdict(list)
# Print predictions for every batch
# evaluator.state.output only stores the last batch
@evaluator.on(Events.ITERATION_COMPLETED)
def print_output(evaluator):
output = evaluator.state.output
# Extract probability of good pose only
pose_pred = torch.exp(output["pose_log"])[:, -1]
assert pose_pred.shape == output["labels"].shape
results["pose_prob"] = np.concatenate(
(results["pose_prob"], pose_pred.cpu().numpy())
)
results["pose_label"] = np.concatenate(
(results["pose_label"], output["labels"].cpu().numpy())
)
try:
# This fails with KeyError if affinity is not present
assert output["affinities_pred"].shape == output["affinities"].shape
assert output["affinities_pred"].shape == output["labels"].shape
results["affinity_pred"] = np.concatenate(
(results["affinity_pred"], output["affinities_pred"].cpu().numpy())
)
# Return absolute binding affinity
# Experimental values are negative for a bad pose
results["affinity_exp"] = np.concatenate(
(results["affinity_exp"], np.abs(output["affinities"].cpu().numpy()))
)
except KeyError:
# No binding affinity prediction available
pass
evaluator.run(test_loader)
for outstream in outstreams:
utils.log_print(
evaluator.state.metrics,
stream=outstream,
)
df = pd.DataFrame(results)
if args.csv:
df.to_csv(os.path.join(args.out_dir, "inference.csv"), float_format="%.5f")
# Close log file
logfile.close()
if __name__ == "__main__":
args = options()
inference(args)