-
Notifications
You must be signed in to change notification settings - Fork 14
/
iteration_comparison.py
168 lines (137 loc) · 7 KB
/
iteration_comparison.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Compare COVID-19 simulation outputs to data.
Estimate Rt using epyestim
"""
import argparse
import os
import pandas as pd
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import sys
import matplotlib.dates as mdates
import seaborn as sns
sys.path.append('../')
from load_paths import load_box_paths
from processing_helpers import *
mpl.rcParams['pdf.fonttype'] = 42
def parse_args():
description = "Simulation run for modeling Covid-19"
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"-loc",
"--Location",
type=str,
help="Local or NUCLUSTER",
default="Local"
)
return parser.parse_args()
def comparison_plot(reg_nr=0, channels=None, n_iter=3):
if reg_nr == 0:
region = "illinois"
region_label = "Illinois"
else:
region = f'covidregion_{reg_nr}'
region_label = f'COVID-19 Region {reg_nr}'
if channels == None:
channels = ['cases', 'deaths_det', 'recovered', 'hosp_bed', 'icu', 'vent']
#channels = ['cases', 'deaths_det', 'recovered','hosp_det_bed', 'icu_det', 'vent']
channel_labels = ['Cases', 'Daily deaths', 'Recovered','Med/surg beds', 'ICU beds', 'Ventilators']
capacity = load_capacity(ems=reg_nr)
new_keys = ['hosp_det_bed', 'icu_det', 'vent']
#new_keys = ['hosp_bed', 'icu', 'vent']
for key, n_key in zip(capacity.keys(), new_keys):
capacity[n_key] = capacity.pop(key)
fig = plt.figure(figsize=(12, 8))
fig.suptitle(x=0.07, y=0.982, t=f"Predicted COVID-19 outcomes in {region_label}", ha='left')
axes = [fig.add_subplot(2, 3, x + 1) for x in range(len(channels))]
fig.subplots_adjust(right=0.96, left=0.07, hspace=0.25, wspace=0.3, top=0.92, bottom=0.07)
palette = sns.color_palette('husl', n_iter)
NU_civis_path = os.path.join(projectpath, 'NU_civis_outputs')
exp_simdates = os.listdir(NU_civis_path)[-n_iter:]
print(exp_simdates)
for i, simdate in enumerate(exp_simdates):
df = pd.read_csv(os.path.join(NU_civis_path, simdate, 'csv', f'nu_{simdate}.csv'))
df['date'] = pd.to_datetime(df['date'])
df = df[df['geography_modeled'] == region]
#df = df[df['scenario_name'] == 'baseline']
for c, channel in enumerate(channels):
ax = axes[c]
ax.grid(b=True, which='major', color='#999999', linestyle='-', alpha=0.3)
ax.plot(df['date'], df['%s_median' % channel], color=palette[i], label=simdate)
ax.fill_between(df['date'].values, df['%s_lower' % channel], df['%s_upper' % channel],
color=palette[i], linewidth=0, alpha=0.3)
if channel in capacity.keys():
ax.plot([np.min(df['date']), np.max(df['date'])],
[capacity[channel], capacity[channel]], '--', linewidth=1, color='black')
ax.set_title(channel_labels[c], y=0.978)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b\n%y'))
axes[-1].legend(loc='upper right')
plotname = f'iteration_comparison_{region}'
plt.savefig(os.path.join(NU_civis_path, simdate, plotname + '.png'))
# plt.savefig(os.path.join(NU_civis_path, simdate, 'pdf', plotname + '.pdf'), format='PDF')
def region_rt_plot(reg_nr=0, n_iter=2, rt_min=0.8, rt_max=2, useylimits=False):
if reg_nr == 0:
region = "illinois"
region_label = "Illinois"
else:
region = f'covidregion_{reg_nr}'
region_label = f'COVID-19 Region {reg_nr}'
if reg_nr == 11:
region_label = f'Chicago'
NU_civis_path = os.path.join(projectpath, 'NU_civis_outputs')
exp_simdates = os.listdir(NU_civis_path)[-n_iter:]
print(exp_simdates)
fig = plt.figure(figsize=(10, 6))
fig.subplots_adjust(right=0.97, left=0.08, hspace=0.4, wspace=0.2, top=0.87, bottom=0.17)
palette = ['#595959', '#1696D2']
ax = fig.add_subplot(1, 1, 1)
ax.grid(b=True, which='major', color='#999999', linestyle='-', alpha=0.3)
#ax.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
for i, simdate in enumerate(exp_simdates):
df = pd.read_csv(os.path.join(NU_civis_path, simdate, 'csv', f'nu_{simdate}.csv'))
df['date'] = pd.to_datetime(df['date'])
df = df[df['geography_modeled'] == region]
#df = df[df['scenario_name'] == 'baseline']
simdate_label = "this weeks's fit"
if i == 0:
simdate_label = "last weeks's fit"
ax.plot(df['date'], df['rt_median'], color=palette[i], label=simdate_label)
ax.fill_between(df['date'], df['rt_lower'], df['rt_upper'], color=palette[i], linewidth=0, alpha=0.2)
if i + 1 == len(exp_simdates):
df_today = df[df['date'] == pd.to_datetime(pd.Timestamp.today().date())]
df_initial = df[df['date'] == df['date'].min()]
rt_median_today = df_today.iloc[0]['rt_median'].round(decimals=3)
rt_lower_today = df_today.iloc[0]['rt_lower'].round(decimals=3)
rt_upper_today = df_today.iloc[0]['rt_upper'].round(decimals=3)
rt_median_initial = df_initial.iloc[0]['rt_median'].round(decimals=3)
rt_lower_initial = df_initial.iloc[0]['rt_lower'].round(decimals=3)
rt_upper_initial = df_initial.iloc[0]['rt_upper'].round(decimals=3)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d\n%b'))
ax.axvline(x=pd.Timestamp.today(), color='#737373', linestyle='--')
ax.axhline(y=1, color='black', linestyle='-')
if useylimits:
ax.set_ylim(rt_min, rt_max)
ax.set_ylabel('Rt')
ax.legend()
caption_text = "\nModel fitted to hospital inpatient census, intensive care unit census data and reported deaths. " \
"\nRt estimated based on predicted new infections using EpiEstim (epyestim)." \
"\nThe lag time between infections and hospitalizations and fitting points in the simulations " \
"affect estimated Rt for the previous weeks\n" \
"Plot truncated at Rt=2, initial Rt is estimated at " \
f"{rt_median_initial} (95%CI{rt_lower_initial} - {rt_upper_initial})"
text_top = "Estimated Rt using NU's COVID-19 model"
text_bottom = f"Estimated Rt for {pd.Timestamp.today().strftime('%Y-%b-%d')}:" \
f" {str(rt_median_today)} (95%CI {str(rt_lower_today)} - {str(rt_upper_today)})"
fig.suptitle(x=0.07, y=0.969, t=f"{region_label}\n{text_top}\n{text_bottom}", ha='left')
fig.text(0.07, 0.02, caption_text, wrap=True, horizontalalignment='left', fontsize=8)
plotname = f'{exp_simdates[-1]}_Rt_{region}'
plt.savefig(os.path.join(NU_civis_path, simdate, 'plots', plotname + '.png'))
if __name__ == '__main__':
args = parse_args()
datapath, projectpath, wdir, exe_dir, git_dir = load_box_paths(Location=args.Location)
first_plot_day = pd.Timestamp.today()- pd.Timedelta(30,'days')
last_plot_day = pd.Timestamp.today()+ pd.Timedelta(30,'days')
comparison_plot()
for reg_nr in range(0,12):
region_rt_plot(reg_nr=reg_nr)