Skip to content

Commit 6f1ff4e

Browse files
Ahil  PonArulAhil  PonArul
authored andcommitted
instrumented Python code with Elastic APM
1 parent 32610a4 commit 6f1ff4e

File tree

4 files changed

+152
-135
lines changed

4 files changed

+152
-135
lines changed

src/penn_chime/model/sir.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ def get_argmin_doubling_time(self, p: Parameters, dts):
195195
intrinsic_growth_rate = get_growth_rate(i_dt)
196196
self.beta = get_beta(intrinsic_growth_rate, self.gamma, self.susceptible, 0.0)
197197
self.beta_t = get_beta(intrinsic_growth_rate, self.gamma, self.susceptible, p.relative_contact_rate)
198-
198+
199199
raw = self.run_projection(p, self.gen_policy(p))
200-
200+
201201
# Skip values the would put the fit past peak
202202
peak_admits_day = raw["admits_hospitalized"].argmax()
203203
if peak_admits_day < 0:
@@ -228,7 +228,7 @@ def gen_policy(self, p: Parameters) -> Sequence[Tuple[float, int]]:
228228
(self.beta, pre_mitigation_days),
229229
(self.beta_t, post_mitigation_days),
230230
]
231-
231+
232232
def run_projection(self, p: Parameters, policy: Sequence[Tuple[float, int]]):
233233
raw = sim_sir(
234234
self.susceptible,
@@ -245,20 +245,17 @@ def run_projection(self, p: Parameters, policy: Sequence[Tuple[float, int]]):
245245

246246
return raw
247247

248-
249248
def get_loss(current_hospitalized, predicted) -> float:
250249
"""Squared error: predicted vs. actual current hospitalized."""
251250
return (current_hospitalized - predicted) ** 2.0
252251

253-
254252
def get_argmin_ds(census, current_hospitalized: float) -> float:
255253
# By design, this forbids choosing a day after the peak
256254
# If that's a problem, see #381
257255
peak_day = census.argmax()
258256
losses = (census[:peak_day] - current_hospitalized) ** 2.0
259257
return losses.argmin()
260258

261-
262259
def get_beta(
263260
intrinsic_growth_rate: float,
264261
gamma: float,
@@ -271,14 +268,12 @@ def get_beta(
271268
* (1.0 - relative_contact_rate)
272269
)
273270

274-
275271
def get_growth_rate(doubling_time: Optional[float]) -> float:
276272
"""Calculates average daily growth rate from doubling time."""
277273
if doubling_time is None or doubling_time == 0.0:
278274
return 0.0
279275
return (2.0 ** (1.0 / doubling_time) - 1.0)
280276

281-
282277
def sir(
283278
s: float, i: float, r: float, beta: float, gamma: float, n: float
284279
) -> Tuple[float, float, float]:
@@ -289,7 +284,6 @@ def sir(
289284
scale = n / (s_n + i_n + r_n)
290285
return s_n * scale, i_n * scale, r_n * scale
291286

292-
293287
def sim_sir(
294288
s: float, i: float, r: float, gamma: float, i_day: int, policies: Sequence[Tuple[float, int]]
295289
):
@@ -334,7 +328,6 @@ def sim_sir(
334328
"ever_infected": i_a + r_a
335329
}
336330

337-
338331
def build_sim_sir_w_date_df(
339332
raw_df: pd.DataFrame,
340333
current_date: datetime,
@@ -350,7 +343,6 @@ def build_sim_sir_w_date_df(
350343
}
351344
})
352345

353-
354346
def build_floor_df(df, keys, prefix):
355347
"""Build floor sim sir w date."""
356348
return pd.DataFrame({
@@ -362,7 +354,6 @@ def build_floor_df(df, keys, prefix):
362354
}
363355
})
364356

365-
366357
def calculate_dispositions(
367358
raw: Dict,
368359
rates: Dict[str, float],
@@ -373,7 +364,6 @@ def calculate_dispositions(
373364
raw["ever_" + key] = raw["ever_infected"] * rate * market_share
374365
raw[key] = raw["ever_infected"] * rate * market_share
375366

376-
377367
def calculate_admits(raw: Dict, rates):
378368
"""Build admits dataframe from dispositions."""
379369
for key in rates.keys():
@@ -384,7 +374,6 @@ def calculate_admits(raw: Dict, rates):
384374
raw["admits_"+key] = admit
385375
raw[key] = admit
386376

387-
388377
def calculate_census(
389378
raw: Dict,
390379
lengths_of_stay: Dict[str, int],

src/penn_chime/view/charts.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import pandas as pd
55
import i18n
66
import numpy as np
7+
import elasticapm
78

89
from ..constants import DATE_FORMAT
910

10-
11+
@elasticapm.capture_span()
1112
def build_admits_chart(
1213
*, alt, admits_floor_df: pd.DataFrame, max_y_axis: Optional[int] = None
1314
) -> Chart:
@@ -51,7 +52,7 @@ def build_admits_chart(
5152
.interactive()
5253
)
5354

54-
55+
@elasticapm.capture_span()
5556
def build_census_chart(
5657
*, alt, census_floor_df: pd.DataFrame, max_y_axis: Optional[int] = None
5758
) -> Chart:
@@ -95,7 +96,7 @@ def build_census_chart(
9596
.interactive()
9697
)
9798

98-
99+
@elasticapm.capture_span()
99100
def build_sim_sir_w_date_chart(
100101
*, alt, sim_sir_w_date_floor_df: pd.DataFrame, max_y_axis: Optional[int] = None
101102
) -> Chart:
@@ -139,6 +140,7 @@ def build_sim_sir_w_date_chart(
139140
.interactive()
140141
)
141142

143+
@elasticapm.capture_span()
142144
def build_table(
143145
*, df: pd.DataFrame, labels: Dict[str, str], modulo: int = 1
144146
) -> pd.DataFrame:

src/penn_chime/view/st_app.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import streamlit as st # type: ignore
77
import i18n # type: ignore
88

9+
from elasticapm import Client
10+
911
i18n.set('filename_format', '{locale}.{format}')
1012
i18n.set('locale', 'en')
1113
i18n.set('fallback', 'en')
@@ -28,6 +30,10 @@
2830

2931

3032
def main():
33+
#client = Client(server_url='http://apm-server:8200',service_name='chime_local')
34+
#client = Client(server_url='${ELASTIC_APM_SERVER_URL}',service_name='${ELASTIC_APM_SERVICE_NAME}')
35+
client = Client()
36+
client.begin_transaction('main_page')
3137
# This is somewhat dangerous:
3238
# Hide the main menu with "Rerun", "run on Save", "clear cache", and "record a screencast"
3339
# This should not be hidden in prod, but removed
@@ -37,8 +43,9 @@ def main():
3743
d = Parameters.create(os.environ, [])
3844
p = display_sidebar(st, d)
3945
m = Sir(p)
40-
46+
4147
display_header(st, m, p)
48+
4249

4350
st.subheader(i18n.t("app-new-admissions-title"))
4451
st.markdown(i18n.t("app-new-admissions-text"))
@@ -73,3 +80,4 @@ def main():
7380
df=m.sim_sir_w_date_df,
7481
)
7582
display_footer(st)
83+
client.end_transaction('main_page')

0 commit comments

Comments
 (0)