covid_stochastic.py 6.45 KB
Newer Older
1
2
import optparse
import time
3
import pickle as pkl
4
5

import tensorflow as tf
6
import tensorflow_probability as tfp
Chris Jewell's avatar
Chris Jewell committed
7

8
import numpy as np
9
10
11
import matplotlib.pyplot as plt
import yaml

12
from covid.model import CovidUKStochastic, load_data
13
14
from covid.util import sanitise_parameter, sanitise_settings, seed_areas

15
16
17
18
19

tfd = tfp.distributions
tfb = tfp.bijectors


20
21
DTYPE = np.float64

Chris Jewell's avatar
Chris Jewell committed
22

23
24
def random_walk_mvnorm_fn(covariance, name=None):
    """Returns callable that adds Multivariate Normal noise to the input"""
Chris Jewell's avatar
Chris Jewell committed
25
    covariance = covariance + tf.eye(covariance.shape[0], dtype=tf.float64) * 1.0e-9
26
    scale_tril = tf.linalg.cholesky(covariance)
Chris Jewell's avatar
Chris Jewell committed
27
28
29
    rv = tfp.distributions.MultivariateNormalTriL(
        loc=tf.zeros(covariance.shape[0], dtype=tf.float64), scale_tril=scale_tril
    )
30
31

    def _fn(state_parts, seed):
Chris Jewell's avatar
Chris Jewell committed
32
        with tf.name_scope(name or "random_walk_mvnorm_fn"):
33
34
35
36
37
38
            new_state_parts = [rv.sample() + state_part for state_part in state_parts]
            return new_state_parts

    return _fn


39
40
41
42
43
44
45
46
def sum_age_groups(sim):
    infec = sim[:, 2, :]
    infec = infec.reshape([infec.shape[0], 152, 17])
    infec_uk = infec.sum(axis=2)
    return infec_uk


def sum_la(sim):
Chris Jewell's avatar
Chris Jewell committed
47
    infec = sim[:, :, 2]
48
    infec = infec.reshape([infec.shape[0], 149])
49
50
51
52
53
54
55
56
57
58
    infec_uk = infec.sum(axis=1)
    return infec_uk


def sum_total_removals(sim):
    remove = sim[:, 3, :]
    return remove.sum(axis=1)


def final_size(sim):
Chris Jewell's avatar
Chris Jewell committed
59
    remove = sim[:, :, 3]
60
61
62
63
64
65
66
67
68
    remove = remove.reshape([remove.shape[0], 152, 17])
    fs = remove[-1, :, :].sum(axis=0)
    return fs


def plot_total_curve(sim):
    infec_uk = sum_la(sim)
    infec_uk = infec_uk.sum(axis=1)
    removals = sum_total_removals(sim)
Chris Jewell's avatar
Chris Jewell committed
69
70
71
72
73
74
    times = np.datetime64("2020-02-20") + np.arange(removals.shape[0])
    plt.plot(times, infec_uk, "r-", label="Infected")
    plt.plot(times, removals, "b-", label="Removed")
    plt.title("UK total cases")
    plt.xlabel("Date")
    plt.ylabel("Num infected or removed")
75
76
77
78
79
80
81
    plt.grid()
    plt.legend()


def plot_infec_curve(ax, sim, label):
    infec_uk = sum_la(sim)
    infec_uk = infec_uk.sum(axis=1)
Chris Jewell's avatar
Chris Jewell committed
82
83
    times = np.datetime64("2020-02-20") + np.arange(infec_uk.shape[0])
    ax.plot(times, infec_uk, "-", label=label)
84
85


Chris Jewell's avatar
Chris Jewell committed
86
def plot_by_age(sim, labels, t0=np.datetime64("2020-02-20"), ax=None):
87
88
89
90
91
    if ax is None:
        ax = plt.figure().gca()
    infec_uk = sum_la(sim)
    total_uk = infec_uk.mean(axis=1)
    t = t0 + np.arange(infec_uk.shape[0])
Chris Jewell's avatar
Chris Jewell committed
92
    colours = plt.cm.viridis(np.linspace(0.0, 1.0, infec_uk.shape[1]))
93
    for i in range(infec_uk.shape[1]):
Chris Jewell's avatar
Chris Jewell committed
94
95
        ax.plot(t, infec_uk[:, i], "r-", alpha=0.4, color=colours[i], label=labels[i])
    ax.plot(t, total_uk, "-", color="black", label="Mean")
96
97
98
    return ax


Chris Jewell's avatar
Chris Jewell committed
99
def plot_by_la(sim, labels, t0=np.datetime64("2020-02-20"), ax=None):
100
101
102
103
104
    if ax is None:
        ax = plt.figure().gca()
    infec_uk = sum_age_groups(sim)
    total_uk = infec_uk.mean(axis=1)
    t = t0 + np.arange(infec_uk.shape[0])
Chris Jewell's avatar
Chris Jewell committed
105
    colours = plt.cm.viridis(np.linspace(0.0, 1.0, infec_uk.shape[1]))
106
    for i in range(infec_uk.shape[1]):
Chris Jewell's avatar
Chris Jewell committed
107
108
        ax.plot(t, infec_uk[:, i], "r-", alpha=0.4, color=colours[i], label=labels[i])
    ax.plot(t, total_uk, "-", color="black", label="Mean")
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    return ax


def draw_figs(sim, N):
    # Attack rate
    N = N.reshape([152, 17]).sum(axis=0)
    fs = final_size(sim)
    attack_rate = fs / N
    print("Attack rate:", attack_rate)
    print("Overall attack rate: ", np.sum(fs) / np.sum(N))

    # Total UK epidemic curve
    plot_total_curve(sim)
    plt.xticks(rotation=45, horizontalalignment="right")
Chris Jewell's avatar
Chris Jewell committed
123
    plt.savefig("total_uk_curve.pdf")
124
125
126
127
    plt.show()

    # TotalUK epidemic curve by age-group
    fig, ax = plt.subplots(1, 2, figsize=[24, 12])
Chris Jewell's avatar
Chris Jewell committed
128
129
    plot_by_la(sim, data["la_names"], ax=ax[0])
    plot_by_age(sim, data["age_groups"], ax=ax[1])
130
131
132
    ax[1].legend()
    plt.xticks(rotation=45, horizontalalignment="right")
    fig.autofmt_xdate()
Chris Jewell's avatar
Chris Jewell committed
133
    plt.savefig("la_age_infec_curves.pdf")
134
135
136
137
    plt.show()

    # Plot attack rate
    plt.figure(figsize=[4, 2])
Chris Jewell's avatar
Chris Jewell committed
138
    plt.plot(data["age_groups"], attack_rate, "o-")
139
    plt.xticks(rotation=90)
Chris Jewell's avatar
Chris Jewell committed
140
141
    plt.title("Age-specific attack rate")
    plt.savefig("age_attack_rate.pdf")
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    plt.show()


def doubling_time(t, sim, t1, t2):
    t1 = np.where(t == np.datetime64(t1))[0]
    t2 = np.where(t == np.datetime64(t2))[0]
    delta = t2 - t1
    r = sum_total_removals(sim)
    q1 = r[t1]
    q2 = r[t2]
    return delta * np.log(2) / np.log(q2 / q1)


def plot_age_attack_rate(ax, sim, N, label):
    Ns = N.reshape([152, 17]).sum(axis=0)
    fs = final_size(sim.numpy())
    attack_rate = fs / Ns
Chris Jewell's avatar
Chris Jewell committed
159
    ax.plot(data["age_groups"], attack_rate, "o-", label=label)
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
# parser = optparse.OptionParser()
# parser.add_option(
#     "--config",
#     "-c",
#     dest="config",
#     default="ode_config.yaml",
#     help="configuration file",
# )
# options, args = parser.parse_args([])
with open("ode_config.yaml", "r") as ymlfile:
    config = yaml.load(ymlfile)

param = sanitise_parameter(config["parameter"])
settings = sanitise_settings(config["settings"])

data = load_data(config["data"], settings, DTYPE)
data["pop"] = data["pop"].sum(level=0)

model = CovidUKStochastic(
    C=data["C"],
    N=data["pop"]["n"].to_numpy(),
    W=data["W"],
    date_range=settings["prediction_period"],
    holidays=settings["holiday"],
    lockdown=settings["lockdown"],
    time_step=1.0,
)

# seeding = seed_areas(data['pop']['n'].to_numpy(), data['pop']['Area.name.2'])  # Seed 40-44 age group, 30 seeds by popn size
# seeding = tf.one_hot(tf.squeeze(tf.where(data['pop'].index=='E09000008')), depth=data['pop'].size, dtype=DTYPE)
seeding = tf.one_hot(58, depth=model.N.shape[0], dtype=DTYPE)  # Manchester
state_init = model.create_initial_state(init_matrix=seeding)

start = time.perf_counter()
t, sim = model.simulate(param, state_init)
end = time.perf_counter()
print(f"Run 1 Complete in {end - start} seconds")

start = time.perf_counter()
for i in range(1):
    t, upd = model.simulate(param, state_init)
end = time.perf_counter()
print(f"Run 2 Complete in {(end - start)/1.} seconds")

# Plotting functions
fig_uk = plt.figure()
sim = tf.reduce_sum(upd, axis=-2)

# plot_age_attack_rate(fig_attack.gca(), sim, data['pop']['n'].to_numpy(), "Attack Rate")
# fig_attack.suptitle("Attack Rate")
# plot_infec_curve(fig_uk.gca(), sim.numpy(), "Infections")
fig_uk.gca().plot(sim[:, :, 2])
fig_uk.suptitle("UK Infections")

fig_uk.autofmt_xdate()
fig_uk.gca().grid(True)
plt.show()

with open("stochastic_sim_covid1.pkl", "wb") as f:
    pkl.dump({"events": upd.numpy(), "state_init": state_init.numpy()}, f)