{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "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",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set this path to where the PubMed database will be stored\n",
    "path2pubmed = '/home/ajgates/PubMed'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mypubmed = pyscisci.PubMed(path2pubmed, 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": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function will download the latest baseline version of PubMed.\n",
    "# Depending on your internet connection, it could take several hours to complete the download.\n",
    "\n",
    "mypubmed.download_from_source(rewrite_existing=False)\n",
    "# if your connetion breaks/download stops for any reason, set rewrite_existing = False and \n",
    "# rerun to continue downloading where you left off"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# before we can start running our analysis, we have to preprocess the raw data into\n",
    "# DataFrames that are more convinent to work with\n",
    "mypubmed.preprocess(show_progress=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# PubMed creates three dataframes:\n",
    "# pub  - keeps all of the publication information\n",
    "# columns : ['PublicationId', 'Title', 'Year', 'Month', 'Day', 'Doi', 'JournalId', 'ISSN', 'Volume', 'Issue', 'Pages', 'TeamSize']\n",
    "\n",
    "# paa  - links the publications to authors and affiliations \n",
    "# NOTE: PubMed does not disambiguate authors!!!\n",
    "# columns : ['PublicationId', 'FirstName', 'LastName', 'FullName', 'Affiliations', 'AuthorSequence']\n",
    "\n",
    "# pub2field  - links the publications to fields (aka subjectAreas)\n",
    "# columns : ['PublicationId', 'FieldId']\n",
    "\n",
    "# pub2ref  - keeps the citation information\n",
    "# columns : ['CitingPublicationId', 'CitedPublicationId']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lets plot the number of publications each year\n",
    "yearly_articles = pyscisci.groupby_count(df=mypubmed.pub , colgroupby='Year', colcountby='PublicationId', count_unique=True,\n",
    "                               show_progress=True)\n",
    "yearly_articles.sort_values(by='Year', inplace=True)\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": 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
}
