Skip to content

Commit

Permalink
Add Parameters.reinitialize_distributions method
Browse files Browse the repository at this point in the history
Added a method to reinit distribution parameters. When the `Parameters`
class is initialized it creates this distribution parameters but also
captures the random seed at the time of initialization which makes the
draws non-random across processes. Added a method to reinitialize
distribution parameters when reseeding. Not a perfect solution, the
ambigious random seed settings is rather confusing for development.
  • Loading branch information
TimothyWillard committed Dec 17, 2024
1 parent 2f93ec3 commit e39f961
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions flepimop/gempyor_pkg/src/gempyor/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ def __init__(
logging.debug(f"Index in arrays are: {self.pnames2pindex}")
logging.debug(f"NPI overlap operation is {self.stacked_modifier_method} ")

def reinitialize_distributions(self) -> None:
"""
Reinitialize all random distributions.
This method reinitializes all random distributions for the parameters contained
within this class. The random seed for each distribution is captured on
initialization so this method will change those seeds.
"""
for pn in self.pnames:
if "dist" in self.pdata[pn]:
self.pdata[pn]["dist"] = self.pconfig[pn]["value"].as_random_distribution()

def picklable_lamda_alpha(self):
"""
Read the `alpha_val` attribute.
Expand Down
2 changes: 2 additions & 0 deletions flepimop/gempyor_pkg/src/gempyor/seir.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def onerun_SEIR(
config=None,
):
np.random.seed()
modinf.parameters.reinitialize_distributions()

npi = None
if modinf.npi_config_seir:
npi = build_npi_SEIR(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ def setup_sample_2pop_vaccine_scenarios(tmp_path: Path) -> Path:
return tmp_path


@pytest.mark.parametrize("n_jobs", [1, 2])
def test_random_seir_parameter_draw_per_slot(
monkeypatch: pytest.MonkeyPatch, setup_sample_2pop_vaccine_scenarios: Path
monkeypatch: pytest.MonkeyPatch, setup_sample_2pop_vaccine_scenarios: Path, n_jobs: int
) -> None:
# Test setup
monkeypatch.chdir(setup_sample_2pop_vaccine_scenarios)

# Test execution of `gempyor-simulate`
runner = CliRunner()
result = runner.invoke(_click_simulate, ["config_sample_2pop_vaccine_scenarios.yml"])
result = runner.invoke(
_click_simulate, ["config_sample_2pop_vaccine_scenarios.yml", "--jobs", str(n_jobs)]
)
assert result.exit_code == 0

# Get the contents of 'spar' and 'hpar' directories as DataFrames
Expand Down

0 comments on commit e39f961

Please sign in to comment.