predict.py 3.46 KB
Newer Older
1
2
"""Run predictions for COVID-19 model"""

3
4
import numpy as np
import xarray
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pickle as pkl
import tensorflow as tf

from covid import model_spec
from gemlib.util import compute_state


def predicted_incidence(posterior_samples, covar_data, init_step, num_steps):
    """Runs the simulation forward in time from `init_state` at time `init_time`
       for `num_steps`.
    :param param: a dictionary of model parameters
    :covar_data: a dictionary of model covariate data
    :param init_step: the initial time step
    :param num_steps: the number of steps to simulate
    :returns: a tensor of srt_quhape [B, M, num_steps, X] where X is the number of state
              transitions
    """

Chris Jewell's avatar
Chris Jewell committed
23
    @tf.function
24
    def sim_fn(args):
Chris Jewell's avatar
Chris Jewell committed
25
        beta1_, beta2_, sigma_, xi_, gamma0_, gamma1_, init_ = args
26
27
28
29

        par = dict(
            beta1=beta1_,
            beta2=beta2_,
30
            sigma=sigma_,
31
32
33
34
35
36
37
38
39
40
41
42
43
44
            xi=xi_,
            gamma0=gamma0_,
            gamma1=gamma1_,
        )
        model = model_spec.CovidUK(
            covar_data,
            initial_state=init_,
            initial_step=init_step,
            num_steps=num_steps,
        )
        sim = model.sample(**par)
        return sim["seir"]

    posterior_state = compute_state(
45
        posterior_samples["init_state"],
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
        posterior_samples["seir"],
        model_spec.STOICHIOMETRY,
    )
    init_state = posterior_state[..., init_step, :]

    events = tf.map_fn(
        sim_fn,
        elems=(
            posterior_samples["beta1"],
            posterior_samples["beta2"],
            posterior_samples["sigma"],
            posterior_samples["xi"],
            posterior_samples["gamma0"],
            posterior_samples["gamma1"],
            init_state,
        ),
        fn_output_signature=(tf.float64),
    )
    return events


def read_pkl(filename):
    with open(filename, "rb") as f:
        return pkl.load(f)


def predict(data, posterior_samples, output_file, initial_step, num_steps):

    covar_data = read_pkl(data)
    samples = read_pkl(posterior_samples)

    if initial_step < 0:
78
79
80
        initial_step = samples["seir"].shape[-2] + initial_step

    del covar_data["date_range"]
81
82
83
84
85

    prediction = predicted_incidence(
        samples, covar_data, initial_step, num_steps
    )

86
87
88
89
90
91
92
93
94
95
    prediction = xarray.DataArray(
        prediction,
        coords=[
            np.arange(prediction.shape[0]),
            covar_data["locations"]["lad19cd"],
            np.arange(prediction.shape[2]),
            np.arange(prediction.shape[3]),
        ],
        dims=("iteration", "location", "time", "event"),
    )
96
    with open(output_file, "wb") as f:
97
        pkl.dump(prediction, f)
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


if __name__ == "__main__":

    from argparse import ArgumentParser

    parser = ArgumentParser()
    parser.add_argument(
        "-i", "--initial-step", type=int, default=0, description="Initial step"
    )
    parser.add_argument(
        "-n", "--num-steps", type=int, default=1, description="Number of steps"
    )
    parser.add_argument(
        "data_pkl", type=str, description="Covariate data pickle"
    )
    parser.add_argument(
        "posterior_samples_pkl",
        type=str,
        description="Posterior samples pickle",
    )
    parser.add_argument(
        "output_file",
        type=str,
        description="Output pkl file",
    )
    args = parser.parse_args()

    predict(
        args.data_pkl,
        args.posterior_samples_pkl,
        args.output_file,
        args.initial_step,
        args.num_steps,
    )