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

Update ruff pre-commit hooks #65

Merged
merged 2 commits into from
Dec 30, 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
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.0
rev: v0.8.4
hooks:
# Sort imports
- id: ruff
args: ["check", "--select", "I", "--fix"]
# Run the linter
- id: ruff
# Run the formatter
- id: ruff-format
#####
- repo: https://github.com/Yelp/detect-secrets
rev: v1.5.0
hooks:
Expand Down
14 changes: 8 additions & 6 deletions ngm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from collections import namedtuple
import numpy as np
from typing import Any

import numpy as np

DominantEigen = namedtuple("DominantEigen", ["value", "vector"])


Expand Down Expand Up @@ -36,9 +37,9 @@ def run_ngm(
return {"M": M_vax, "Re": eigen.value, "infection_distribution": eigen.vector}


def severity(eigenvalue: float, eigenvector: np.ndarray, p_severe: np.ndarray, G: int
def severity(
eigenvalue: float, eigenvector: np.ndarray, p_severe: np.ndarray, G: int
) -> np.ndarray:

"""
Calculate cumulative severe infections up to and including the Gth generation.

Expand Down Expand Up @@ -168,7 +169,7 @@ def distribute_vaccines(
remaining_proportions = np.where(
np.isin(np.arange(n_groups), target_indices, invert=True),
N_i / remaining_population,
0.0
0.0,
)

n_vax += remaining_doses * np.array(remaining_proportions)
Expand All @@ -178,6 +179,7 @@ def distribute_vaccines(

return n_vax


def exp_growth_model_severity(R_e, inf_distribution, p_severe, G) -> np.ndarray:
"""
Get cumulative infections and severe infections in generations 0, 1, ..., G
Expand All @@ -195,8 +197,8 @@ def exp_growth_model_severity(R_e, inf_distribution, p_severe, G) -> np.ndarray:
[:,1] is the number of infections
[:,2] is the number of severe infections
"""
gens = np.arange(G+1)
infections = np.cumsum(R_e ** gens)
gens = np.arange(G + 1)
infections = np.cumsum(R_e**gens)
severe = np.outer(infections, inf_distribution * p_severe).sum(axis=1)

return np.stack((gens, infections, severe), 1)
47 changes: 30 additions & 17 deletions scripts/simulate.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import griddler
import griddler.griddle
import numpy as np
import ngm as ngm
import polars as pl
import polars.selectors as cs
import griddler
import griddler.griddle

import ngm as ngm


def simulate_scenario(params, distributions_as_percents=False):
assert sum(params["pop_props"]) == 1.0
Expand All @@ -25,16 +27,22 @@ def simulate_scenario(params, distributions_as_percents=False):
params["n_vax_total"], N_i, strategy=params["vax_strategy"]
)

result = ngm.run_ngm(
M_novax=M_novax, n=N_i, n_vax=n_vax, ve=params["ve"]
)
result = ngm.run_ngm(M_novax=M_novax, n=N_i, n_vax=n_vax, ve=params["ve"])

Re = result["Re"]
ifr = np.dot(result["infection_distribution"], p_severe)
fatalities_per_prior_infection = ngm.severity(eigenvalue = Re, eigenvector = result["infection_distribution"],
p_severe = p_severe, G = 1)
fatalities_after_G_generations = ngm.severity(eigenvalue = Re, eigenvector = result["infection_distribution"],
p_severe = p_severe, G = params["G"])
fatalities_per_prior_infection = ngm.severity(
eigenvalue=Re,
eigenvector=result["infection_distribution"],
p_severe=p_severe,
G=1,
)
fatalities_after_G_generations = ngm.severity(
eigenvalue=Re,
eigenvector=result["infection_distribution"],
p_severe=p_severe,
G=params["G"],
)

infection_distribution_dict = {
f"infections_{group}": result["infection_distribution"][i] * mult
Expand Down Expand Up @@ -67,22 +75,27 @@ def simulate_scenario(params, distributions_as_percents=False):
if __name__ == "__main__":
parameter_sets = griddler.griddle.read("scripts/config.yaml")

strategy_names = {"even": "even", "0": "core first", "1": "group 1 first", "2": "group 2 first", "0_1": "core and group 1 first"}
strategy_names = {
"even": "even",
"0": "core first",
"1": "group 1 first",
"2": "group 2 first",
"0_1": "core and group 1 first",
}
results_all = griddler.run_squash(simulate_scenario, parameter_sets).with_columns(
pl.col("vax_strategy").replace_strict(strategy_names)
)

scen = results_all.select("scenario").row(0)[0]

cols_to_select = [
"n_vax_total",
"vax_strategy",
"Re",
"ifr"]
cols_to_select = ["n_vax_total", "vax_strategy", "Re", "ifr"]

results = (
results_all.with_columns(cs.float().round(3))
.select(cs.by_name(cols_to_select) | cs.starts_with("deaths_per_prior", "infections_"))
.select(
cs.by_name(cols_to_select)
| cs.starts_with("deaths_per_prior", "infections_")
)
.sort(["n_vax_total", "vax_strategy"])
)

Expand Down
Loading
Loading