{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ad19f8f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from time import time\n",
    "import os\n",
    "\n",
    "import sys\n",
    "sys.path += ['utils/']  \n",
    "\n",
    "from EDRep import *\n",
    "from dcsbm import *\n",
    "from node_embedding import *\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2e78dd5",
   "metadata": {},
   "source": [
    "## Test against spectral clustering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6512871c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "α = 3.1990319561281755\n",
      "Running the optimization for k = 1\n",
      "Learning embeddings using the Skip-Gram model...\n",
      "Preprocessing progress: 97.59%%00%%0%%0%ogress: \n",
      "Preprocessing transition probabilities: done!\n",
      "Walking progress: 86.11%%% progress: 0\n",
      "Simulating random walks: done!\n",
      "Learning embeddings: done!\n",
      "\n",
      "\n",
      "Estimating zeta : 02"
     ]
    }
   ],
   "source": [
    "# set the parameters\n",
    "\n",
    "n = 10000 # number of nodes\n",
    "n_clusters = 4 # number of communities\n",
    "c = 10 # average degree\n",
    "c_out = 4 # across-classes connectivity\n",
    "γ = 6 # degree distribution heterogeneity parameter\n",
    "dim = 32 # embedding dimension\n",
    "\n",
    "# degree distribution latent parameter\n",
    "θ = np.random.uniform(3,12, n)**γ\n",
    "θ = θ/np.mean(θ)\n",
    "sq = np.mean(θ**2)\n",
    "print(f'α = {(c-c_out)*np.sqrt(sq/c)}')\n",
    "\n",
    "# label vector\n",
    "π = np.ones(n_clusters)/n_clusters\n",
    "label = np.concatenate([[j for i in range(int(π[j]*n))] for j in range(n_clusters)])\n",
    "\n",
    "# create the C matrix\n",
    "C = matrix_C(c_out, c, 0., π)\n",
    "\n",
    "# generate the adjacency matrix\n",
    "A, ℓ = adj(C,c, label, θ, True)\n",
    "\n",
    "NMIv, tv = [], []\n",
    "\n",
    "\n",
    "# Node embedding with EDRep\n",
    "t0 = time()\n",
    "res = NodeEmbedding(A, dim, n_epochs = 50, walk_length = 3, k = 1, verbose = True, η = 1., sym = True)\n",
    "NMIv.append(computeScore(res.X, ℓ))\n",
    "tv.append(time()-t0)\n",
    "\n",
    "# Node2Vec\n",
    "t0 = time()\n",
    "Y = Node2VecNS(A, dim, verbose = True)\n",
    "tv.append(time() - t0)\n",
    "NMIv.append(computeScore(Y, ℓ))\n",
    "\n",
    "# Spectral clustering\n",
    "t0 = time()\n",
    "X = community_detection(A , n_clusters = n_clusters, verbose = True)\n",
    "tv.append(time()-t0)\n",
    "NMIv.append(computeScore(X, ℓ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "23c7000e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "EDRep\n",
      "NMI: 0.6679467172426897\n",
      "Execution time: 6.463024616241455\n",
      "...................\n",
      "\n",
      "Node2Vec\n",
      "NMI: 0.6478906339844409\n",
      "Execution time: 15.87546157836914\n",
      "...................\n",
      "\n",
      "SC\n",
      "NMI: 0.6672236542155506\n",
      "Execution time: 4.039486408233643\n",
      "...................\n",
      "\n"
     ]
    }
   ],
   "source": [
    "names = ['EDRep', 'Node2Vec', 'SC']\n",
    "\n",
    "for i in range(3):\n",
    "    print(f'{names[i]}\\nNMI: {NMIv[i]}\\nExecution time: {tv[i]}\\n...................\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d096693b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "EDRep",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
