forked from BDI-pathogens/OpenABM-Covid19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
56 lines (45 loc) · 1.61 KB
/
example.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
import logging
import multiprocessing
import os
import pickle
import sys
try:
from tqdm import tqdm
except ModuleNotFoundError:
def tqdm(x):
return x
from adapter_covid19.scenarios import *
from adapter_covid19.simulator import Simulator
logger = logging.getLogger(__file__)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
if len(sys.argv) < 2 or not os.path.exists(sys.argv[1]):
valid = "|".join(SCENARIOS)
print(f"Example usage: python -m {sys.argv[0]} -m <data_path> [{valid}, ...]")
data_path = sys.argv[1]
if len(sys.argv) > 2:
scenario_names = sys.argv[2:]
else:
scenario_names = SCENARIOS.keys()
unknown_scenarios = [n for n in scenario_names if n not in SCENARIOS.keys()]
if len(unknown_scenarios) > 0:
logger.error(f"Unknown scenarios! {unknown_scenarios}")
exit(1)
def _run_scenario(scenario_name):
s = Simulator(data_path)
logger.info(f"Running scenario {scenario_name}")
scenario = SCENARIOS[scenario_name]
result = s.simulate(
scenario=scenario, show_plots=False, scenario_name=scenario_name
)
file_name = f"scenario_{scenario_name}.pkl"
logger.info(f"Writing file {file_name}")
with open(file_name, "wb") as f:
pickle.dump((scenario_name, scenario, result), f)
logger.info(f"Finished writing")
if len(scenario_names) == 1:
# Makes debugging a little easier
_run_scenario(scenario_names[0])
else:
with multiprocessing.Pool() as pool:
pool.map(_run_scenario, scenario_names)