Newer
Older
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
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import os\n",
"from IPython.display import display, HTML, Markdown\n",
"import covid_19_dashboard as helper"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"ts_folder = \"../data/covid-19_jhu-csse/\"\n",
"rates_folder = \"../data/covid-19_rates/\"\n",
"geodata_path = \"../data/geodata/geo_data.csv\"\n",
"out_folder = None\n",
"PAPERMILL_OUTPUT_PATH = None"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read in the data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"jhu_frames_map = helper.read_jhu_frames_map(ts_folder)\n",
"rates_frames_map = helper.read_rates_frames_map(rates_folder)\n",
"geodata_df = helper.read_geodata(geodata_path)\n",
"\n",
"# Identify countries with 100 or more cases\n",
"countries_over_thresh = helper.countries_with_number_of_cases(jhu_frames_map, 'confirmed', 100)\n",
"# Filter out some countries with very high case/population ratio\n",
"countries_over_thresh = [c for c in countries_over_thresh if c not in set(['Andorra', 'Iceland', 'San Marino'])]"
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
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Questions About COVID-19 and Its Spread\n",
"\n",
"Understanding the spread, distribution, and deadliness of COVID-19 is difficult, despite the data available about it. Differences in rates of testing, quality of data, demographics, etc. make it difficult to compare data between countries. \n",
"\n",
"All this needs to be considered when looking at the plots below. But despite those caveats, I found it helpful to plot the raw data, even though direct comparisons between countries might not be inaccurate."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_ts = jhu_frames_map['confirmed'].iloc[:,-1].name.strftime(\"%b %d %Y\")\n",
"display(HTML(f\"<em>Data up to {data_ts}; countries with 100 or more confirmed cases.</em>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How are cases per 100,000 distributed geographically?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import altair as alt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"map_df = helper.compute_map_df(rates_frames_map, jhu_frames_map, geodata_df, countries_over_thresh)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(helper.map_of_variable(map_df, 'Confirmed/100k', 'Confirmed'))\n",
"display(HTML('''\n",
"<p style=\"font-size: smaller\">Data Sources: \n",
" <a href=\"https://github.com/CSSEGISandData/COVID-19\">JHU CSSE</a>,\n",
" <a href=\"https://data.worldbank.org/indicator/SP.POP.TOTL\">World Bank</a>,\n",
" <a href=\"https://worldmap.harvard.edu/data/geonode:country_centroids_az8\">Harvard Worldmap</a>\n",
"</p>'''))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bars = alt.Chart(map_df).mark_bar().encode(\n",
" x='Confirmed/100k:Q',\n",
" y=alt.Y(\"Country/Region:N\", sort='-x'),\n",
" tooltip=[\"Country/Region:N\", \n",
" \"Confirmed:Q\", \"Deaths:Q\", \"Recovered:Q\",\n",
" \"Confirmed/100k:Q\", \"Deaths/100k:Q\", \"Recovered/100k:Q\"]\n",
")\n",
"\n",
"text = bars.mark_text(\n",
" align='left',\n",
" baseline='middle',\n",
" dx=3 # Nudges text to right so it doesn't appear on top of the bar\n",
").encode(\n",
" text=alt.Text('Confirmed/100k:Q', format=\".3\")\n",
")\n",
"\n",
"chart = (bars + text).properties(height=900, title=f\"Confirmed cases per 100k inhabitants\")\n",
"display(chart)\n",
"display(HTML('''\n",
"<p style=\"font-size: smaller\">Data Sources: \n",
" <a href=\"https://github.com/CSSEGISandData/COVID-19\">JHU CSSE</a>,\n",
" <a href=\"https://data.worldbank.org/indicator/SP.POP.TOTL\">World Bank</a>,\n",
" <a href=\"https://worldmap.harvard.edu/data/geonode:country_centroids_az8\">Harvard Worldmap</a>\n",
"</p>'''))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## How have cases been growing?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"confirmed_rate_df = helper.growth_df(rates_frames_map, geodata_df, 'confirmed', countries_over_thresh, 10)\n",
"latest_confirmed_ser = confirmed_rate_df.set_index(\n",
" ['Country/Region', 'Geo Region', 'Date']).drop(\n",
" ['Longitude', 'Latitude'], axis=1).unstack().iloc[:,-1]\n",
"sort_order = latest_confirmed_ser.groupby('Geo Region').mean().sort_values(ascending=False).index.tolist()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"confirmed_rate_df['Country'] = confirmed_rate_df['Country/Region']\n",
"base = alt.Chart(confirmed_rate_df.dropna()).properties(\n",
" width=300, height=200, title=\"Countries with 2 or more cases per 100k\")\n",
"confirmed_rate_df['Country'] = confirmed_rate_df['Country/Region']\n",
"selection = alt.selection_multi(fields=['Country'], bind='legend')\n",
"opacity=alt.condition(selection, alt.value(1), alt.value(0.2))\n",
"line = base.mark_line().encode(\n",
" alt.X('Date'),\n",
" alt.Y('Confirmed/100k', scale=alt.Scale(type='symlog')),\n",
" color=alt.Color('Country'),\n",
" facet=alt.Facet('Geo Region:N', columns=1, sort=alt.SortArray(sort_order), title='Geographic Region'),\n",
" tooltip=[\"Country:N\", \"Date:T\", \"Confirmed/100k:Q\"],\n",
" opacity=opacity\n",
").add_selection(selection)\n",
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"display(line)\n",
"display(HTML('''\n",
"<p style=\"font-size: smaller\">Data Sources: \n",
" <a href=\"https://github.com/CSSEGISandData/COVID-19\">JHU CSSE</a>,\n",
" <a href=\"https://data.worldbank.org/indicator/SP.POP.TOTL\">World Bank</a>,\n",
" <a href=\"https://worldmap.harvard.edu/data/geonode:country_centroids_az8\">Harvard Worldmap</a>\n",
"</p>'''))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def country_increase_df(c, df_nominal, growth_in_rate_df):\n",
" over_100 = df_nominal[df_nominal['Confirmed'] >= 100]\n",
" tdf = (over_100[['Date', 'Confirmed']] - over_100.iloc[0][['Date', 'Confirmed']]).reset_index()\n",
" tdfr = growth_in_rate_df[(growth_in_rate_df['Date'] >= over_100.iloc[0]['Date']) &\n",
" (growth_in_rate_df['Country/Region'] == c)].reset_index()\n",
" tdf['Confirmed/100k'] = tdfr['Confirmed/100k']\n",
" tdf['Country/Region'] = c\n",
" tdf['Days'] = (tdf['Date'] / np.timedelta64(1, 'D')).astype(int)\n",
" return tdf[['Country/Region', 'Days', 'Confirmed', 'Confirmed/100k']]\n",
"\n",
"\n",
"growth_in_rate_df = helper.growth_df(rates_frames_map, geodata_df, 'confirmed', countries_over_thresh, 0)\n",
"frame_map = {'confirmed': jhu_frames_map['confirmed'].groupby(level='Country/Region').sum()}\n",
"growth_in_value_df = helper.growth_df(frame_map, geodata_df, 'confirmed', countries_over_thresh, 1000)\n",
"growth_in_value_df = growth_in_value_df.rename({'Confirmed/100k':'Confirmed'}, axis=1)\n",
"increase_df = pd.concat([country_increase_df(c, df_nominal, growth_in_rate_df) for \n",
" c, df_nominal in growth_in_value_df.groupby('Country/Region')])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def facetted_growth_plot(df, variable, sort_order, ref_country, title, yscale='linear'):\n",
" base = alt.Chart(df).properties(\n",
" width=250, height=150)\n",
" line = base.mark_line().encode(\n",
" x='Days',\n",
" y=variable,\n",
" color='Country/Region',\n",
" tooltip=[\"Country/Region:N\", \"Days:Q\", f\"{variable}:Q\"]\n",
" )\n",
" label_loc = increase_df[increase_df['Country/Region'] == ref_country]['Days'].iloc[-2]\n",
" ref = base.mark_line(opacity=0.3).encode(\n",
" x='Days',\n",
" y=alt.Y(variable, scale=alt.Scale(type=yscale)),\n",
" color=alt.ColorValue('steelblue'),\n",
" ).transform_filter(f\"datum['Country/Region'] == '{ref_country}'\")\n",
" ref += ref.mark_text().encode(text='Country/Region:N').transform_filter(f\"datum['Days'] == {label_loc}\")\n",
" charts = []\n",
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
" # make our small multiples\n",
" for country in sort_order:\n",
" smallm = line.transform_filter(f\"datum['Country/Region'] == '{country}'\").properties(\n",
" title=country)\n",
" smallm += ref\n",
" charts.append(smallm)\n",
"\n",
" # group the small multiples into 3 horizontal charts\n",
" groups = []\n",
" c = None\n",
" for i, chart in enumerate(charts):\n",
" if not i%3:\n",
" if c != None:\n",
" groups.append(c)\n",
" c = alt.hconcat()\n",
" c |= chart\n",
" # vertically combine the horizontal charts\n",
" chart = alt.vconcat(title=title)\n",
" for c in groups:\n",
" chart &= c\n",
" return chart"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sort_order = growth_in_value_df.groupby(\n",
" 'Country/Region').max().sort_values(\n",
" 'Confirmed', ascending=False).index.tolist()\n",
"# Exclude China in this plot because its numbers are far greater then everywhere else\n",
"sort_order = [o for o in sort_order if o != 'China']\n",
"chart = facetted_growth_plot(increase_df[increase_df['Country/Region'] != 'China'], \n",
" 'Confirmed',\n",
" sort_order,\n",
" 'Italy',\n",
" \"Growth of cases from case 100, compared to Italy\")\n",
"display(chart)\n",
"display(HTML('''\n",
"<p style=\"font-size: smaller\">Data Sources: \n",
" <a href=\"https://github.com/CSSEGISandData/COVID-19\">JHU CSSE</a>,\n",
" <a href=\"https://data.worldbank.org/indicator/SP.POP.TOTL\">World Bank</a>,\n",
" <a href=\"https://worldmap.harvard.edu/data/geonode:country_centroids_az8\">Harvard Worldmap</a>\n",
"</p>\n",
"<p style=\"font-size: smaller\">Inspired by <a href=\"https://covid19dashboards.com/growth-analysis/\">Thomas Wiecki</a>'''))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sort_order = growth_in_value_df.groupby(\n",
" 'Country/Region').max().sort_values(\n",
" 'Confirmed', ascending=False).index.tolist()\n",
"chart = facetted_growth_plot(increase_df, \n",
" 'Confirmed/100k',\n",
" sort_order,\n",
" 'Italy',\n",
" \"Growth of cases/100k from case 100, compared to Italy (log scale)\",\n",
" 'log')\n",
"display(chart)\n",
"display(HTML('''\n",
"<p style=\"font-size: smaller\">Data Sources: \n",
" <a href=\"https://github.com/CSSEGISandData/COVID-19\">JHU CSSE</a>,\n",
" <a href=\"https://data.worldbank.org/indicator/SP.POP.TOTL\">World Bank</a>,\n",
" <a href=\"https://worldmap.harvard.edu/data/geonode:country_centroids_az8\">Harvard Worldmap</a>\n",
"</p>'''))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
}
],
"metadata": {
"hide_input": true,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}