{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dd2d6783",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3516d2d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import pyscisci.all as pyscisci\n",
    "\n",
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pylab as plt\n",
    "\n",
    "import dask.dataframe as dd\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9265c653",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set this path to where the MAG database will be stored\n",
    "path2mag = '/home/ajgates/MAG'\n",
    "path2mag = '/users/hgt6rn/Documents/DataSets/MAG'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9ae59de0",
   "metadata": {},
   "outputs": [],
   "source": [
    "mymag = pyscisci.MAG(path2mag, database_extension='csv.gz', enable_dask=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8cdec26a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a52584b761bb4d14931200e6e0828df5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading Publications:   0%|          | 0/132 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "magpubs = mymag.load_publications(columns=['PublicationId', 'Year', 'JournalId', 'DocType'],\n",
    "                                  dropna = ['Year', 'JournalId', 'DocType'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "b74345ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "magpubs = magpubs[magpubs['DocType'] == 'j']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "335c768f",
   "metadata": {},
   "outputs": [],
   "source": [
    "npubs_mag = magpubs.groupby('Year')['PublicationId'].nunique().compute()\n",
    "njournals_mag = magpubs.groupby('Year')['JournalId'].nunique().compute()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "01bdb99b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Year</th>\n",
       "      <th>NumPubs</th>\n",
       "      <th>NumJournals</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1800.0</td>\n",
       "      <td>661</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1801.0</td>\n",
       "      <td>165</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1802.0</td>\n",
       "      <td>237</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1805.0</td>\n",
       "      <td>219</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1806.0</td>\n",
       "      <td>233</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>218</th>\n",
       "      <td>2022.0</td>\n",
       "      <td>1371</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>219</th>\n",
       "      <td>1803.0</td>\n",
       "      <td>264</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>220</th>\n",
       "      <td>1804.0</td>\n",
       "      <td>198</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>221</th>\n",
       "      <td>1816.0</td>\n",
       "      <td>300</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>222</th>\n",
       "      <td>1817.0</td>\n",
       "      <td>274</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>223 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       Year  NumPubs  NumJournals\n",
       "0    1800.0      661           38\n",
       "1    1801.0      165            5\n",
       "2    1802.0      237            6\n",
       "3    1805.0      219            5\n",
       "4    1806.0      233            5\n",
       "..      ...      ...          ...\n",
       "218  2022.0     1371          116\n",
       "219  1803.0      264            5\n",
       "220  1804.0      198            6\n",
       "221  1816.0      300           10\n",
       "222  1817.0      274           12\n",
       "\n",
       "[223 rows x 3 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mag_values = npubs_mag.to_frame().reset_index().merge(njournals_mag.to_frame().reset_index(), how='left', on='Year')\n",
    "mag_values = mag_values.rename(columns={'PublicationId':'NumPubs', 'JournalId':'NumJournals'})\n",
    "mag_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d95e5f95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 50200 characters]\n",
      "text/plain": [
       "<Figure size 600x800 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "mag_color = '#12664F'\n",
    "\n",
    "dflabels = ['MAG']\n",
    "dfcolors = [mag_color]\n",
    "dfs = [mag_values.sort_values('Year')]\n",
    "\n",
    "vmax = 2017\n",
    "\n",
    "fig, ax = plt.subplots(2, 1, figsize=(6, 8))\n",
    "\n",
    "for idf in range(1):\n",
    "    idy = np.logical_and(dfs[idf]['Year'].values > 1000, dfs[idf]['Year'].values < vmax)\n",
    "    for iax,v in enumerate(['NumPubs', 'NumJournals']):\n",
    "        ax[iax].plot(dfs[idf]['Year'].values[idy], dfs[idf][v].values[idy], lw=3, \n",
    "               color=dfcolors[idf], label=dflabels[idf])\n",
    "    #ax[1].plot(njournals_dblp.index.values[years], njournals_dblp.values[years], lw=2, color=dblp_color)\n",
    "    #ax[2].plot(nauthors_dblp.index.values[years], nauthors_dblp.values[years], lw=2, color=dblp_color)\n",
    "\n",
    "ax[0].set_ylabel('Number of publications')\n",
    "ax[1].set_ylabel('Number of journals')\n",
    "\n",
    "\n",
    "handles, labels = ax[0].get_legend_handles_labels()\n",
    "ax[0].legend(handles, labels, loc='lower left')\n",
    "\n",
    "for iax in range(2):\n",
    "    ax[iax].set_yscale('log')\n",
    "    \n",
    "    ax[iax].set_xlim([1800, 2020])\n",
    "\n",
    "for iax in [0]:\n",
    "    ax[iax].set_yticks([10**0, 10**2, 10**4, 10**6])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "625c95ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
