-
Notifications
You must be signed in to change notification settings - Fork 15
/
covid_plot_active.py
180 lines (161 loc) · 7.26 KB
/
covid_plot_active.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
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import pandas as pd
from covid_plot_utils import plot_area
from covid_plot_utils import source
from utils_pandas import cum2daily
from utils_pandas import import_csv
from utils_pandas import perc_format
from utils_scraping import logger
def save_active_plots(df: pd.DataFrame) -> None:
#######################
# Hospital plots
#######################
cols_delayed = ["Hospitalized", "Recovered", "Hospitalized Severe", "Hospitalized Respirator", "Hospitalized Field"]
# TODO: we are missing some severe, ventilator mid april. why?
df[cols_delayed] = df[cols_delayed].interpolate(limit_area="inside")
# TODO: use unknowns to show this plot earlier?
# because severe includes those on respirators
df["Hospitalized Severe excl vent"] = df["Hospitalized Severe"].sub(df["Hospitalized Respirator"], fill_value=None)
non_split = df[["Hospitalized Severe excl vent", "Hospitalized Respirator", "Hospitalized Field"]].sum(skipna=True,
axis=1)
df["Hospitalized Hospital"] = df["Hospitalized"].sub(non_split, fill_value=None)
cols = [
'Hospitalized Respirator',
'Hospitalized Severe excl vent',
'Hospitalized Hospital',
'Hospitalized Field',
]
legends = [
'In Serious Condition on Ventilator',
'In Serious Condition without Ventilator',
'In Isolation/Hospital',
'In Field Hospital',
]
# plot_area(df=df, png_prefix='cases_active', cols_subset=cols,
# title='Thailand Active Covid Cases\n(Severe, Field, and Respirator only available from '
# '2021-04-24 onwards)',
# legends=legends,
# kind='area', stacked=True, percent_fig=False, ma_days=None, cmap='tab10')
cols = [
'Hospitalized Severe',
'Hospitalized Severe excl vent',
'Hospitalized Respirator',
]
legends = [
'In Serious Condition',
'In Serious Condition without Ventilator',
'In Serious Condition on Ventilator',
]
plot_area(df=df,
title='Active Covid Cases in Serious Condition - Thailand',
legends=legends,
png_prefix='active_severe', cols_subset=cols,
actuals=False,
ma_days=7,
kind='line', stacked=False, percent_fig=False,
cmap='tab10',
footnote_left=f'{source}Data Source: CCSA Daily Briefing')
# show cumulative deaths, recoveries and hospitalisations (which should all add up to cases)
df['Recovered since 2021-04-01'] = df['2021-04-14':]['Recovered'].cumsum()
df['Died since 2021-04-01'] = df['2021-04-01':]['Deaths'].cumsum()
df['Cases since 2021-04-01'] = df['2021-04-01':]['Cases'].cumsum()
# This is those in hospital but we make this the catch all
exits = df[['Recovered since 2021-04-01', 'Died since 2021-04-01']].sum(axis=1, skipna=True)
df['Other Active Cases'] = \
df['Cases since 2021-04-01'].sub(non_split, fill_value=0).sub(exits, fill_value=0).clip(0)
cols = [
'Died since 2021-04-01',
'Hospitalized Respirator',
'Hospitalized Severe',
'Other Active Cases',
'Hospitalized Field',
'Recovered since 2021-04-01',
]
legends = [
'Deaths from Cases since 1st April',
'In Serious Condition on Ventilator',
'In Serious Condition without Ventilator',
'In Hospital/Mild Condition',
'In Field Hospital',
'Recovered from Cases Confirmed since 1st April',
]
plot_area(df=df,
title='Covid Cases by Current Outcome since 1st April 2021 - Thailand',
legends=legends,
png_prefix='cases_cumulative', cols_subset=cols,
ma_days=None,
kind='area', stacked=True, percent_fig=False,
cmap='tab10',
footnote_left=f'{source}Data Source: CCSA Daily Briefing')
# TODO: I think we can replace the recovered since april with plot showing just hospitalisations?
df["Hospitalized Field Unknown"] = df["Hospitalized Field"].sub(
df[["Hospitalized Field Hospitel", "Hospitalized Field HICI"]].sum(axis=1, skipna=True), fill_value=0).clip(0)
cols = [
'Hospitalized Respirator',
'Hospitalized Severe',
'Hospitalized Field Unknown',
'Hospitalized Field Hospitel',
'Hospitalized Field HICI',
]
df["Hospitalized Mild"] = df["Hospitalized"].sub(df[cols].sum(axis=1, skipna=True), fill_value=0).clip(0)
cols = [
'Hospitalized Respirator',
'Hospitalized Severe',
'Hospitalized Mild',
'Hospitalized Field Unknown',
'Hospitalized Field Hospitel',
'Hospitalized Field HICI',
]
legends = [
'In Serious Condition on Ventilator',
'In Serious Condition without Ventilator',
'In Mild Condition in Hospital',
'In Mild Condition in Field Hospital/Other',
'In Mild Condition in Hotel Field Hospital (Hospitel)',
'In Mild Condition in Home/Community Isolation (HICI)',
]
plot_area(df=df,
title='Active Cases by Condition - Thailand',
png_prefix='active_hospital', cols_subset=cols, legends=legends,
# unknown_name='Hospitalized Other', unknown_total='Hospitalized', unknown_percent=True,
ma_days=7,
kind='area', stacked=True, percent_fig=True, clean_end=True,
cmap='tab10',
footnote_left='Data Source: MOPH Covid-19 Dashboard, CCSA Daily Briefing')
df["Hospitalized All Mild"] = df["Hospitalized Mild"] + df["Hospitalized Field"]
cols = [
'Hospitalized Respirator',
'Hospitalized Severe',
'Hospitalized All Mild',
'Deaths'
]
peaks = df[cols] / df[cols].rolling(7).mean().max(axis=0) * 100
peaks["Hospitalized Occupancy Level 2-3 %"] = df["Hospitalized Occupancy Level 2-3 %"]
legends = [
'Serious on Ventilator (% of peak)',
'Serious no Ventilator (% of peak)',
'Mild (% of peak)',
'Deaths (% of peak)',
'Serious Bed Occupancy % (Lvl 2-3)'
]
plot_area(df=peaks,
title='Active Covid Cases by Condition as % of Peak - Thailand',
png_prefix='active_peak', cols_subset=list(peaks.columns),
legends=legends,
ma_days=7,
kind='line', stacked=False, percent_fig=False, clean_end=True,
cmap='tab10',
y_formatter=perc_format,
footnote_left='Data Source: MOPH Covid-19 Dashboard')
if __name__ == "__main__":
df = import_csv("combined", index=["Date"])
briefings = import_csv("cases_briefings", ["Date"], False)
dash = import_csv("moph_dashboard", ["Date"], False, dir="inputs/json") # so we cache it
dash_weekly = import_csv("moph_dash_weekly", ["Date"], False, dir="inputs/json") # so we cache it
vaccols = [f"Vac Given {d} Cum" for d in range(1, 5)]
hospcols = [c for c in df.columns if 'Hospitalized' in c]
daily = cum2daily(dash_weekly, exclude=vaccols + hospcols)
df = briefings.combine_first(dash).combine_first(daily).combine_first(df)
os.environ["MAX_DAYS"] = '0'
os.environ['USE_CACHE_DATA'] = 'True'
save_active_plots(df)