From 65b248eb4e9d77b00b6784180236f63a6c480cff Mon Sep 17 00:00:00 2001 From: sbidari Date: Tue, 20 Aug 2024 14:31:40 -0400 Subject: [PATCH 01/18] testing convolve mode --- model/src/pyrenew/latent/hospitaladmissions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/src/pyrenew/latent/hospitaladmissions.py b/model/src/pyrenew/latent/hospitaladmissions.py index 101ede18..959854f0 100644 --- a/model/src/pyrenew/latent/hospitaladmissions.py +++ b/model/src/pyrenew/latent/hospitaladmissions.py @@ -189,7 +189,7 @@ def sample( latent_hospital_admissions = jnp.convolve( latent_hospital_admissions_raw, infection_to_admission_interval.value, - mode="full", + mode="valid", )[: latent_hospital_admissions_raw.shape[0]] # Applying the day of the week effect From 3a548550ced935144dedf905ede363481aa90c3d Mon Sep 17 00:00:00 2001 From: sbidari Date: Wed, 21 Aug 2024 11:41:57 -0400 Subject: [PATCH 02/18] update tutorial to work with convolve mode valid --- .../tutorials/hospital_admissions_model.qmd | 26 ++++++++----------- src/pyrenew/latent/hospitaladmissions.py | 2 +- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/docs/source/tutorials/hospital_admissions_model.qmd b/docs/source/tutorials/hospital_admissions_model.qmd index cbd73e39..9bbfeade 100644 --- a/docs/source/tutorials/hospital_admissions_model.qmd +++ b/docs/source/tutorials/hospital_admissions_model.qmd @@ -118,17 +118,17 @@ inf_hosp_int = datasets.load_infection_admission_interval() # We only need the probability_mass column of each dataset gen_int_array = gen_int["probability_mass"].to_numpy() gen_int = gen_int_array -inf_hosp_int = inf_hosp_int["probability_mass"].to_numpy() +inf_hosp_int_array = inf_hosp_int["probability_mass"].to_numpy() # Taking a peek at the first 5 elements of each -gen_int[:5], inf_hosp_int[:5] +gen_int[:5], inf_hosp_int_array[:5] # Visualizing both quantities side by side fig, axs = plt.subplots(1, 2) axs[0].plot(gen_int) axs[0].set_title("Generation interval") -axs[1].plot(inf_hosp_int) +axs[1].plot(inf_hosp_int_array) axs[1].set_title("Infection to hospital admission interval") plt.show() ``` @@ -142,7 +142,7 @@ import jax.numpy as jnp import numpyro.distributions as dist inf_hosp_int = deterministic.DeterministicPMF( - name="inf_hosp_int", value=inf_hosp_int + name="inf_hosp_int", value=inf_hosp_int_array ) hosp_rate = metaclass.DistributionalRV( @@ -175,7 +175,7 @@ I0 = InfectionInitializationProcess( distribution=dist.LogNormal(loc=jnp.log(100), scale=jnp.log(1.75)), ), InitializeInfectionsExponentialGrowth( - gen_int_array.size, + inf_hosp_int_array.size, deterministic.DeterministicVariable(name="rate", value=0.05), ), t_unit=1, @@ -313,11 +313,7 @@ We can use the `Model` object's `plot_posterior` method to visualize the model f out = hosp_model.plot_posterior( var="latent_hospital_admissions", ylab="Hospital Admissions", - obs_signal=np.pad( - daily_hosp_admits.astype(float), - (gen_int_array.size, 0), - constant_values=np.nan, - ), + obs_signal=daily_hosp_admits.astype(float), ) ``` @@ -504,7 +500,7 @@ def compute_eti(dataset, eti_prob): fig, axes = plt.subplots(figsize=(6, 5)) az.plot_hdi( - idata.prior_predictive["negbinom_rv_dim_0"] + gen_int.size(), + idata.prior_predictive["negbinom_rv_dim_0"], hdi_data=compute_eti(idata.prior_predictive["negbinom_rv"], 0.9), color="C0", smooth=False, @@ -513,7 +509,7 @@ az.plot_hdi( ) az.plot_hdi( - idata.prior_predictive["negbinom_rv_dim_0"] + gen_int.size(), + idata.prior_predictive["negbinom_rv_dim_0"], hdi_data=compute_eti(idata.prior_predictive["negbinom_rv"], 0.5), color="C0", smooth=False, @@ -522,7 +518,7 @@ az.plot_hdi( ) plt.scatter( - idata.observed_data["negbinom_rv_dim_0"] + gen_int.size(), + idata.observed_data["negbinom_rv_dim_0"], idata.observed_data["negbinom_rv"], color="black", ) @@ -538,7 +534,7 @@ And now we plot the posterior predictive distributions with a `{python} n_foreca ```{python} # | label: fig-output-posterior-predictive-forecast # | fig-cap: Posterior predictive admissions, including a forecast. -x_data = idata.posterior_predictive["negbinom_rv_dim_0"] + gen_int.size() +x_data = idata.posterior_predictive["negbinom_rv_dim_0"] y_data = idata.posterior_predictive["negbinom_rv"] fig, axes = plt.subplots(figsize=(6, 5)) az.plot_hdi( @@ -569,7 +565,7 @@ plt.plot( label="Median", ) plt.scatter( - idata.observed_data["negbinom_rv_dim_0"] + gen_int.size(), + idata.observed_data["negbinom_rv_dim_0"], idata.observed_data["negbinom_rv"], color="black", ) diff --git a/src/pyrenew/latent/hospitaladmissions.py b/src/pyrenew/latent/hospitaladmissions.py index 8e01c4d2..11472a13 100644 --- a/src/pyrenew/latent/hospitaladmissions.py +++ b/src/pyrenew/latent/hospitaladmissions.py @@ -191,7 +191,7 @@ def sample( latent_hospital_admissions_raw, infection_to_admission_interval.value, mode="valid", - )[: latent_hospital_admissions_raw.shape[0]] + ) # Applying the day of the week effect latent_hospital_admissions = ( From 9a7cbb3f921e4d080f737059687868f0628605a3 Mon Sep 17 00:00:00 2001 From: sbidari Date: Wed, 21 Aug 2024 12:44:29 -0400 Subject: [PATCH 03/18] update latent admissions test --- src/test/test_latent_admissions.py | 85 +++++++++++++----------------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/src/test/test_latent_admissions.py b/src/test/test_latent_admissions.py index 92acec2b..14333fc9 100644 --- a/src/test/test_latent_admissions.py +++ b/src/test/test_latent_admissions.py @@ -1,20 +1,16 @@ # -*- coding: utf-8 -*- # numpydoc ignore=GL08 +from test.utils import simple_rt + import jax.numpy as jnp import numpy.testing as testing import numpyro import numpyro.distributions as dist -from pyrenew import transformation as t from pyrenew.deterministic import DeterministicPMF, DeterministicVariable from pyrenew.latent import HospitalAdmissions, Infections -from pyrenew.metaclass import ( - DistributionalRV, - SampledValue, - TransformedRandomVariable, -) -from pyrenew.process import SimpleRandomWalkProcess +from pyrenew.metaclass import DistributionalRV, SampledValue def test_admissions_sample(): @@ -25,26 +21,36 @@ def test_admissions_sample(): # Generating Rt and Infections to compute the hospital admissions - rt = TransformedRandomVariable( - name="Rt_rv", - base_rv=SimpleRandomWalkProcess( - name="log_rt", - step_rv=DistributionalRV( - name="rw_step_rv", distribution=dist.Normal(0, 0.025) - ), - init_rv=DistributionalRV( - name="init_log_rt", distribution=dist.Normal(0, 0.2) - ), - ), - transforms=t.ExpTransform(), - ) + rt = simple_rt() + n_steps = 30 with numpyro.handlers.seed(rng_seed=223): - sim_rt = rt(n_steps=30)[0].value + sim_rt = rt(n_steps=n_steps)[0].value gen_int = jnp.array([0.5, 0.1, 0.1, 0.2, 0.1]) - i0 = 10 * jnp.ones_like(gen_int) - + inf_hosp_int_array = jnp.array( + [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0.25, + 0.5, + 0.1, + 0.1, + 0.05, + ] + ) + i0 = 10 * jnp.ones_like(inf_hosp_int_array) inf1 = Infections() with numpyro.handlers.seed(rng_seed=223): @@ -53,28 +59,7 @@ def test_admissions_sample(): # Testing the hospital admissions inf_hosp = DeterministicPMF( name="inf_hosp", - value=jnp.array( - [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0.25, - 0.5, - 0.1, - 0.1, - 0.05, - ] - ), + value=inf_hosp_int_array, ) hosp1 = HospitalAdmissions( @@ -85,10 +70,16 @@ def test_admissions_sample(): ) with numpyro.handlers.seed(rng_seed=223): - sim_hosp_1 = hosp1(latent_infections=inf_sampled1[0]) + sim_hosp_1 = hosp1( + latent_infections=SampledValue( + value=jnp.hstack( + [i0, inf_sampled1.post_initialization_infections.value] + ) + ) + ) testing.assert_array_less( - sim_hosp_1.latent_hospital_admissions.value, + sim_hosp_1.latent_hospital_admissions.value[-n_steps:], inf_sampled1[0].value, ) From cbff93c060b7265a7a5d315f20ca4882fc3a6c54 Mon Sep 17 00:00:00 2001 From: sbidari Date: Wed, 21 Aug 2024 13:03:34 -0400 Subject: [PATCH 04/18] update DOW tutorial for convolve mode valid --- docs/source/tutorials/day_of_the_week.qmd | 24 ++++++----------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/docs/source/tutorials/day_of_the_week.qmd b/docs/source/tutorials/day_of_the_week.qmd index ceadc985..976955a9 100644 --- a/docs/source/tutorials/day_of_the_week.qmd +++ b/docs/source/tutorials/day_of_the_week.qmd @@ -43,7 +43,7 @@ inf_hosp_int = datasets.load_infection_admission_interval() # We only need the probability_mass column of each dataset gen_int_array = gen_int["probability_mass"].to_numpy() gen_int = gen_int_array -inf_hosp_int = inf_hosp_int["probability_mass"].to_numpy() +inf_hosp_int_array = inf_hosp_int["probability_mass"].to_numpy() ``` 2. Next, we defined the model's components: @@ -56,7 +56,7 @@ import jax.numpy as jnp import numpyro.distributions as dist inf_hosp_int = deterministic.DeterministicPMF( - name="inf_hosp_int", value=inf_hosp_int + name="inf_hosp_int", value=inf_hosp_int_array ) hosp_rate = metaclass.DistributionalRV( @@ -84,7 +84,7 @@ I0 = InfectionInitializationProcess( distribution=dist.LogNormal(loc=jnp.log(100), scale=jnp.log(1.75)), ), InitializeInfectionsExponentialGrowth( - gen_int_array.size, + inf_hosp_int_array.size, deterministic.DeterministicVariable(name="rate", value=0.05), ), t_unit=1, @@ -201,11 +201,7 @@ hosp_model.run( out = hosp_model.plot_posterior( var="latent_hospital_admissions", ylab="Hospital Admissions", - obs_signal=np.pad( - daily_hosp_admits.astype(float), - (gen_int_array.size, 0), - constant_values=np.nan, - ), + obs_signal=daily_hosp_admits.astype(float), ) ``` @@ -329,11 +325,7 @@ The new model with the day-of-the-week effect can be compared to the previous mo out = hosp_model.plot_posterior( var="latent_hospital_admissions", ylab="Hospital Admissions", - obs_signal=np.pad( - daily_hosp_admits.astype(float), - (gen_int_array.size, 0), - constant_values=np.nan, - ), + obs_signal=daily_hosp_admits.astype(float), ) ``` @@ -344,10 +336,6 @@ out = hosp_model.plot_posterior( out_dow = hosp_model_dow.plot_posterior( var="latent_hospital_admissions", ylab="Hospital Admissions", - obs_signal=np.pad( - daily_hosp_admits.astype(float), - (gen_int_array.size, 0), - constant_values=np.nan, - ), + obs_signal=daily_hosp_admits.astype(float), ) ``` From 41b070f02adb34ae9fd22db38b5d84f03b618ce4 Mon Sep 17 00:00:00 2001 From: sbidari Date: Wed, 21 Aug 2024 13:55:51 -0400 Subject: [PATCH 05/18] update hosp model tests --- src/test/test_model_hosp_admissions.py | 94 +++++++++++++------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/src/test/test_model_hosp_admissions.py b/src/test/test_model_hosp_admissions.py index fc2e4f57..e752f2c3 100644 --- a/src/test/test_model_hosp_admissions.py +++ b/src/test/test_model_hosp_admissions.py @@ -197,16 +197,6 @@ def test_model_hosp_no_obs_model(): value=jnp.array([0.25, 0.25, 0.25, 0.25]), ) - I0 = InfectionInitializationProcess( - "I0_initialization", - DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), - InitializeInfectionsZeroPad(n_timepoints=gen_int.size()), - t_unit=1, - ) - - latent_infections = Infections() - Rt_process = simple_rt() - inf_hosp = DeterministicPMF( name="inf_hosp", value=jnp.array( @@ -233,6 +223,16 @@ def test_model_hosp_no_obs_model(): ), ) + I0 = InfectionInitializationProcess( + "I0_initialization", + DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), + InitializeInfectionsZeroPad(n_timepoints=inf_hosp.size()), + t_unit=1, + ) + + latent_infections = Infections() + Rt_process = simple_rt() + latent_admissions = HospitalAdmissions( infection_to_admission_interval_rv=inf_hosp, infection_hospitalization_ratio_rv=DistributionalRV( @@ -307,17 +307,6 @@ def test_model_hosp_with_obs_model(): name="gen_int", value=jnp.array([0.25, 0.25, 0.25, 0.25]) ) - I0 = InfectionInitializationProcess( - "I0_initialization", - DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), - InitializeInfectionsZeroPad(n_timepoints=gen_int.size()), - t_unit=1, - ) - - latent_infections = Infections() - Rt_process = simple_rt() - observed_admissions = PoissonObservation("poisson_rv") - inf_hosp = DeterministicPMF( name="inf_hosp", value=jnp.array( @@ -340,10 +329,21 @@ def test_model_hosp_with_obs_model(): 0.1, 0.1, 0.05, - ], + ] ), ) + I0 = InfectionInitializationProcess( + "I0_initialization", + DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), + InitializeInfectionsZeroPad(n_timepoints=inf_hosp.size()), + t_unit=1, + ) + + latent_infections = Infections() + Rt_process = simple_rt() + observed_admissions = PoissonObservation("poisson_rv") + latent_admissions = HospitalAdmissions( infection_to_admission_interval_rv=inf_hosp, infection_hospitalization_ratio_rv=DistributionalRV( @@ -394,17 +394,6 @@ def test_model_hosp_with_obs_model_weekday_phosp_2(): value=jnp.array([0.25, 0.25, 0.25, 0.25]), ) - I0 = InfectionInitializationProcess( - "I0_initialization", - DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), - InitializeInfectionsZeroPad(n_timepoints=gen_int.size()), - t_unit=1, - ) - - latent_infections = Infections() - Rt_process = simple_rt() - observed_admissions = PoissonObservation("poisson_rv") - inf_hosp = DeterministicPMF( name="inf_hosp", value=jnp.array( @@ -427,10 +416,21 @@ def test_model_hosp_with_obs_model_weekday_phosp_2(): 0.1, 0.1, 0.05, - ], + ] ), ) + I0 = InfectionInitializationProcess( + "I0_initialization", + DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), + InitializeInfectionsZeroPad(n_timepoints=inf_hosp.size()), + t_unit=1, + ) + + latent_infections = Infections() + Rt_process = simple_rt() + observed_admissions = PoissonObservation("poisson_rv") + hosp_report_prob_dist = UniformProbForTest(1, "hosp_report_prob_dist") weekday = UniformProbForTest(7, "weekday") @@ -487,18 +487,6 @@ def test_model_hosp_with_obs_model_weekday_phosp(): n_obs_to_generate = 30 pad_size = 5 - I0 = InfectionInitializationProcess( - "I0_initialization", - DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), - InitializeInfectionsZeroPad(n_timepoints=gen_int.size()), - t_unit=1, - ) - - latent_infections = Infections() - Rt_process = simple_rt() - - observed_admissions = PoissonObservation("poisson_rv") - inf_hosp = DeterministicPMF( name="inf_hosp", value=jnp.array( @@ -521,10 +509,22 @@ def test_model_hosp_with_obs_model_weekday_phosp(): 0.1, 0.1, 0.05, - ], + ] ), ) + I0 = InfectionInitializationProcess( + "I0_initialization", + DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), + InitializeInfectionsZeroPad(n_timepoints=inf_hosp.size()), + t_unit=1, + ) + + latent_infections = Infections() + Rt_process = simple_rt() + + observed_admissions = PoissonObservation("poisson_rv") + # Other random components total_length = n_obs_to_generate + pad_size + gen_int.size() weekday = jnp.array([1, 1, 1, 1, 2, 2, 2]) From e9130cecb69e4e2daf59990d48a5bc1c3f80f046 Mon Sep 17 00:00:00 2001 From: sbidari Date: Wed, 21 Aug 2024 15:28:52 -0400 Subject: [PATCH 06/18] create helper function for convolve and add tests --- src/pyrenew/latent/hospitaladmissions.py | 13 ++++--- src/pyrenew/metaclass.py | 32 +++++++++++++++++ .../test_incidence_observed_with_delay.py | 34 +++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/test/test_incidence_observed_with_delay.py diff --git a/src/pyrenew/latent/hospitaladmissions.py b/src/pyrenew/latent/hospitaladmissions.py index 947c4fd5..f8d9a2a8 100644 --- a/src/pyrenew/latent/hospitaladmissions.py +++ b/src/pyrenew/latent/hospitaladmissions.py @@ -10,7 +10,11 @@ import pyrenew.arrayutils as au from pyrenew.deterministic import DeterministicVariable -from pyrenew.metaclass import RandomVariable, SampledValue +from pyrenew.metaclass import ( + RandomVariable, + SampledValue, + compute_incidence_observed_with_delay, +) class HospitalAdmissionsSample(NamedTuple): @@ -210,11 +214,12 @@ def sample( *_, ) = self.infection_to_admission_interval_rv(**kwargs) - latent_hospital_admissions = jnp.convolve( - infection_hosp_rate.value * latent_infections.value, + latent_hospital_admissions = compute_incidence_observed_with_delay( + infection_hosp_rate.value, + latent_infections.value, infection_to_admission_interval.value, - mode="valid", ) + # Applying the day of the week effect. For this we need to: # 1. Get the day of the week effect # 2. Identify the offset of the latent_infections diff --git a/src/pyrenew/metaclass.py b/src/pyrenew/metaclass.py index b5be0a54..aad82ba4 100644 --- a/src/pyrenew/metaclass.py +++ b/src/pyrenew/metaclass.py @@ -126,6 +126,38 @@ def _assert_sample_and_rtype( return None +def compute_incidence_observed_with_delay( + incidence_to_observation_rate: float, + latent_incidence: ArrayLike, + incidence_to_observation_delay_interval: ArrayLike, +) -> ArrayLike: + """ + Computes incidences observed according + to a given observation rate and based + on a delay interval. + + Parameters + ---------- + incidence_to_observation_rate: float + The rate at which latent incidences are observed. + latent_incidence: ArrayLike + Incidence values based on the true underlying process. + incidence_to_observation_delay_interval: ArrayLike + Pmf of delay interval between incidence to observation. + + Returns + -------- + ArrayLike + The incidence after the observation delay. + """ + delay_obs_incidence = jnp.convolve( + incidence_to_observation_rate * latent_incidence, + incidence_to_observation_delay_interval, + mode="valid", + ) + return delay_obs_incidence + + class SampledValue(NamedTuple): """ A container for a value sampled from a RandomVariable. diff --git a/src/test/test_incidence_observed_with_delay.py b/src/test/test_incidence_observed_with_delay.py new file mode 100644 index 00000000..48d67b0c --- /dev/null +++ b/src/test/test_incidence_observed_with_delay.py @@ -0,0 +1,34 @@ +# numpydoc ignore=GL08 + +import jax +import jax.numpy as jnp +import numpy as np +from numpy.testing import assert_array_equal +import pytest + +from pyrenew.metaclass import compute_incidence_observed_with_delay + + +@pytest.mark.parametrize( + ["obs_rate", "latent_incidence", "delay_interval", "expected_output"], + [ + [ + jnp.array([1.0]), + jnp.array([1.0, 2.0, 3.0]), + jnp.array([1.0]), + jnp.array([1.0, 2.0, 3.0]), + ], + ], +) +def test(obs_rate, latent_incidence, delay_interval, expected_output): + """ + Tests for helper function to compute + incidence observed with a delay + """ + result = compute_incidence_observed_with_delay( + obs_rate, + latent_incidence, + delay_interval, + ) + + assert_array_equal(result, expected_output) From 47372886abbecc73b849d5cbffafb837b68e6ded Mon Sep 17 00:00:00 2001 From: sbidari Date: Wed, 21 Aug 2024 15:58:37 -0400 Subject: [PATCH 07/18] forgot to run precommit earlier --- src/pyrenew/metaclass.py | 4 ++-- .../test_incidence_observed_with_delay.py | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/pyrenew/metaclass.py b/src/pyrenew/metaclass.py index aad82ba4..e6a19c1c 100644 --- a/src/pyrenew/metaclass.py +++ b/src/pyrenew/metaclass.py @@ -127,7 +127,7 @@ def _assert_sample_and_rtype( def compute_incidence_observed_with_delay( - incidence_to_observation_rate: float, + incidence_to_observation_rate: ArrayLike, latent_incidence: ArrayLike, incidence_to_observation_delay_interval: ArrayLike, ) -> ArrayLike: @@ -138,7 +138,7 @@ def compute_incidence_observed_with_delay( Parameters ---------- - incidence_to_observation_rate: float + incidence_to_observation_rate: ArrayLike The rate at which latent incidences are observed. latent_incidence: ArrayLike Incidence values based on the true underlying process. diff --git a/src/test/test_incidence_observed_with_delay.py b/src/test/test_incidence_observed_with_delay.py index 48d67b0c..180144b9 100644 --- a/src/test/test_incidence_observed_with_delay.py +++ b/src/test/test_incidence_observed_with_delay.py @@ -1,10 +1,8 @@ # numpydoc ignore=GL08 -import jax import jax.numpy as jnp -import numpy as np -from numpy.testing import assert_array_equal import pytest +from numpy.testing import assert_array_equal from pyrenew.metaclass import compute_incidence_observed_with_delay @@ -18,6 +16,24 @@ jnp.array([1.0]), jnp.array([1.0, 2.0, 3.0]), ], + [ + jnp.array([1.0, 0.1, 1.0]), + jnp.array([1.0, 2.0, 3.0]), + jnp.array([1.0]), + jnp.array([1.0, 0.2, 3.0]), + ], + [ + jnp.array([1.0]), + jnp.array([1.0, 2.0, 3.0]), + jnp.array([0.5, 0.5]), + jnp.array([1.5, 2.5]), + ], + [ + jnp.array([1.0]), + jnp.array([0, 2.0, 4.0]), + jnp.array([0.25, 0.5, 0.25]), + jnp.array([2]), + ], ], ) def test(obs_rate, latent_incidence, delay_interval, expected_output): @@ -30,5 +46,4 @@ def test(obs_rate, latent_incidence, delay_interval, expected_output): latent_incidence, delay_interval, ) - assert_array_equal(result, expected_output) From e87d742f8078bd6a2afe8bc5b16df6522fc814cb Mon Sep 17 00:00:00 2001 From: sbidari Date: Wed, 21 Aug 2024 19:31:05 -0400 Subject: [PATCH 08/18] update test for model with DOW effect --- src/test/test_model_hosp_admissions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/test_model_hosp_admissions.py b/src/test/test_model_hosp_admissions.py index e752f2c3..da606e97 100644 --- a/src/test/test_model_hosp_admissions.py +++ b/src/test/test_model_hosp_admissions.py @@ -526,7 +526,7 @@ def test_model_hosp_with_obs_model_weekday_phosp(): observed_admissions = PoissonObservation("poisson_rv") # Other random components - total_length = n_obs_to_generate + pad_size + gen_int.size() + total_length = n_obs_to_generate + pad_size + 1 # gen_int.size() weekday = jnp.array([1, 1, 1, 1, 2, 2, 2]) weekday = weekday / weekday.sum() From b4c5ca28b389c418e38eb10431c83b62143586a7 Mon Sep 17 00:00:00 2001 From: sbidari Date: Thu, 22 Aug 2024 15:46:24 -0400 Subject: [PATCH 09/18] renaming helper function, add n_initialization_point --- docs/source/tutorials/day_of_the_week.qmd | 4 +++- .../tutorials/hospital_admissions_model.qmd | 3 ++- src/pyrenew/latent/hospitaladmissions.py | 4 ++-- src/pyrenew/metaclass.py | 3 ++- src/test/test_latent_admissions.py | 3 ++- src/test/test_model_hosp_admissions.py | 23 ++++++++++++++----- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/docs/source/tutorials/day_of_the_week.qmd b/docs/source/tutorials/day_of_the_week.qmd index 6cf411f9..d1c47e09 100644 --- a/docs/source/tutorials/day_of_the_week.qmd +++ b/docs/source/tutorials/day_of_the_week.qmd @@ -77,6 +77,8 @@ from pyrenew.latent import ( # Infection process latent_inf = latent.Infections() +n_initialization_points = max(gen_int_array.size, inf_hosp_int_array.size) + I0 = InfectionInitializationProcess( "I0_initialization", metaclass.DistributionalRV( @@ -84,7 +86,7 @@ I0 = InfectionInitializationProcess( distribution=dist.LogNormal(loc=jnp.log(100), scale=jnp.log(1.75)), ), InitializeInfectionsExponentialGrowth( - inf_hosp_int_array.size, + n_initialization_points, deterministic.DeterministicVariable(name="rate", value=0.05), ), t_unit=1, diff --git a/docs/source/tutorials/hospital_admissions_model.qmd b/docs/source/tutorials/hospital_admissions_model.qmd index 259eeb30..d7fb2c6f 100644 --- a/docs/source/tutorials/hospital_admissions_model.qmd +++ b/docs/source/tutorials/hospital_admissions_model.qmd @@ -168,6 +168,7 @@ from pyrenew.latent import ( # Infection process latent_inf = latent.Infections() +n_initialization_points = max(gen_int_array.size, inf_hosp_int_array.size) I0 = InfectionInitializationProcess( "I0_initialization", metaclass.DistributionalRV( @@ -175,7 +176,7 @@ I0 = InfectionInitializationProcess( distribution=dist.LogNormal(loc=jnp.log(100), scale=jnp.log(1.75)), ), InitializeInfectionsExponentialGrowth( - inf_hosp_int_array.size, + n_initialization_points, deterministic.DeterministicVariable(name="rate", value=0.05), ), t_unit=1, diff --git a/src/pyrenew/latent/hospitaladmissions.py b/src/pyrenew/latent/hospitaladmissions.py index 9d900146..382a8522 100644 --- a/src/pyrenew/latent/hospitaladmissions.py +++ b/src/pyrenew/latent/hospitaladmissions.py @@ -13,7 +13,7 @@ from pyrenew.metaclass import ( RandomVariable, SampledValue, - compute_incidence_observed_with_delay, + compute_delay_ascertained_incidence, ) @@ -214,7 +214,7 @@ def sample( *_, ) = self.infection_to_admission_interval_rv(**kwargs) - latent_hospital_admissions = compute_incidence_observed_with_delay( + latent_hospital_admissions = compute_delay_ascertained_incidence( infection_hosp_rate.value, latent_infections.value, infection_to_admission_interval.value, diff --git a/src/pyrenew/metaclass.py b/src/pyrenew/metaclass.py index 98422276..44d8278a 100644 --- a/src/pyrenew/metaclass.py +++ b/src/pyrenew/metaclass.py @@ -8,6 +8,7 @@ from typing import Callable, NamedTuple, Self, get_type_hints import jax +import jax.numpy as jnp import jax.random as jr import matplotlib.pyplot as plt import numpy as np @@ -125,7 +126,7 @@ def _assert_sample_and_rtype( return None -def compute_incidence_observed_with_delay( +def compute_delay_ascertained_incidence( incidence_to_observation_rate: ArrayLike, latent_incidence: ArrayLike, incidence_to_observation_delay_interval: ArrayLike, diff --git a/src/test/test_latent_admissions.py b/src/test/test_latent_admissions.py index c7e0a64b..526fbc31 100644 --- a/src/test/test_latent_admissions.py +++ b/src/test/test_latent_admissions.py @@ -22,9 +22,10 @@ def test_admissions_sample(): # Generating Rt and Infections to compute the hospital admissions rt = SimpleRt() + n_steps = 30 with numpyro.handlers.seed(rng_seed=223): - sim_rt = rt(n_steps=30)[0].value + sim_rt = rt(n=n_steps)[0].value gen_int = jnp.array([0.5, 0.1, 0.1, 0.2, 0.1]) inf_hosp_int_array = jnp.array( diff --git a/src/test/test_model_hosp_admissions.py b/src/test/test_model_hosp_admissions.py index 977e15a7..293c8ea5 100644 --- a/src/test/test_model_hosp_admissions.py +++ b/src/test/test_model_hosp_admissions.py @@ -222,11 +222,12 @@ def test_model_hosp_no_obs_model(): ] ), ) + n_initialization_points = max(gen_int.size(), inf_hosp.size()) I0 = InfectionInitializationProcess( "I0_initialization", DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), - InitializeInfectionsZeroPad(n_timepoints=inf_hosp.size()), + InitializeInfectionsZeroPad(n_timepoints=n_initialization_points), t_unit=1, ) @@ -259,7 +260,9 @@ def test_model_hosp_no_obs_model(): with numpyro.handlers.seed(rng_seed=223): model1_samp = model0.sample(n_datapoints=30) - np.testing.assert_array_almost_equal(model0_samp.Rt.value, model1_samp.Rt.value) + np.testing.assert_array_almost_equal( + model0_samp.Rt.value, model1_samp.Rt.value + ) np.testing.assert_array_equal( model0_samp.latent_infections.value, model1_samp.latent_infections.value, @@ -331,10 +334,12 @@ def test_model_hosp_with_obs_model(): ), ) + n_initialization_points = max(gen_int.size(), inf_hosp.size()) + I0 = InfectionInitializationProcess( "I0_initialization", DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), - InitializeInfectionsZeroPad(n_timepoints=inf_hosp.size()), + InitializeInfectionsZeroPad(n_timepoints=n_initialization_points), t_unit=1, ) @@ -418,10 +423,12 @@ def test_model_hosp_with_obs_model_weekday_phosp_2(): ), ) + n_initialization_points = max(gen_int.size(), inf_hosp.size()) + I0 = InfectionInitializationProcess( "I0_initialization", DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), - InitializeInfectionsZeroPad(n_timepoints=inf_hosp.size()), + InitializeInfectionsZeroPad(n_timepoints=n_initialization_points), t_unit=1, ) @@ -511,10 +518,12 @@ def test_model_hosp_with_obs_model_weekday_phosp(): ), ) + n_initialization_points = max(gen_int.size(), inf_hosp.size()) + I0 = InfectionInitializationProcess( "I0_initialization", DistributionalRV(name="I0", distribution=dist.LogNormal(0, 1)), - InitializeInfectionsZeroPad(n_timepoints=inf_hosp.size()), + InitializeInfectionsZeroPad(n_timepoints=n_initialization_points), t_unit=1, ) @@ -561,7 +570,9 @@ def test_model_hosp_with_obs_model_weekday_phosp(): # Sampling and fitting model 0 (with no obs for infections) with numpyro.handlers.seed(rng_seed=223): - model1_samp = model1.sample(n_datapoints=n_obs_to_generate, padding=pad_size) + model1_samp = model1.sample( + n_datapoints=n_obs_to_generate, padding=pad_size + ) # Showed during merge conflict, but unsure if it will be needed # pad_size = 5 From ed19002cdc626943538a0a5af1d9cfc1a7524dff Mon Sep 17 00:00:00 2001 From: Subekshya Bidari <37636707+sbidari@users.noreply.github.com> Date: Thu, 22 Aug 2024 19:22:18 -0400 Subject: [PATCH 10/18] Apply suggestions from code review Co-authored-by: Dylan H. Morris --- pyrenew/metaclass.py | 16 +++++++++++++--- test/test_model_hosp_admissions.py | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pyrenew/metaclass.py b/pyrenew/metaclass.py index 44d8278a..439921fa 100644 --- a/pyrenew/metaclass.py +++ b/pyrenew/metaclass.py @@ -139,16 +139,26 @@ def compute_delay_ascertained_incidence( Parameters ---------- incidence_to_observation_rate: ArrayLike - The rate at which latent incidences are observed. + The rate at which latent incident counts translated into observed counts. + For example, setting ``incidence_to_observation_rate=0.001`` + when the incident counts are infections and the observed counts are + reported hospital admissions could be used to model disease and population + for which the probability (reported) hospital.admission given infection is + 0.001. latent_incidence: ArrayLike Incidence values based on the true underlying process. incidence_to_observation_delay_interval: ArrayLike - Pmf of delay interval between incidence to observation. + Probability mass function of delay interval from incidence to observation, + where the :math`i^{th}` entry (0-indexed) represents a delay of :math:`1+i` + time units, i.e. ``incidence_to_observation_delay_interval[0]`` represents + the fraction of observations that are delayed 1 time unit, + ``incidence_to_observation_delay_interval[1]`` represents the fraction + that are delayed 2 time units, et cetera. Returns -------- ArrayLike - The incidence after the observation delay. + The predicted timeseries of delayed observations. """ delay_obs_incidence = jnp.convolve( incidence_to_observation_rate * latent_incidence, diff --git a/test/test_model_hosp_admissions.py b/test/test_model_hosp_admissions.py index 293c8ea5..58e64cbb 100644 --- a/test/test_model_hosp_admissions.py +++ b/test/test_model_hosp_admissions.py @@ -533,7 +533,7 @@ def test_model_hosp_with_obs_model_weekday_phosp(): observed_admissions = PoissonObservation("poisson_rv") # Other random components - total_length = n_obs_to_generate + pad_size + 1 # gen_int.size() + total_length = n_obs_to_generate + pad_size + 1 weekday = jnp.array([1, 1, 1, 1, 2, 2, 2]) weekday = weekday / weekday.sum() From 9095259a8dab33bcea0a9563433e913d341f0cac Mon Sep 17 00:00:00 2001 From: sbidari Date: Fri, 23 Aug 2024 11:00:47 -0400 Subject: [PATCH 11/18] move helper function from metaclass to convolve.py --- .../tutorials/hospital_admissions_model.qmd | 4 +- pyrenew/convolve.py | 42 ++++++++++++++++++ pyrenew/latent/hospitaladmissions.py | 7 +-- pyrenew/metaclass.py | 43 ------------------- test/test_incidence_observed_with_delay.py | 2 +- 5 files changed, 48 insertions(+), 50 deletions(-) diff --git a/docs/source/tutorials/hospital_admissions_model.qmd b/docs/source/tutorials/hospital_admissions_model.qmd index d7fb2c6f..2b85416c 100644 --- a/docs/source/tutorials/hospital_admissions_model.qmd +++ b/docs/source/tutorials/hospital_admissions_model.qmd @@ -393,7 +393,9 @@ We can also look at credible intervals for the posterior distribution of latent ```{python} # | label: fig-output-infections-distribution # | fig-cap: Posterior Latent Infections -x_data = idata.posterior["all_latent_infections_dim_0"] +x_data = ( + idata.posterior["all_latent_infections_dim_0"] - n_initialization_points +) y_data = idata.posterior["all_latent_infections"] fig, axes = plt.subplots(figsize=(6, 5)) diff --git a/pyrenew/convolve.py b/pyrenew/convolve.py index 44797805..3c116e99 100755 --- a/pyrenew/convolve.py +++ b/pyrenew/convolve.py @@ -165,3 +165,45 @@ def _new_scanner( return latest, (new_val, m_net1) return _new_scanner + + +def compute_delay_ascertained_incidence( + incidence_to_observation_rate: ArrayLike, + latent_incidence: ArrayLike, + incidence_to_observation_delay_interval: ArrayLike, +) -> ArrayLike: + """ + Computes incidences observed according + to a given observation rate and based + on a delay interval. + + Parameters + ---------- + incidence_to_observation_rate: ArrayLike + The rate at which latent incident counts translated into observed counts. + For example, setting ``incidence_to_observation_rate=0.001`` + when the incident counts are infections and the observed counts are + reported hospital admissions could be used to model disease and population + for which the probability (reported) hospital.admission given infection is + 0.001. + latent_incidence: ArrayLike + Incidence values based on the true underlying process. + incidence_to_observation_delay_interval: ArrayLike + Probability mass function of delay interval from incidence to observation, + where the :math`i^{th}` entry (0-indexed) represents a delay of :math:`1+i` + time units, i.e. ``incidence_to_observation_delay_interval[0]`` represents + the fraction of observations that are delayed 1 time unit, + ``incidence_to_observation_delay_interval[1]`` represents the fraction + that are delayed 2 time units, et cetera. + + Returns + -------- + ArrayLike + The predicted timeseries of delayed observations. + """ + delay_obs_incidence = jnp.convolve( + incidence_to_observation_rate * latent_incidence, + incidence_to_observation_delay_interval, + mode="valid", + ) + return delay_obs_incidence diff --git a/pyrenew/latent/hospitaladmissions.py b/pyrenew/latent/hospitaladmissions.py index 382a8522..55689b8b 100644 --- a/pyrenew/latent/hospitaladmissions.py +++ b/pyrenew/latent/hospitaladmissions.py @@ -9,12 +9,9 @@ import numpyro import pyrenew.arrayutils as au +from pyrenew.convolve import compute_delay_ascertained_incidence from pyrenew.deterministic import DeterministicVariable -from pyrenew.metaclass import ( - RandomVariable, - SampledValue, - compute_delay_ascertained_incidence, -) +from pyrenew.metaclass import RandomVariable, SampledValue class HospitalAdmissionsSample(NamedTuple): diff --git a/pyrenew/metaclass.py b/pyrenew/metaclass.py index 439921fa..424ffead 100644 --- a/pyrenew/metaclass.py +++ b/pyrenew/metaclass.py @@ -8,7 +8,6 @@ from typing import Callable, NamedTuple, Self, get_type_hints import jax -import jax.numpy as jnp import jax.random as jr import matplotlib.pyplot as plt import numpy as np @@ -126,48 +125,6 @@ def _assert_sample_and_rtype( return None -def compute_delay_ascertained_incidence( - incidence_to_observation_rate: ArrayLike, - latent_incidence: ArrayLike, - incidence_to_observation_delay_interval: ArrayLike, -) -> ArrayLike: - """ - Computes incidences observed according - to a given observation rate and based - on a delay interval. - - Parameters - ---------- - incidence_to_observation_rate: ArrayLike - The rate at which latent incident counts translated into observed counts. - For example, setting ``incidence_to_observation_rate=0.001`` - when the incident counts are infections and the observed counts are - reported hospital admissions could be used to model disease and population - for which the probability (reported) hospital.admission given infection is - 0.001. - latent_incidence: ArrayLike - Incidence values based on the true underlying process. - incidence_to_observation_delay_interval: ArrayLike - Probability mass function of delay interval from incidence to observation, - where the :math`i^{th}` entry (0-indexed) represents a delay of :math:`1+i` - time units, i.e. ``incidence_to_observation_delay_interval[0]`` represents - the fraction of observations that are delayed 1 time unit, - ``incidence_to_observation_delay_interval[1]`` represents the fraction - that are delayed 2 time units, et cetera. - - Returns - -------- - ArrayLike - The predicted timeseries of delayed observations. - """ - delay_obs_incidence = jnp.convolve( - incidence_to_observation_rate * latent_incidence, - incidence_to_observation_delay_interval, - mode="valid", - ) - return delay_obs_incidence - - class SampledValue(NamedTuple): """ A container for a value sampled from a RandomVariable. diff --git a/test/test_incidence_observed_with_delay.py b/test/test_incidence_observed_with_delay.py index c98eebbc..9bff64ad 100644 --- a/test/test_incidence_observed_with_delay.py +++ b/test/test_incidence_observed_with_delay.py @@ -4,7 +4,7 @@ import pytest from numpy.testing import assert_array_equal -from pyrenew.metaclass import compute_delay_ascertained_incidence +from pyrenew.convolve import compute_delay_ascertained_incidence @pytest.mark.parametrize( From c69d6ccfd717cd0b9313fc92bb0808b3f83de165 Mon Sep 17 00:00:00 2001 From: sbidari Date: Fri, 23 Aug 2024 11:25:19 -0400 Subject: [PATCH 12/18] uniformize starting point of all plots --- docs/source/tutorials/hospital_admissions_model.qmd | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/docs/source/tutorials/hospital_admissions_model.qmd b/docs/source/tutorials/hospital_admissions_model.qmd index 2b85416c..4b82d467 100644 --- a/docs/source/tutorials/hospital_admissions_model.qmd +++ b/docs/source/tutorials/hospital_admissions_model.qmd @@ -378,16 +378,6 @@ axes.set_ylabel("Hospital Admissions", fontsize=10) plt.show() ``` -We can look at individual draws from the posterior distribution of latent infections: - -```{python} -# | label: fig-output-infections -# | fig-cap: Latent infections -out2 = hosp_model.plot_posterior( - var="all_latent_infections", ylab="Latent Infections" -) -``` - We can also look at credible intervals for the posterior distribution of latent infections: ```{python} From c28ec02f5409bf7c44fd730a49244b4c036305b4 Mon Sep 17 00:00:00 2001 From: sbidari Date: Fri, 23 Aug 2024 13:04:50 -0400 Subject: [PATCH 13/18] adopt new var names --- pyrenew/convolve.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pyrenew/convolve.py b/pyrenew/convolve.py index 3c116e99..2ab0b647 100755 --- a/pyrenew/convolve.py +++ b/pyrenew/convolve.py @@ -168,9 +168,9 @@ def _new_scanner( def compute_delay_ascertained_incidence( - incidence_to_observation_rate: ArrayLike, + p_observed_given_incident: ArrayLike, latent_incidence: ArrayLike, - incidence_to_observation_delay_interval: ArrayLike, + delay_incidence_to_observation_delay_pmf: ArrayLike, ) -> ArrayLike: """ Computes incidences observed according @@ -179,21 +179,21 @@ def compute_delay_ascertained_incidence( Parameters ---------- - incidence_to_observation_rate: ArrayLike - The rate at which latent incident counts translated into observed counts. - For example, setting ``incidence_to_observation_rate=0.001`` + p_observed_given_incident: ArrayLike + The rate at which latent incident counts translate into observed counts. + For example, setting ``p_observed_given_incident=0.001`` when the incident counts are infections and the observed counts are reported hospital admissions could be used to model disease and population for which the probability (reported) hospital.admission given infection is 0.001. latent_incidence: ArrayLike Incidence values based on the true underlying process. - incidence_to_observation_delay_interval: ArrayLike + delay_incidence_to_observation_delay_pmf: ArrayLike Probability mass function of delay interval from incidence to observation, where the :math`i^{th}` entry (0-indexed) represents a delay of :math:`1+i` - time units, i.e. ``incidence_to_observation_delay_interval[0]`` represents + time units, i.e. ``delay_incidence_to_observation_delay_pmf[0]`` represents the fraction of observations that are delayed 1 time unit, - ``incidence_to_observation_delay_interval[1]`` represents the fraction + ``delay_incidence_to_observation_delay_pmf[1]`` represents the fraction that are delayed 2 time units, et cetera. Returns @@ -202,8 +202,8 @@ def compute_delay_ascertained_incidence( The predicted timeseries of delayed observations. """ delay_obs_incidence = jnp.convolve( - incidence_to_observation_rate * latent_incidence, - incidence_to_observation_delay_interval, + p_observed_given_incident * latent_incidence, + delay_incidence_to_observation_delay_pmf, mode="valid", ) return delay_obs_incidence From 4bc1736d0e1fd2485975cb9d18a44e021896fa8d Mon Sep 17 00:00:00 2001 From: sbidari Date: Fri, 23 Aug 2024 13:13:34 -0400 Subject: [PATCH 14/18] fix var names --- pyrenew/convolve.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pyrenew/convolve.py b/pyrenew/convolve.py index 2ab0b647..78a8dd7f 100755 --- a/pyrenew/convolve.py +++ b/pyrenew/convolve.py @@ -170,7 +170,7 @@ def _new_scanner( def compute_delay_ascertained_incidence( p_observed_given_incident: ArrayLike, latent_incidence: ArrayLike, - delay_incidence_to_observation_delay_pmf: ArrayLike, + delay_incidence_to_observation_pmf: ArrayLike, ) -> ArrayLike: """ Computes incidences observed according @@ -188,12 +188,12 @@ def compute_delay_ascertained_incidence( 0.001. latent_incidence: ArrayLike Incidence values based on the true underlying process. - delay_incidence_to_observation_delay_pmf: ArrayLike + delay_incidence_to_observation_pmf: ArrayLike Probability mass function of delay interval from incidence to observation, where the :math`i^{th}` entry (0-indexed) represents a delay of :math:`1+i` - time units, i.e. ``delay_incidence_to_observation_delay_pmf[0]`` represents + time units, i.e. ``delay_incidence_to_observation_pmf[0]`` represents the fraction of observations that are delayed 1 time unit, - ``delay_incidence_to_observation_delay_pmf[1]`` represents the fraction + ``delay_incidence_to_observation_pmf[1]`` represents the fraction that are delayed 2 time units, et cetera. Returns @@ -203,7 +203,7 @@ def compute_delay_ascertained_incidence( """ delay_obs_incidence = jnp.convolve( p_observed_given_incident * latent_incidence, - delay_incidence_to_observation_delay_pmf, + delay_incidence_to_observation_pmf, mode="valid", ) return delay_obs_incidence From ea723330c9c9229735397cc92f574d4121a2d3c9 Mon Sep 17 00:00:00 2001 From: sbidari Date: Fri, 23 Aug 2024 14:47:59 -0400 Subject: [PATCH 15/18] fix docstring --- pyrenew/convolve.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pyrenew/convolve.py b/pyrenew/convolve.py index 78a8dd7f..1d6f530c 100755 --- a/pyrenew/convolve.py +++ b/pyrenew/convolve.py @@ -184,17 +184,17 @@ def compute_delay_ascertained_incidence( For example, setting ``p_observed_given_incident=0.001`` when the incident counts are infections and the observed counts are reported hospital admissions could be used to model disease and population - for which the probability (reported) hospital.admission given infection is - 0.001. + for which the probability of a latent infection leading to a reported + hospital admission is 0.001. latent_incidence: ArrayLike Incidence values based on the true underlying process. delay_incidence_to_observation_pmf: ArrayLike Probability mass function of delay interval from incidence to observation, - where the :math`i^{th}` entry (0-indexed) represents a delay of :math:`1+i` + where the :math`i^{th}` entry represents a delay of :math:`i` time units, i.e. ``delay_incidence_to_observation_pmf[0]`` represents - the fraction of observations that are delayed 1 time unit, + the fraction of observations that are delayed 0 time unit, ``delay_incidence_to_observation_pmf[1]`` represents the fraction - that are delayed 2 time units, et cetera. + that are delayed 1 time units, et cetera. Returns -------- From 5c4395ffc5d7a5b4d58afc1525e21028287f5449 Mon Sep 17 00:00:00 2001 From: sbidari Date: Fri, 23 Aug 2024 15:23:58 -0400 Subject: [PATCH 16/18] update n_initialization_points --- docs/source/tutorials/day_of_the_week.qmd | 2 +- docs/source/tutorials/hospital_admissions_model.qmd | 2 +- test/test_model_hosp_admissions.py | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/source/tutorials/day_of_the_week.qmd b/docs/source/tutorials/day_of_the_week.qmd index d1c47e09..43228731 100644 --- a/docs/source/tutorials/day_of_the_week.qmd +++ b/docs/source/tutorials/day_of_the_week.qmd @@ -77,7 +77,7 @@ from pyrenew.latent import ( # Infection process latent_inf = latent.Infections() -n_initialization_points = max(gen_int_array.size, inf_hosp_int_array.size) +n_initialization_points = max(gen_int_array.size, inf_hosp_int_array.size) - 1 I0 = InfectionInitializationProcess( "I0_initialization", diff --git a/docs/source/tutorials/hospital_admissions_model.qmd b/docs/source/tutorials/hospital_admissions_model.qmd index 4b82d467..c9dca3b0 100644 --- a/docs/source/tutorials/hospital_admissions_model.qmd +++ b/docs/source/tutorials/hospital_admissions_model.qmd @@ -168,7 +168,7 @@ from pyrenew.latent import ( # Infection process latent_inf = latent.Infections() -n_initialization_points = max(gen_int_array.size, inf_hosp_int_array.size) +n_initialization_points = max(gen_int_array.size, inf_hosp_int_array.size) - 1 I0 = InfectionInitializationProcess( "I0_initialization", metaclass.DistributionalRV( diff --git a/test/test_model_hosp_admissions.py b/test/test_model_hosp_admissions.py index 58e64cbb..bb740944 100644 --- a/test/test_model_hosp_admissions.py +++ b/test/test_model_hosp_admissions.py @@ -222,7 +222,7 @@ def test_model_hosp_no_obs_model(): ] ), ) - n_initialization_points = max(gen_int.size(), inf_hosp.size()) + n_initialization_points = max(gen_int.size(), inf_hosp.size()) - 1 I0 = InfectionInitializationProcess( "I0_initialization", @@ -334,7 +334,7 @@ def test_model_hosp_with_obs_model(): ), ) - n_initialization_points = max(gen_int.size(), inf_hosp.size()) + n_initialization_points = max(gen_int.size(), inf_hosp.size()) - 1 I0 = InfectionInitializationProcess( "I0_initialization", @@ -423,7 +423,7 @@ def test_model_hosp_with_obs_model_weekday_phosp_2(): ), ) - n_initialization_points = max(gen_int.size(), inf_hosp.size()) + n_initialization_points = max(gen_int.size(), inf_hosp.size()) - 1 I0 = InfectionInitializationProcess( "I0_initialization", @@ -518,7 +518,7 @@ def test_model_hosp_with_obs_model_weekday_phosp(): ), ) - n_initialization_points = max(gen_int.size(), inf_hosp.size()) + n_initialization_points = max(gen_int.size(), inf_hosp.size()) - 1 I0 = InfectionInitializationProcess( "I0_initialization", @@ -533,7 +533,7 @@ def test_model_hosp_with_obs_model_weekday_phosp(): observed_admissions = PoissonObservation("poisson_rv") # Other random components - total_length = n_obs_to_generate + pad_size + 1 + total_length = n_obs_to_generate + pad_size weekday = jnp.array([1, 1, 1, 1, 2, 2, 2]) weekday = weekday / weekday.sum() From 554301bf5da72d1e724f3179cb3c286acd7fa042 Mon Sep 17 00:00:00 2001 From: Damon Bayer Date: Mon, 26 Aug 2024 10:29:03 -0500 Subject: [PATCH 17/18] Update pyrenew/convolve.py --- pyrenew/convolve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrenew/convolve.py b/pyrenew/convolve.py index 1d6f530c..4310d022 100755 --- a/pyrenew/convolve.py +++ b/pyrenew/convolve.py @@ -190,7 +190,7 @@ def compute_delay_ascertained_incidence( Incidence values based on the true underlying process. delay_incidence_to_observation_pmf: ArrayLike Probability mass function of delay interval from incidence to observation, - where the :math`i^{th}` entry represents a delay of :math:`i` + where the :math:`i`th entry represents a delay of :math:`i` time units, i.e. ``delay_incidence_to_observation_pmf[0]`` represents the fraction of observations that are delayed 0 time unit, ``delay_incidence_to_observation_pmf[1]`` represents the fraction From 3efcfad0a231b6b17a55de7154e51aaa8b7826d1 Mon Sep 17 00:00:00 2001 From: Damon Bayer Date: Mon, 26 Aug 2024 10:58:51 -0500 Subject: [PATCH 18/18] Update pyrenew/convolve.py --- pyrenew/convolve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrenew/convolve.py b/pyrenew/convolve.py index 4310d022..de98b04f 100755 --- a/pyrenew/convolve.py +++ b/pyrenew/convolve.py @@ -190,7 +190,7 @@ def compute_delay_ascertained_incidence( Incidence values based on the true underlying process. delay_incidence_to_observation_pmf: ArrayLike Probability mass function of delay interval from incidence to observation, - where the :math:`i`th entry represents a delay of :math:`i` + where the :math:`i`\th entry represents a delay of :math:`i` time units, i.e. ``delay_incidence_to_observation_pmf[0]`` represents the fraction of observations that are delayed 0 time unit, ``delay_incidence_to_observation_pmf[1]`` represents the fraction