forked from broadinstitute/pyro-cov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutrans.py
executable file
·565 lines (481 loc) · 18.5 KB
/
mutrans.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# Copyright Contributors to the Pyro-Cov project.
# SPDX-License-Identifier: Apache-2.0
import argparse
import functools
import gc
import logging
import os
import re
from typing import Callable, Union
import pyro
import torch
from pyrocov import mutrans, pangolin, sarscov2
from pyrocov.util import torch_map
logger = logging.getLogger(__name__)
logging.basicConfig(format="%(relativeCreated) 9d %(message)s", level=logging.INFO)
def cached(filename: Union[str, Callable]):
"""
Simple utiltity to cache results based on filename.
"""
def decorator(fn):
@functools.wraps(fn)
def cached_fn(*args, **kwargs):
base_args = args[0]
if base_args.no_cache:
return fn(*args, **kwargs)
f = filename(*args, **kwargs) if callable(filename) else filename
if os.path.exists(f) and not base_args.force:
logger.info(f"loading cached {f}")
return torch.load(f, map_location=torch.empty(()).device)
if base_args.no_new:
raise ValueError(f"Missing {f}")
result = fn(*args, **kwargs)
if not args[0].test:
logger.info(f"saving {f}")
torch.save(result, f)
return result
return cached_fn
return decorator
def _safe_str(v):
v = str(v)
v = re.sub("[^A-Za-x0-9-]", "_", v)
return v
def holdout_to_hashable(holdout):
return tuple((k, tuple(sorted(v.items()))) for k, v in sorted(holdout.items()))
def hashable_to_holdout(holdout):
return {k: dict(v) for k, v in holdout}
def _load_data_filename(args, **kwargs):
parts = ["data", "double" if args.double else "single"]
for k, v in sorted(kwargs.get("include", {}).items()):
parts.append(f"I{k}={_safe_str(v)}")
for k, v in sorted(kwargs.get("exclude", {}).items()):
parts.append(f"E{k}={_safe_str(v)}")
parts.append(str(kwargs.get("end_day")))
return "results/mutrans.{}.pt".format(".".join(parts))
@cached(_load_data_filename)
def load_data(args, **kwargs):
"""
Cached wrapper to load GISAID data.
"""
return mutrans.load_gisaid_data(device=args.device, **kwargs)
def _fit_filename(name, *args):
strs = [name]
for arg in args[2:]:
if isinstance(arg, tuple):
strs.append("-".join(f"{k}={_safe_str(v)}" for k, v in arg))
else:
strs.append(str(arg))
return "results/mutrans.{}.pt".format(".".join(strs))
@cached(lambda *args: _fit_filename("svi", *args))
def fit_svi(
args,
dataset,
cond_data="",
model_type="sparse-skip-reparam",
guide_type="mvn_dependent",
n=1001,
lr=0.01,
lrd=0.1,
cn=10.0,
r=200,
f=6,
end_day=None,
holdout=(),
):
"""
Cached wrapper to fit a model via SVI.
"""
cond_data = [kv.split("=") for kv in cond_data.split(",") if kv]
cond_data = {k: float(v) for k, v in cond_data}
holdout = hashable_to_holdout(holdout)
result = mutrans.fit_svi(
dataset,
cond_data=cond_data,
model_type=model_type,
guide_type=guide_type,
num_steps=n,
learning_rate=lr,
learning_rate_decay=lrd,
clip_norm=cn,
rank=r,
forecast_steps=f,
log_every=args.log_every,
seed=args.seed,
jit=args.jit,
num_samples=args.num_samples,
)
if "lineage" in holdout.get("exclude", {}):
# Save only what's needed to evaluate loo predictions.
result = {
"median": {
"coef": result["median"]["coef"].float(), # [F]
"rate_loc": result["median"]["rate_loc"].float(), # [S]
},
}
result["args"] = args
return result
def backtesting(args, default_config):
configs = []
empty_holdout = ()
for max_day in args.backtesting_max_day.split(","):
max_day = int(max_day)
configs.append(
(
args.cond_data,
args.model_type,
args.guide_type,
args.num_steps,
args.learning_rate,
args.learning_rate_decay,
args.clip_norm,
args.rank,
args.forecast_steps,
max_day,
empty_holdout,
)
)
# Sequentially fit models.
results = {}
for config in configs:
logger.info(f"Config: {config}")
# Holdout is the last in the config
holdout = hashable_to_holdout(config[-1])
# end_day is second from last
end_day = config[-2]
# load dataset
dataset = load_data(args, end_day=end_day, **holdout)
# Run SVI
result = fit_svi(args, dataset, *config)
mutrans.log_stats(dataset, result)
# Save the results for this config
# Augment gisaid dataset with JHU timeseries counts
dataset.update(mutrans.load_jhu_data(dataset))
# Generate results
result["mutations"] = dataset["mutations"]
result["weekly_strains"] = dataset["weekly_strains"]
result["weekly_cases"] = dataset["weekly_cases"]
result["weekly_strains_shape"] = tuple(dataset["weekly_strains"].shape)
result["location_id"] = dataset["location_id"]
result["lineage_id_inv"] = dataset["lineage_id_inv"]
result = torch_map(result, device="cpu", dtype=torch.float) # to save space
results[config] = result
# Ensure number of regions match
assert dataset["weekly_strains"].shape[1] == result["mean"]["probs"].shape[1]
assert dataset["weekly_cases"].shape[1] == result["mean"]["probs"].shape[1]
# Cleanup
del dataset
pyro.clear_param_store()
gc.collect()
if args.vary_holdout:
mutrans.log_holdout_stats({k[-1]: v for k, v in results.items()})
if not args.test:
logger.info("saving results/mutrans.backtesting.pt")
torch.save(results, "results/mutrans.backtesting.pt")
def vary_leaves(args, default_config):
"""
Run a leave-one-out experiment over a set of leaf lineages, saving results
to ``results/mutrans.vary_leaves.pt``.
"""
# Load a single common dataset.
dataset = load_data(args)
lineage_id = {name: i for i, name in enumerate(dataset["lineage_id_inv"])}
descendents = pangolin.find_descendents(dataset["lineage_id_inv"])
if args.only_gene:
for m in dataset["mutations"]:
assert m.startswith(args.only_gene + ":"), m
# Run default config to get a ranking of leaves.
def make_config(**holdout):
if args.only_gene:
include = holdout.setdefault("include", {})
include["gene"] = args.only_gene
config = list(default_config)
config[-1] = holdout_to_hashable(holdout)
config = tuple(config)
return config
result = fit_svi(args, dataset, *make_config())
# Rank lineages by divergence from parent.
lineages = mutrans.rank_loo_lineages(dataset, result)
lineages = lineages[: args.vary_leaves]
logger.info(
"\n".join(
[f"Leave-one-out predicting growth rate of {len(lineages)} lineages:"]
+ lineages
)
)
# Run inference for each lineage. This is very expensive.
results = {}
for lineage in lineages:
config = make_config(exclude={"lineage": "^" + lineage + "$"})
logger.info(f"Config: {config}")
# Construct a leave-one-out dataset by zeroing out a subclade.
clade = [lineage_id[lineage]]
for descendent in descendents[lineage]:
clade.append(lineage_id[descendent])
loo_dataset = dataset.copy()
loo_dataset["weekly_strains"] = dataset["weekly_strains"].clone()
loo_dataset["weekly_strains"][:, :, clade] = 0
# Run SVI
result = fit_svi(args, loo_dataset, *config)
result["mutations"] = dataset["mutations"]
result["location_id"] = dataset["location_id"]
result["lineage_id_inv"] = dataset["lineage_id_inv"]
results[config] = result
# Cleanup
del result
pyro.clear_param_store()
gc.collect()
if not args.test:
logger.info("saving results/mutrans.vary_leaves.pt")
torch.save(results, "results/mutrans.vary_leaves.pt")
def vary_gene(args, default_config):
"""
Train on the whole genome and on various single genes, saving results to
``results/mutrans.vary_gene.pt``.
"""
# Collect a set of genes.
mutations = load_data(args)["mutations"]
genes = sorted({m.split(":")[0] for m in mutations})
logger.info("Fitting to each of genes: {}".format(", ".join(genes)))
# Construct a grid of holdouts.
grid = [{}, {"exclude": {"gene": ".*"}}] # full and empty sets of genes
for gene in genes:
grid.append({"include": {"gene": f"^{gene}:"}})
grid.append({"exclude": {"gene": f"^{gene}:"}})
def make_config(**holdout):
config = list(default_config)
config[-1] = holdout_to_hashable(holdout)
config = tuple(config)
return config
results = {}
for holdout in grid:
# Fit a single model.
logger.info(f"Holdout: {holdout}")
dataset = load_data(args, **holdout)
result = fit_svi(args, dataset, *make_config(**holdout))
# Save metrics.
key = holdout_to_hashable(holdout)
results[key] = mutrans.log_stats(dataset, result)
# Clean up to save memory.
del dataset, result
pyro.clear_param_store()
gc.collect()
if not args.test:
logger.info("saving results/mutrans.vary_gene.pt")
torch.save(results, "results/mutrans.vary_gene.pt")
def vary_nsp(args, default_config):
"""
Train on ORF1 and on various single nsps, saving results to
``results/mutrans.vary_nsp.pt``.
"""
# Construct a grid of holdouts, including full ORF1, empty, and each nsp.
grid = [{"include": {"gene": "^ORF1[ab]:"}}, {"exclude": {"gene": ".*"}}]
for gene in ["ORF1a", "ORF1b"]:
for nsp in sarscov2.GENE_STRUCTURE[gene]:
grid.append({"include": {"region": (gene, nsp)}})
def make_config(**holdout):
config = list(default_config)
config[-1] = holdout_to_hashable(holdout)
config = tuple(config)
return config
results = {}
for holdout in grid:
# Fit a single model.
logger.info(f"Holdout: {holdout}")
dataset = load_data(args, **holdout)
result = fit_svi(args, dataset, *make_config(**holdout))
# Save metrics.
key = holdout_to_hashable(holdout)
results[key] = mutrans.log_stats(dataset, result)
# Clean up to save memory.
del dataset, result
pyro.clear_param_store()
gc.collect()
if not args.test:
logger.info("saving results/mutrans.vary_nsp.pt")
torch.save(results, "results/mutrans.vary_nsp.pt")
def main(args):
"""Main Entry Point"""
# Torch configuration
torch.set_default_dtype(torch.double if args.double else torch.float)
if args.cuda:
torch.set_default_tensor_type(
torch.cuda.DoubleTensor if args.double else torch.cuda.FloatTensor
)
if args.debug:
torch.autograd.set_detect_anomaly(True)
# Configure fits.
configs = []
empty_holdout = ()
empty_end_day = None
default_config = (
args.cond_data,
args.model_type,
args.guide_type,
args.num_steps,
args.learning_rate,
args.learning_rate_decay,
args.clip_norm,
args.rank,
args.forecast_steps,
empty_end_day,
empty_holdout,
)
if args.vary_leaves:
return vary_leaves(args, default_config)
if args.vary_gene:
return vary_gene(args, default_config)
if args.vary_nsp:
return vary_nsp(args, default_config)
if args.vary_num_steps:
grid = sorted(int(n) for n in args.vary_num_steps.split(","))
for num_steps in grid:
configs.append()
elif args.vary_model_type:
for model_type in args.vary_model_type.split(","):
configs.append(
(
args.cond_data,
model_type,
args.guide_type,
args.num_steps,
args.learning_rate,
args.learning_rate_decay,
args.clip_norm,
args.rank,
args.forecast_steps,
empty_end_day,
empty_holdout,
)
)
elif args.vary_guide_type:
for guide_type in args.vary_guide_type.split(","):
configs.append(
(
args.cond_data,
args.model_type,
guide_type,
args.num_steps,
args.learning_rate,
args.learning_rate_decay,
args.clip_norm,
args.rank,
args.forecast_steps,
empty_end_day,
empty_holdout,
)
)
elif args.vary_holdout:
grid = [
{},
{"include": {"location": "^Europe"}},
{"exclude": {"location": "^Europe"}},
# {"include": {"location": "^North America"}},
# {"exclude": {"location": "^North America"}},
# {"include": {"location": "^North America / USA"}},
# {"exclude": {"location": "^North America / USA"}},
# {"include": {"location": "^Europe / United Kingdom"}},
# {"exclude": {"location": "^Europe / United Kingdom"}},
# {"include": {"virus_name": "^hCoV-19/USA/..-CDC-"}},
# {"include": {"virus_name": "^hCoV-19/USA/..-CDC-2-"}},
]
for holdout in grid:
if args.only_gene:
include = holdout.setdefault("include", {})
include["gene"] = f"^{args.only_gene}:"
configs.append(
(
args.cond_data,
args.model_type,
args.guide_type,
args.num_steps,
args.learning_rate,
args.learning_rate_decay,
args.clip_norm,
args.rank,
args.forecast_steps,
empty_end_day,
holdout_to_hashable(holdout),
)
)
elif args.backtesting_max_day:
backtesting(args, default_config)
else:
configs.append(default_config)
# Sequentially fit models.
results = {}
for config in configs:
logger.info(f"Config: {config}")
# Holdout is the last in the config
holdout = hashable_to_holdout(config[-1])
# end_day is second from last
end_day = config[-2]
# load dataset
dataset = load_data(args, end_day=end_day, **holdout)
# Run SVI
result = fit_svi(args, dataset, *config)
mutrans.log_stats(dataset, result)
# Save the results for this config
# Augment gisaid dataset with JHU timeseries counts
dataset.update(mutrans.load_jhu_data(dataset))
# Generate results
result["mutations"] = dataset["mutations"]
result["weekly_strains"] = dataset["weekly_strains"]
result["weekly_cases"] = dataset["weekly_cases"]
result["weekly_strains_shape"] = tuple(dataset["weekly_strains"].shape)
result["location_id"] = dataset["location_id"]
result["lineage_id_inv"] = dataset["lineage_id_inv"]
result = torch_map(result, device="cpu", dtype=torch.float) # to save space
results[config] = result
# Ensure number of regions match
assert dataset["weekly_strains"].shape[1] == result["mean"]["probs"].shape[1]
assert dataset["weekly_cases"].shape[1] == result["mean"]["probs"].shape[1]
# Cleanup
del dataset
pyro.clear_param_store()
gc.collect()
if args.vary_holdout:
mutrans.log_holdout_stats({k[-1]: v for k, v in results.items()})
if not args.test:
logger.info("saving results/mutrans.pt")
torch.save(results, "results/mutrans.pt")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fit mutation-transmissibility models")
parser.add_argument("--vary-model-type", help="comma delimited list of model types")
parser.add_argument("--vary-guide-type", help="comma delimited list of guide types")
parser.add_argument("--vary-num-steps", help="comma delimited list of num_steps")
parser.add_argument("--vary-holdout", action="store_true")
parser.add_argument(
"--vary-leaves", type=int, help="min number of samples per held out lineage"
)
parser.add_argument("--vary-gene", action="store_true")
parser.add_argument("--vary-nsp", action="store_true")
parser.add_argument("--only-gene")
parser.add_argument("-cd", "--cond-data", default="coef_scale=0.5")
parser.add_argument("-m", "--model-type", default="sparse-skip-reparam")
parser.add_argument("-g", "--guide-type", default="custom")
parser.add_argument("-n", "--num-steps", default=10001, type=int)
parser.add_argument("-s", "--num-samples", default=1000, type=int)
parser.add_argument("-lr", "--learning-rate", default=0.05, type=float)
parser.add_argument("-lrd", "--learning-rate-decay", default=0.1, type=float)
parser.add_argument("-cn", "--clip-norm", default=10.0, type=float)
parser.add_argument("-r", "--rank", default=200, type=int)
parser.add_argument("-f", "--forecast-steps", default=6, type=int)
parser.add_argument("-fp64", "--double", action="store_true")
parser.add_argument("-fp32", "--float", action="store_false", dest="double")
parser.add_argument(
"--cuda", action="store_true", default=torch.cuda.is_available()
)
parser.add_argument("-b", "--backtesting-max-day", default=None)
parser.add_argument("--cpu", dest="cuda", action="store_false")
parser.add_argument("--jit", action="store_true", default=False)
parser.add_argument("--no-jit", dest="jit", action="store_false")
parser.add_argument("--seed", default=20210319, type=int)
parser.add_argument("-l", "--log-every", default=50, type=int)
parser.add_argument("--no-new", action="store_true")
parser.add_argument("--no-cache", action="store_true")
parser.add_argument("--force", action="store_true")
parser.add_argument("--test", action="store_true")
parser.add_argument("--debug", action="store_true")
args = parser.parse_args()
args.device = "cuda" if args.cuda else "cpu"
main(args)