{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "from matplotlib import pyplot\n",
    "import aisa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# This is an example notebook for AISA\n",
    "\n",
    "In this notebook we show how to use the module `aisa` to create a *range dependent graph* and find its planted partition.\n",
    "\n",
    "See the following papers for reference:\n",
    "\n",
    "- **State aggregations in Markov chains and block models of networks**,\n",
    "    *Faccin, Schaub and Delvenne* [[ArXiv](https://arxiv.org/abs/2005.00337)]\n",
    "\n",
    "## Load a range dependent graph\n",
    "\n",
    "A *range depentent graph* is a graph in which nodes connect with a probability that depends both on their class and on the angular distance between nodes ordered on a circle."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nodes: 200\n",
      "Edges: 1132\n"
     ]
    }
   ],
   "source": [
    "nnodes = [80, 60, 60]\n",
    "eps = 0.001\n",
    "alphas = [\n",
    "    [0.8, eps, eps],\n",
    "    [eps, 0.8, eps],\n",
    "    [eps, eps, 0.8],\n",
    "]\n",
    "gammas = [\n",
    "    [0.8, 1, 1],\n",
    "    [1, 0.8, 1],\n",
    "    [1, 1, 0.8],\n",
    "]\n",
    "graph = aisa.utils.range_dependent_graph(nnodes, alphas, gammas)\n",
    "graph = nx.from_scipy_sparse_matrix(graph)\n",
    "print('Nodes:', len(graph))\n",
    "print('Edges:', graph.number_of_edges())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Find the best partition\n",
    "according to the time scale parameter $T$ and the model selection parameter $\\beta$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "0 [3]:   5%|▌         | 500/10000 [00:04<01:30, 105.25it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Autoinformation 0.6217996520351114\n"
     ]
    }
   ],
   "source": [
    "best_partition, ai = aisa.best_partition(graph, T=3, beta=0.3)\n",
    "print(\"Autoinformation\", ai)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.LineCollection at 0x7f3cac8bdac0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 254236 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "_ = pyplot.figure(figsize=(10, 10))\n",
    "\n",
    "nold = 0\n",
    "pos = {}\n",
    "centers = [(-2, 0), (2, 0), (0, 3.4)]\n",
    "for n, c in zip(nnodes, centers):\n",
    "    subg = graph.subgraph(range(nold, nold + n))\n",
    "    nold += n\n",
    "    subg_pos = nx.circular_layout(subg, center=c)\n",
    "    pos.update(subg_pos)\n",
    "    \n",
    "degree = [d * 5 for _, d in graph.degree()]\n",
    "nx.draw_networkx_nodes(graph, pos=pos, node_size=degree, node_color=[best_partition[node] for node in graph.nodes()])\n",
    "nx.draw_networkx_edges(graph, pos=pos, alpha=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "text_representation": {
    "extension": ".py",
    "format_name": "percent",
    "format_version": "1.3",
    "jupytext_version": "1.9.1"
   }
  },
  "kernelspec": {
   "display_name": "Python 3",
   "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
