{
 "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": [
    "\n",
    "import pyscisci.all as pyscisci\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pylab as plt\n",
    "try:\n",
    "    import seaborn as sns\n",
    "    sns.set_style('white')\n",
    "except:\n",
    "    pass\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set this path to where the APS database is stored\n",
    "path2aps = '/home/ajgates/APS'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "myaps = pyscisci.APS(path2aps, database_extension='csv.gz', keep_in_memory=False) \n",
    "\n",
    "# set keep_in_memory=False if you want to load the database each time its needed - good for when you \n",
    "# cant keep more than one DataFrame in memory at a time\n",
    "\n",
    "# otherwise keep_in_memory=True will keep each DataFrame in memory after its loaded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "cef17349625d4f508bc5257572c99861",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading pub2ref:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7fb0f82985f547b2838fe4004c6243dc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading Publications:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pub2ref  = myaps.pub2ref \n",
    "pubdf = myaps.pub[['PublicationId', 'Date', 'Year']]\n",
    "pubdf['Date'] = pd.to_datetime(pubdf['Date'], infer_datetime_format=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8324631, 2)\n",
      "(8267865, 6)\n"
     ]
    }
   ],
   "source": [
    "print(pub2ref.shape)\n",
    "\n",
    "pub2ref = pub2ref.merge(pubdf[['PublicationId', 'Date', 'Year']], \n",
    "                                            how='left', left_on='CitingPublicationId', right_on='PublicationId')\n",
    "del pub2ref ['PublicationId']\n",
    "pub2ref  = pub2ref.rename(columns={'Date':'CitingDate', 'Year':'CitingYear'})\n",
    "\n",
    "pub2ref  = pub2ref.merge(pubdf[['PublicationId', 'Date', 'Year']], \n",
    "                                            how='left', left_on='CitedPublicationId', right_on='PublicationId')\n",
    "del pub2ref ['PublicationId']\n",
    "pub2ref  = pub2ref .rename(columns={'Date':'CitedDate', 'Year':'CitedYear'})\n",
    "\n",
    "pub2ref.dropna(inplace=True)\n",
    "\n",
    "print(pub2ref.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CitedPublicationId\n",
       "10.1103/PhysRev.1.1                1\n",
       "10.1103/PhysRevA.39.2411           1\n",
       "10.1103/PhysRevD.55.6403           1\n",
       "10.1103/PhysRevD.55.6584           1\n",
       "10.1103/PhysRevD.55.6603           1\n",
       "                               ...  \n",
       "10.1103/PhysRevB.50.17953       5464\n",
       "10.1103/PhysRev.136.B864        6218\n",
       "10.1103/PhysRevB.54.11169       7444\n",
       "10.1103/PhysRev.140.A1133       7734\n",
       "10.1103/PhysRevLett.77.3865    10666\n",
       "Name: CitingPublicationId, Length: 561040, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pub2ref.groupby('CitedPublicationId')['CitingPublicationId'].nunique().sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/69/w28tw6xx26nd7x4yv_sh2mgm0000gp/T/ipykernel_53024/337035123.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  focus_cites.sort_values(by=['CitingDate'], inplace=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(24618, 6)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "focusids = np.sort(['10.1103/PhysRev.136.B864', '10.1103/PhysRevLett.77.3865', '10.1103/PhysRev.140.A1133'])\n",
    "focus_cites = pub2ref.loc[pyscisci.isin_sorted(pub2ref ['CitedPublicationId'].values, focusids)]\n",
    "focus_cites.sort_values(by=['CitingDate'], inplace=True)\n",
    "focus_cites.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Longterm Citation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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>CitedPublicationId</th>\n",
       "      <th>lam</th>\n",
       "      <th>mu</th>\n",
       "      <th>sig</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>6.205512</td>\n",
       "      <td>9.044825</td>\n",
       "      <td>0.788842</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.1103/PhysRev.140.A1133</td>\n",
       "      <td>6.678506</td>\n",
       "      <td>8.936498</td>\n",
       "      <td>0.977601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>8.120082</td>\n",
       "      <td>8.191955</td>\n",
       "      <td>1.419675</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            CitedPublicationId       lam        mu       sig\n",
       "0     10.1103/PhysRev.136.B864  6.205512  9.044825  0.788842\n",
       "1    10.1103/PhysRev.140.A1133  6.678506  8.936498  0.977601\n",
       "2  10.1103/PhysRevLett.77.3865  8.120082  8.191955  1.419675"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "longtermimpact = pyscisci.longterm_impact(focus_cites, colgroupby = 'CitedPublicationId', coldate='CitingDate')\n",
    "longtermimpact"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/69/w28tw6xx26nd7x4yv_sh2mgm0000gp/T/ipykernel_4769/3681678537.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  focus_cites.sort_values(by=['CitingDate'], inplace=True)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 78120 characters]\n",
      "text/plain": [
       "<Figure size 1500x500 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(1,3, figsize = (15,5))\n",
    "\n",
    "focus_cites.sort_values(by=['CitingDate'], inplace=True)\n",
    "\n",
    "for iax in range(len(focusids)):\n",
    "    focus  = focus_cites[focus_cites['CitedPublicationId']==focusids[iax]]\n",
    "    recenter_time = (focus ['CitingDate'] - focus ['CitingDate'].min() ) / np.timedelta64(1,'D')\n",
    "    recenter_time = recenter_time[recenter_time>0]\n",
    "    \n",
    "    lam, mu, sig = longtermimpact[longtermimpact['CitedPublicationId'] == focusids[iax]][['lam', 'mu', 'sig']].values[0]\n",
    "    ax[iax].plot(recenter_time, np.arange(1,len(recenter_time) + 1), label='actual') \n",
    "    \n",
    "    ax[iax].plot(recenter_time, [pyscisci.predicted_c(t, lam, mu, sig) \n",
    "                                 for t in recenter_time], label='predicted')\n",
    "    \n",
    "ax[0].set_ylabel('total citations')\n",
    "ax[0].set_xlabel('time since publication')\n",
    "    \n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Disruption Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "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>PublicationId</th>\n",
       "      <th>DisruptionIndex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>0.780082</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.1103/PhysRev.140.A1133</td>\n",
       "      <td>-0.358467</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>0.252897</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 PublicationId  DisruptionIndex\n",
       "0     10.1103/PhysRev.136.B864         0.780082\n",
       "1    10.1103/PhysRev.140.A1133        -0.358467\n",
       "2  10.1103/PhysRevLett.77.3865         0.252897"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "disrupt = pyscisci.disruption_index(pub2ref, focus_pub_ids = focusids)\n",
    "disrupt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sleeping Beauty Coefficient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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>CitedPublicationId</th>\n",
       "      <th>CitingYear</th>\n",
       "      <th>Citations</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>112224</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>1965.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>112225</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>1966.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>112226</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>1967.0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>112227</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>1968.0</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>112228</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>1969.0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3080030</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3080031</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>2016.0</td>\n",
       "      <td>818</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3080032</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>2017.0</td>\n",
       "      <td>854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3080033</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3080034</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>2019.0</td>\n",
       "      <td>996</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>132 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                  CitedPublicationId  CitingYear  Citations\n",
       "112224      10.1103/PhysRev.136.B864      1965.0          3\n",
       "112225      10.1103/PhysRev.136.B864      1966.0          1\n",
       "112226      10.1103/PhysRev.136.B864      1967.0          5\n",
       "112227      10.1103/PhysRev.136.B864      1968.0          3\n",
       "112228      10.1103/PhysRev.136.B864      1969.0          4\n",
       "...                              ...         ...        ...\n",
       "3080030  10.1103/PhysRevLett.77.3865      2015.0        728\n",
       "3080031  10.1103/PhysRevLett.77.3865      2016.0        818\n",
       "3080032  10.1103/PhysRevLett.77.3865      2017.0        854\n",
       "3080033  10.1103/PhysRevLett.77.3865      2018.0        956\n",
       "3080034  10.1103/PhysRevLett.77.3865      2019.0        996\n",
       "\n",
       "[132 rows x 3 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "annual_cites = pub2ref.groupby(['CitedPublicationId', 'CitingYear'])['CitingPublicationId'].nunique()\n",
    "annual_cites = annual_cites.to_frame().reset_index().rename(columns={'CitingPublicationId':'Citations'})\n",
    "\n",
    "annual_cites = annual_cites[pyscisci.isin_sorted(annual_cites['CitedPublicationId'].values, focusids)]\n",
    "annual_cites"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "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>CitedPublicationId</th>\n",
       "      <th>BeautyCoefficient</th>\n",
       "      <th>Awakening</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>55.165570</td>\n",
       "      <td>1996.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.1103/PhysRev.140.A1133</td>\n",
       "      <td>21.272430</td>\n",
       "      <td>1997.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>4.664782</td>\n",
       "      <td>2014.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            CitedPublicationId  BeautyCoefficient  Awakening\n",
       "0     10.1103/PhysRev.136.B864          55.165570     1996.0\n",
       "1    10.1103/PhysRev.140.A1133          21.272430     1997.0\n",
       "2  10.1103/PhysRevLett.77.3865           4.664782     2014.0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "beauty = pyscisci.compute_sleepingbeauty(annual_cites, colgroupby='CitedPublicationId', colcountby='Citations',\n",
    "                                        coldate='CitingYear')\n",
    "beauty"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Network-normalized citation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "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>PublicationId</th>\n",
       "      <th>CitedYear</th>\n",
       "      <th>Cnorm5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.1103/PhysRev.136.B864</td>\n",
       "      <td>1964.0</td>\n",
       "      <td>3.757856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.1103/PhysRev.140.A1133</td>\n",
       "      <td>1965.0</td>\n",
       "      <td>18.087836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.1103/PhysRevLett.77.3865</td>\n",
       "      <td>1996.0</td>\n",
       "      <td>59.054782</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 PublicationId  CitedYear     Cnorm5\n",
       "0     10.1103/PhysRev.136.B864     1964.0   3.757856\n",
       "1    10.1103/PhysRev.140.A1133     1965.0  18.087836\n",
       "2  10.1103/PhysRevLett.77.3865     1996.0  59.054782"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "focusids = np.sort(['10.1103/PhysRev.136.B864', '10.1103/PhysRevLett.77.3865', '10.1103/PhysRev.140.A1133'])\n",
    "\n",
    "netnorm = pyscisci.netnormcite_index(pub2ref, T=5, focus_pub_ids = focusids)\n",
    "netnorm"
   ]
  }
 ],
 "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
