Skip to content

Commit f3efe64

Browse files
committed
Fix dispositions and census df construction
1 parent 7007468 commit f3efe64

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

src/penn_chime/models.py

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,18 @@ def __init__(self, p: Parameters) -> SimSirModel:
7676
for key, d in p.dispositions.items()
7777
}
7878

79-
i_dict_v = get_dispositions(raw_df.infected, rates, p.market_share)
80-
r_dict_v = get_dispositions(raw_df.recovered, rates, p.market_share)
79+
# i_dict_v = get_dispositions(raw_df.infected, rates, p.market_share)
80+
# r_dict_v = get_dispositions(raw_df.recovered, rates, p.market_share)
8181

82-
self.dispositions = {
83-
key: value + r_dict_v[key]
84-
for key, value in i_dict_v.items()
85-
}
82+
#self.dispositions = {
83+
# key: value + r_dict_v[key]
84+
# for key, value in i_dict_v.items()
85+
#}
86+
87+
#self.dispositions_df = pd.DataFrame(self.dispositions)
8688

87-
self.dispositions_df = pd.DataFrame(self.dispositions)
88-
self.admits_df = admits_df = build_admits_df(p.n_days, self.dispositions)
89+
self.dispositions_df = dispositions_df = build_dispositions_df(raw_df, rates, p.market_share)
90+
self.admits_df = admits_df = build_admits_df(dispositions_df)
8991
self.census_df = build_census_df(admits_df, lengths_of_stay)
9092

9193

@@ -119,55 +121,49 @@ def gen_sir(
119121

120122

121123
def sim_sir_df(
122-
s: float, i: float, r: float, beta: float, gamma: float, n_days
124+
s: float, i: float, r: float, beta: float, gamma: float, n_days: int
123125
) -> pd.DataFrame:
124126
"""Simulate the SIR model forward in time."""
125127
return pd.DataFrame(
126128
data=gen_sir(s, i, r, beta, gamma, n_days),
127129
columns=("day", "susceptible", "infected", "recovered"),
128130
)
129131

130-
131-
def get_dispositions(
132-
patients: np.ndarray,
132+
def build_dispositions_df(
133+
sim_sir_df: pd.DataFrame,
133134
rates: Dict[str, float],
134135
market_share: float,
135-
) -> Dict[str, np.ndarray]:
136+
) -> pd.DataFrame:
136137
"""Get dispositions of patients adjusted by rate and market_share."""
137-
return {
138-
key: patients * rate * market_share
139-
for key, rate in rates.items()
140-
}
138+
patients = sim_sir_df.infected + sim_sir_df.recovered
139+
return pd.DataFrame({
140+
"day": sim_sir_df.day,
141+
**{
142+
key: patients * rate * market_share
143+
for key, rate in rates.items()
144+
}
145+
})
141146

142147

143-
def build_admits_df(n_days, dispositions) -> pd.DataFrame:
148+
def build_admits_df(dispositions_df: pd.DataFrame) -> pd.DataFrame:
144149
"""Build admits dataframe from Parameters and Model."""
145-
days = np.arange(0, n_days + 1)
146-
projection = pd.DataFrame({
147-
"day": days,
148-
**dispositions,
149-
})
150-
# New cases
151-
admits_df = projection.iloc[:-1, :] - projection.shift(1)
152-
admits_df["day"] = range(admits_df.shape[0])
150+
admits_df = dispositions_df.iloc[:-1, :] - dispositions_df.shift(1)
151+
admits_df.day = dispositions_df.day
153152
return admits_df
154153

155154

156155
def build_census_df(
157-
admits_df: pd.DataFrame, lengths_of_stay
156+
admits_df: pd.DataFrame,
157+
lengths_of_stay: Dict[str, int],
158158
) -> pd.DataFrame:
159159
"""ALOS for each category of COVID-19 case (total guesses)"""
160-
n_days = np.shape(admits_df)[0]
161-
census_dict = {}
162-
for key, los in lengths_of_stay.items():
163-
census = (
164-
admits_df.cumsum().iloc[:-los, :]
165-
- admits_df.cumsum().shift(los).fillna(0)
166-
).apply(np.ceil)
167-
census_dict[key] = census[key]
168-
169-
census_df = pd.DataFrame(census_dict)
170-
census_df["day"] = census_df.index
171-
census_df = census_df[["day", *lengths_of_stay.keys()]]
172-
census_df = census_df.head(n_days)
173-
return census_df
160+
return pd.DataFrame({
161+
'day': admits_df.day,
162+
**{
163+
key: (
164+
admits_df[key].cumsum().iloc[:-los]
165+
- admits_df[key].cumsum().shift(los).fillna(0)
166+
).apply(np.ceil)
167+
for key, los in lengths_of_stay.items()
168+
}
169+
})

0 commit comments

Comments
 (0)