{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "import igraph as ig\n",
    "import dionysus as d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "autoscroll": false,
    "collapsed": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "plt.style.use(\"seaborn\")\n",
    "plt.rcParams[\"figure.figsize\"] = 10, 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "celegans = ig.read(\"data/celegansneural.gml\", format=\"gml\")\n",
    "celegans.es[\"weight\"] = celegans.es[\"value\"]\n",
    "celegans.to_undirected(combine_edges=\"sum\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17932 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 36 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "unique_weights = np.unique(celegans.es[\"weight\"])\n",
    "fig, ax = plt.subplots(6,6)\n",
    "for i, w in enumerate(unique_weights):\n",
    "    subg = celegans.subgraph_edges(celegans.es(lambda e: e[\"weight\"] < w), delete_vertices=True)\n",
    "    cl = subg.maximal_cliques()\n",
    "    cl = np.array(list(map(len,cl)))\n",
    "    if len(cl) > 0:\n",
    "        ax[i//6,i%6].hist(cl,bins=10)\n",
    "        # print(f\"Number of cliques: {len(cl)}\\tMax size: {max(map(len, cl))}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "subg = celegans.subgraph_edges(celegans.es(lambda e: e[\"weight\"] < 3), delete_vertices=True)\n",
    "cl = subg.maximal_cliques()\n",
    "cl = np.array(list(map(len,cl)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 7744 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.hist(cl);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "filt = d.Filtration()\n",
    "for v in celegans.vs:\n",
    "    filt.append(d.Simplex([v.index], 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "distinct_weights = np.unique(celegans.es[\"weight\"])[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "for t, w in enumerate(distinct_weights):\n",
    "    subg = celegans.subgraph_edges(celegans.es(lambda e: e[\"weight\"] >= w))\n",
    "    for clique in subg.maximal_cliques():\n",
    "        for s in d.closure([d.Simplex(clique)], len(clique)):\n",
    "            filt.append(d.Simplex(s, t+1))\n",
    "filt.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Diagram with 297 points, Diagram with 2678 points, Diagram with 171 points, Diagram with 4 points, Diagram with 0 points, Diagram with 0 points, Diagram with 0 points, Diagram with 0 points]\n"
     ]
    }
   ],
   "source": [
    "pers = d.homology_persistence(filt)\n",
    "dgms = d.init_diagrams(pers, filt)\n",
    "print(dgms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12368 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "d.plot.plot_bars(dgms[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14868 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "d.plot.plot_diagram(dgms[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
