{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  Similarities\n",
    "> Identifying the similarities between parties and members of the parliament based on their average voting behavior during roll call votes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import polars as pl\n",
    "from bundestag.fine_logging import setup_logging\n",
    "import logging\n",
    "from bundestag.paths import get_paths\n",
    "from bundestag.ml.similarity import (\n",
    "    get_votes_by_party,\n",
    "    pivot_party_votes_df,\n",
    "    prepare_votes_of_mdb,\n",
    "    align_mdb_with_parties,\n",
    "    compute_similarity,\n",
    "    plot_overall_similarity,\n",
    "    plot_similarity_over_time,\n",
    "    plot,\n",
    "    align_party_with_party,\n",
    "    align_party_with_all_parties,\n",
    "    compute_similarity,\n",
    ")\n",
    "from plotnine import (\n",
    "    ggplot,\n",
    "    aes,\n",
    "    geom_point,\n",
    "    scale_color_manual,\n",
    ")\n",
    "\n",
    "logger = logging.getLogger(__name__)\n",
    "setup_logging(logging.DEBUG)\n",
    "\n",
    "paths = get_paths(\"../data\")\n",
    "paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = paths.preprocessed_bundestag / \"bundestag.de_votes.parquet\"\n",
    "file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pl.read_parquet(file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Voting outcomes by party"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "party_votes = get_votes_by_party(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "party_votes.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "party = \"CDU/CSU\"\n",
    "\n",
    "(\n",
    "    ggplot(\n",
    "        party_votes.filter(pl.col(\"Fraktion/Gruppe\") == party),\n",
    "        aes(\"date\", \"fraction\", color=\"vote\"),\n",
    "    )\n",
    "    + geom_point(alpha=0.3)\n",
    "    + scale_color_manual(\n",
    "        breaks=[\"ja\", \"nein\", \"nichtabgegeben\", \"Enthaltung\", \"ungültig\"],\n",
    "        values=[\"green\", \"red\", \"grey\", \"orange\", \"salmon\"],\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Member-Party similarity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A member of the German federal parliament is a Mitglied des Bundestages, so `mdb`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "party_votes_pivoted = pivot_party_votes_df(party_votes)\n",
    "party_votes_pivoted.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdb = \"Peter Altmaier\"\n",
    "mdb_votes = prepare_votes_of_mdb(df, mdb)\n",
    "mdb_votes.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdb_vs_parties = align_mdb_with_parties(mdb_votes, party_votes_pivoted)\n",
    "mdb_vs_parties = compute_similarity(mdb_vs_parties, suffix=\"_party\")\n",
    "\n",
    "mdb_vs_parties.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdb_vs_parties[[\"Fraktion/Gruppe\", \"similarity\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_overall_similarity(\n",
    "    mdb_vs_parties,\n",
    "    x=\"Fraktion/Gruppe_party\",\n",
    "    title=f\"Overall similarity of {mdb} with all parties\",\n",
    ")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similarity over time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdb_vs_parties.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_similarity_over_time(\n",
    "    mdb_vs_parties, \"Fraktion/Gruppe_party\", title=f\"{mdb} vs time\"\n",
    ")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(\n",
    "    mdb_vs_parties,\n",
    "    title_overall=f\"Overall similarity of {mdb} with all parties\",\n",
    "    title_over_time=f\"{mdb} vs time\",\n",
    ")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Party-Party similarity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "party_a = \"SPD\"\n",
    "party_b = \"BÜ90/GR\"\n",
    "partyA_vs_partyB = align_party_with_party(\n",
    "    party_votes_pivoted, party_a=party_a, party_b=party_b\n",
    ")\n",
    "partyA_vs_partyB = compute_similarity(partyA_vs_partyB, suffix=\"_b\")\n",
    "\n",
    "partyA_vs_partyB.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "party = \"SPD\"\n",
    "partyA_vs_rest = align_party_with_all_parties(party_votes_pivoted, party)\n",
    "partyA_vs_rest = compute_similarity(partyA_vs_rest, suffix=\"_b\")\n",
    "\n",
    "partyA_vs_rest.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(\n",
    "    partyA_vs_rest,\n",
    "    title_overall=f\"Overall similarity of {party} with all parties\",\n",
    "    title_over_time=f\"{party} vs time\",\n",
    "    party_col=\"Fraktion/Gruppe_b\",\n",
    ")\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
