{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "import altair as alt\n", "import pandas as pd\n", "from IPython.display import display, HTML" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "save_figures = False\n", "data_path = '../data/openzh-covid-19'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "html_credits=HTML('''\n", "<p style=\"font-size: smaller\">Data Sources: \n", " <a href=\"https://github.com/openZH/covid_19\">OpenData Zuerich</a>,\n", " <a href=\"https://www.bfs.admin.ch\">Federal Statistical Office</a>\n", "<br>\n", "Analysis:\n", " <a href=\"https://renkulab.io/projects/covid-19/covid-19-public-data\">Covid-19 Public Data Collaboration Project</a>\n", "</p>''')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary data for Covid-19 cases in Switzerland\n", "\n", "The data for Switzerland comes from the effort initiated by [OpenData Zürich](https://github.com/openZH/covid_19) and collected during the [Case data #covid19mon hackathon challenge](https://db.schoolofdata.ch/project/73). \n", "\n", "Below we make plots of total cases, total cases per 10k population and total deaths. You can click on the canton abbreviations in the legend to highlight individual lines. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# read in cantonal data and produce one dataframe\n", "df_list = []\n", "\n", "for f in Path(data_path).glob('COVID19_Fallzahlen_Kanton_*total.csv'):\n", " df_list.append(pd.read_csv(f))\n", "\n", "df = pd.concat(df_list)\n", "\n", "df['date'] = pd.to_datetime(df['date'], dayfirst=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# read in population data\n", "from covid_19_dashboard import helper, plotting\n", "\n", "population = {key[3:]: value for (key,value) in helper.get_region_populations('CHE').items()}\n", "\n", "for x in ['conf', 'deceased']:\n", " df[f'ncumul_{x}_100k'] = df.apply(\n", " lambda row: row[f'ncumul_{x}']/population[row.abbreviation_canton_and_fl]*100000, axis=1\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Total cases" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "base = alt.Chart(df)\n", "base.configure_header(titleFontSize=25)\n", "base.configure_axis(labelFontSize=15, titleFontSize=15)\n", "\n", "# cumul = generate_canton_chart(base, 'ncumul_conf', 'Cases', 'Cases')\n", "cumul = plotting.generate_region_chart(\n", " base, \n", " column='ncumul_conf', \n", " region_column='abbreviation_canton_and_fl',\n", " ytitle='Cases', \n", " legend_title='Canton',\n", " tooltip_title='Cases')\n", "cumul_100k = plotting.generate_region_chart(\n", " base, \n", " column='ncumul_conf_100k', \n", " region_column='abbreviation_canton_and_fl',\n", " ytitle='Cases per 100k population', \n", " legend_title='Canton',\n", " tooltip_title='Cases/100k')\n", "\n", "chart = alt.hconcat(\n", " cumul, cumul_100k, title='Covid-19 cases in Switzerland by Canton'\n", ").configure_title(\n", " anchor='middle'\n", ")\n", "\n", "display(chart)\n", "if save_figures:\n", " chart.save(str(Path(figures_path) / 'switzerland-cases-by-canton.html'))\n", " \n", "display(html_credits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Deaths" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "base = alt.Chart(df)\n", "base.configure_header(titleFontSize=25)\n", "base.configure_axis(labelFontSize=15, titleFontSize=15)\n", "\n", "deaths = plotting.generate_region_chart(\n", " base, \n", " column='ncumul_deceased', \n", " region_column='abbreviation_canton_and_fl',\n", " ytitle='Deaths', \n", " tooltip_title='Deaths',\n", " legend_title='Canton'\n", ")\n", "deaths_10k = plotting.generate_region_chart(\n", " base, \n", " column='ncumul_deceased_100k', \n", " region_column='abbreviation_canton_and_fl',\n", " ytitle='Deaths per 100k population', \n", " tooltip_title='Deaths/100k',\n", " legend_title='Canton'\n", ")\n", "\n", "chart = alt.hconcat(\n", " deaths, deaths_10k, title='Covid-19 deaths in Switzerland by Canton'\n", ").configure_title(\n", " anchor='middle'\n", ")\n", "display(chart) \n", "display(html_credits)\n", "\n", "if save_figures:\n", " chart.save(str(Path(figures_path) / 'switzerland-deaths-by-canton.html'))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "since_100th_case = helper.make_since_df(df, 'ncumul_conf', 'abbreviation_canton_and_fl')\n", "\n", "base = alt.Chart(since_100th_case, title=\"Switzerland: total cases since 100th case\").encode(alt.Y(scale=alt.Scale(type='log')))\n", "\n", "lineChart = plotting.make_region_since_chart(\n", " base, \n", " 'ncumul_conf', \n", " 'sinceDay0', \n", " 'abbreviation_canton_and_fl', \n", " 'Days since 100th case',\n", " 'Cumulative positive cases',\n", " 'Cases',\n", " 'Canton'\n", ").properties(\n", " width=450,\n", " height=450\n", ")\n", "\n", "rule_chart = plotting.make_rule_chart(max_days=20, pos_day=(9,5000), pos_3days=(19,8000))\n", "\n", "lineChart + rule_chart" ] } ], "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 }