forked from HopkinsIDD/COVIDScenarioPipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.py
executable file
·190 lines (173 loc) · 5.91 KB
/
simulate.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
#!/usr/bin/env python
##
# @file
# @brief Runs hospitalization model
#
# @details
#
# ## Configuration Items
#
# ```yaml
# name: <string>
# start_date: <date>
# end_date: <date>
# dt: float
# dynfilter_path: <path to file> optional. Will not do filter step if not present
# nsimulations: <integer> overridden by the -n/--nsim script parameter
# spatial_setup:
# setup_name: <string>
# base_path: <path to directory>
# geodata: <path to file>
# mobility: <path to file>
# nodenames: <string>
# popnodes: <string>
#
# seir:
# parameters
# alpha: <float>
# sigma: <float>
# gamma: <random distribution>
# R0s: <random distribution>
#
# interventions:
# scenarios:
# - <scenario 1 name>
# - <scenario 2 name>
# - ...
# settings:
# <scenario 1 name>:
# template: choose one - "ReduceR0", "Stacked"
# ...
# <scenario 2 name>:
# template: choose one - "Reduce R0", "Stacked"
# ...
#
# seeding:
# method: choose one - "PoissonDistributed", "FolderDraw"
# ```
#
# ### interventions::scenarios::settings::<scenario name>
#
# If {template} is ReduceR0
# ```yaml
# interventions:
# scenarios:
# <scenario name>:
# template: ReduceR0
# period_start_date: <date>
# period_end_date: <date>
# value: <random distribution>
# affected_geoids: <list of strings> optional
# ```
#
# If {template} is Stacked
# ```yaml
# interventions:
# scenarios:
# <scenario name>:
# template: Stacked
# scenarios: <list of scenario names>
# ```
#
# ### seeding
#
# If {seeding::method} is PoissonDistributed
# ```yaml
# seeding:
# method: PoissonDistributed
# lambda_file: <path to file>
# ```
#
# If {seeding::method} is FolderDraw
# ```yaml
# seeding:
# method: FolderDraw
# folder_path: \<path to dir\>; make sure this ends in a '/'
# ```
#
# ## Input Data
#
# * <b>{spatial_setup::base_path}/{spatial_setup::geodata}</b> is a csv with columns {spatial_setup::nodenames} and {spatial_setup::popnodes}
# * <b>{spatial_setup::base_path}/{spatial_setup::mobility}</b>
#
# If {seeding::method} is PoissonDistributed
# * {seeding::lambda_file}
#
# If {seeding::method} is FolderDraw
# * {seeding::folder_path}/importation_[number].csv
#
# ## Output Data
#
# * model_output/{spatial_setup::setup_name}_[scenario]/[simulation ID].seir.[csv/parquet]
# * model_parameters/{spatial_setup::setup_name}_[scenario]/[simulation ID].spar.[csv/parquet]
# * model_parameters/{spatial_setup::setup_name}_[scenario]/[simulation ID].npi.[csv/parquet]
## @cond
import multiprocessing
import pathlib
import time
import click
from SEIR import seir, setup
from SEIR.utils import config
from SEIR.profile import profile_options
@click.command()
@click.option("-c", "--config", "config_file", envvar="CONFIG_PATH", type=click.Path(exists=True), required=True,
help="configuration file for this simulation")
@click.option("-s", "--scenario", "scenarios", type=str, default=[], multiple=True,
help="override the scenario(s) run for this simulation [supports multiple scenarios: `-s Wuhan -s None`]")
@click.option("-n", "--nsim", type=click.IntRange(min=1),
help="override the # of simulation runs in the config file")
@click.option("-j", "--jobs", type=click.IntRange(min=1),
default=multiprocessing.cpu_count(), show_default=True,
help="the parallelization factor")
@click.option("--interactive/--batch", default=False,
help="run in interactive or batch mode [default: batch]")
@click.option("--write-csv/--no-write-csv", default=False, show_default=True,
help="write CSV output at end of simulation")
@click.option("--write-parquet/--no-write-parquet", default=True, show_default=True,
help="write parquet file output at end of simulation")
@profile_options
def simulate(config_file, scenarios, nsim, jobs, interactive, write_csv, write_parquet):
config.set_file(config_file)
spatial_config = config["spatial_setup"]
spatial_base_path = pathlib.Path(spatial_config["base_path"].get())
if not scenarios:
scenarios = config["interventions"]["scenarios"].as_str_seq()
print(f"Scenarios to be run: {', '.join(scenarios)}")
if not nsim:
nsim = config["nsimulations"].as_number()
start = time.monotonic()
for scenario in scenarios:
s = setup.Setup(setup_name=config["name"].get() + "_" + str(scenario),
spatial_setup=setup.SpatialSetup(
setup_name=spatial_config["setup_name"].get(),
geodata_file=spatial_base_path / spatial_config["geodata"].get(),
mobility_file=spatial_base_path / spatial_config["mobility"].get(),
popnodes_key=spatial_config["popnodes"].get(),
nodenames_key=spatial_config["nodenames"].get()
),
nsim=nsim,
npi_scenario=scenario,
npi_config=config["interventions"]["settings"][scenario],
seeding_config=config["seeding"],
ti=config["start_date"].as_date(),
tf=config["end_date"].as_date(),
interactive=interactive,
write_csv=write_csv,
write_parquet=write_parquet,
dt=config["dt"].as_number())
try:
s.load_filter(config["dynfilter_path"].get())
print(' We are using a filter')
except:
print('No filter used')
print(f"""
>> Scenario: {scenario}
>> Starting {s.nsim} model runs on {jobs} processes
>> Setup *** {s.setup_name} *** from {s.ti} to {s.tf}
>> writing to folder : {s.datadir}{s.setup_name}
""")
seir.run_parallel(s, n_jobs=jobs)
print(f">> All runs completed in {time.monotonic() - start:.1f} seconds")
if __name__ == "__main__":
simulate()
## @endcond