{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Collecting Bundestag data from Abgeordnetenwatch\n",
    "> [Abgeordnetenwatch](https://www.abgeordnetenwatch.de) provides an [open API](https://www.abgeordnetenwatch.de/api) that provides info on, among other things, politicians, the politicians' votes and the different polls in parliament, including meta info."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How to use\n",
    "\n",
    "Run the notebook top to bottom. Given parameters control the download and transformation components.\n",
    "\n",
    "### CLI equivalent\n",
    "\n",
    "Download\n",
    "\n",
    "    uv run bundestag download abgeordnetenwatch 111\n",
    "\n",
    "transformation\n",
    "\n",
    "    uv run bundestag transform abgeordnetenwatch 111\n",
    "\n",
    "### Skip processing\n",
    "\n",
    "You can run this notebook to re-do some data processing. But know you can also skip this by running\n",
    "\n",
    "    uv run bundestag download huggingface\n",
    "\n",
    "instead."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "TODOs:\n",
    "- identify why in vote json files some mandate_id values (politicians / mandates) appear multiple times (not always with the same vote result) -> affects `compile_votes_data` -> currently ignored and first of the duplicates used"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bundestag.data.transform.abgeordnetenwatch.transform import (\n",
    "    run as transform_abgeordnetenwatch,\n",
    ")\n",
    "from bundestag.data.download.abgeordnetenwatch.download import (\n",
    "    run as download_abgeordnetenwatch,\n",
    ")\n",
    "\n",
    "from bundestag.fine_logging import setup_logging\n",
    "from bundestag.paths import get_paths\n",
    "import logging\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "logger = logging.getLogger(__name__)\n",
    "setup_logging(logging.INFO)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "paths = get_paths(\"../data\")\n",
    "paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dry = False  # set `True` for testing, `False` otherwise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Collecting polls for 2017-2021"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "legislature_id = 111\n",
    "max_mandates = 999\n",
    "max_polls = 999\n",
    "assume_yes = True\n",
    "raw_path = paths.raw_abgeordnetenwatch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "download_abgeordnetenwatch(\n",
    "    legislature_id,\n",
    "    dry=dry,\n",
    "    raw_path=raw_path,\n",
    "    max_mandates=max_mandates,\n",
    "    max_polls=max_polls,\n",
    "    assume_yes=assume_yes,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Transforming abgeordnetenwatch data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "preprocessed_path = paths.preprocessed_abgeordnetenwatch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "transform_abgeordnetenwatch(\n",
    "    legislature_id,\n",
    "    raw_path=raw_path,\n",
    "    preprocessed_path=preprocessed_path,\n",
    "    dry=dry,\n",
    "    assume_yes=assume_yes,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "votes = pd.read_parquet(preprocessed_path / f\"votes_{legislature_id}.parquet\")\n",
    "votes.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mandates = pd.read_parquet(preprocessed_path / f\"mandates_{legislature_id}.parquet\")\n",
    "mandates.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "polls = pd.read_parquet(preprocessed_path / f\"polls_{legislature_id}.parquet\")\n",
    "polls.head()"
   ]
  }
 ],
 "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": {
    "height": "296px",
    "width": "456px"
   },
   "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
}
