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

refactor infectionswithfeedback.py to allow shared infection feedback strength across sites #470

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
27 changes: 10 additions & 17 deletions pyrenew/latent/infectionswithfeedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import jax.numpy as jnp
from numpy.typing import ArrayLike

import pyrenew.arrayutils as au
import pyrenew.latent.infection_functions as inf
from pyrenew.metaclass import RandomVariable

Expand Down Expand Up @@ -168,23 +167,17 @@
)
)

if inf_feedback_strength.ndim == Rt.ndim - 1:
inf_feedback_strength = inf_feedback_strength[jnp.newaxis]

# Making sure inf_feedback_strength spans the Rt length
if inf_feedback_strength.shape[0] == 1:
inf_feedback_strength = au.pad_edges_to_match(
x=inf_feedback_strength,
y=Rt,
axis=0,
)[0]
if inf_feedback_strength.shape != Rt.shape:
raise ValueError(
"Infection feedback strength must be of length 1 "
"or the same length as the reproduction number array. "
f"Got {inf_feedback_strength.shape} "
f"and {Rt.shape} respectively."
try:
inf_feedback_strength = jnp.broadcast_to(
inf_feedback_strength, Rt.shape
)
except Exception as e:
raise ValueError(

Check warning on line 175 in pyrenew/latent/infectionswithfeedback.py

View check run for this annotation

Codecov / codecov/patch

pyrenew/latent/infectionswithfeedback.py#L174-L175

Added lines #L174 - L175 were not covered by tests
"Could not broadcast inf_feedback_strength "
f"(shape {inf_feedback_strength.shape}) "
"to the shape of Rt"
f"{Rt.shape}"
) from e

# Sampling inf feedback pmf
inf_feedback_pmf = self.infection_feedback_pmf(**kwargs)
Expand Down