Skip to content

Commit

Permalink
Merge pull request #269 from HopkinsIDD/fix-no-seir-npi-for-inference
Browse files Browse the repository at this point in the history
Fix: inference.py fails when no seir modifier config.
  • Loading branch information
jcblemai authored Sep 13, 2024
2 parents ed29ca8 + c602459 commit d98c752
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
14 changes: 9 additions & 5 deletions flepimop/gempyor_pkg/src/gempyor/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,12 @@ def one_simulation(
self.outcomes_parameters = outcomes.read_parameters_from_config(self.modinf)

npi_outcomes = None
npi_seir = None
if parallel:
with Timer("//things"):
with ProcessPoolExecutor(max_workers=max(mp.cpu_count(), 3)) as executor:
ret_seir = executor.submit(seir.build_npi_SEIR, self.modinf, load_ID, sim_id2load, config)
if self.modinf.seir_config is not None and self.modinf.npi_config_seir is not None:
ret_seir = executor.submit(seir.build_npi_SEIR, self.modinf, load_ID, sim_id2load, config)
if self.modinf.outcomes_config is not None and self.modinf.npi_config_outcomes:
ret_outcomes = executor.submit(
outcomes.build_outcome_modifiers,
Expand All @@ -563,15 +565,17 @@ def one_simulation(
self.proportion_info,
) = ret_comparments.result()
self.already_built = True
npi_seir = ret_seir.result()
if self.modinf.seir_config is not None and self.modinf.npi_config_seir is not None:
npi_seir = ret_seir.result()
if self.modinf.outcomes_config is not None and self.modinf.npi_config_outcomes:
npi_outcomes = ret_outcomes.result()
else:
if not self.already_built:
self.build_structure()
npi_seir = seir.build_npi_SEIR(
modinf=self.modinf, load_ID=load_ID, sim_id2load=sim_id2load, config=config
)
if self.modinf.seir_config is not None and self.modinf.npi_config_seir is not None:
npi_seir = seir.build_npi_SEIR(
modinf=self.modinf, load_ID=load_ID, sim_id2load=sim_id2load, config=config
)
if self.modinf.outcomes_config is not None and self.modinf.npi_config_outcomes:
npi_outcomes = outcomes.build_outcome_modifiers(
modinf=self.modinf,
Expand Down
2 changes: 1 addition & 1 deletion flepimop/gempyor_pkg/src/gempyor/steps_rk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def rhs(t, x, today):
rate_change_compartment *= parameters[transitions[transition_rate_col][transition_index]][
today
][visiting_subpop]
total_rate[spatial_node] *= rate_keep_compartment + rate_change_compartment.sum()
total_rate[spatial_node] *= (rate_keep_compartment + rate_change_compartment.sum())

# compute the number of individual transitioning from source to destination from the total rate
# number_move has shape (nspatial_nodes)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: minimal_test
setup_name: minimal_test_setup
start_date: 2020-01-31
end_date: 2020-05-31
nslots: 5

subpop_setup:
geodata: data/geodata.csv
mobility: data/mobility.csv

initial_conditions:
method: Default

compartments:
infection_stage: ["S", "E", "I1", "I2", "I3", "R"]
vaccination_stage: ["unvaccinated"]

seir:
integration:
method: legacy
dt: 1/6
parameters:
alpha:
value: .9
sigma:
value:
distribution: fixed
value: 1 / 5.2
gamma:
value:
distribution: uniform
low: 1 / 6
high: 1 / 2.6
R0s:
value:
distribution: uniform
low: 2
high: 3
transitions:
- source: ["S", "unvaccinated"]
destination: ["E", "unvaccinated"]
rate: ["R0s * gamma", 1]
proportional_to: [
["S", "unvaccinated"],
[[["I1", "I2", "I3"]], "unvaccinated"],
]
proportion_exponent: [["1", "1"], ["alpha", "1"]]
- source: [["E"], ["unvaccinated"]]
destination: [["I1"], ["unvaccinated"]]
rate: ["sigma", 1]
proportional_to: [[["E"], ["unvaccinated"]]]
proportion_exponent: [["1", "1"]]
- source: [["I1"], ["unvaccinated"]]
destination: [["I2"], ["unvaccinated"]]
rate: ["3 * gamma", 1]
proportional_to: [[["I1"], ["unvaccinated"]]]
proportion_exponent: [["1", "1"]]
- source: [["I2"], ["unvaccinated"]]
destination: [["I3"], ["unvaccinated"]]
rate: ["3 * gamma", 1]
proportional_to: [[["I2"], ["unvaccinated"]]]
proportion_exponent: [["1", "1"]]
- source: [["I3"], ["unvaccinated"]]
destination: [["R"], ["unvaccinated"]]
rate: ["3 * gamma", 1]
proportional_to: [[["I3"], ["unvaccinated"]]]
proportion_exponent: [["1", "1"]]
12 changes: 12 additions & 0 deletions flepimop/gempyor_pkg/tests/inference/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ def test_GempyorInference_success(self):

i.already_built = False
i.one_simulation(sim_id2write=0, parallel=True)

# HopkinsIDD/flepiMoP#269
def test_inference_without_seir_modifiers(self):
# Setup
os.chdir(os.path.dirname(__file__))
gempyor_inference = inference.GempyorInference(
f"{DATA_DIR}/config_inference_without_seir_modifiers.yml",
)

# Test
simulation_int = gempyor_inference.one_simulation(0)
assert simulation_int == 0

0 comments on commit d98c752

Please sign in to comment.