1
+ from functools import reduce
2
+ from typing import Tuple , Dict , Any
1
3
import pandas as pd
2
4
import streamlit as st
3
5
import numpy as np
4
- import matplotlib
5
-
6
- matplotlib .use ("Agg" )
7
- import matplotlib .pyplot as plt
6
+ import altair as alt
8
7
9
8
hide_menu_style = """
10
9
<style>
@@ -236,17 +235,26 @@ def sim_sir(S, I, R, beta, gamma, n_days, beta_decay=None):
236
235
plot_projection_days = n_days - 10
237
236
projection_admits ["day" ] = range (projection_admits .shape [0 ])
238
237
239
- fig , ax = plt .subplots (1 , 1 , figsize = (10 , 4 ))
240
- ax .plot (
241
- projection_admits .head (plot_projection_days )["hosp" ], ".-" , label = "Hospitalized"
242
- )
243
- ax .plot (projection_admits .head (plot_projection_days )["icu" ], ".-" , label = "ICU" )
244
- ax .plot (projection_admits .head (plot_projection_days )["vent" ], ".-" , label = "Ventilated" )
245
- ax .legend (loc = 0 )
246
- ax .set_xlabel ("Days from today" )
247
- ax .grid ("on" )
248
- ax .set_ylabel ("Daily Admissions" )
249
- st .pyplot ()
238
+
239
+ def new_admissions_chart (projection_admits : pd .DataFrame , plot_projection_days : int ) -> alt .Chart :
240
+ """docstring"""
241
+ projection_admits = projection_admits .rename (columns = {"hosp" : "Hospitalized" , "icu" : "ICU" , "vent" : "Ventilated" })
242
+ return (
243
+ alt
244
+ .Chart (projection_admits .head (plot_projection_days ))
245
+ .transform_fold (fold = ["Hospitalized" , "ICU" , "Ventilated" ])
246
+ .mark_line (point = True )
247
+ .encode (
248
+ x = alt .X ("day" , title = "Days from today" ),
249
+ y = alt .Y ("value:Q" , title = "Daily admissions" ),
250
+ color = "key:N" ,
251
+ tooltip = ["day" , "key:N" ]
252
+ )
253
+ .interactive ()
254
+ )
255
+
256
+ st .altair_chart (new_admissions_chart (projection_admits , plot_projection_days ), use_container_width = True )
257
+
250
258
251
259
admits_table = projection_admits [np .mod (projection_admits .index , 7 ) == 0 ].copy ()
252
260
admits_table ["day" ] = admits_table .index
@@ -269,22 +277,14 @@ def sim_sir(S, I, R, beta, gamma, n_days, beta_decay=None):
269
277
"vent" : vent_los ,
270
278
}
271
279
272
- fig , ax = plt .subplots (1 , 1 , figsize = (10 , 4 ))
273
-
274
- census_dict = {}
280
+ census_dict = dict ()
275
281
for k , los in los_dict .items ():
276
282
census = (
277
283
projection_admits .cumsum ().iloc [:- los , :]
278
284
- projection_admits .cumsum ().shift (los ).fillna (0 )
279
285
).apply (np .ceil )
280
286
census_dict [k ] = census [k ]
281
- ax .plot (census .head (plot_projection_days )[k ], ".-" , label = k + " census" )
282
- ax .legend (loc = 0 )
283
287
284
- ax .set_xlabel ("Days from today" )
285
- ax .grid ("on" )
286
- ax .set_ylabel ("Census" )
287
- st .pyplot ()
288
288
289
289
census_df = pd .DataFrame (census_dict )
290
290
census_df ["day" ] = census_df .index
@@ -295,6 +295,26 @@ def sim_sir(S, I, R, beta, gamma, n_days, beta_decay=None):
295
295
census_table .loc [0 , :] = 0
296
296
census_table = census_table .dropna ().astype (int )
297
297
298
+ def admitted_patients_chart (census : pd .DataFrame ) -> alt .Chart :
299
+ """docstring"""
300
+ census = census .rename (columns = {"hosp" : "Hospital Census" , "icu" : "ICU Census" , "vent" : "Ventilated Census" })
301
+
302
+ return (
303
+ alt
304
+ .Chart (census )
305
+ .transform_fold (fold = ["Hospital Census" , "ICU Census" , "Ventilated Census" ])
306
+ .mark_line (point = True )
307
+ .encode (
308
+ x = alt .X ("day" , title = "Days from today" ),
309
+ y = alt .Y ("value:Q" , title = "Census" ),
310
+ color = "key:N" ,
311
+ tooltip = ["day" , "key:N" ]
312
+ )
313
+ .interactive ()
314
+ )
315
+
316
+ st .altair_chart (admitted_patients_chart (census_table ), use_container_width = True )
317
+
298
318
if st .checkbox ("Show Projected Census in tabular form" ):
299
319
st .dataframe (census_table )
300
320
@@ -305,15 +325,26 @@ def sim_sir(S, I, R, beta, gamma, n_days, beta_decay=None):
305
325
st .subheader (
306
326
"The number of infected and recovered individuals in the hospital catchment region at any given moment"
307
327
)
308
- fig , ax = plt .subplots (1 , 1 , figsize = (10 , 4 ))
309
- ax .plot (i , label = "Infected" )
310
- ax .plot (r , label = "Recovered" )
311
- ax .legend (loc = 0 )
312
- ax .set_xlabel ("days from today" )
313
- ax .set_ylabel ("Case Volume" )
314
- ax .grid ("on" )
315
- st .pyplot ()
316
328
329
+ def additional_projections_chart (i : np .ndarray , r : np .ndarray ) -> alt .Chart :
330
+ dat = pd .DataFrame ({"Infected" : i , "Recovered" : r })
331
+
332
+ return (
333
+ alt
334
+ .Chart (dat .reset_index ())
335
+ .transform_fold (fold = ["Infected" , "Recovered" ])
336
+ .mark_line ()
337
+ .encode (
338
+ x = alt .X ("index" , title = "Days from today" ),
339
+ y = alt .Y ("value:Q" , title = "Case Volume" ),
340
+ tooltip = ["key:N" , "value:Q" ],
341
+ color = "key:N"
342
+ )
343
+ .interactive ()
344
+ )
345
+
346
+ st .altair_chart (additional_projections_chart (i , r ), use_container_width = True )
347
+
317
348
# Show data
318
349
days = np .array (range (0 , n_days + 1 ))
319
350
data_list = [days , s , i , r ]
0 commit comments