{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import Python packages\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import macrosynergy\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import polars as pl\n",
    "import os\n",
    "\n",
    "from macrosynergy.panel import view_timelines\n",
    "from macrosynergy.management.types import QuantamentalDataFrame\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import Python bindings - `msyrs`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import msyrs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_FOLDER_PATH = \"E:/Work/jpmaqs-data\"\n",
    "# DATA_FOLDER_PATH = \"C:/Users/PalashTyagi/Code/go-dataquery/jpmaqs-data\"\n",
    "DQ_CLIENT_ID = os.getenv(\"DQ_CLIENT_ID\")\n",
    "DQ_CLIENT_SECRET = os.getenv(\"DQ_CLIENT_SECRET\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "nb_start_time = time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken to load qdf: 0.010072469711303711\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><style>\n",
       ".dataframe > thead > tr,\n",
       ".dataframe > tbody > tr {\n",
       "  text-align: right;\n",
       "  white-space: pre-wrap;\n",
       "}\n",
       "</style>\n",
       "<small>shape: (5, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>real_date</th><th>cid</th><th>xcat</th><th>value</th><th>grading</th><th>eop_lag</th><th>mop_lag</th></tr><tr><td>date</td><td>str</td><td>str</td><td>f64</td><td>f64</td><td>i64</td><td>i64</td></tr></thead><tbody><tr><td>2010-03-03</td><td>&quot;USD&quot;</td><td>&quot;ADPEMPL_SA_P1M1ML1&quot;</td><td>-0.173806</td><td>3.0</td><td>3</td><td>33</td></tr><tr><td>2010-03-04</td><td>&quot;USD&quot;</td><td>&quot;ADPEMPL_SA_P1M1ML1&quot;</td><td>-0.173806</td><td>3.0</td><td>4</td><td>34</td></tr><tr><td>2010-03-05</td><td>&quot;USD&quot;</td><td>&quot;ADPEMPL_SA_P1M1ML1&quot;</td><td>-0.173806</td><td>3.0</td><td>5</td><td>35</td></tr><tr><td>2010-03-08</td><td>&quot;USD&quot;</td><td>&quot;ADPEMPL_SA_P1M1ML1&quot;</td><td>-0.173806</td><td>3.0</td><td>8</td><td>38</td></tr><tr><td>2010-03-09</td><td>&quot;USD&quot;</td><td>&quot;ADPEMPL_SA_P1M1ML1&quot;</td><td>-0.173806</td><td>3.0</td><td>9</td><td>39</td></tr></tbody></table></div>"
      ],
      "text/plain": [
       "shape: (5, 7)\n",
       "┌────────────┬─────┬────────────────────┬───────────┬─────────┬─────────┬─────────┐\n",
       "│ real_date  ┆ cid ┆ xcat               ┆ value     ┆ grading ┆ eop_lag ┆ mop_lag │\n",
       "│ ---        ┆ --- ┆ ---                ┆ ---       ┆ ---     ┆ ---     ┆ ---     │\n",
       "│ date       ┆ str ┆ str                ┆ f64       ┆ f64     ┆ i64     ┆ i64     │\n",
       "╞════════════╪═════╪════════════════════╪═══════════╪═════════╪═════════╪═════════╡\n",
       "│ 2010-03-03 ┆ USD ┆ ADPEMPL_SA_P1M1ML1 ┆ -0.173806 ┆ 3.0     ┆ 3       ┆ 33      │\n",
       "│ 2010-03-04 ┆ USD ┆ ADPEMPL_SA_P1M1ML1 ┆ -0.173806 ┆ 3.0     ┆ 4       ┆ 34      │\n",
       "│ 2010-03-05 ┆ USD ┆ ADPEMPL_SA_P1M1ML1 ┆ -0.173806 ┆ 3.0     ┆ 5       ┆ 35      │\n",
       "│ 2010-03-08 ┆ USD ┆ ADPEMPL_SA_P1M1ML1 ┆ -0.173806 ┆ 3.0     ┆ 8       ┆ 38      │\n",
       "│ 2010-03-09 ┆ USD ┆ ADPEMPL_SA_P1M1ML1 ┆ -0.173806 ┆ 3.0     ┆ 9       ┆ 39      │\n",
       "└────────────┴─────┴────────────────────┴───────────┴─────────┴─────────┴─────────┘"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfpath = f\"{DATA_FOLDER_PATH}/data/ADPEMPL_SA_P1M1ML1/USD_ADPEMPL_SA_P1M1ML1.csv\"\n",
    "\n",
    "starttime = time.time()\n",
    "ldf: pl.DataFrame = msyrs.qdf.load_qdf(dfpath)\n",
    "print(f\"Time taken to load qdf: {time.time() - starttime}\")\n",
    "ldf.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "cids_dm = \"AUD.CAD.CHF.EUR.GBP.JPY.NOK.NZD.SEK.USD\".split(\".\")\n",
    "cids_em = \"CLP.COP.CZK.HUF.IDR.ILS.INR.KRW.MXN.PLN.THB.TRY.TWD.ZAR\".split(\".\")\n",
    "cids = cids_dm + cids_em\n",
    "cids_dux = list(set(cids) - set([\"IDR\", \"NZD\"]))\n",
    "ecos = (\n",
    "    \"CPIC_SA_P1M1ML12.CPIC_SJA_P3M3ML3AR.CPIC_SJA_P6M6ML6AR.CPIH_SA_P1M1ML12.\"\n",
    "    \"CPIH_SJA_P3M3ML3AR.CPIH_SJA_P6M6ML6AR.INFTEFF_NSA.INTRGDP_NSA_P1M1ML12_3MMA.\"\n",
    "    \"INTRGDPv5Y_NSA_P1M1ML12_3MMA.PCREDITGDP_SJA_D1M1ML12.RGDP_SA_P1Q1QL4_20QMA.\"\n",
    "    \"RYLDIRS02Y_NSA.RYLDIRS05Y_NSA.PCREDITBN_SJA_P1M1ML12\".split(\".\")\n",
    ")\n",
    "\n",
    "mkts = (\n",
    "    \"DU02YXR_NSA.DU05YXR_NSA.DU02YXR_VT10.DU05YXR_VT10.EQXR_NSA.EQXR_VT10.\"\n",
    "    \"FXXR_NSA.FXXR_VT10.FXCRR_NSA.FXTARGETED_NSA.FXUNTRADABLE_NSA\".split(\".\")\n",
    ")\n",
    "xcats = ecos + mkts\n",
    "\n",
    "tickers = [f\"{c}_{x}\" for c in cids for x in xcats]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken to load qdf batch: 1.202073335647583\n"
     ]
    }
   ],
   "source": [
    "starttime = time.time()\n",
    "\n",
    "big_df: pl.DataFrame = msyrs.qdf.load_qdf_from_download_bank(\n",
    "    folder_path=DATA_FOLDER_PATH,\n",
    "    xcats=xcats,\n",
    ")\n",
    "print(f\"Time taken to load qdf batch: {time.time() - starttime}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "286.69339656829834"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "big_df.estimated_size(\"mb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken to reduce qdf: 0.9944026470184326\n",
      "99.33047294616699\n"
     ]
    }
   ],
   "source": [
    "starttime = time.time()\n",
    "\n",
    "test_df = msyrs.qdf.reduce_dataframe(df=big_df, xcats=mkts)\n",
    "\n",
    "print(f\"Time taken to reduce qdf: {time.time() - starttime}\")\n",
    "print(test_df.estimated_size(\"mb\"))\n",
    "test_df = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "sel_cids = [\"USD\", \"EUR\", \"GBP\", \"AUD\", \"CAD\", \"CHF\", \"JPY\", \"INR\"]\n",
    "start = \"1990-01-01\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken to reduce qdf: 0.32672691345214844\n"
     ]
    }
   ],
   "source": [
    "eq_xcats = [xc for xc in xcats if xc.startswith(\"EQ\")]\n",
    "starttime = time.time()\n",
    "\n",
    "eq_df = msyrs.qdf.reduce_dataframe(\n",
    "    df=big_df, cids=sel_cids, xcats=eq_xcats, start=start\n",
    ")\n",
    "\n",
    "print(f\"Time taken to reduce qdf: {time.time() - starttime}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken to reduce qdf: 0.4290201663970947\n"
     ]
    }
   ],
   "source": [
    "fx_xcats = [xc for xc in xcats if xc.startswith(\"FX\")]\n",
    "\n",
    "starttime = time.time()\n",
    "\n",
    "fx_df = msyrs.qdf.reduce_dataframe(\n",
    "    df=big_df, cids=sel_cids, xcats=fx_xcats, start=start\n",
    ")\n",
    "new_df: pl.DataFrame = msyrs.qdf.update_dataframe(df=eq_df, df_add=fx_df)\n",
    "\n",
    "print(f\"Time taken to reduce qdf: {time.time() - starttime}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken to update qdf: 0.02323746681213379\n"
     ]
    }
   ],
   "source": [
    "starttime = time.time()\n",
    "\n",
    "new_df: pl.DataFrame = msyrs.qdf.update_dataframe(df=eq_df, df_add=fx_df)\n",
    "\n",
    "print(f\"Time taken to update qdf: {time.time() - starttime}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 263328 characters]",
      "text/plain": [
       "<Figure size 2075.09x1282.5 with 8 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view_timelines(df=new_df.to_pandas())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time taken: 4.3923423290252686 seconds\n"
     ]
    }
   ],
   "source": [
    "end_time = time.time()\n",
    "print(f\"Time taken: {end_time - nb_start_time} seconds\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
