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

Avoid divide by zero error when there are no non-index infections #52

Merged
merged 4 commits into from
Dec 24, 2024
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
32 changes: 22 additions & 10 deletions ringvax/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
)


def render_percents(df: pl.DataFrame) -> pl.DataFrame:
return df.with_columns(
pl.when(pl.col(col).is_nan())
.then(pl.lit("Not a number"))
.otherwise(
pl.col(col).map_elements(
lambda x: f"{round(x):.0f}%", return_dtype=pl.String
)
)
.alias(col)
for col in df.columns
)


def make_graph(sim: Simulation) -> graphviz.Digraph:
"""Make a transmission graph"""
graph = graphviz.Digraph()
Expand Down Expand Up @@ -268,19 +282,17 @@ def app():
)

st.write(
"The following table provides summaries of marginal probabilities regarding detection. Aside from the marginal probability of active detection, these are the observed probabilities that any individual is detected in this manner. The marginal probability of active detection excludes index cases, which are not eligible for active detection."
(
"The following table provides summaries of marginal probabilities regarding detection. "
"Aside from the marginal probability of active detection, these are the observed "
"probabilities that any individual is detected in this manner, including the index case. "
"The marginal probability of active detection excludes index cases, which are not "
"eligible for active detection."
)
)
detection = summarize_detections(sim_df)
st.dataframe(
detection.select(
(pl.col(col) * 100).round(0).cast(pl.Int64)
for col in detection.columns
)
.with_columns(
pl.concat_str([pl.col(col), pl.lit("%")], separator="")
for col in detection.columns
)
.rename(
render_percents(detection).rename(
{
"prob_detect": "Any detection",
"prob_active": "Active detection",
Expand Down
17 changes: 10 additions & 7 deletions ringvax/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def _prepare_for_df(infection: dict) -> dict:
}


@np.errstate(invalid="ignore")
def summarize_detections(df: pl.DataFrame) -> pl.DataFrame:
"""
Get marginal detection probabilities from simulations.
Expand Down Expand Up @@ -93,13 +94,15 @@ def summarize_detections(df: pl.DataFrame) -> pl.DataFrame:

return pl.DataFrame(
{
"prob_detect": 1.0 - count_nodetect / n_infections,
"prob_active": count_active / n_active_eligible,
"prob_passive": count_passive / n_infections,
"prob_detect_before_infectious": df.filter(pl.col("detected"))
.filter(pl.col("t_detected") < pl.col("t_infectious"))
.shape[0]
/ n_infections,
"prob_detect": 1.0 - np.divide(count_nodetect, n_infections),
"prob_active": np.divide(count_active, n_active_eligible),
"prob_passive": np.divide(count_passive, n_infections),
"prob_detect_before_infectious": np.divide(
df.filter(pl.col("detected"))
.filter(pl.col("t_detected") < pl.col("t_infectious"))
.shape[0],
n_infections,
),
}
)

Expand Down
Loading