Skip to content

Commit

Permalink
Merge pull request github#325 from vladpke/master
Browse files Browse the repository at this point in the history
More various quality improvements
  • Loading branch information
Hamel Husain committed Apr 21, 2020
2 parents 797a828 + 297a2cc commit ff7219b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
9 changes: 4 additions & 5 deletions _notebooks/2020-03-19-cases-and-deaths-per-million.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@
"\n",
"# per habitant\n",
"data_pc = data.copy()\n",
"countries = [\"China\", \"Italy\", \"Spain\", \"France\", \"United Kingdom\", \"Germany\", \n",
"countries = [\"Italy\", \"Spain\", \"France\", \"United Kingdom\", \"Germany\", \n",
" \"Portugal\", \"United States\", \"Singapore\", \"South Korea\", \"Japan\", \n",
" \"Brazil\", \"Iran\", 'Netherlands', 'Belgium', 'Sweden', \n",
" 'Switzerland', 'Norway', 'Denmark', 'Austria', 'Slovenia', 'Greece',\n",
" 'Cyprus']\n",
" 'Switzerland', 'Norway', 'Denmark', 'Austria', 'Slovenia', 'Greece']\n",
"data_countries = []\n",
"data_countries_pc = []\n",
"\n",
Expand Down Expand Up @@ -258,7 +257,7 @@
"# Plot it using Altair\n",
"source = data_plot\n",
"\n",
"scales = alt.selection_interval(bind='scales')\n",
"scales = alt.selection_interval(bind='scales', zoom=False)\n",
"selection = alt.selection_multi(fields=['location'], bind='legend')\n",
"\n",
"base = alt.Chart(source, title = \"COVID-19 Deaths Per Million of Inhabitants\").encode(\n",
Expand Down Expand Up @@ -766,7 +765,7 @@
"# Plot it using Altair\n",
"source = data_plot\n",
"\n",
"scales = alt.selection_interval(bind='scales')\n",
"scales = alt.selection_interval(bind='scales', zoom=False)\n",
"selection = alt.selection_multi(fields=['location'], bind='legend')\n",
"\n",
"base = alt.Chart(source, title = \"COVID-19 Confirmed Cases Per Million of Inhabitants\").encode(\n",
Expand Down
40 changes: 24 additions & 16 deletions _notebooks/2020-03-27-compare-trajectories-us.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@
"from IPython.display import HTML\n",
"import matplotlib.pyplot as plt\n",
"\n",
"CHART_WIDTH = 600\n",
"CHART_HEIGHT = 450\n",
"NUM_COLUMNS = 2\n",
"\n",
"states_url = \"https://covidtracking.com/api/states/daily\"\n",
"us_url = \"https://covidtracking.com/api/us/daily\"\n",
"case_threshold = 100\n",
Expand Down Expand Up @@ -287,29 +291,31 @@
"baseline_states = ['CA', 'WA', 'NY']\n",
"max_date = dfc['date'].max()\n",
"color_domain = list(states_df.state.unique())\n",
"y_domain_max_cases = pow(10, math.ceil(math.log10(dfc['Confirmed Cases'].max())))\n",
"ref_max_day_cases = math.ceil(math.log(y_domain_max_cases / SINCE_CASES_NUM, 1.33))\n",
"\n",
"def make_since_chart(highlight_states=[], baseline_states=baseline_states):\n",
" selection = alt.selection_multi(fields=['State'], bind='legend', \n",
" init=[{'State': x} for x in highlight_states + baseline_states])\n",
"\n",
" base = alt.Chart(dfc, width=550).encode(\n",
" base = alt.Chart(dfc, width=CHART_WIDTH, height=CHART_HEIGHT).encode(\n",
" x='Days since 100 cases:Q',\n",
" y=alt.Y('Confirmed Cases:Q', scale=alt.Scale(type='log'), title=\"Confirmed Cases (log scale)\"),\n",
" color=alt.Color(\n",
" 'State:N',\n",
" scale=alt.Scale(scheme=\"category20b\"),\n",
" legend=alt.Legend(columns=len(color_domain)//18+1, symbolLimit=len(color_domain))),\n",
" legend=alt.Legend(columns=NUM_COLUMNS, symbolLimit=len(color_domain))),\n",
" tooltip=list(dfc),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.05))\n",
" )\n",
" max_day = dfc['Days since 100 cases'].max()\n",
" ref = pd.DataFrame([[x, 100*1.33**x] for x in range(max_day+1)], columns=['Days since 100 cases', 'Confirmed Cases'])\n",
" ref = pd.DataFrame([[x, 100*1.33**x] for x in range(ref_max_day_cases)], columns=['Days since 100 cases', 'Confirmed Cases'])\n",
" base_ref = alt.Chart(ref).encode(x='Days since 100 cases:Q', y='Confirmed Cases:Q')\n",
" return (\n",
" base_ref.mark_line(color='black', opacity=.5, strokeDash=[3,3]) +\n",
" base_ref.transform_filter(\n",
" alt.datum['Days since 100 cases'] >= max_day\n",
" ).mark_text(dy=-6, align='right', fontSize=12, fontWeight='bold', text='33% Daily Growth') +\n",
" alt.datum['Days since 100 cases'] >= ref_max_day_cases - 1\n",
" ).mark_text(dx=-40, dy=30, align='right', fontSize=12, fontWeight='bold', text='33% Daily Growth') +\n",
" base.mark_line(point=True).add_selection(selection) + \n",
" base.transform_filter(\n",
" alt.datum['date'] >= int(max_date.timestamp() * 1000)\n",
Expand Down Expand Up @@ -457,13 +463,13 @@
" selection = alt.selection_multi(fields=['State'], bind='legend', \n",
" init=[{'State': x} for x in highlight_states + baseline_states])\n",
"\n",
" base = alt.Chart(dfc.loc[lambda x: x['Days since 10 cases per million'] >= 0], width=550).encode(\n",
" base = alt.Chart(dfc.loc[lambda x: x['Days since 10 cases per million'] >= 0], width=CHART_WIDTH, height=CHART_HEIGHT).encode(\n",
" x='Days since 10 cases per million:Q',\n",
" y=alt.Y('cases_per_m:Q', scale=alt.Scale(type='log'), title=\"Confirmed Cases Per Million (log scale)\"),\n",
" color=alt.Color(\n",
" 'State:N',\n",
" scale=alt.Scale(scheme=\"category20b\"),\n",
" legend=alt.Legend(columns=len(color_domain)//18+1, symbolLimit=len(color_domain))),\n",
" legend=alt.Legend(columns=NUM_COLUMNS, symbolLimit=len(color_domain))),\n",
" tooltip=list(dfc),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.05))\n",
" )\n",
Expand Down Expand Up @@ -628,35 +634,37 @@
"baseline_states = ['CA', 'WA', 'NY']\n",
"max_date = dfd['date'].max()\n",
"color_domain = list(states_df.state.unique())\n",
"y_domain_max_deaths = pow(10, math.ceil(math.log10(dfd['Confirmed Cases'].max())))\n",
"ref_max_day_deaths = math.ceil(math.log(y_domain_max_deaths / SINCE_CASES_NUM, 1.33))\n",
"\n",
"def make_death_chart(highlight_states=[], baseline_states=baseline_states):\n",
" selection = alt.selection_multi(fields=['State'], bind='legend', \n",
" init=[{'State': x} for x in highlight_states + baseline_states])\n",
"\n",
" base = alt.Chart(dfd.loc[lambda x: x['Confirmed Deaths'] >= 1], width=550).encode(\n",
" base = alt.Chart(dfd.loc[lambda x: x['Confirmed Deaths'] >= 1], width=CHART_WIDTH, height=CHART_HEIGHT).encode(\n",
" x='Days since first death:Q',\n",
" y=alt.Y('Confirmed Deaths:Q', scale=alt.Scale(type='log'), title=\"Confirmed Deaths (log scale)\"),\n",
" color=alt.Color(\n",
" 'State:N',\n",
" scale=alt.Scale(scheme=\"category20b\"),\n",
" legend=alt.Legend(columns=len(color_domain)//18+1, symbolLimit=len(color_domain))),\n",
" legend=alt.Legend(columns=NUM_COLUMNS, symbolLimit=len(color_domain))),\n",
" tooltip=list(df),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.05))\n",
" )\n",
" max_day = dfd['Days since first death'].max()\n",
" ref = pd.DataFrame([[x, 20*1.33**x] for x in range(max_day+1)], columns=['Days since first death', 'Confirmed Deaths'])\n",
" ref = pd.DataFrame([[x, 10*1.33**x] for x in range(ref_max_day_deaths)], columns=['Days since first death', 'Confirmed Deaths'])\n",
" base_ref = alt.Chart(ref).encode(x='Days since first death:Q', y='Confirmed Deaths:Q')\n",
" return (\n",
" base_ref.mark_line(color='black', opacity=.5, strokeDash=[3,3]) +\n",
" base_ref.transform_filter(\n",
" alt.datum['Days since first death'] >= max_day\n",
" ).mark_text(dy=-6, dx=-100, align='left', fontSize=12, fontWeight='bold', text='33% Daily Growth') +\n",
" alt.datum['Days since first death'] >= ref_max_day_deaths - 1\n",
" ).mark_text(dx=-140, dy=30, align='left', fontSize=12, fontWeight='bold', text='33% Daily Growth') +\n",
" base.mark_line(point=True).add_selection(selection) + \n",
" base.transform_filter(\n",
" alt.datum['date'] >= int(max_date.timestamp() * 1000)\n",
" ).mark_text(dy=-8, align='left', fontWeight='bold').encode(text='State:N')\n",
" ).properties(\n",
" title=f\"Case Counts over time for US States\" \n",
" title=f\"Death Counts over time for US States\" \n",
" ).configure_title(fontSize=18).configure_legend(labelFontSize=13, titleFontSize=15).configure_axis(\n",
" labelFontSize=13,\n",
" titleFontSize=15\n",
Expand Down Expand Up @@ -782,13 +790,13 @@
" selection = alt.selection_multi(fields=['State'], bind='legend', \n",
" init=[{'State': x} for x in highlight_states + baseline_states])\n",
"\n",
" base = alt.Chart(dfd.loc[lambda x: x.deaths_per_m >= 1], width=550).encode(\n",
" base = alt.Chart(dfd.loc[lambda x: x.deaths_per_m >= 1], width=CHART_WIDTH, height=CHART_HEIGHT).encode(\n",
" x='Days since 1 death per million:Q',\n",
" y=alt.Y('deaths_per_m:Q', scale=alt.Scale(type='log'), title=\"Confirmed Deaths Per Million (log scale)\"),\n",
" color=alt.Color(\n",
" 'State:N',\n",
" scale=alt.Scale(scheme=\"category20b\"),\n",
" legend=alt.Legend(columns=len(color_domain)//18+1, symbolLimit=len(color_domain))),\n",
" legend=alt.Legend(columns=NUM_COLUMNS, symbolLimit=len(color_domain))),\n",
" tooltip=list(df),\n",
" opacity=alt.condition(selection, alt.value(1), alt.value(0.05))\n",
" )\n",
Expand All @@ -801,7 +809,7 @@
" alt.datum['date'] >= int(max_date.timestamp() * 1000)\n",
" ).mark_text(dy=-8, align='right', fontWeight='bold').encode(text='State:N')\n",
" ).properties(\n",
" title=f\"Case Counts over time for US States\" \n",
" title=f\"Death Counts over time for US States\" \n",
" ).configure_title(fontSize=18).configure_legend(labelFontSize=13, titleFontSize=15).configure_axis(\n",
" labelFontSize=13,\n",
" titleFontSize=15\n",
Expand Down

0 comments on commit ff7219b

Please sign in to comment.