Skip to content

Commit 6f8aa06

Browse files
began argmin_days_since
1 parent 8e7f8e9 commit 6f8aa06

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

src/penn_chime/charts.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ def additional_projections_chart(
109109
})
110110
dat["day"] = dat.index
111111

112+
if parameters.n_days_since_first_hospitalized:
113+
dat["day"] = dat.day - parameters.n_days_since_first_hospitalized
114+
112115
as_date = parameters.as_date
113116
max_y_axis = parameters.max_y_axis
114117

src/penn_chime/models.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from __future__ import annotations
99

1010
from typing import Dict, Generator, Tuple, Optional
11+
from datetime import date, timedelta
1112

1213
import numpy as np # type: ignore
1314
import pandas as pd # type: ignore
@@ -98,16 +99,16 @@ def __init__(self, p: Parameters):
9899
self.census_df = census_df
99100

100101
#if p.n_days_since_first_hospitalized is None:
101-
# continue
102-
102+
# p.n_days_since_first_hospitalized = int(self.argmin_ds(p))
103+
# p.date_first_hospitalized = date.today() - timedelta(days=p.n_days_since_first_hospitalized)
103104
if p.n_days_since_first_hospitalized is not None and p.doubling_time is None:
104105
# optimize doubling_time
105106
argmin_dt = None
106107
min_loss = 2.0**99
107108
censes = dict()
108109
current_infecteds = dict()
109110
for dt in np.linspace(1,15,29):
110-
censes[dt], current_infecteds[dt] = self.run_projection(p, dt)
111+
censes[dt], current_infecteds[dt] = self.run_projection_dt(p, dt)
111112
# self.current_infected = current_infecteds[dt] # log it into state for no reason really
112113
self.census_df = censes[dt] # log it into state for loss
113114
loss_dt = self.loss_dt(p)
@@ -156,7 +157,7 @@ def __init__(self, p: Parameters):
156157

157158
return None
158159

159-
def run_projection(self, p: Parameters, doubling_time: float) -> Tuple[pd.DataFrame, float]:
160+
def run_projection_dt(self, p: Parameters, doubling_time: float) -> Tuple[pd.DataFrame, float]:
160161
intrinsic_growth_rate = self._intrinsic_growth_rate(doubling_time)
161162

162163
recovery_days = p.recovery_days
@@ -207,8 +208,18 @@ def loss_dt(self, p: Parameters) -> float:
207208
return (p.current_hospitalized - predicted_current_hospitalized) ** 2
208209

209210
def loss_ds(self, p: Parameters) -> float:
210-
pass
211-
211+
"""loss with respect to days_since """
212+
213+
## get the predicted number hospitalized today
214+
pred_current_hospitalized = self.census_df['hospitalized'].loc[p.n_days_since_first_hospitalized]
215+
216+
## compare against the actual (user inputed) number
217+
## squared difference is the loss to be optimized
218+
return (p.current_hospitalized - pred_current_hospitalized)**2
219+
220+
def argmin_ds(self, p: Parameters) -> float:
221+
losses_df = (self.census_df.hospitalized - p.current_hospitalized) ** 2
222+
return losses_df.argmin()
212223

213224
@staticmethod
214225
def _intrinsic_growth_rate(doubling_time: Optional[float]) -> float:
@@ -264,10 +275,11 @@ def sim_sir_df(
264275
s: float, i: float, r: float, beta: float, gamma: float, n_days: int
265276
) -> pd.DataFrame:
266277
"""Simulate the SIR model forward in time."""
267-
return pd.DataFrame(
278+
dat = pd.DataFrame(
268279
data=gen_sir(s, i, r, beta, gamma, n_days),
269280
columns=("day", "susceptible", "infected", "recovered"),
270281
)
282+
return dat
271283

272284

273285
def get_dispositions(

src/penn_chime/presentation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ def draw_census_table(
497497
def draw_raw_sir_simulation_table(st, model, parameters):
498498
as_date = parameters.as_date
499499
projection_area = model.raw_df
500+
if parameters.n_days_since_first_hospitalized is not None:
501+
projection_area.loc[:, "day"] = projection_area.day - parameters.n_days_since_first_hospitalized
500502
infect_table = (projection_area.iloc[::7, :]).apply(np.floor)
501503
infect_table.index = range(infect_table.shape[0])
502504
infect_table["day"] = infect_table.day.astype(int)

src/penn_chime/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ def add_date_column(
5353
if date_first_hospitalized:
5454
delta = today - date_first_hospitalized
5555
start = today - delta
56+
delta_days = delta.days
5657
else:
5758
start = today
58-
end = start + timedelta(days=n_days + 1) # + delta # the +delta part is a hypothesis by phil
59+
delta_days = 0
60+
delta = timedelta(days=0)
61+
end = start + timedelta(days=n_days + 1) + delta
62+
5963
# And pick dates present in frame
60-
dates = pd.date_range(start=start, end=end, freq="D")[df.day.tolist()]
64+
dates = pd.date_range(
65+
start=start, end=end, freq="D", closed="right"
66+
)[[x + delta_days for x in df.day.tolist()]]
6167

6268
if date_format is not None:
6369
dates = dates.strftime(date_format)
@@ -72,7 +78,6 @@ def add_date_column(
7278

7379
# sort columns
7480
df = df[date_columns + non_date_columns]
75-
7681
return df
7782

7883
def dataframe_to_base64(df: pd.DataFrame) -> str:

0 commit comments

Comments
 (0)