{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import networkx as nx\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "import scipy.sparse as spsparse\n",
    "import matplotlib.pylab as plt\n",
    "\n",
    "\n",
    "import pyscisci.all as pyscisci\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "path2aps = '/home/ajgates/APS'\n",
    "path2aps = '/Volumes/GatesExpansionDrive/DataSets/APS/APS2019'\n",
    "\n",
    "myaps = pyscisci.APS(path2aps, keep_in_memory=False)\n",
    "\n",
    "# NOTE: APS does not contain disambiguated author or affiliation information by default, although researchers \n",
    "# have produced their own disambiguation to supplement the raw data\n",
    "\n",
    "# Here, we include the author disambiguation used in Sinatra et al. (2016)\n",
    "# if you didn't already download the file, uncomment the line below\n",
    "#myaps.download_from_source(files_to_download='paa_supplement')\n",
    "myaps.set_new_data_path(dataframe_name='paa ', new_path='publicationauthoraffiliation_supp2010')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f07e0e2dafe448ddbe8df9a509b50c5a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(HTML(value='Loading Publications'), FloatProgress(value=0.0, max=1.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "323ac728696c45b8812c45d0e8aac859",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(HTML(value='Loading pub2ref'), FloatProgress(value=0.0, max=1.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a41b406cb9c840c4a517a16c36ef1206",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(HTML(value='Loading Publication Author Affiliation'), FloatProgress(value=0.0, max=1.0), HTML(v…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "pub  = myaps.pub \n",
    "\n",
    "# limit the publications to those published on/before 1966 \n",
    "pub  = pub .loc[pub ['Year'] <= 1966]\n",
    "\n",
    "# get their references\n",
    "pub2ref  = myaps.load_references(filter_dict={'CitingPublicationId':np.sort(pub ['PublicationId'].unique())})\n",
    "\n",
    "# and get their authors\n",
    "pub2author  = myaps.load_publicationauthoraffiliation(columns = ['PublicationId', 'AuthorId', 'FullName'],\n",
    "    filter_dict={'PublicationId':np.sort(pub ['PublicationId'].unique())})\n",
    "  \n",
    "aid2name = {aid:name for aid, name in pub2author [['AuthorId', 'FullName']].values}\n",
    "del pub2author ['FullName']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(22015,)\n"
     ]
    }
   ],
   "source": [
    "sc, author2int = pyscisci.diffusion_of_scientific_credit(pub2ref , pub2author , \n",
    "                                        pub =pub , alpha = 0.9, max_iter = 100, tol = 1.0e-10)\n",
    "\n",
    "int2aid = {i:aid for aid, i in author2int.items()}\n",
    "\n",
    "print(sc.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hans.a..bethe 0.008774540389162876\n",
      "john.c..slater 0.008551603901008543\n",
      "g...breit 0.007495337492029187\n",
      "j..s..schwinger 0.006353349118286071\n",
      "eugene.p..wigner 0.005233086936687401\n",
      "robert.a..millikan 0.005045043029409964\n",
      "robert.s..mulliken 0.004200793335143117\n",
      "arthur.h..compton 0.004038834819293501\n",
      "irving..langmuir 0.004025411535935083\n",
      "john.h..van vleck 0.004013967086421246\n"
     ]
    }
   ],
   "source": [
    "# print the top k authors\n",
    "# Note: here we use an algorithmicly disambiguated author careers.  The original paper just\n",
    "# disambiguated authors based on unique name.  So we expect the rankings to differ.\n",
    "\n",
    "topk = 10\n",
    "\n",
    "topk_authors = np.argpartition(sc, -topk)[-topk:]\n",
    "topk_authors = topk_authors[np.argsort(sc[topk_authors])][::-1]\n",
    "\n",
    "for int_id in topk_authors:\n",
    "    print(aid2name[int2aid[int_id]], sc[int_id])\n"
   ]
  },
  {
   "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.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
