{
"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.006093,
"end_time": "2024-09-11T16:02:03.351114",
"exception": false,
"start_time": "2024-09-11T16:02:03.345021",
"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-09-11T16:02:03.365700Z",
"iopub.status.busy": "2024-09-11T16:02:03.365076Z",
"iopub.status.idle": "2024-09-11T16:02:03.635141Z",
"shell.execute_reply": "2024-09-11T16:02:03.635940Z"
},
"papermill": {
"duration": 0.27991,
"end_time": "2024-09-11T16:02:03.636130",
"exception": false,
"start_time": "2024-09-11T16:02:03.356220",
"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-09-11T16:02:03.653075Z",
"iopub.status.busy": "2024-09-11T16:02:03.652659Z",
"iopub.status.idle": "2024-09-11T16:02:03.655612Z",
"shell.execute_reply": "2024-09-11T16:02:03.655252Z"
},
"papermill": {
"duration": 0.013046,
"end_time": "2024-09-11T16:02:03.655701",
"exception": false,
"start_time": "2024-09-11T16:02:03.642655",
"status": "completed"
},
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"# Parameters\n",
"secondary_axis = False"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-11T16:02:03.668689Z",
"iopub.status.busy": "2024-09-11T16:02:03.668256Z",
"iopub.status.idle": "2024-09-11T16:02:03.670194Z",
"shell.execute_reply": "2024-09-11T16:02:03.669798Z"
},
"papermill": {
"duration": 0.009564,
"end_time": "2024-09-11T16:02:03.670280",
"exception": false,
"start_time": "2024-09-11T16:02:03.660716",
"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-09-11T16:02:03.683365Z",
"iopub.status.busy": "2024-09-11T16:02:03.682966Z",
"iopub.status.idle": "2024-09-11T16:02:03.690843Z",
"shell.execute_reply": "2024-09-11T16:02:03.690295Z"
},
"papermill": {
"duration": 0.015265,
"end_time": "2024-09-11T16:02:03.690930",
"exception": false,
"start_time": "2024-09-11T16:02:03.675665",
"status": "completed"
},
"tags": []
},
"outputs": [],
"source": [
"pd.options.plotting.backend = \"plotly\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-11T16:02:03.705897Z",
"iopub.status.busy": "2024-09-11T16:02:03.705502Z",
"iopub.status.idle": "2024-09-11T16:02:19.602701Z",
"shell.execute_reply": "2024-09-11T16:02:19.602228Z"
},
"papermill": {
"duration": 15.906156,
"end_time": "2024-09-11T16:02:19.602862",
"exception": false,
"start_time": "2024-09-11T16:02:03.696706",
"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-09-11T16:02:19.620021Z",
"iopub.status.busy": "2024-09-11T16:02:19.619570Z",
"iopub.status.idle": "2024-09-11T16:02:19.698178Z",
"shell.execute_reply": "2024-09-11T16:02:19.697582Z"
},
"papermill": {
"duration": 0.089704,
"end_time": "2024-09-11T16:02:19.698375",
"exception": true,
"start_time": "2024-09-11T16:02:19.608671",
"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": 17.797167,
"end_time": "2024-09-11T16:02:20.106683",
"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-09-11T16:02:02.309516",
"version": "2.1.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}