{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ea7d1a2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "id": "181190f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\nikol\\AppData\\Local\\Temp\\ipykernel_12032\\1861347910.py:10: DtypeWarning: Columns (10,11,14,15,16) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  df_temp = pd.read_csv(datei)\n",
      "C:\\Users\\nikol\\AppData\\Local\\Temp\\ipykernel_12032\\1861347910.py:10: DtypeWarning: Columns (10,11,14,15,16) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  df_temp = pd.read_csv(datei)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import glob\n",
    "import os\n",
    "\n",
    "pfad = \"../data/tageswerte_pro_Jahr/*.csv\"   # z.B. \"data/*.csv\" oder \"C:/Users/du/fahrrad/*.csv\"\n",
    "\n",
    "dateien = glob.glob(pfad)\n",
    "dfs = []\n",
    "for datei in dateien:\n",
    "    df_temp = pd.read_csv(datei)\n",
    "    jahr = os.path.basename(datei).split(\"_\")[2].split(\".\")[0]\n",
    "    df_temp[\"year\"] = jahr\n",
    "    dfs.append(df_temp.astype({\"year\":int}))\n",
    "\n",
    "df = pd.concat(dfs, ignore_index=True)\n",
    "\n",
    "# Mannheim subset, saved to file\n",
    "df_ma = df[df[\"domain_name\"] == 'Stadt Mannheim']\n",
    "df_ma.to_csv(\"../data/processed/bikes_mannheim.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "id": "d3f4c318",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "operator_name                  0\n",
      "domain_name                    0\n",
      "domain_id                      0\n",
      "counter_site                   0\n",
      "counter_site_id                0\n",
      "counter_serial              1593\n",
      "longitude                      0\n",
      "latitude                       0\n",
      "timezone                       0\n",
      "iso_timestamp                  0\n",
      "channels_in                26510\n",
      "channels_out               26559\n",
      "channels_unknown          220492\n",
      "channels_all                   0\n",
      "site_temperature           20642\n",
      "site_rain_accumulation     20642\n",
      "site_snow_accumulation     20656\n",
      "year                           0\n",
      "dtype: int64\n",
      "operator_name                 0\n",
      "domain_name                   0\n",
      "domain_id                     0\n",
      "counter_site                  0\n",
      "counter_site_id               0\n",
      "counter_serial                0\n",
      "longitude                     0\n",
      "latitude                      0\n",
      "timezone                      0\n",
      "iso_timestamp                 0\n",
      "channels_in                   0\n",
      "channels_out                  0\n",
      "channels_unknown          27415\n",
      "channels_all                  0\n",
      "site_temperature              0\n",
      "site_rain_accumulation        0\n",
      "site_snow_accumulation        0\n",
      "year                          0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# Daten bereinigen\n",
    "\n",
    "# replace various forms of 'na' with np.nan\n",
    "df = df.replace([\"na\", \"NA\", \"Na\"], np.nan)\n",
    "print(df.isna().sum())\n",
    "# TODO Find if temp rain and snow values are missing in a pattern or at random\n",
    "# convert numeric values to corresponding dtypes\n",
    "df[\"site_temperature\"] = df[\"site_temperature\"].astype(float)\n",
    "df[\"site_rain_accumulation\"] = df[\"site_rain_accumulation\"].astype(float)\n",
    "df[\"site_snow_accumulation\"] = df[\"site_snow_accumulation\"].astype(float)\n",
    "print(df_ma.isna().sum())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb082a0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\nikol\\AppData\\Local\\Temp\\ipykernel_12032\\3952536231.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df_ma[\"short_labels\"] = df_ma[\"counter_site\"].apply(lambda x: x[:max_length] + (\"…\" if len(x) > max_length else \"\"))\n"
     ]
    }
   ],
   "source": [
    "# Shorten der Labels für bessere Lesbarkeit im Plot\n",
    "max_length = 15\n",
    "df_ma[\"short_labels\"] = df_ma[\"counter_site\"].apply(lambda x: x[:max_length] + (\"…\" if len(x) > max_length else \"\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "ea13a213",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31296 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "avg_per_year = df_ma.groupby(\"year\")[\"channels_all\"].mean()\n",
    "\n",
    "avg_per_year = avg_per_year.sort_index()\n",
    "\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.bar(avg_per_year.index.astype(str), avg_per_year.values)\n",
    "\n",
    "plt.xlabel(\"Year\")\n",
    "plt.ylabel(\"Average channels_all\")\n",
    "plt.title(\"Average observations per year (Stadt Mannheim)\")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "f938cd69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28968 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sum_per_year = df_ma.groupby(\"year\")[\"channels_all\"].sum()\n",
    "\n",
    "sum_per_year = sum_per_year.sort_index()\n",
    "\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.bar(sum_per_year.index.astype(str), sum_per_year.values)\n",
    "\n",
    "plt.xlabel(\"Year\")\n",
    "plt.ylabel(\"Total observations in millions\")\n",
    "plt.title(\"Total observations per year (Stadt Mannheim)\")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "ab084d8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 78044 characters]",
      "text/plain": [
       "<Figure size 1000x800 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "station_sum = df_ma.groupby(\"short_labels\")[\"channels_all\"].sum().sort_values(ascending=False)\n",
    "\n",
    "plt.figure(figsize=(10, 8))\n",
    "plt.bar(station_sum.index, station_sum.values)\n",
    "plt.xticks(rotation=75)\n",
    "plt.xlabel(\"Station\")\n",
    "plt.ylabel(\"Total observations (2014-2024) in millions\")\n",
    "plt.title(\"Total observations per station (Stadt Mannheim)\")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad8639cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 93016 characters]",
      "text/plain": [
       "<Figure size 2000x1000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "max_length = 15\n",
    "\n",
    "plt.figure(figsize=(20, 10))\n",
    "sns.boxplot(data=df_ma, x=\"short_labels\", y=\"channels_all\")\n",
    "\n",
    "plt.xticks(rotation=45)\n",
    "plt.ylabel(\"Daily observations\")\n",
    "plt.title(\"Distribution of daily counts per station (Stadt Mannheim- 2014-2024)\")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "cf4ab2b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 40200 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y = df_ma.groupby(by=\"year\")[\"counter_site\"].nunique()\n",
    "plt.figure(figsize=(10, 6))\n",
    "sns.lineplot(x=y.index, y=y.values, marker='o')\n",
    "plt.title(\"Number of counting stations per year in Mannheim\", fontsize=16)\n",
    "plt.xlabel(\"Year\", fontsize=12)\n",
    "plt.ylabel(\"Number of stations\", fontsize=12)\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "a175b2c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 58044 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# WICHTIG Hier kommt der Abfall des Mittelwerts dadurch, dass mehr Zählstationen hinzugefügt wurden, die nicht an einem Hotspot standen, also Mittelwert sinkt.\n",
    "plot_data = pd.DataFrame({\n",
    "    'Year': df_ma['year'].unique(),\n",
    "    'Average': avg_per_year\n",
    "})\n",
    "\n",
    "plt.figure(figsize=(10, 6))\n",
    "sns.lineplot(x='Year', y='Average', data=plot_data, marker='o')\n",
    "\n",
    "plt.title(\"Daily Average of Tracked Bikes (Mannheim 2014-2025)\", fontsize=16)\n",
    "plt.xlabel(\"Year\", fontsize=12)\n",
    "plt.ylabel(\"Average Number of bikes\", fontsize=12)\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e6c3493",
   "metadata": {},
   "source": [
    "# Korrelation zwischen Temperatur und Fahrradnutzung"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95626fa7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 378840 characters]",
      "text/plain": [
       "<Figure size 1500x500 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pearson: 0.202\n",
      "Spearman: 0.223\n",
      "Pearson: 0.195\n",
      "Spearman: 0.272\n"
     ]
    }
   ],
   "source": [
    "fig, axs = plt.subplots(1,2,figsize=(15, 5))\n",
    "sns.scatterplot(data=df_ma, y=\"site_temperature\", x=\"channels_all\", ax=axs[0])\n",
    "sns.regplot(data=df_ma, y=\"site_temperature\", x=\"channels_all\",scatter_kws={'s': 10}, line_kws={'color': 'red'}, ax=axs[1])\n",
    "plt.xlabel(\"Site temperature\")\n",
    "plt.ylabel(\"Bike counts\") \n",
    "plt.title(\"Temperature vs. bicycle counts (Stadt Mannheim)\")\n",
    "plt.tight_layout()\n",
    "plt.show()\n",
    "# Pearson\n",
    "pearson_ma = df_ma[[\"channels_all\", \"site_temperature\"]].corr(method=\"pearson\").loc[\"channels_all\", \"site_temperature\"]\n",
    "\n",
    "# Spearman\n",
    "spearman_ma = df_ma[[\"channels_all\", \"site_temperature\"]].corr(method=\"spearman\").loc[\"channels_all\", \"site_temperature\"]\n",
    "\n",
    "print(f\"Pearson: {pearson_ma:.3f}\")\n",
    "print(f\"Spearman: {spearman_ma:.3f}\")\n",
    "\n",
    "# Drop all nan values in site_temperature from big dataframe\n",
    "df[\"site_temperature\"] = df[\"site_temperature\"].replace([\"na\", \"NA\", \"Na\"], np.nan)\n",
    "df = df.dropna(subset=[\"site_temperature\"])\n",
    "pearson_all = df[[\"channels_all\", \"site_temperature\"]].corr(method=\"pearson\").loc[\"channels_all\", \"site_temperature\"]\n",
    "spearman_all = df[[\"channels_all\", \"site_temperature\"]].corr(method=\"spearman\").loc[\"channels_all\", \"site_temperature\"]\n",
    "\n",
    "print(f\"Pearson: {pearson_all:.3f}\")\n",
    "print(f\"Spearman: {spearman_all:.3f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fe1513a",
   "metadata": {},
   "source": [
    "#### Die insgesamte Korrelation zwischen der Temperatur und der Fahrradnutzung in Mannheim beträgt nach Pearson 0.202.\n",
    "#### Betrachtet man jedoch die Korrelation der einzelnen der Messstationen so ergeben sich folgende Werte."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "46404807",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        short_labels  pearson_corr\n",
      "1       B38. RI. AUS      0.011942\n",
      "15  Lindenhofüberfü…      0.235979\n",
      "25  Theodor-Heuss-A…      0.381730\n",
      "21        Renzstraße      0.445103\n",
      "19  Neckarauer Über…      0.486533\n",
      "5   Feudenheimerstr…      0.510540\n",
      "7   Feudenheimstr. …      0.533080\n",
      "13    Kurpfalzbrücke      0.548310\n",
      "9    Jungbuschbrücke      0.557844\n",
      "11  Konrad-Adenauer…      0.568114\n",
      "17     Luzenbergstr.      0.584917\n",
      "3     Fernmeldeturm.      0.619432\n",
      "23  Schlosspark Lin…      0.629848\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 54668 characters]",
      "text/plain": [
       "<Figure size 1000x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "corr_per_station = (\n",
    "    df_ma.groupby(\"short_labels\")[[\"channels_all\", \"site_temperature\"]]\n",
    "         .corr(method=\"pearson\")\n",
    "         .reset_index()\n",
    ")\n",
    "\n",
    "# Nur die Zeilen channels_all vs. site_temperature extrahieren\n",
    "corr_per_station = corr_per_station[\n",
    "    (corr_per_station[\"level_1\"] == \"site_temperature\") &\n",
    "    (corr_per_station[\"short_labels\"].notna())\n",
    "][[\"short_labels\", \"channels_all\"]].rename(columns={\"channels_all\": \"pearson_corr\"})\n",
    "\n",
    "print(corr_per_station.sort_values(\"pearson_corr\"))\n",
    "\n",
    "corr_per_station_sorted = corr_per_station.sort_values(\"pearson_corr\")\n",
    "\n",
    "plt.figure(figsize=(10, 5))\n",
    "plt.barh(corr_per_station_sorted[\"short_labels\"], corr_per_station_sorted[\"pearson_corr\"])\n",
    "plt.xlabel(\"Pearson correlation (site_temperature vs. channels_all)\")\n",
    "plt.ylabel(\"Station\")\n",
    "plt.title(\"Correlation between temperature and bicycle counts by station\")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faf7f222",
   "metadata": {},
   "source": [
    "An den meisten Stationen ist die Korrelation positiv (d.h. wärmere Tage → mehr Radverkehr).“\n",
    "„Die Stärke variiert jedoch deutlich zwischen den Stationen (von ~0 bis ~0.63).“\n",
    "„Insbesondere an Stationen X, Y, Z ist der Zusammenhang besonders stark.“"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dat",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
