|
1 |
| -from typing import Tuple |
| 1 | +from typing import Generator, Tuple |
2 | 2 |
|
3 | 3 | import numpy as np
|
| 4 | +import pandas as pd |
4 | 5 | import streamlit as st
|
5 | 6 |
|
6 | 7 |
|
7 |
| -# The SIR model, one time step |
8 | 8 | @st.cache
|
9 |
| -def sir(y, beta, gamma, N): |
10 |
| - S, I, R = y |
11 |
| - Sn = (-beta * S * I) + S |
12 |
| - In = (beta * S * I - gamma * I) + I |
13 |
| - Rn = gamma * I + R |
14 |
| - if Sn < 0: |
15 |
| - Sn = 0 |
16 |
| - if In < 0: |
17 |
| - In = 0 |
18 |
| - if Rn < 0: |
19 |
| - Rn = 0 |
20 |
| - |
21 |
| - scale = N / (Sn + In + Rn) |
22 |
| - return Sn * scale, In * scale, Rn * scale |
23 |
| - |
24 |
| - |
25 |
| -# Run the SIR model forward in time |
| 9 | +def sir( |
| 10 | + s: float, i: float, r: float, |
| 11 | + beta: float, gamma: float, n: float |
| 12 | +) -> Tuple[float, float, float]: |
| 13 | + """The SIR model, one time step.""" |
| 14 | + s_n = (-beta * s * i) + s |
| 15 | + i_n = (beta * s * i - gamma * i) + i |
| 16 | + r_n = gamma * i + r |
| 17 | + if s_n < 0.0: |
| 18 | + s_n = 0.0 |
| 19 | + if i_n < 0.0: |
| 20 | + i_n = 0.0 |
| 21 | + if r_n < 0.0: |
| 22 | + r_n = 0.0 |
| 23 | + |
| 24 | + scale = n / (s_n + i_n + r_n) |
| 25 | + return s_n * scale, i_n * scale, r_n * scale |
| 26 | + |
| 27 | + |
| 28 | +def gen_sir( |
| 29 | + s: float, i: float, r: float, |
| 30 | + beta: float, gamma: float, n_days: int, beta_decay: float = 0.0 |
| 31 | +) -> Generator[Tuple[float, float, float], None, None]: |
| 32 | + """Simulate SIR model forward in time yielding tuples.""" |
| 33 | + s, i, r = (float(v) for v in (s, i, r)) |
| 34 | + n = s + i + r |
| 35 | + f = 1.0 - beta_decay # okay even if beta_decay is 0.0 |
| 36 | + for _ in range(n_days + 1): |
| 37 | + yield s, i, r |
| 38 | + s, i, r = sir(s, i, r, beta, gamma, n) |
| 39 | + beta *= f |
| 40 | + |
| 41 | + |
26 | 42 | @st.cache
|
27 | 43 | def sim_sir(
|
28 |
| - S, I, R, beta, gamma, n_days, beta_decay=0 |
| 44 | + s: float, i: float, r: float, |
| 45 | + beta: float, gamma: float, n_days: int, beta_decay: float = 0.0 |
29 | 46 | ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
30 |
| - N = S + I + R |
31 |
| - s, i, r = [S], [I], [R] |
| 47 | + """Simulate the SIR model forward in time.""" |
| 48 | + s, i, r = (float(v) for v in (s, i, r)) |
| 49 | + n = s + i + r |
| 50 | + f = 1.0 - beta_decay # okay even if beta_decay is 0.0 |
| 51 | + s_v, i_v, r_v = [s], [i], [r] |
32 | 52 | for day in range(n_days):
|
33 |
| - y = S, I, R |
34 |
| - S, I, R = sir(y, beta, gamma, N) |
35 |
| - beta = beta * (1 - beta_decay) # okay even if beta_decay is 0 |
36 |
| - s.append(S) |
37 |
| - i.append(I) |
38 |
| - r.append(R) |
| 53 | + s, i, r = sir(s, i, r, beta, gamma, n) |
| 54 | + beta *= f |
| 55 | + s_v.append(s) |
| 56 | + i_v.append(i) |
| 57 | + r_v.append(r) |
| 58 | + |
| 59 | + return ( |
| 60 | + np.array(s_v), |
| 61 | + np.array(i_v), |
| 62 | + np.array(r_v), |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +@st.cache |
| 67 | +def sim_sir_df( |
| 68 | + s: float, i: float, r: float, |
| 69 | + beta: float, gamma: float, n_days: int, beta_decay: float = 0.0 |
| 70 | +) -> pd.DataFrame: |
| 71 | + """Simulate the SIR model forward in time.""" |
| 72 | + return pd.DataFrame( |
| 73 | + data=gen_sir(s, i, r, beta, gamma, n_days, beta_decay), |
| 74 | + columns=("S", "I", "R"), |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +@st.cache |
| 79 | +def get_hospitalizations2( |
| 80 | + infected: np.ndarray, rates: Tuple[float, ...], market_share: float = 1.0 |
| 81 | +) -> Tuple[np.ndarray, ...]: |
| 82 | + """Get hopitalizations adjusted by rate and market_share.""" |
| 83 | + return (*(infected * rate * market_share for rate in rates),) |
39 | 84 |
|
40 |
| - s, i, r = np.array(s), np.array(i), np.array(r) |
41 |
| - return s, i, r |
42 | 85 |
|
43 | 86 | @st.cache
|
44 | 87 | def get_hospitalizations(
|
45 | 88 | infected: np.ndarray, rates: Tuple[float, float, float], market_share: float
|
46 | 89 | ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
| 90 | + """Get hopitalizations adjusted by rate and market_share.""" |
47 | 91 | hosp_rate, icu_rate, vent_rate = rates
|
48 | 92 |
|
49 | 93 | hosp = infected * hosp_rate * market_share
|
|
0 commit comments