Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaming variables throughout for clarity #179

Merged
merged 10 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions model/docs/example_with_datasets.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,18 @@ from pyrenew import latent, deterministic, metaclass
import jax.numpy as jnp
import numpyro.distributions as dist

inf_hosp_int = deterministic.DeterministicPMF(inf_hosp_int, name="inf_hosp_int")
inf_hosp_int = deterministic.DeterministicPMF(
inf_hosp_int, name="inf_hosp_int"
)

hosp_rate = metaclass.DistributionalRV(
dist=dist.LogNormal(jnp.log(0.05), 0.1),
name="IHR",
)

latent_hosp = latent.HospitalAdmissions(
infection_to_admission_interval=inf_hosp_int,
infect_hosp_rate_dist=hosp_rate,
infection_to_admission_interval_rv=inf_hosp_int,
infect_hosp_rate_rv=hosp_rate,
)
```

Expand Down Expand Up @@ -173,12 +175,12 @@ Notice all the components are `RandomVariable` instances. We can now build the m
```{python}
# | label: init-model
hosp_model = model.HospitalAdmissionsModel(
latent_infections=latent_inf,
latent_admissions=latent_hosp,
I0=I0,
gen_int=gen_int,
Rt_process=rtproc,
observation_process=obs,
latent_infections_rv=latent_inf,
latent_hosp_admissions_rv=latent_hosp,
I0_rv=I0,
gen_int_rv=gen_int,
Rt_process_rv=rtproc,
hosp_admission_obs_process_rv=obs,
)
```

Expand Down Expand Up @@ -208,7 +210,7 @@ axs[0].plot(sim_data.Rt)
axs[0].set_ylabel("Rt")

# Infections plot
axs[1].plot(sim_data.sampled_admissions)
axs[1].plot(sim_data.sampled_observed_hosp_admissions)
axs[1].set_ylabel("Infections")
axs[1].set_yscale("log")

Expand All @@ -230,7 +232,7 @@ import jax
hosp_model.run(
num_samples=2000,
num_warmup=2000,
observed_admissions=dat["daily_hosp_admits"].to_numpy(),
observed_hosp_admissions=dat["daily_hosp_admits"].to_numpy(),
rng_key=jax.random.PRNGKey(54),
mcmc_args=dict(progress_bar=False),
)
Expand All @@ -244,7 +246,7 @@ We can use the `plot_posterior` method to visualize the results[^capture]:
# | label: fig-output-hospital-admissions
# | fig-cap: Hospital Admissions posterior distribution
out = hosp_model.plot_posterior(
var="predicted_admissions",
var="observed_hosp_admissions",
ylab="Hospital Admissions",
obs_signal=dat["daily_hosp_admits"].to_numpy(),
)
Expand All @@ -268,7 +270,7 @@ dat_w_padding = np.hstack((np.repeat(np.nan, days_to_impute), dat_w_padding))
hosp_model.run(
num_samples=2000,
num_warmup=2000,
observed_admissions=dat_w_padding,
observed_hosp_admissions=dat_w_padding,
rng_key=jax.random.PRNGKey(54),
mcmc_args=dict(progress_bar=False),
padding=days_to_impute, # Padding the model
Expand All @@ -281,7 +283,7 @@ And plotting the results:
# | label: fig-output-admissions-with-padding
# | fig-cap: Hospital Admissions posterior distribution
out = hosp_model.plot_posterior(
var="predicted_admissions",
var="observed_hosp_admissions",
ylab="Hospital Admissions",
obs_signal=dat_w_padding,
)
Expand Down Expand Up @@ -343,18 +345,18 @@ Notice that the instance's `nweeks` and `len` members are passed during construc
```{python}
# | label: latent-hosp-weekday
latent_hosp_wday_effect = latent.HospitalAdmissions(
infection_to_admission_interval=inf_hosp_int,
infect_hosp_rate_dist=hosp_rate,
weekday_effect_dist=weekday_effect,
infection_to_admission_interval_rv=inf_hosp_int,
infect_hosp_rate_rv=hosp_rate,
weekday_effect_rv=weekday_effect,
)

hosp_model_weekday = model.HospitalAdmissionsModel(
latent_infections=latent_inf,
latent_admissions=latent_hosp_wday_effect,
I0=I0,
gen_int=gen_int,
Rt_process=rtproc,
observation_process=obs,
latent_infections_rv=latent_inf,
latent_hosp_admissions_rv=latent_hosp_wday_effect,
I0_rv=I0,
gen_int_rv=gen_int,
Rt_process_rv=rtproc,
hosp_admission_obs_process_rv=obs,
)
```

Expand All @@ -365,7 +367,7 @@ Running the model (with the same padding as before):
hosp_model_weekday.run(
num_samples=2000,
num_warmup=2000,
observed_admissions=dat_w_padding,
observed_hosp_admissions=dat_w_padding,
rng_key=jax.random.PRNGKey(54),
mcmc_args=dict(progress_bar=False),
padding=days_to_impute,
Expand All @@ -378,7 +380,7 @@ And plotting the results:
# | label: fig-output-admissions-padding-and-weekday
# | fig-cap: Hospital Admissions posterior distribution
out = hosp_model_weekday.plot_posterior(
var="predicted_admissions",
var="observed_hosp_admissions",
ylab="Hospital Admissions",
obs_signal=dat_w_padding,
)
Expand Down
28 changes: 14 additions & 14 deletions model/docs/pyrenew_demo.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ inf_hosp_int = DeterministicPMF(
)

latent_admissions = HospitalAdmissions(
infection_to_admission_interval=inf_hosp_int,
infect_hosp_rate_dist=DistributionalRV(
infection_to_admission_interval_rv=inf_hosp_int,
infect_hosp_rate_rv=DistributionalRV(
dist=dist.LogNormal(jnp.log(0.05), 0.05), name="IHR"
),
)
Expand All @@ -131,12 +131,12 @@ The `HospitalAdmissionsModel` is then initialized using the initial conditions j
```{python}
# Initializing the model
hospmodel = HospitalAdmissionsModel(
gen_int=gen_int,
I0=I0,
latent_admissions=latent_admissions,
observation_process=admissions_process,
latent_infections=latent_infections,
Rt_process=Rt_process,
gen_int_rv=gen_int,
I0_rv=I0,
latent_hosp_admissions_rv=latent_admissions,
hosp_admission_obs_process_rv=admissions_process,
latent_infections_rv=latent_infections,
Rt_process_rv=Rt_process,
)
```

Expand All @@ -151,13 +151,13 @@ x
Visualizations of the single model output show (top) infections over the 30 time steps, (middle) hospital admissions over the 30 time steps, and (bottom)

```{python}
#| label: fig-hosp
#| fig-cap: Infections
# | label: fig-hosp
# | fig-cap: Infections
fig, ax = plt.subplots(nrows=3, sharex=True)
ax[0].plot(x.latent_infections)
ax[0].set_ylim([1/5, 5])
ax[1].plot(x.latent_admissions)
ax[2].plot(x.sampled_admissions, 'o')
ax[0].set_ylim([1 / 5, 5])
ax[1].plot(x.latent_hosp_admissions)
ax[2].plot(x.sampled_observed_hosp_admissions, "o")
for axis in ax[:-1]:
axis.set_yscale("log")
```
Expand All @@ -169,7 +169,7 @@ To fit the `hospmodel` to the simulated data, we call `hospmodel.run()`, an MCMC
hospmodel.run(
num_warmup=1000,
num_samples=1000,
observed_admissions=x.sampled_admissions,
observed_hosp_admissions=x.sampled_observed_hosp_admissions,
rng_key=jax.random.PRNGKey(54),
mcmc_args=dict(progress_bar=False),
)
Expand Down
17 changes: 17 additions & 0 deletions model/pyproject.toml
AFg6K7h4fhy2 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ pytest-cov = "^5.0.0"
pytest-mpl = "^0.17.0"
numpydoc = "^1.7.0"

[tool.numpydoc_validation]
checks = [
"GL03",
"GL08",
"SS01",
"PR03",
"PR04",
"PR07",
"RT01"
]
ignore = [
"ES01",
"SA01",
"EX01",
"SS06",
"RT05"
]

[build-system]
requires = ["poetry-core"]
Expand Down
6 changes: 3 additions & 3 deletions model/src/pyrenew/deterministic/nullrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def validate() -> None:

def sample(
self,
predicted: ArrayLike,
mu: ArrayLike,
obs: ArrayLike | None = None,
name: str | None = None,
**kwargs,
Expand All @@ -139,8 +139,8 @@ def sample(

Parameters
----------
predicted : ArrayLike
Rate parameter of the Poisson distribution.
mu : ArrayLike
Rate parameter of the Poisson distribution. #TODO
AFg6K7h4fhy2 marked this conversation as resolved.
Show resolved Hide resolved
obs : ArrayLike, optional
Observed data. Defaults to None.
name : str, optional
Expand Down
Loading
Loading