{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "import pyscisci.all as pyscisci\n",
    "\n",
    "from pyscisci.embedding import Node2Vec\n",
    "\n",
    "path2dblp = '/u/yoonjis/ember_home/DBLP_new' #put yout own DBLP path here\n",
    "path2dblp = '/users/hgt6rn/Documents/DataSets/DBLP'\n",
    "mydblp = pyscisci.DBLP(path2database= path2dblp, keep_in_memory=False, show_progress=True)\n",
    "\n",
    "#a2p  = mydblp.author2pub "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coauthorship network of Albert-Laszlo Barabasi and Mark E. J. Newman\n",
    "author  = mydblp.author \n",
    "target_researcher = ['Albert-Laszlo Barabasi', 'Mark E. J. Newman']\n",
    "target_index = list(author[author.FullName.isin(target_researcher)].AuthorId)\n",
    "coauthornet, author2int = pyscisci.coauthorship_network(a2p , focus_author_ids = target_index, focus_constraint='ego', show_progress=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Get Node2Vec Embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "model = Node2Vec(coauthornet, author2int)\n",
    "emb = model.learn_embedding()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ids = list(author2int.keys())\n",
    "id_to_name = author.set_index('AuthorId')['FullName'].to_dict()\n",
    "names = [id_to_name[k] for k in author2int.keys()]\n",
    "emb_array = [emb[k] for k in ids]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 2-D projection of embeddings "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import umap\n",
    "fit = umap.UMAP(metric='cosine')\n",
    "u = fit.fit_transform(emb_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import plotly.graph_objects as go\n",
    "import plotly as py\n",
    "import plotly.express as px\n",
    "\n",
    "df = pd.DataFrame({\n",
    "    'x': u[:,0],\n",
    "    'y': u[:,1],\n",
    "    'name': names\n",
    "})\n",
    "\n",
    "fig = px.scatter(df, x=\"x\", y=\"y\", hover_name=\"name\")\n",
    "fig.update_layout(\n",
    "            autosize=False,\n",
    "            width=1000,\n",
    "            height=800,\n",
    "        )\n",
    "fig.update_traces(marker=dict(size=3),\n",
    "                          selector=dict(mode='markers'))\n",
    "\n",
    "py.offline.plot(fig, filename=\"example_interactive_html/umap_fig.html\",  auto_open=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. Sem_axis results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For the detail, please read Sem_aixs paper, https://arxiv.org/abs/1806.05521. <br>\n",
    "Sem_axis usually used in word space, to characterize word semantics using many semantic axes. But It can be applied on the network also.<br>\n",
    "In this example, we define axis from two-person (Newman as a negative anchor and Barabasi as a positive anchor). \n",
    "Then, we can interpret persons with negative values as more Newman friendly researcher, and the person with positive values as more Barabasi friendly researcher."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "positive_entities = [245542] # Barabasi's vector\n",
    "negative_entities = [301349] # Newman's vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sem_aixs_dict = pyscisci.sem_axis(emb, positive_entities , negative_entities)\n",
    "sem_axis_array = [sem_aixs_dict[id_] for id_ in ids]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({\n",
    "    'sem_axis_result': sem_axis_array,\n",
    "    'y': 0,\n",
    "    'name': names,\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = px.scatter(df, x=\"sem_axis_result\", y=\"y\", hover_name=\"name\")\n",
    "fig.update_layout(\n",
    "    autosize=False,\n",
    "    width=1200,\n",
    "    height=300,\n",
    "    yaxis={\n",
    "        'range': [-0.1, 0.1],\n",
    "        'showgrid': False, # thin lines in the background\n",
    "        'zeroline': False, # thick line at x=0\n",
    "        'visible': False,  # numbers below\n",
    "    },\n",
    "    xaxis={\n",
    "        'showgrid': False, # thin lines in the background\n",
    "        'zeroline': False, # thick line at x=0\n",
    "    }\n",
    "    \n",
    ")\n",
    "fig.update_traces(marker=dict(size=3),\n",
    "                          selector=dict(mode='markers'))\n",
    "\n",
    "py.offline.plot(fig, filename=\"example_interactive_html/sem_axis.html\",  auto_open=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
