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

remove default name arg #306

Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/source/tutorials/periodic_effects.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ from pyrenew import process, deterministic
```{python}
# The random process for Rt
rt_proc = process.RtWeeklyDiffProcess(
name="rt_weekly_diff",
offset=0,
log_rt_prior=deterministic.DeterministicVariable(
jnp.array([0.1, 0.2]), name="log_rt_prior"
Expand Down
2 changes: 1 addition & 1 deletion model/src/pyrenew/deterministic/deterministic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(
----------
vars : ArrayLike
A tuple with arraylike objects.
name : str, optional
name : str
A name to assign to the process.

Returns
Expand Down
3 changes: 0 additions & 3 deletions model/src/pyrenew/deterministic/nullrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ def sample(
self,
mu: ArrayLike,
obs: ArrayLike | None = None,
name: str | None = None,
**kwargs,
) -> tuple:
"""
Expand All @@ -143,8 +142,6 @@ def sample(
Unused parameter, represents mean of non-null distributions
obs : ArrayLike, optional
Observed data. Defaults to None.
name : str, optional
Name of the random variable. Defaults to None.
**kwargs : dict, optional
Additional keyword arguments passed through to internal sample calls, should there be any.

Expand Down
10 changes: 1 addition & 9 deletions model/src/pyrenew/latent/hospitaladmissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def __init__(
self,
infection_to_admission_interval_rv: RandomVariable,
infect_hosp_rate_rv: RandomVariable,
latent_hospital_admissions_varname: str = "latent_hospital_admissions",
day_of_week_effect_rv: RandomVariable | None = None,
hosp_report_prob_rv: RandomVariable | None = None,
) -> None:
Expand All @@ -79,9 +78,6 @@ def __init__(
pyrenew.observations.Deterministic).
infect_hosp_rate_rv : RandomVariable
Infection to hospitalization rate random variable.
latent_hospital_admissions_varname : str
Name to assign to the deterministic component in numpyro of
observed hospital admissions.
day_of_week_effect_rv : RandomVariable, optional
Day of the week effect.
hosp_report_prob_rv : RandomVariable, optional
Expand All @@ -104,10 +100,6 @@ def __init__(
hosp_report_prob_rv,
)

self.latent_hospital_admissions_varname = (
latent_hospital_admissions_varname
)

self.infect_hosp_rate_rv = infect_hosp_rate_rv
self.day_of_week_effect_rv = day_of_week_effect_rv
self.hosp_report_prob_rv = hosp_report_prob_rv
Expand Down Expand Up @@ -200,7 +192,7 @@ def sample(
)

npro.deterministic(
self.latent_hospital_admissions_varname, latent_hospital_admissions
"latent_hospital_admissions", latent_hospital_admissions
)

return HospitalAdmissionsSample(
Expand Down
2 changes: 1 addition & 1 deletion model/src/pyrenew/observation/poisson.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(

Parameters
----------
name : str, optional
name : str
Passed to numpyro.sample.
eps : float, optional
Small value added to the rate parameter to avoid zero values.
Expand Down
12 changes: 6 additions & 6 deletions model/src/pyrenew/process/ar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ARProcess(RandomVariable):

def __init__(
self,
name: str,
mean: float,
autoreg: ArrayLike,
noise_sd: float,
Expand All @@ -29,6 +30,8 @@ def __init__(

Parameters
----------
name : str
Name of the parameter passed to numpyro.sample.
mean: float
Mean parameter.
autoreg : ArrayLike
Expand All @@ -40,6 +43,7 @@ def __init__(
-------
None
"""
self.name = name
self.mean = mean
self.autoreg = autoreg
self.noise_sd = noise_sd
Expand All @@ -48,7 +52,6 @@ def sample(
self,
duration: int,
inits: ArrayLike = None,
name: str = "arprocess",
**kwargs,
) -> tuple:
"""
Expand All @@ -61,9 +64,6 @@ def sample(
inits : ArrayLike, optional
Initial points, if None, then these are sampled.
Defaults to None.
name : str, optional
Name of the parameter passed to numpyro.sample.
Defaults to "arprocess".
**kwargs : dict, optional
Additional keyword arguments passed through to internal sample()
calls, should there be any.
Expand All @@ -76,7 +76,7 @@ def sample(
order = self.autoreg.shape[0]
if inits is None:
inits = numpyro.sample(
name + "_sampled_inits",
self.name + "_sampled_inits",
dist.Normal(0, self.noise_sd).expand((order,)),
)

Expand All @@ -86,7 +86,7 @@ def _ar_scanner(carry, next): # numpydoc ignore=GL08
return new_carry, new_term

noise = numpyro.sample(
name + "_noise",
self.name + "_noise",
dist.Normal(0, self.noise_sd).expand((duration - inits.size,)),
)

Expand Down
13 changes: 8 additions & 5 deletions model/src/pyrenew/process/firstdifferencear.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FirstDifferenceARProcess(RandomVariable):

def __init__(
self,
name: str,
autoreg: ArrayLike,
noise_sd: float,
) -> None:
Expand All @@ -26,6 +27,8 @@ def __init__(

Parameters
----------
name : str
Passed to ARProcess()
autoreg : ArrayLike
Process parameters pyrenew.processesARprocess.
noise_sd : float
Expand All @@ -35,14 +38,16 @@ def __init__(
-------
None
"""
self.rate_of_change_proc = ARProcess(0, jnp.array([autoreg]), noise_sd)
self.rate_of_change_proc = ARProcess(
"arprocess", 0, jnp.array([autoreg]), noise_sd
)
self.name = name

def sample(
self,
duration: int,
init_val: ArrayLike = None,
init_rate_of_change: ArrayLike = None,
name: str = "trend_rw",
**kwargs,
) -> tuple:
"""
Expand All @@ -56,8 +61,6 @@ def sample(
Starting point of the AR process, by default None.
init_rate_of_change : ArrayLike, optional
Passed to ARProcess.sample, by default None.
name : str, optional
Passed to ARProcess(), by default "trend_rw"
**kwargs : dict, optional
Additional keyword arguments passed through to internal sample()
calls, should there be any.
Expand All @@ -70,7 +73,7 @@ def sample(
rates_of_change, *_ = self.rate_of_change_proc.sample(
duration=duration,
inits=jnp.atleast_1d(init_rate_of_change),
name=name + "_rate_of_change",
name=self.name + "_rate_of_change",
)
return (init_val + jnp.cumsum(rates_of_change.flatten()),)

Expand Down
19 changes: 9 additions & 10 deletions model/src/pyrenew/process/rtperiodicdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ class RtPeriodicDiffProcess(RandomVariable):

def __init__(
self,
name: str,
offset: int,
period_size: int,
log_rt_prior: RandomVariable,
autoreg: RandomVariable,
periodic_diff_sd: RandomVariable,
site_name: str = "rt_periodic_diff",
) -> None:
"""
Default constructor for RtPeriodicDiffProcess class.

Parameters
----------
name : str
Name of the site.
offset : int
Relative point at which data starts, must be between 0 and
period_size - 1.
Expand All @@ -66,14 +68,12 @@ def __init__(
Autoregressive parameter.
periodic_diff_sd : RandomVariable
Standard deviation of the noise.
site_name : str, optional
Name of the site. Defaults to "rt_periodic_diff".

Returns
-------
None
"""

self.name = name
self.broadcaster = PeriodicBroadcaster(
offset=offset,
period_size=period_size,
Expand All @@ -91,7 +91,6 @@ def __init__(
self.log_rt_prior = log_rt_prior
self.autoreg = autoreg
self.periodic_diff_sd = periodic_diff_sd
self.site_name = site_name

return None

Expand Down Expand Up @@ -180,7 +179,7 @@ def sample(
n_periods = int(jnp.ceil(duration / self.period_size))

# Running the process
ar_diff = FirstDifferenceARProcess(autoreg=b, noise_sd=s_r)
ar_diff = FirstDifferenceARProcess("trend_rw", autoreg=b, noise_sd=s_r)
log_rt = ar_diff.sample(
duration=n_periods,
init_val=log_rt_prior[1],
Expand All @@ -199,17 +198,19 @@ class RtWeeklyDiffProcess(RtPeriodicDiffProcess):

def __init__(
self,
name: str,
offset: int,
log_rt_prior: RandomVariable,
autoreg: RandomVariable,
periodic_diff_sd: RandomVariable,
site_name: str = "rt_weekly_diff",
) -> None:
"""
Default constructor for RtWeeklyDiffProcess class.

Parameters
----------
name : str
Name of the site.
offset : int
Relative point at which data starts, must be between 0 and 6.
log_rt_prior : RandomVariable
Expand All @@ -218,21 +219,19 @@ def __init__(
Autoregressive parameter.
periodic_diff_sd : RandomVariable
Standard deviation of the noise.
site_name : str, optional
Name of the site. Defaults to "rt_weekly_diff".

Returns
-------
None
"""

super().__init__(
name=name,
offset=offset,
period_size=7,
log_rt_prior=log_rt_prior,
autoreg=autoreg,
periodic_diff_sd=periodic_diff_sd,
site_name=site_name,
)

return None
8 changes: 5 additions & 3 deletions model/src/test/test_ar_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ def test_ar_can_be_sampled():
Check that an AR process
can be initialized and sampled from
"""
ar1 = ARProcess(5, jnp.array([0.95]), jnp.array([0.5]))
ar1 = ARProcess("arprocess", 5, jnp.array([0.95]), jnp.array([0.5]))
with numpyro.handlers.seed(rng_seed=62):
# can sample with and without inits
ar1(duration=3532, inits=jnp.array([50.0]))
ar1(duration=5023)

ar3 = ARProcess(5, jnp.array([0.05, 0.025, 0.025]), jnp.array([0.5]))
ar3 = ARProcess(
"arprocess", 5, jnp.array([0.05, 0.025, 0.025]), jnp.array([0.5])
)
with numpyro.handlers.seed(rng_seed=62):
# can sample with and without inits
ar3(duration=1230)
Expand All @@ -32,7 +34,7 @@ def test_ar_samples_correctly_distributed():
ar_mean = 5
noise_sd = jnp.array([0.5])
ar_inits = jnp.array([25.0])
ar1 = ARProcess(ar_mean, jnp.array([0.75]), noise_sd)
ar1 = ARProcess("arprocess", ar_mean, jnp.array([0.75]), noise_sd)
with numpyro.handlers.seed(rng_seed=62):
# check it regresses to mean
# when started away from it
Expand Down
2 changes: 1 addition & 1 deletion model/src/test/test_first_difference_ar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_fd_ar_can_be_sampled():
can be initialized and sampled
from
"""
ar_fd = FirstDifferenceARProcess(0.5, 0.5)
ar_fd = FirstDifferenceARProcess("trend_rw", 0.5, 0.5)

with numpyro.handlers.seed(rng_seed=62):
# can sample with and without inits
Expand Down
1 change: 0 additions & 1 deletion model/src/test/test_model_hosp_admissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ def test_model_hosp_no_obs_model():

latent_admissions = HospitalAdmissions(
infection_to_admission_interval_rv=inf_hosp,
latent_hospital_admissions_varname="latent_hospital_admissions",
infect_hosp_rate_rv=DistributionalRV(
dist=dist.LogNormal(jnp.log(0.05), 0.05), name="IHR"
),
Expand Down
Loading