|
12 | 12 | MultivariateNormalKernel, |
13 | 13 | SeedKernel, |
14 | 14 | ) |
| 15 | +from mrp.api import apply_dict_overrides |
15 | 16 |
|
16 | 17 | from ixa_epi_covid import CovidModel |
17 | 18 |
|
18 | | -with open(Path("experiments", "phase1", "input", "priors.json"), "r") as f: |
19 | | - priors = json.load(f) |
| 19 | +# Run-specific parameters declaration ------------------------------------------------------ |
| 20 | +# Default model and parameters |
| 21 | +exe_file = Path("target", "release", "ixa-epi-covid") |
| 22 | +output_dir = Path("experiments", "phase1", "calibration", "output") |
| 23 | +default_ixa_params_file = Path( |
| 24 | + "experiments", "phase1", "input", "default_params.json" |
| 25 | +) |
| 26 | +ixa_overrides = { |
| 27 | + "synth_population_file": "/mnt/S_CFA_Predict/team-CMEI/synthetic_populations/cbsa_all_work_school_household_2020-04-24/cbsa_all_work_school_household/IN/Bloomington IN.csv" |
| 28 | +} |
| 29 | +force_overwrite = True |
20 | 30 |
|
21 | | -with open( |
22 | | - Path("experiments", "phase1", "input", "default_params.json"), "r" |
23 | | -) as f: |
| 31 | +# State importation model declaration parameters |
| 32 | +state = "Indiana" |
| 33 | +year = 2020 |
| 34 | + |
| 35 | +# Calibration inputs |
| 36 | +priors_file = Path("experiments", "phase1", "input", "priors.json") |
| 37 | +tolerance_values = [30.0, 20.0, 10.0, 5.0] # , 2.0, 0.01] |
| 38 | +generation_particle_count = 500 |
| 39 | +target_data = 75 |
| 40 | + |
| 41 | + |
| 42 | +# Output processing function for calibration |
| 43 | +def outputs_to_distance(model_output: pl.DataFrame, target_data: int): |
| 44 | + first_death_observed = model_output.filter( |
| 45 | + (pl.col("event") == "Dead") & (pl.col("count") > 0) |
| 46 | + ).filter(pl.col("t_upper") == pl.min("t_upper")) |
| 47 | + if first_death_observed.height > 0: |
| 48 | + return abs(target_data - first_death_observed.item(0, "t_upper")) |
| 49 | + else: |
| 50 | + return 1000 |
| 51 | + |
| 52 | + |
| 53 | +# Load environment files, defaults, and setup configurations --------------------- |
| 54 | +with open(default_ixa_params_file, "r") as f: |
24 | 55 | default_params = json.load(f) |
25 | 56 |
|
| 57 | + |
| 58 | +default_params = apply_dict_overrides( |
| 59 | + default_params, {"epimodel.GlobalParams": ixa_overrides} |
| 60 | +) |
| 61 | + |
26 | 62 | mrp_defaults = { |
27 | 63 | "ixa_inputs": default_params, |
28 | 64 | "config_inputs": { |
29 | | - "exe_file": "./target/release/ixa-epi-covid", |
30 | | - "output_dir": "./experiments/phase1/calibration/output", |
31 | | - "force_overwrite": True, |
32 | | - # "ixa_overrides": { |
33 | | - # "synth_population_file": Path(os.path.expanduser(os.getenv("SYNTH_POPULATION_DIR"))) / "in.csv" |
34 | | - # } |
| 65 | + "exe_file": str(exe_file), |
| 66 | + "output_dir": str(output_dir), |
| 67 | + "force_overwrite": force_overwrite, |
35 | 68 | }, |
36 | 69 | "importation_inputs": { |
37 | 70 | "state": "Indiana", |
|
49 | 82 |
|
50 | 83 | output_dir.mkdir(parents=True, exist_ok=False) |
51 | 84 |
|
52 | | -P = priors |
| 85 | +# Create the model and sampler objects ------------------------------------------------ |
| 86 | +with open(priors_file, "r") as f: |
| 87 | + priors = json.load(f) |
| 88 | + |
| 89 | +P: dict[dict, dict] = priors |
53 | 90 | K = IndependentKernels( |
54 | 91 | [ |
55 | | - MultivariateNormalKernel( |
56 | | - [p for p in P["priors"].keys()], |
57 | | - ), |
| 92 | + MultivariateNormalKernel(list(P["priors"].keys())), |
58 | 93 | SeedKernel("seed"), |
59 | 94 | ] |
60 | 95 | ) |
61 | 96 |
|
62 | 97 | model = CovidModel() |
63 | 98 |
|
64 | | - |
65 | | -def outputs_to_distance(model_output: pl.DataFrame, target_data: int): |
66 | | - first_death_observed = model_output.filter( |
67 | | - (pl.col("event") == "Dead") & (pl.col("count") > 0) |
68 | | - ).filter(pl.col("t_upper") == pl.min("t_upper")) |
69 | | - if first_death_observed.height > 0: |
70 | | - return abs(target_data - first_death_observed.item(0, "t_upper")) |
71 | | - else: |
72 | | - return 1000 |
73 | | - |
74 | | - |
75 | 99 | sampler = ABCSampler( |
76 | | - generation_particle_count=500, |
77 | | - tolerance_values=[30.0, 20.0, 10.0, 5.0], |
| 100 | + generation_particle_count=generation_particle_count, |
| 101 | + tolerance_values=tolerance_values, |
78 | 102 | priors=P, |
79 | 103 | perturbation_kernel=K, |
80 | 104 | variance_adapter=AdaptMultivariateNormalVariance(), |
81 | 105 | outputs_to_distance=outputs_to_distance, |
82 | | - target_data=75, |
| 106 | + target_data=target_data, |
83 | 107 | model_runner=model, |
84 | 108 | seed=123, |
85 | 109 | ) |
86 | 110 |
|
87 | | -results = sampler.run( |
88 | | - default_params=mrp_defaults, |
89 | | - parameter_headers=["ixa_inputs", "epimodel.GlobalParams"], |
90 | | -) |
| 111 | +# Execute the sampler ---------------------------------------------------------------------- |
| 112 | +results = sampler.run(default_params=mrp_defaults) |
91 | 113 |
|
92 | 114 | print(results) |
93 | 115 |
|
|
0 commit comments