{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set this path to where the new Custom DataBase is stored\n",
    "path2cdb = '/home/ajgates/myCustomDB'\n",
    "path2cdb = '/users/hgt6rn/Documents/DataSets/myCustomDB'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "mycdb = pyscisci.CustomDB(path2cdb, database_extension='hdf', show_progress=True) \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 database in memory at a time\n",
    "# otherwise keep_in_memory=True will keep each database in memory after its loaded\n",
    "\n",
    "\n",
    "# The Custom DB requires that the preprocessing be run on initialization to check for available data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The Custom DB module looks for and validates the following dataframes:\n",
    "\n",
    "# pub  - keeps all of the publication information\n",
    "# columns : ['PublicationId', 'Year', 'JournalId', 'FamilyId',  'Doi', 'Title', 'Date', 'Volume', 'Issue', 'DocType']\n",
    "\n",
    "# author  - keeps all of the author information (only some versions of WoS ship with the AuthorId known as the AuthorDAIS)\n",
    "# columns : ['AuthorId', 'FullName', 'LastName', 'FirstName', 'MiddleName']\n",
    "\n",
    "# pub2ref  - links publications to their references or citations\n",
    "# columns : ['CitingPublicationId', 'CitedPublicationId']\n",
    "\n",
    "# paa  - links publications, authors, and affiliations (only some versions of WoS ship with the AuthorId known as the AuthorDAIS)\n",
    "# columns : ['PublicationId', 'AuthorId', 'AffiliationId', 'AuthorSequence',  'OrigAuthorName', 'OrigAffiliationName']\n",
    "\n",
    "# author2pub  - links the authors to their publications\n",
    "# columns : ['PublicationId', 'AuthorId', 'AuthorOrder']\n",
    "\n",
    "# field  - field information\n",
    "# columns : ['FieldId', 'FieldLevel', 'NumberPublications', 'FieldName']\n",
    "\n",
    "# pub2field  - links publications to their fields\n",
    "# columns : ['PublicationId', 'FieldId']\n",
    "\n",
    "# affiliation  - affiliation information\n",
    "# columns : ['AffiliationId', 'NumberPublications', 'NumberCitations', 'FullName', 'GridId', 'OfficialPage', 'WikiPage', 'Latitude', 'Longitude']\n",
    "\n",
    "# journal  - journal information\n",
    "# columns : ['JournalId', 'FullName', 'Issn', 'Publisher', 'Webpage']\n",
    "\n",
    "\n",
    "# after additional processing, these DataFrames become available\n",
    "\n",
    "# pub2refnoself  - links publications to their references or citations with self-citations removed\n",
    "# columns : ['CitingPublicationId', 'CitedPublicationId']\n",
    "\n",
    "# impact  - precomputed citation counts, columns will depend on which counts are computed\n",
    "# columns : ['PublicationId', 'Year', ....]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ca41914bd4e840079efea2ac93771137",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading Publications:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 37956 characters]\n",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# lets plot the number of publications each year\n",
    "pub  = mycdb.pub \n",
    "\n",
    "yearly_articles = pyscisci.groupby_count(df=pub , colgroupby='Year', colcountby='PublicationId', count_unique=True)\n",
    "yearly_articles.sort_values(by='Year', inplace=True)\n",
    "yearly_articles = yearly_articles.loc[yearly_articles['Year'] > 0]\n",
    "\n",
    "fig, ax = plt.subplots(1,1,figsize=(8,5))\n",
    "\n",
    "ax.plot(yearly_articles['Year'],yearly_articles['PublicationIdCount'])\n",
    "\n",
    "ax.set_xlabel('Year')\n",
    "ax.set_ylabel(\"# of publications\")\n",
    "ax.set_yscale('log')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "53d62bafebc84314b125be3a6cce82b8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading Publication Author Affiliation:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 25104 characters]\n",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# now we can see the distribution of author productivity \n",
    "pa  = mycdb.author2pub  # publication author relationships\n",
    "\n",
    "author_prod = pyscisci.author_productivity(pa , colgroupby = 'AuthorId', colcountby = 'PublicationId')\n",
    "\n",
    "prodvalues, prodcounts = np.unique(author_prod['Productivity'].values, return_counts=True)\n",
    "\n",
    "fig, ax = plt.subplots(1,1,figsize=(8,5))\n",
    "\n",
    "ax.scatter(prodvalues, prodcounts)\n",
    "\n",
    "ax.set_xlabel('Productivity')\n",
    "ax.set_ylabel(\"# of authors\")\n",
    "ax.set_xscale('log')\n",
    "ax.set_yscale('log')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "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": 2
}
