-
Notifications
You must be signed in to change notification settings - Fork 254
/
run_sample.py
executable file
·266 lines (233 loc) · 8.28 KB
/
run_sample.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
#!/usr/bin/env python3
"""Run the sample data.
See README.md in this directory for more information.
"""
import argparse
import gzip
import multiprocessing
import os
import shutil
import subprocess
import sys
def try_remove(f):
try:
os.remove(f)
except OSError as e:
pass
def parse_args():
"""Parse the arguments.
On exit: Returns the result of calling argparse.parse()
args.covidsim is the name of the CovidSim executable
args.datadir is the directory with the input data
args.paramdir is the directory with the parameters in it
args.outputdir is the directory where output will be stored
args.threads is the number of threads to use
"""
parser = argparse.ArgumentParser()
try:
cpu_count = len(os.sched_getaffinity(0))
except AttributeError:
# os.sched_getaffinity isn't available
cpu_count = multiprocessing.cpu_count()
if cpu_count is None or cpu_count == 0:
cpu_count = 2
script_path = os.path.dirname(os.path.realpath(__file__))
# Default values
data_dir = script_path
param_dir = os.path.join(script_path, "param_files")
output_dir = os.getcwd()
src_dir = os.path.join(data_dir, os.pardir)
parser.add_argument(
"country",
help="Country to run sample for")
parser.add_argument(
"--covidsim",
help="Location of CovidSim binary, if none specified will build")
parser.add_argument(
"--datadir",
help="Directory at root of input data",
default=script_path)
parser.add_argument(
"--paramdir",
help="Directory with input parameter files",
default=param_dir)
parser.add_argument(
"--srcdir",
help="Directory with source in - needed if --covidsim isn't specified",
default=src_dir)
parser.add_argument(
"--outputdir",
help="Directory to store output data",
default=output_dir)
parser.add_argument(
"--threads",
help="Number of threads to use",
default=cpu_count
)
args = parser.parse_args()
return args
args = parse_args()
# Lists of places that need to be handled specially
united_states = [ "United_States" ]
canada = [ "Canada" ]
usa_territories = ["Alaska", "Hawaii", "Guam", "Virgin_Islands_US", "Puerto_Rico", "American_Samoa"]
nigeria = ["Nigeria"]
# Determine whether we need to build the tool or use a user supplied one:
if args.covidsim is not None:
exe = args.covidsim
else:
build_dir = os.path.join(args.outputdir, "build")
# Ensure we do a clean build
shutil.rmtree(build_dir, ignore_errors=True)
os.makedirs(build_dir, exist_ok=False)
cwd = os.getcwd()
os.chdir(build_dir)
# Build
subprocess.run(['cmake', args.srcdir], check=True)
subprocess.run(['cmake', '--build', '.'], check=True)
# Where the exe ends up depends on the OS.
if os.name == 'nt':
exe = os.path.join(build_dir, "src", "Debug", "CovidSim.exe")
else:
exe = os.path.join(build_dir, "src", "CovidSim")
os.chdir(cwd)
# Ensure output directory exists
os.makedirs(args.outputdir, exist_ok=True)
# The admin file to use
admin_file = os.path.join(args.datadir, "admin_units",
"{0}_admin.txt".format(args.country))
if not os.path.exists(admin_file):
print("Unable to find admin file for country: {0}".format(args.country))
print("Data directory: {0}".format(args.datadir))
print("Looked for: {0}".format(admin_file))
exit(1)
# Population density file in gziped form, text file, and binary file as
# processed by CovidSim
if args.country in united_states + canada:
wpop_file_root = "usacan"
elif args.country in usa_territories:
wpop_file_root = "us_terr"
elif args.country in nigeria:
wpop_file_root = "nga_adm1"
else:
wpop_file_root = "eur"
wpop_file_gz = os.path.join(
args.datadir,
"populations",
"wpop_{0}.txt.gz".format(wpop_file_root))
if not os.path.exists(wpop_file_gz):
print("Unable to find population file for country: {0}".format(args.country))
print("Data directory: {0}".format(args.datadir))
print("Looked for: {0}".format(wpop_file_gz))
exit(1)
wpop_file = os.path.join(
args.outputdir,
"wpop_{0}.txt".format(wpop_file_root))
wpop_bin = os.path.join(
args.outputdir,
"{0}_pop_density.bin".format(args.country))
# gunzip wpop fie
try_remove(wpop_file)
try_remove(wpop_bin)
with gzip.open(wpop_file_gz, 'rb') as f_in:
with open(wpop_file, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
# Configure pre-parameter file. This file doesn't change between runs:
if args.country in united_states:
pp_file = os.path.join(args.paramdir, "preUS_R0=2.0.txt")
elif args.country in nigeria:
pp_file = os.path.join(args.paramdir, "preNGA_R0=2.0.txt")
else:
pp_file = os.path.join(args.paramdir, "preUK_R0=2.0.txt")
if not os.path.exists(pp_file):
print("Unable to find pre-parameter file")
print("Param directory: {0}".format(args.paramdir))
print("Looked for: {0}".format(pp_file))
exit(1)
# Configure No intervention parameter file. This is run first
# and provides a baseline
no_int_file = os.path.join(args.paramdir, "p_NoInt.txt")
if not os.path.exists(no_int_file):
print("Unable to find parameter file")
print("Param directory: {0}".format(args.paramdir))
print("Looked for: {0}".format(no_int_file))
exit(1)
# Configure an intervention (controls) parameter file.
# In reality you will run CovidSim many times with different parameter
# controls.
control_roots = [ "PC7_CI_HQ_SD" ]
for root in control_roots:
cf = os.path.join(args.paramdir, "p_{0}.txt".format(root))
if not os.path.exists(cf):
print("Unable to find parameter file")
print("Param directory: {0}".format(args.paramdir))
print("Looked for: {0}".format(cf))
exit(1)
school_file = None
if args.country in united_states:
school_file = os.path.join(args.datadir, "populations", "USschools.txt")
if not os.path.exists(school_file):
print("Unable to find school file for country: {0}".format(args.country))
print("Data directory: {0}".format(args.datadir))
print("Looked for: {0}".format(school_file))
exit(1)
# Some command_line settings
r = 3.0
rs = r/2
# This is the temporary network that represents initial state of the
# simulation
network_bin = os.path.join(
args.outputdir,
"Network_{0}_T{1}_R{2}.bin".format(args.country, args.threads, r))
try_remove(network_bin)
# Run the no intervention sim. This also does some extra setup which is one
# off for each R.
print("No intervention: {0} NoInt {1}".format(args.country, r))
cmd = [
exe,
"/c:{0}".format(args.threads),
"/A:" + admin_file
]
if school_file:
cmd.extend(["/s:" + school_file])
cmd.extend([
"/PP:" + pp_file, # Preparam file
"/P:" + no_int_file, # Param file
"/O:" + os.path.join(args.outputdir,
"{0}_NoInt_R0={1}".format(args.country, r)), # Output
"/D:" + wpop_file, # Input (this time text) pop density
"/M:" + wpop_bin, # Where to save binary pop density
"/S:" + network_bin, # Where to save binary net setup
"/R:{0}".format(rs),
"98798150", # These four numbers are RNG seeds
"729101",
"17389101",
"4797132"
])
print("Command line: " + " ".join(cmd))
process = subprocess.run(cmd, check=True)
for root in control_roots:
cf = os.path.join(args.paramdir, "p_{0}.txt".format(root))
print("Intervention: {0} {1} {2}".format(args.country, root, r))
cmd = [
exe,
"/c:{0}".format(args.threads),
"/A:" + admin_file
]
if school_file:
cmd.extend(["/s:" + school_file])
cmd.extend([
"/PP:" + pp_file,
"/P:" + cf,
"/O:" + os.path.join(args.outputdir,
"{0}_{1}_R0={2}".format(args.country, root, r)),
"/D:" + wpop_bin, # Binary pop density file (speedup)
"/L:" + network_bin, # Network to load
"/R:{0}".format(rs),
"98798150",
"729101",
"17389101",
"4797132"
])
print("Command line: " + " ".join(cmd))
process = subprocess.run(cmd, check=True)