-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_simulations.py
344 lines (278 loc) · 9.69 KB
/
run_simulations.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
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
from ..risk_simulation.covid_simulation import CovidSimulation
from ..plotting_utils.plotting_utils import add_caption
from ..utils.utils import get_percent
DEFAULT_CONFIG = {
'infection_to_detectable_delay': 0,
'beta': 0.2,
'gamma': 0.07,
'Q_duration': 14,
'I_initial': 5,
'N': 200,
'num_days': 130,
'external_infection_rate': 0.001,
}
def run_simulations(config, niter=100):
all_state_counts = []
all_cumulative_infections = []
for i in range(niter):
state_counts, cumulative_infections, _, _ = (
CovidSimulation(**config).run_simulation()
)
all_state_counts.append(state_counts)
all_cumulative_infections.append(cumulative_infections)
return {
'state_counts': pd.concat(all_state_counts).groupby(level=0).mean(),
'cumulative_infections': (
pd.concat(all_cumulative_infections).groupby(level=0).mean()
),
}
def simulate_pcr_vs_antigen(config):
config = {**config, **DEFAULT_CONFIG}
config_types = [
'No testing',
'PCR every 4 days',
'Antigen every 4 days',
'No intra-office infections',
]
configs = [config.copy() for _ in range(len(config_types))]
# No testing parameters
configs[0]['testing_interval'] = None
# PCR parameters
configs[1]['testing_delay'] = 5
configs[1]['sensitivity'] = 0.98
# Antigen parameters
configs[2]['testing_delay'] = 0
configs[2]['sensitivity'] = 0.75
# No intra-office infections parameters
configs[3]['beta'] = 0
return {
info: run_simulations(config) for info, config in zip(
config_types, configs
)
}
def simulate_pcr_vs_antigen_weekly(config):
config = {**config, **DEFAULT_CONFIG}
config_types = [
'40% Antigen weekly',
'75% Antigen weekly',
'40% Antigen twice weekly',
'75% Antigen twice weekly',
'98% PCR weekly',
]
configs = [config.copy() for _ in range(len(config_types))]
# Antigen parameters
configs[0]['testing_delay'] = 0
configs[0]['sensitivity'] = 0.4 # 0.75
configs[0]['testing_interval'] = 7
# Antigen parameters
configs[1]['testing_delay'] = 0
configs[1]['sensitivity'] = 0.75 # 0.75
configs[1]['testing_interval'] = 7
# Antigen parameters
configs[2]['testing_delay'] = 0
configs[2]['sensitivity'] = 0.4 # 0.75
configs[2]['testing_interval'] = 3.5
# Antigen parameters
configs[3]['testing_delay'] = 0
configs[3]['sensitivity'] = 0.75 # 0.75
configs[3]['testing_interval'] = 3.5
cadences = [None, None, None, None, 7]
# PCR parameters
for i in range(4, len(config_types)):
configs[i]['testing_delay'] = 5
configs[i]['sensitivity'] = 0.98
configs[i]['testing_interval'] = cadences[i]
return {
info: run_simulations(config) for info, config in zip(
config_types, configs
)
}
def simulate_testing_candence(config):
config = {**config, **DEFAULT_CONFIG}
config_types = [
'No testing',
'Testing every 2 weeks',
'Testing every week',
'Testing every 4 days',
'Testing every 3 days',
'Testing every 2 days',
'Testing everyday',
]
configs = [config.copy() for _ in range(len(config_types))]
for i, value in enumerate([None, 14, 7, 4, 3, 2, 1]):
configs[i]['testing_interval'] = value
return {
info: run_simulations(config) for info, config in zip(
config_types, configs
)
}
def quantify_difference(cumulative_infs, result):
last_ind = len(cumulative_infs) - 1
percent = get_percent(cumulative_infs[last_ind], DEFAULT_CONFIG['N'])
print(
f'For {result}, {percent:.2f}% of individuals were infected at the end '
'of the n-days'
)
def simulate_testing_process(config, include_asy=False):
config = {**config, **DEFAULT_CONFIG}
config_types = ['Random selection', 'Symptomatic first']
if include_asy:
config_types += ['Asymptomatic first']
configs = [config.copy() for _ in range(len(config_types))]
configs[0]['testing_process'] = 'random'
configs[1]['testing_process'] = 'sym_first'
if include_asy:
configs[2]['testing_process'] = 'asy_first'
return {
info: run_simulations(config) for info, config in zip(
config_types, configs
)
}
def simulate_risk_behavior(config):
config = {**config, **DEFAULT_CONFIG}
config_types = [
'0% with symptoms stay home',
'25% with symptoms stay home',
'50% with symptoms stay home',
'75% with symptoms stay home',
'100% with symptoms stay home',
]
configs = [config.copy() for _ in range(len(config_types))]
configs[0]['risk_behavior'] = 0.0
configs[1]['risk_behavior'] = 0.25
configs[2]['risk_behavior'] = 0.5
configs[3]['risk_behavior'] = 0.75
configs[4]['risk_behavior'] = 1.0
return {
info: run_simulations(config) for info, config in zip(
config_types, configs
)
}
def plot_cumulative_infections(results):
colors = [
'black', 'coral', 'royalblue', 'orange', 'gray', 'darkred', 'purple'
]
for result, color in zip(results, colors):
cumulative_infs = results[result]['cumulative_infections']
quantify_difference(cumulative_infs, result)
plt.plot(cumulative_infs, color=color, alpha=1.0, label=result,)
plt.xlabel('Day', fontsize=14)
plt.ylabel('Cumulative infections', fontsize=14)
sns.despine()
plt.legend()
def get_config_info(config, keys):
return ', '.join([f'{x}={config[x]}' for x in keys])
def save_plot(filename):
plt.tight_layout()
plt.savefig(f'plots/simulations/{filename}', facecolor='white')
plt.close()
def run_all_simulations(sym_pos_rate=0.65):
default_config = {
'sym_pos_rate': sym_pos_rate,
'testing_interval': 4,
'testing_process': 'random',
'risk_behavior': 0,
}
sr_str = int(sym_pos_rate * 100) # to be used for filenames
'''
###############################
### PCR vs Antigen ############
###############################
for rb in [0.4]: # [0.0, 0.4, 0.5, 0.6, 0.9]:
print(f'PCR vs Antigen ---- {rb}')
config_1 = {
**default_config, **{'testing_delay': 3, 'sensitivity': 0.8,},
}
config_1['risk_behavior'] = rb
results = simulate_pcr_vs_antigen(config_1)
plot_cumulative_infections(results)
caption = get_config_info(
config_1, ['testing_interval', 'testing_delay', 'sensitivity'],
)
if rb > 0.0:
plt.title(f'Risk Behavior={rb}')
add_caption(caption, locx=0.1, locy=-0.0)
save_plot(f'pcr_vs_antigen_{int(rb * 100)}_{sr_str}_sr')
plt.close()
print('------------------------------------')
'''
###############################
### PCR vs Antigen # 2 ########
###############################
config_1 = default_config
results = simulate_pcr_vs_antigen_weekly(config_1)
plot_cumulative_infections(results)
save_plot(f'pcr_vs_antigen_weekly_{sr_str}_sr')
plt.close()
print('------------------------------------')
'''
###############################
### Testing Cadence ###########
###############################
print('Testing Cadence')
config_1 = {
**default_config, **{'testing_delay': 0, 'sensitivity': 0.75,},
}
results = simulate_testing_candence(config_1)
plot_cumulative_infections(results)
caption = get_config_info(
config_1,
['testing_delay', 'sensitivity', 'risk_behavior', 'sym_pos_rate'],
)
add_caption(caption, locx=0.1, locy=-0.01)
save_plot(f'testing_cadence_{sr_str}_sr')
print('------------------------------------')
###############################
### Testing Process ###########
###############################
config_1 = {
**default_config, **{'testing_delay': 0, 'sensitivity': 0.75,},
}
for rb in [0.0, 0.5, 0.9, 1.0]:
print(f'Testing process ---- {rb}')
config_1['risk_behavior'] = rb
results = simulate_testing_process(config_1, include_asy=True)
plot_cumulative_infections(results)
caption = get_config_info(
config_1,
[
'testing_interval',
'testing_delay',
'sensitivity',
'sym_pos_rate',
],
)
add_caption(caption, locx=0.1, locy=-0.01)
plt.title(f'Risk Behavior={rb}')
save_plot(f'testing_process_rb_{int(rb * 100)}_{sr_str}_sr')
print('------------------------------------')
'''
###############################
### Risk Behavior #############
###############################
config_1 = {
**default_config, **{'testing_delay': 0, 'sensitivity': 0.75,},
}
for tp in ['random', 'sym_first']:
print(f'Risk Behavior ---- {tp}')
config_1['testing_process'] = tp
results = simulate_risk_behavior(config_1)
plot_cumulative_infections(results)
caption = get_config_info(
config_1,
[
'testing_interval',
'testing_delay',
'sensitivity',
'testing_process',
],
)
add_caption(caption, locx=0.1, locy=-0.001)
save_plot(f'risk_behavior_{tp}_{sr_str}_sr')
print('------------------------------------')
if __name__ == "__main__":
run_all_simulations(sym_pos_rate=0.65)