{ "cells": [ { "cell_type": "markdown", "id": "110f7f47", "metadata": {}, "source": [ "This notebooks has a dual purpose:\n", "\n", "1. it is is execute in the `germany-hospitalisierungsrate.ipynb` notebook via the `%run NOTEBOOK` magic. So we need to fetch the data here, and make the `plot_hi` function available, as the namespace of the two notebooks is shared.\n", "\n", "2. it is also used as a development pad for new features. The commented out commands below record some of the development work, and help to re-start that activity.\n", " \n", "If we create figures in this notebook, they will be displayed in the `germany-hospitalisierungsrate.ipynb`. Display of tables seems to be suppressed. Output to stdout semes to be displayed." ] }, { "cell_type": "code", "execution_count": null, "id": "ae92a2dc", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import datetime\n", "\n", "%config InlineBackend.figure_format = 'svg'\n", "\n", "data_url = \"https://github.com/robert-koch-institut/COVID-19-Hospitalisierungen_in_Deutschland/blob/master/Aktuell_Deutschland_COVID-19-Hospitalisierungen.csv?raw=true\"\n" ] }, { "cell_type": "code", "execution_count": null, "id": "db7ca72f", "metadata": {}, "outputs": [], "source": [ "def germany_fetch_hospitalisierungs_inzidenz():\n", "\n", " data = pd.read_csv(data_url)\n", " data = data.set_index(pd.to_datetime(data[\"Datum\"]))\n", " # Select a sum over all age groups\n", " data = data[data[\"Altersgruppe\"] == \"00+\"]\n", " return data\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c8cf1632", "metadata": {}, "outputs": [], "source": [ "# local copy to work offline\n", "# !wget \"https://github.com/robert-koch-institut/COVID-19-Hospitalisierungen_in_Deutschland/blob/master/Aktuell_Deutschland_COVID-19-Hospitalisierungen.csv?raw=true\"\n" ] }, { "cell_type": "code", "execution_count": null, "id": "27b774e6", "metadata": {}, "outputs": [], "source": [ "# filter for the data we care about\n", "data = germany_fetch_hospitalisierungs_inzidenz()\n", "# select subset of data we care about\n", "data = data[[\"Bundesland\", \"7T_Hospitalisierung_Inzidenz\"]]" ] }, { "cell_type": "code", "execution_count": null, "id": "fa979f8e", "metadata": {}, "outputs": [], "source": [ "# HospitalisierungsInzidenz\n", "hi = data[[\"Bundesland\", \"7T_Hospitalisierung_Inzidenz\"]]" ] }, { "cell_type": "code", "execution_count": null, "id": "ee58d75e", "metadata": {}, "outputs": [], "source": [ "hi_bund = hi[hi[\"Bundesland\"] == \"Bundesgebiet\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "7ae73fe4", "metadata": {}, "outputs": [], "source": [ "def fine_tune_plot(ax):\n", " fig = ax.get_figure()\n", " fig.autofmt_xdate()" ] }, { "cell_type": "code", "execution_count": null, "id": "9b3e45aa", "metadata": {}, "outputs": [], "source": [ "## Example:\n", "# fig, ax = plt.subplots()\n", "# ax.plot(hi_bund.index, hi_bund[\"7T_Hospitalisierung_Inzidenz\"].values)\n", "# fine_tune_plot(ax)" ] }, { "cell_type": "code", "execution_count": null, "id": "bf0768e4", "metadata": {}, "outputs": [], "source": [ "# Overview of current numbers\n", "today = hi.loc[hi.index[0]]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fbfa590e", "metadata": {}, "outputs": [], "source": [ "today.sort_values(by='7T_Hospitalisierung_Inzidenz', ascending=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "ab0bfb95", "metadata": {}, "outputs": [], "source": [ "def plot_hi(data, ymax=None):\n", " \"\"\"Plot HospitalisierungsInzidenz in Germany. \n", " \n", " data is a pandas Dataframe with index:\n", " \n", " DatetimeIndex(['2021-11-24', '2021-11-24', '2021-11-24', '2021-11-24',\n", " '2021-11-24', '2021-11-24', '2021-11-24', '2021-11-24',\n", " '2021-11-24', '2021-11-24',\n", " ...\n", " '2020-03-05', '2020-03-05', '2020-03-05', '2020-03-05',\n", " '2020-03-05', '2020-03-05', '2020-03-05', '2020-03-05',\n", " '2020-03-05', '2020-03-05'],\n", " dtype='datetime64[ns]', name='Datum', length=10710, freq=None)\n", " \n", " and columns:\n", " \n", " Index(['Bundesland', '7T_Hospitalisierung_Inzidenz'], dtype='object')\n", " \n", " Example Dataframes can be created in `germany-compute-hospitalisierungsrate.ipynb`.\n", " \"\"\"\n", " \n", " fig, ax = plt.subplots(figsize=(8, 10))\n", "\n", " # plot whole of Germany separately\n", " land = \"Bundesgebiet\"\n", " land_data = data[data['Bundesland'] == land]\n", " # use shortcut for the Data (d) we care about\n", " d = land_data['7T_Hospitalisierung_Inzidenz']\n", " \n", " ax.plot(d.index, d, \n", " linewidth=5, alpha=0.9, label=land)\n", "\n", " # and all other states (\"Länder\")\n", " bundeslaender = set(data['Bundesland']) - {\"Bundesgebiet\"}\n", " for land in bundeslaender:\n", " land_data = data[data['Bundesland'] == land]\n", " d = land_data['7T_Hospitalisierung_Inzidenz']\n", " \n", " ax.plot(d.index, d, '.-', label=land,)\n", " # label last data point\n", " ax.text(d.index[0], d[0], land, rotation=45)\n", "\n", " \n", " ax.legend(loc='center', bbox_to_anchor=(1.3, 0.5))\n", " fig.autofmt_xdate()\n", " ax.grid()\n", " ax.margins(0) # remove default margins (matplotlib verision 2+)\n", "\n", " \n", " min_date = data.index[-1]\n", " label_pos_x = min_date + datetime.timedelta(days=1)\n", " \n", " props = dict(boxstyle='round', facecolor='white', alpha=1.0)\n", " \n", " ax.axhspan(0, 3, facecolor='green', alpha=0.2)\n", " ax.text(label_pos_x, 4, \"2G\", bbox=props)\n", " ax.axhspan(3, 6, facecolor='yellow', alpha=0.2)\n", " ax.text(label_pos_x, 7, \"2G+\", bbox=props)\n", " ax.axhspan(6, 9, facecolor='orange', alpha=0.2)\n", " ax.text(label_pos_x, 10, \"restrictions\", bbox=props)\n", " ax.axhspan(9, 12, facecolor='red', alpha=0.2)\n", " ax.text(label_pos_x, 13, \"additional measures required?\", bbox=props)\n", " ax.axhspan(9, data['7T_Hospitalisierung_Inzidenz'].max(), facecolor='purple', alpha=0.1)\n", "\n", " if ymax:\n", " ax.set_ylim(top=ymax)\n", " \n", " return fig, ax\n", "\n", "## Example usage:\n", "# fig, ax = plot_hi(data[\"2021-10-01\":])\n" ] }, { "cell_type": "markdown", "id": "43f9228f", "metadata": {}, "source": [ "Possible extensions:\n", "\n", "- show also intensive care bed capacities (because the hospitalisierungsrate is only about the number of new people going to hospital: it gives no information about the situation in the hospital)\n", "\n", "- explore correlation between infections / hospitalisierungsrate / deaths / vaccination status\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "37a3278a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.4" } }, "nbformat": 4, "nbformat_minor": 5 }