{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\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",
    "# to make pretty plots\n",
    "try:\n",
    "    import seaborn as sns\n",
    "    sns.set_style('white')\n",
    "except:\n",
    "    pass\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Derek J. de Solla Price\n",
    "focus_aid = 2537508914\n",
    "\n",
    "path2mag = '/home/ajgates/MAG'\n",
    "path2mag = '/users/hgt6rn/Documents/DataSets/MAG'\n",
    "mymag = pyscisci.MAG(path2mag, database_extension='hdf', keep_in_memory=False, show_progress=True) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Filter the career data from the DataBase"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "we also include the filtered data in the examples folder, so you can skip this section"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1664c917d1a74121b3ff4845909e6403",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading Publication Author Affiliation:   0%|          | 0/143 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "author_career = mymag.load_publicationauthoraffiliation(columns=['PublicationId', 'AuthorId', 'AffiliationId'],\n",
    "                                                        filter_dict = {'AuthorId':np.sort([focus_aid])},\n",
    "                                                      duplicate_subset=['PublicationId', 'AuthorId', 'AffiliationId'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6e9693383f6a44f8a0917b011e65bb3e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading Publications:   0%|          | 0/132 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10,11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10,11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10,11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (11) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n",
      "/Users/hgt6rn/Documents/GitHub/pyscisci/pyscisci/datasource/readwrite.py:155: DtypeWarning: Columns (9,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  return pd.read_csv(fname, compression='gzip')\n"
     ]
    }
   ],
   "source": [
    "authorpubs = np.sort(author_career['PublicationId'].unique())\n",
    "\n",
    "author_pub = mymag.load_publications(columns=['PublicationId', 'Year', 'Date', 'JournalId', 'DocType'],\n",
    "                                                        filter_dict = {'PublicationId':authorpubs})\n",
    "\n",
    "\n",
    "author_career = author_career[['PublicationId', 'AuthorId', 'AffiliationId']].merge(author_pub, how='left', on='PublicationId')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d6f038fcaf334fc89c9eab3be07bca68",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading pub2ref:   0%|          | 0/186 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "authorpubs = np.sort(author_career['PublicationId'].unique())\n",
    "\n",
    "author_cites = mymag.load_references(filter_dict = {'CitedPublicationId':authorpubs})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "pub2year = mymag.pub2year\n",
    "author_cites['CitingYear'] = [pub2year.get(pid, None) for pid in author_cites['CitingPublicationId'].values]\n",
    "author_cites['CitedYear'] = [pub2year.get(pid, None) for pid in author_cites['CitedPublicationId'].values]\n",
    "\n",
    "author_cites = author_cites.loc[author_cites['CitingYear'] >= author_cites['CitedYear']]\n",
    "\n",
    "author_impact = author_cites.groupby('CitedPublicationId')['CitingPublicationId'].nunique().to_frame().reset_index()\n",
    "author_impact = author_impact.rename(columns={'CitingPublicationId':'Ctotal', 'CitedPublicationId':'PublicationId'})\n",
    "\n",
    "author_cites = author_cites.loc[author_cites['CitingYear'] <= author_cites['CitedYear'] + 10]\n",
    "c10 = author_cites.groupby('CitedPublicationId')['CitingPublicationId'].nunique().to_frame().reset_index()\n",
    "c10 = c10.rename(columns={'CitingPublicationId':'C10', 'CitedPublicationId':'PublicationId'})\n",
    "author_impact = author_impact.merge(c10, how='left', on='PublicationId')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "author_career = author_career.merge(author_impact[['PublicationId', 'C10', 'Ctotal']], \n",
    "                                    how='left', on='PublicationId').fillna(0)\n",
    "author_career.sort_values('Year', inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "author_career.to_csv('../example_data/DeSollaPriceCareer.csv', mode='w', header=True, index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Career Metrics "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "author_career = pd.read_csv('../example_data/DeSollaPriceCareer.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "157\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "print(author_career['PublicationId'].nunique())\n",
    "author_career = author_career[author_career['DocType']=='j']\n",
    "author_career = author_career[author_career['Year'] < 2010]\n",
    "print(author_career['PublicationId'].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28004 characters]\n",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(1,1,figsize=(10,6))\n",
    "\n",
    "ax = pyscisci.career_impacttimeline(author_career, datecol='Date', impactcol='Ctotal', \n",
    "                              fill_color='orange', edge_color='k', ax=ax)\n",
    "\n",
    "ax.set_ylabel('citations', fontsize=14)\n",
    "ax.set_xlabel('year', fontsize=14)\n",
    "#plt.savefig('DeSollaPriceCarrerCitations.pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     AuthorId  AuthorIdHindex\n",
      "0  2537508914              25\n",
      "     AuthorId  AuthorIdGindex\n",
      "0  2537508914              78\n",
      "     AuthorId  AuthorIdQfactor\n",
      "0  2537508914        11.309299\n"
     ]
    }
   ],
   "source": [
    "\n",
    "hindex = pyscisci.author_hindex(author_career, colgroupby='AuthorId', colcountby='Ctotal')\n",
    "print(hindex)\n",
    "\n",
    "gindex = pyscisci.author_gindex(author_career, colgroupby='AuthorId', colcountby='Ctotal')\n",
    "print(gindex)\n",
    "\n",
    "qfact = pyscisci.author_qfactor(author_career, colgroupby='AuthorId', colcountby='Ctotal')\n",
    "print(qfact)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34116 characters]\n",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "yearly_productivity = pyscisci.author_yearly_productivity(author_career, colgroupby='AuthorId', \n",
    "                                                          datecol='Year', colcountby='PublicationId')\n",
    "\n",
    "prodtraj = pyscisci.author_productivity_trajectory(author_career, colgroupby = 'AuthorId', \n",
    "                                        datecol = 'Year', colcountby = 'PublicationId')\n",
    "\n",
    "fig, ax = plt.subplots(1,1,figsize=(10,6))\n",
    "\n",
    "ax = pyscisci.career_productivitytimeline(yearly_productivity, productivity_trajectory=prodtraj, \n",
    "                                          datecol='Year', fill_color=sns.color_palette()[2], ax=ax)\n",
    "\n",
    "ax.set_ylabel('yearly productivity', fontsize=14)\n",
    "ax.set_xlabel('year', fontsize=14)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Career HotStreak"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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>Baseline</th>\n",
       "      <th>StreakGamma</th>\n",
       "      <th>StreakStart</th>\n",
       "      <th>StreakEnd</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.719092</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Baseline StreakGamma StreakStart StreakEnd\n",
       "0  0.719092        None        None      None"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "author_career_streak = author_career[author_career['C10'] > 0].reset_index(drop=True)\n",
    "pyscisci.career_hotstreak(author_career_streak, citecol='C10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "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>AuthorId</th>\n",
       "      <th>StreakNumber</th>\n",
       "      <th>Baseline</th>\n",
       "      <th>StreakGamma</th>\n",
       "      <th>StreakStart</th>\n",
       "      <th>StreakEnd</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2537508914</td>\n",
       "      <td>0</td>\n",
       "      <td>0.720276</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     AuthorId  StreakNumber  Baseline StreakGamma StreakStart StreakEnd\n",
       "0  2537508914             0  0.720276        None        None      None"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# This example does not have a hot streak so only the baseline is filled without a start/end index\n",
    "pyscisci.author_hotstreak(author_career_streak, colgroupby='AuthorId', datecol='Date', citecol='C10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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": 4
}
