{
 "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\n",
    "\n",
    "from covid_19_utils import helper, plotting\n",
    "from covid_19_utils.converters import CaseConverter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "parameters"
    ]
   },
   "outputs": [],
   "source": [
    "save_figures = False\n",
    "data_path = '../data/openzh-covid-19'\n",
    "atlas_path = '../data/atlas'"
   ]
  },
  {
   "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",
    "converter = CaseConverter(atlas_path)\n",
    "df = converter.read_convert(data_path)\n",
    "df[\"canton\"] = df.apply(lambda row: row[\"region_iso\"][3:], axis=1)"
   ]
  },
  {
   "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, 'positive', 'Cases', 'Cases')\n",
    "cumul = plotting.generate_region_chart(\n",
    "    base, \n",
    "    column='positive', \n",
    "    region_column='canton',\n",
    "    ytitle='Cases', \n",
    "    legend_title='Canton',\n",
    "    tooltip_title='Cases')\n",
    "cumul_100k = plotting.generate_region_chart(\n",
    "    base, \n",
    "    column='positive_100k', \n",
    "    region_column='canton',\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='deceased', \n",
    "    region_column='canton',\n",
    "    ytitle='Deaths', \n",
    "    tooltip_title='Deaths',\n",
    "    legend_title='Canton'\n",
    ")\n",
    "deaths_10k = plotting.generate_region_chart(\n",
    "    base, \n",
    "    column='deceased_100k', \n",
    "    region_column='canton',\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, 'positive', 'canton')\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",
    "    'positive', \n",
    "    'sinceDay0', \n",
    "    'canton', \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=max(since_100th_case['sinceDay0']), pos_day=(9,5000), pos_3days=(19,8000))\n",
    "\n",
    "lineChart + rule_chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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
}