Files
HowBadIsMyBatch/src/intensivstationen/Intensivstationen.ipynb
2022-03-01 17:59:41 +01:00

242 lines
6.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "9de5907f-18f5-4cb1-903e-26028ff1fa03",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"pd.set_option('display.max_rows', 100)\n",
"pd.set_option('display.max_columns', None)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0927a6c2",
"metadata": {},
"outputs": [],
"source": [
"kreise = pd.read_excel('04-kreise.xlsx', sheet_name = 1, header = 5, index_col = 0)\n",
"kreise"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1dea262",
"metadata": {},
"outputs": [],
"source": [
"class TimeseriesReader:\n",
" \n",
" def readTimeseries(self):\n",
" timeseries = pd.read_csv(\n",
" 'zeitreihe-tagesdaten.csv',\n",
" low_memory = False,\n",
" usecols = ['date', 'bundesland', 'gemeindeschluessel', 'betten_belegt', 'betten_frei'],\n",
" parse_dates = ['date'],\n",
" date_parser = lambda dateStr: pd.to_datetime(dateStr, format = \"%Y-%m-%d\"),\n",
" dtype = {\n",
" 'gemeindeschluessel': 'string',\n",
" 'bundesland': 'string'\n",
" })\n",
" return timeseries.sort_values(by = 'date', ascending = True)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d978b069",
"metadata": {},
"outputs": [],
"source": [
"timeSeries = TimeseriesReader().readTimeseries()\n",
"timeSeries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af96fb11",
"metadata": {},
"outputs": [],
"source": [
"class ColumnsAdder:\n",
"\n",
" def __init__(self, kreise):\n",
" self.kreise = kreise\n",
"\n",
" def addKreisAndBundeslandAndEinwohnerzahlColumns(self, dataFrame):\n",
" dataFrame['Kreis'] = dataFrame['gemeindeschluessel'].map(\n",
" lambda gemeindeschluessel: self.kreise.loc[gemeindeschluessel, 3])\n",
"\n",
" dataFrame['Bundesland'] = dataFrame['bundesland'].map(\n",
" lambda bundesland: self.kreise.loc[bundesland, '2'])\n",
"\n",
" dataFrame['Einwohnerzahl'] = dataFrame['gemeindeschluessel'].map(\n",
" lambda gemeindeschluessel: int(self.kreise.loc[gemeindeschluessel, 6]))\n",
"\n",
" return dataFrame\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "62a20115",
"metadata": {},
"outputs": [],
"source": [
"timeSeries = ColumnsAdder(kreise).addKreisAndBundeslandAndEinwohnerzahlColumns(timeSeries)\n",
"timeSeries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "356494d3",
"metadata": {},
"outputs": [],
"source": [
"kreisValues = timeSeries['Kreis'].drop_duplicates().values\n",
"kreisValues"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05aa0117",
"metadata": {},
"outputs": [],
"source": [
"def printKreisOptions(kreisValues):\n",
" for kreis in kreisValues:\n",
" printKreisOption(kreis)\n",
"\n",
"def printKreisOption(kreis):\n",
" print('<option value=\"{kreis}\">{kreis}</option>'.format(kreis = kreis))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33a4b725",
"metadata": {},
"outputs": [],
"source": [
"kreisValues = sorted(kreisValues)\n",
"printKreisOptions(kreisValues)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "43c2f826",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"\n",
"class IOUtils:\n",
"\n",
" @staticmethod\n",
" def saveDataFrameAsJson(dataFrame, file):\n",
" IOUtils.ensurePath(file)\n",
" df = dataFrame.copy()\n",
" df['date'] = df['date'].dt.strftime('%Y-%m-%d')\n",
" df.to_json(file, orient=\"records\")\n",
"\n",
" @staticmethod\n",
" def ensurePath(file):\n",
" directory = os.path.dirname(file)\n",
" if not os.path.exists(directory):\n",
" os.makedirs(directory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "997a4bdb",
"metadata": {},
"outputs": [],
"source": [
"def getIntensiveCareBeds(timeSeries, kreis = None):\n",
" if kreis is not None:\n",
" return timeSeries[timeSeries['Kreis'] == kreis][['date', 'betten_belegt', 'betten_frei']]\n",
" else:\n",
" return timeSeries.groupby('date').agg(**{\n",
" 'betten_belegt': pd.NamedAgg(column = 'betten_belegt', aggfunc = 'sum'),\n",
" 'betten_frei': pd.NamedAgg(column = 'betten_frei', aggfunc = 'sum') \n",
" }).reset_index()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a97f5b2b",
"metadata": {},
"outputs": [],
"source": [
"def getAndPersistIntensiveCareBeds(timeSeries, kreis = None):\n",
" dataFrame = getIntensiveCareBeds(timeSeries, kreis)\n",
" display(kreis)\n",
" IOUtils.saveDataFrameAsJson(dataFrame, _getFilename(kreis))\n",
" return dataFrame\n",
"\n",
"def _getFilename(kreis):\n",
" return '../../docs/data/intensivstationen/intensivstationen-{suffix}.json'.format(suffix = _getSuffix(kreis))\n",
"\n",
"def _getSuffix(kreis):\n",
" return kreis if kreis is not None else 'de'\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "349edd73",
"metadata": {},
"outputs": [],
"source": [
"getAndPersistIntensiveCareBeds(timeSeries)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b97137f",
"metadata": {},
"outputs": [],
"source": [
"for kreis in kreisValues:\n",
" getAndPersistIntensiveCareBeds(timeSeries, kreis)"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}