Last active
January 16, 2021 18:17
-
-
Save xenatisch/3b8d5cacdf9c97bcc62929aff3650d98 to your computer and use it in GitHub Desktop.
Reproduction of population from incidence rates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from uk_covid19 import Cov19API" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>areaCode</th>\n", | |
" <th>areaName</th>\n", | |
" <th>date</th>\n", | |
" <th>cumCasesBySpecimenDate</th>\n", | |
" <th>cumCasesBySpecimenDateRate</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>E12000004</td>\n", | |
" <td>East Midlands</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>235286</td>\n", | |
" <td>4865.4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>E12000006</td>\n", | |
" <td>East of England</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>292530</td>\n", | |
" <td>4690.9</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>E12000007</td>\n", | |
" <td>London</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>567931</td>\n", | |
" <td>6337.1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>E12000001</td>\n", | |
" <td>North East</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>148883</td>\n", | |
" <td>5576.3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>E12000002</td>\n", | |
" <td>North West</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>466629</td>\n", | |
" <td>6356.3</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" areaCode areaName date cumCasesBySpecimenDate \\\n", | |
"0 E12000004 East Midlands 2021-01-15 235286 \n", | |
"1 E12000006 East of England 2021-01-15 292530 \n", | |
"2 E12000007 London 2021-01-15 567931 \n", | |
"3 E12000001 North East 2021-01-15 148883 \n", | |
"4 E12000002 North West 2021-01-15 466629 \n", | |
"\n", | |
" cumCasesBySpecimenDateRate \n", | |
"0 4865.4 \n", | |
"1 4690.9 \n", | |
"2 6337.1 \n", | |
"3 5576.3 \n", | |
"4 6356.3 " | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"metric_names = [\n", | |
" \"areaCode\",\n", | |
" \"areaName\",\n", | |
" \"date\",\n", | |
" \"cumCasesBySpecimenDate\",\n", | |
" \"cumCasesBySpecimenDateRate\"\n", | |
"]\n", | |
"\n", | |
"api = Cov19API(\n", | |
" filters=[\"areaType=region\"],\n", | |
" structure=dict(zip(metric_names, metric_names)),\n", | |
" latest_by=\"cumCasesBySpecimenDate\"\n", | |
")\n", | |
"\n", | |
"df = api.get_dataframe()\n", | |
"\n", | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>areaCode</th>\n", | |
" <th>areaName</th>\n", | |
" <th>date</th>\n", | |
" <th>cumCasesBySpecimenDate</th>\n", | |
" <th>cumCasesBySpecimenDateRate</th>\n", | |
" <th>population_repr</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>E12000004</td>\n", | |
" <td>East Midlands</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>235286</td>\n", | |
" <td>4865.4</td>\n", | |
" <td>4835902</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>E12000006</td>\n", | |
" <td>East of England</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>292530</td>\n", | |
" <td>4690.9</td>\n", | |
" <td>6236117</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>E12000007</td>\n", | |
" <td>London</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>567931</td>\n", | |
" <td>6337.1</td>\n", | |
" <td>8962002</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>E12000001</td>\n", | |
" <td>North East</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>148883</td>\n", | |
" <td>5576.3</td>\n", | |
" <td>2669925</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>E12000002</td>\n", | |
" <td>North West</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>466629</td>\n", | |
" <td>6356.3</td>\n", | |
" <td>7341205</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>E12000008</td>\n", | |
" <td>South East</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>415842</td>\n", | |
" <td>4529.8</td>\n", | |
" <td>9180140</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>E12000009</td>\n", | |
" <td>South West</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>162556</td>\n", | |
" <td>2890.0</td>\n", | |
" <td>5624775</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>E12000005</td>\n", | |
" <td>West Midlands</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>309690</td>\n", | |
" <td>5218.9</td>\n", | |
" <td>5934009</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>E12000003</td>\n", | |
" <td>Yorkshire and The Humber</td>\n", | |
" <td>2021-01-15</td>\n", | |
" <td>294186</td>\n", | |
" <td>5346.0</td>\n", | |
" <td>5502918</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" areaCode areaName date cumCasesBySpecimenDate \\\n", | |
"0 E12000004 East Midlands 2021-01-15 235286 \n", | |
"1 E12000006 East of England 2021-01-15 292530 \n", | |
"2 E12000007 London 2021-01-15 567931 \n", | |
"3 E12000001 North East 2021-01-15 148883 \n", | |
"4 E12000002 North West 2021-01-15 466629 \n", | |
"5 E12000008 South East 2021-01-15 415842 \n", | |
"6 E12000009 South West 2021-01-15 162556 \n", | |
"7 E12000005 West Midlands 2021-01-15 309690 \n", | |
"8 E12000003 Yorkshire and The Humber 2021-01-15 294186 \n", | |
"\n", | |
" cumCasesBySpecimenDateRate population_repr \n", | |
"0 4865.4 4835902 \n", | |
"1 4690.9 6236117 \n", | |
"2 6337.1 8962002 \n", | |
"3 5576.3 2669925 \n", | |
"4 6356.3 7341205 \n", | |
"5 4529.8 9180140 \n", | |
"6 2890.0 5624775 \n", | |
"7 5218.9 5934009 \n", | |
"8 5346.0 5502918 " | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df = df.assign(\n", | |
" population_repr=(\n", | |
" df.cumCasesBySpecimenDate / df.cumCasesBySpecimenDateRate * 100000\n", | |
" ).round().astype(int)\n", | |
")\n", | |
"\n", | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'56,286,993'" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"format(df.population_repr.sum(), \",d\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"England - regions: 55,977,178\n", | |
"England - Nation: 55,977,178\n" | |
] | |
} | |
], | |
"source": [ | |
"from requests import request\n", | |
"from itertools import chain\n", | |
"\n", | |
"\n", | |
"region_url = (\n", | |
" 'https://coronavirus.data.gov.uk/api/v1/'\n", | |
" 'lookup?filters=areaType=region'\n", | |
" '&structure=[\"areaCode\"]'\n", | |
")\n", | |
"\n", | |
"resp = request(\"GET\", region_url)\n", | |
"region_codes = list(chain(*resp.json()[\"data\"]))\n", | |
"\n", | |
"\n", | |
"pop_url = \"https://coronavirus.data.gov.uk/public/assets/population/population.json\"\n", | |
"\n", | |
"resp = request(\"GET\", pop_url)\n", | |
"\n", | |
"data = resp.json()\n", | |
"england_regions = {\n", | |
" key: value for key, value in data.items() \n", | |
" if key.startswith(\"E\") and key in region_codes\n", | |
"}\n", | |
"\n", | |
"print(f\"England - regions: {sum(england_regions.values()):,d}\")\n", | |
"print(f\"England - Nation: {data['E92000001']:,d}\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'E12000001': 2657909,\n", | |
" 'E12000002': 7292093,\n", | |
" 'E12000003': 5479615,\n", | |
" 'E12000004': 4804149,\n", | |
" 'E12000005': 5900757,\n", | |
" 'E12000006': 6201214,\n", | |
" 'E12000007': 8908081,\n", | |
" 'E12000008': 9133625,\n", | |
" 'E12000009': 5599735}" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"england_regions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.7.7 64-bit ('anaconda3': virtualenv)", | |
"language": "python", | |
"name": "python37764bitanaconda3virtualenv170a8a6454fc493eac0c047df6a6a6c0" | |
}, | |
"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.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment