{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "papermill-error-cell-tag" ] }, "source": [ "An Exception was encountered at 'In [6]'." ] }, { "cell_type": "markdown", "metadata": { "papermill": { "duration": 0.006089, "end_time": "2024-04-17T23:02:02.676707", "exception": false, "start_time": "2024-04-17T23:02:02.670618", "status": "completed" }, "tags": [] }, "source": [ "# Cases in London\n", "\n", "The graphs below use the data from . Daily cases per region is not available, but they do provide the raw data in . Note that the data are now shown by the date the specimen was taken from the person being tested. This gives a much more useful analysis of the progression of cases over time. It does mean that the latest days’ figures are always incomplete, and **only data from 5 days or more ago can be considered complete**. Because of that, in the graphs below, we only use data from 5 days ago or previous." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T23:02:02.689756Z", "iopub.status.busy": "2024-04-17T23:02:02.689271Z", "iopub.status.idle": "2024-04-17T23:02:02.934383Z", "shell.execute_reply": "2024-04-17T23:02:02.934887Z" }, "papermill": { "duration": 0.25333, "end_time": "2024-04-17T23:02:02.935080", "exception": false, "start_time": "2024-04-17T23:02:02.681750", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "import datetime\n", "import gzip\n", "import io\n", "import time\n", "\n", "import pandas as pd\n", "import plotly.graph_objs as go\n", "from plotly.subplots import make_subplots\n", "import requests" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T23:02:02.948114Z", "iopub.status.busy": "2024-04-17T23:02:02.947659Z", "iopub.status.idle": "2024-04-17T23:02:02.949938Z", "shell.execute_reply": "2024-04-17T23:02:02.950308Z" }, "papermill": { "duration": 0.010217, "end_time": "2024-04-17T23:02:02.950415", "exception": false, "start_time": "2024-04-17T23:02:02.940198", "status": "completed" }, "tags": [ "parameters" ] }, "outputs": [], "source": [ "# Parameters\n", "secondary_axis = False" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T23:02:02.961460Z", "iopub.status.busy": "2024-04-17T23:02:02.961011Z", "iopub.status.idle": "2024-04-17T23:02:02.963312Z", "shell.execute_reply": "2024-04-17T23:02:02.963661Z" }, "papermill": { "duration": 0.008645, "end_time": "2024-04-17T23:02:02.963764", "exception": false, "start_time": "2024-04-17T23:02:02.955119", "status": "completed" }, "tags": [ "injected-parameters" ] }, "outputs": [], "source": [ "# Parameters\n", "secondary_axis = True\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T23:02:02.975913Z", "iopub.status.busy": "2024-04-17T23:02:02.975498Z", "iopub.status.idle": "2024-04-17T23:02:02.982749Z", "shell.execute_reply": "2024-04-17T23:02:02.983134Z" }, "papermill": { "duration": 0.013805, "end_time": "2024-04-17T23:02:02.983246", "exception": false, "start_time": "2024-04-17T23:02:02.969441", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "pd.options.plotting.backend = \"plotly\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T23:02:02.994698Z", "iopub.status.busy": "2024-04-17T23:02:02.994261Z", "iopub.status.idle": "2024-04-17T23:02:09.971495Z", "shell.execute_reply": "2024-04-17T23:02:09.970507Z" }, "papermill": { "duration": 6.983489, "end_time": "2024-04-17T23:02:09.971715", "exception": false, "start_time": "2024-04-17T23:02:02.988226", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "content = requests.get(\"https://coronavirus.data.gov.uk/downloads/csv/coronavirus-cases_latest.csv\").content\n", "with gzip.open(f\"coronavirus-cases-{round(time.time())}.csv.gz\", \"wb\") as fp:\n", " fp.write(content)\n", "latest = io.StringIO(content.decode(\"utf-8\"))\n", "df = pd.read_csv(latest)" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "papermill-error-cell-tag" ] }, "source": [ "Execution using papermill encountered an exception here and stopped:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T23:02:09.989228Z", "iopub.status.busy": "2024-04-17T23:02:09.988732Z", "iopub.status.idle": "2024-04-17T23:02:10.066384Z", "shell.execute_reply": "2024-04-17T23:02:10.065028Z" }, "papermill": { "duration": 0.088073, "end_time": "2024-04-17T23:02:10.066556", "exception": true, "start_time": "2024-04-17T23:02:09.978483", "status": "failed" }, "tags": [] }, "outputs": [ { "ename": "KeyError", "evalue": "'Specimen date'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m~/python-3.8.3/lib/python3.8/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m 2645\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2646\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2647\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;31mKeyError\u001b[0m: 'Specimen date'", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Specimen date\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_datetime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Specimen date\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mmost_recent_date\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Specimen date\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m#df = df[df[\"Specimen date\"] < (most_recent_date - datetime.timedelta(days=5))]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/python-3.8.3/lib/python3.8/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 2798\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnlevels\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2799\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_multilevel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2800\u001b[0;31m \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2801\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_integer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2802\u001b[0m \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mindexer\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/python-3.8.3/lib/python3.8/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m 2646\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2647\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2648\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_maybe_cast_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2649\u001b[0m \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2650\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mindexer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n", "\u001b[0;31mKeyError\u001b[0m: 'Specimen date'" ] } ], "source": [ "df[\"Specimen date\"] = pd.to_datetime(df[\"Specimen date\"])\n", "most_recent_date = df[\"Specimen date\"].max()\n", "#df = df[df[\"Specimen date\"] < (most_recent_date - datetime.timedelta(days=5))]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "print(\"Lastest data available:\", most_recent_date)\n", "print(\"Last update:\", datetime.datetime.utcnow())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "def create_region_df(df, region):\n", " df = df[df[\"Area name\"] == region]\n", " df = df.rename(columns={\"Daily lab-confirmed cases\": \"Daily\"})\n", " df = df[[\"Specimen date\", \"Daily\"]]\n", " df = df.groupby(\"Specimen date\").max()\n", "\n", " df[\"Cummulative\"] = df[\"Daily\"].cumsum()\n", "\n", " return df\n", "\n", "\n", "def plot(df, regions):\n", " fig = make_subplots(\n", " cols=1,\n", " rows=len(regions),\n", " shared_xaxes=True,\n", " specs=[[{\"secondary_y\": True}]] * len(regions) if secondary_axis else None,\n", " subplot_titles=regions,\n", " vertical_spacing=0.02\n", " )\n", " for i, region in enumerate(regions):\n", " region_df = create_region_df(df, region)\n", "\n", " fig.add_trace(\n", " go.Bar(\n", " x=region_df.index,\n", " y=region_df.Daily,\n", " name=\"Daily\",\n", " legendgroup=\"Daily\",\n", " marker={\"color\": \"blue\"},\n", " showlegend=True if i == 0 else False,\n", " ),\n", " row=i + 1,\n", " col=1\n", " )\n", " daily_resample = region_df.resample(\"D\").sum()\n", " rolling = daily_resample.Daily.rolling(20).sum()\n", " fig.add_trace(\n", " go.Scatter(\n", " x=daily_resample.index,\n", " y=rolling,\n", " name=\"Rolling sum (20 days)\",\n", " legendgroup=\"Rolling\",\n", " mode=\"lines+markers\",\n", " marker={\"color\": \"green\"},\n", " showlegend=True if i == 0 else False,\n", " ),\n", " secondary_y=secondary_axis,\n", " row=i + 1,\n", " col=1\n", " )\n", "\n", " first_good_date = most_recent_date - datetime.timedelta(days=5)\n", " fig.update_layout(shapes=[\n", " {\"type\": \"line\", \"yref\": \"paper\", \"y0\": 0, \"y1\": 1, \"xref\": \"x\", \"x0\": first_good_date, \"x1\": first_good_date, \"line\": {\"dash\": \"dot\", \"color\": \"DarkRed\"}}\n", " ])\n", " fig.update_yaxes(title_text=\"Daily Cases\" if secondary_axis else \"Daily Cases & Rolling sum\")\n", " if secondary_axis:\n", " fig.update_yaxes(title_text=\"Rolling sum\", secondary_y=True)\n", " fig.update_layout(height=1800)\n", " fig.show()\n", "\n", "\n", "def plot_cum(df, regions):\n", " fig = make_subplots(\n", " cols=1,\n", " rows=len(regions),\n", " shared_xaxes=True,\n", " subplot_titles=regions,\n", " vertical_spacing=0.02,\n", " )\n", " for i, region in enumerate(regions):\n", " region_df = create_region_df(df, region)\n", "\n", " fig.add_trace(\n", " go.Scatter(\n", " x=region_df.index,\n", " y=region_df.Cummulative,\n", " name=\"Cummulative\",\n", " legendgroup=\"Cummulative\",\n", " mode=\"lines+markers\",\n", " marker={\"color\": \"red\"},\n", " showlegend=True if i == 0 else False,\n", " ),\n", " row=i + 1,\n", " col=1\n", " )\n", "\n", " first_good_date = most_recent_date - datetime.timedelta(days=5)\n", " fig.update_layout(shapes=[\n", " {\"type\": \"line\", \"yref\": \"paper\", \"y0\": 0, \"y1\": 1, \"xref\": \"x\", \"x0\": first_good_date, \"x1\": first_good_date, \"line\": {\"dash\": \"dot\", \"color\": \"DarkRed\"}}\n", " ])\n", " fig.update_yaxes(title_text=\"Cummulative Cases\")\n", " fig.update_layout(height=1800)\n", " fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "plot(df, [\"London\", \"Wandsworth\", \"Hounslow\", \"Ealing\", \"Hammersmith and Fulham\", \"Newham\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "plot_cum(df, [\"London\", \"Wandsworth\", \"Hounslow\", \"Ealing\", \"Hammersmith and Fulham\", \"Newham\"])" ] } ], "metadata": { "celltoolbar": "Tags", "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.8.3" }, "papermill": { "duration": 8.678092, "end_time": "2024-04-17T23:02:10.475065", "environment_variables": {}, "exception": true, "input_path": "/home/rafael/public_html/covid-19/London.ipynb", "output_path": "/home/rafael/public_html/covid-19/London-alt.ipynb", "parameters": { "secondary_axis": true }, "start_time": "2024-04-17T23:02:01.796973", "version": "2.1.3" } }, "nbformat": 4, "nbformat_minor": 4 }