You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
atlo-reproduction/Covid_weekly_infectious_cas...

287 lines
7.5 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "2b8d4789-78b3-4f31-9207-4ba6fcf90ad4",
"metadata": {},
"outputs": [],
"source": [
"# data = \"data/data-3d9Ex.csv\"\n",
"import plotly.express as px\n",
"import pandas as pd\n",
"import geopandas as gpd\n",
"import numpy as np\n",
"import json\n",
"\n",
"colorscales = px.colors.named_colorscales()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ef312bf-3c18-4417-806b-3d07830d96d3",
"metadata": {},
"outputs": [],
"source": [
"# Hungary counties shapefile\n",
"url = \"https://maps.princeton.edu/download/file/stanford-dt251rh6351-shapefile.zip\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6822dbd-d897-445f-9743-f7267d66537c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#!cd data && wget https://maps.princeton.edu/download/file/stanford-dt251rh6351-shapefile.zip && unzip stanford-dt251rh6351-shapefile.zip"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb150ab8-aa94-4b76-849f-554f32a30fde",
"metadata": {},
"outputs": [],
"source": [
"# read shp data into geopandas\n",
"gg = gpd.read_file(\"data/dt251rh6351.shp\")\n",
"ggjson = gg.to_json()\n",
"gg.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63e6b826-8563-4c3e-bc81-6141bf6d08f3",
"metadata": {},
"outputs": [],
"source": [
"gg.iloc[1].geometry"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a29a6736-1cf5-485e-abcc-dd5970773d3d",
"metadata": {},
"outputs": [],
"source": [
"# We will need gejson\n",
"dggjson = json.loads(ggjson)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0bf4ed85-0f56-4ede-b75a-cbc7a8bdbd5f",
"metadata": {},
"outputs": [],
"source": [
"# Turns out one of the county's name is misspelled so we rename it for now in the dataframe\n",
"[f['properties']['name_1'] for f in dggjson['features']]"
]
},
{
"cell_type": "markdown",
"id": "bb6b0eda-51f1-4bf9-b161-487db05e4ebc",
"metadata": {},
"source": [
"## Heti adatok"
]
},
{
"cell_type": "markdown",
"id": "770e6ba1-e48a-4fde-aa77-5b898e9853e5",
"metadata": {},
"source": [
"### Letoltes\n",
"https://atlo.team/koronaterkep/#megyeibovebb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e5eeb9f-847c-4e73-8b73-9fc6a97ba56b",
"metadata": {},
"outputs": [],
"source": [
"# https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/hungary-shapefile\n",
"url = \"https://docs.google.com/spreadsheets/d/1djH-yUHLPwuEExCjiXS__6-8W2Yp_msFvShpL4bBcuM/export?format=xlsx&gid=1283792994\"\n",
"heti_df = pd.read_excel(url) \n",
"\n",
"heti_df['date'] = pd.to_datetime(heti_df['Dátum']) #, format='%y-%m-%d')\n",
"heti_df = heti_df.drop(columns=['Dátum', 'Összesen'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e89ed3a-3afe-42ee-b923-cd7e659ae9a8",
"metadata": {},
"outputs": [],
"source": [
"heti_df.head(7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4290d58d-5dfc-46ee-afd7-c46148c62e7c",
"metadata": {},
"outputs": [],
"source": [
"#Rename Győr to Gyor\n",
"heti_df = heti_df.rename(columns={'Győr-Moson-Sopron':'Gyor-Moson-Sopron'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33c27409-bebd-4cd6-a1a6-14935565e94b",
"metadata": {},
"outputs": [],
"source": [
"import datetime as dt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "153128f5-3e5b-4176-aebf-5bf40ddb703f",
"metadata": {},
"outputs": [],
"source": [
"start_date = heti_df.loc[0,'date']\n",
"end_date = start_date + dt.timedelta(days=7)\n",
"\n",
"mask = (heti_df['date'] >= start_date) & (heti_df['date'] < end_date)\n",
"\n",
"hhdf = heti_df.loc[mask].drop(columns=['date']).transpose()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d13aee1a-6702-44fb-8382-985ff582a012",
"metadata": {},
"outputs": [],
"source": [
"hhdf['sum'] = hhdf.sum(axis=1)\n",
"hhdf = hhdf.reset_index().rename(columns={'index':'name_1'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b4fe7f4-796b-4b1c-bcdf-6b440b4ef3f8",
"metadata": {},
"outputs": [],
"source": [
"hhdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d50af1bc-b8d4-4fee-9992-f9939071945b",
"metadata": {},
"outputs": [],
"source": [
"# Get the mean of the county's centroids\n",
"cc = gg.centroid\n",
"\n",
"clon = cc.apply(lambda x: x.x).mean()\n",
"clat = cc.apply(lambda x: x.y).mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c319fee-1e1b-4b8a-bea0-690b5e3d67d3",
"metadata": {},
"outputs": [],
"source": [
"fig = px.choropleth_mapbox(hhdf.drop(columns=[0,1,2,3,4,5,6]), geojson=dggjson, locations='name_1', color='sum',\n",
" color_continuous_scale=\"Viridis\",\n",
" #locationmode='geojson-id',\n",
" featureidkey='properties.name_1',\n",
" range_color=(0, 1800),\n",
" mapbox_style=\"carto-positron\",\n",
" zoom=5.7, center = {\"lat\": clat, \"lon\": clon},\n",
" opacity=0.5,\n",
" labels={'sum':'Megfertozodesek szama'}\n",
" )\n",
"fig.update_layout(margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0})\n",
"fig.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a757a20-9c59-4e31-b1db-bc187f1e3aee",
"metadata": {},
"outputs": [],
"source": [
"thhdf = hhdf.drop(columns=[0,1,2,3,4,5,6])"
]
},
{
"cell_type": "markdown",
"id": "a2294fa4-d611-40e7-a768-a39943b9020b",
"metadata": {},
"source": [
"## Using the built-in plotly choropleth maps\n",
"This is not very decorative"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76163eb4-4587-4561-9dc2-02a321a4e27f",
"metadata": {},
"outputs": [],
"source": [
"fig = px.choropleth(thhdf, geojson=dggjson, locations='name_1',#, color='sum',\n",
" color_continuous_scale=\"Viridis\",\n",
" range_color=(0, 1800),\n",
" locationmode='geojson-id',\n",
" featureidkey='properties.name_1',\n",
" color=thhdf['sum'],\n",
" labels={'sum' : 'Megfertozodesek szama'}\n",
" )\n",
"fig.update_layout(margin={\"r\":0,\"t\":0,\"l\": 0,\"b\":0}, geo_scope='europe'\n",
" )\n",
"fig.layout.geo.center.lat = clat\n",
"fig.layout.geo.center.lon = clon\n",
"fig.layout.geo.projection.scale = 9\n",
"fig.show()\n"
]
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}