{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "autoscroll": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import paths\n",
    "\n",
    "import pygraph\n",
    "\n",
    "from pygraph.utils.graphfiles import loadDataset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "autoscroll": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "import numpy as np\n",
    "import matplotlib.pyplot  as plt\n",
    "\n",
    "# We load a ds dataset\n",
    "# load it from https://brunl01.users.greyc.fr/CHEMISTRY/Acyclic.tar.gz\n",
    "dataset, y = loadDataset(\"/home/bgauzere/work/Datasets/Acyclic/dataset_bps.ds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "autoscroll": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 183/183 [07:41<00:00,  2.52s/it]\n",
      "100%|██████████| 183/183 [08:39<00:00,  2.84s/it]\n",
      "100%|██████████| 183/183 [05:19<00:00,  1.75s/it]\n",
      "100%|██████████| 183/183 [05:50<00:00,  1.91s/it]\n"
     ]
    }
   ],
   "source": [
    "#Compute graph edit distances\n",
    "\n",
    "from tqdm import tqdm\n",
    "from pygraph.c_ext.lsape_binders import  lsap_solverHG\n",
    "from pygraph.ged.costfunctions import ConstantCostFunction\n",
    "from pygraph.ged.GED import ged\n",
    "import time\n",
    "\n",
    "cf = ConstantCostFunction(1,3,1,3)\n",
    "N=len(dataset)\n",
    "\n",
    "methods=['Riesen + LSAP', 'Neigh + LSAP', 'Riesen + LSAPE', 'Neigh + LSAPE']\n",
    "ged_distances = [ np.zeros((N,N)), np.zeros((N,N)), np.zeros((N,N)), np.zeros((N,N))]\n",
    "\n",
    "times = list()\n",
    "start = time.clock()\n",
    "for i in tqdm(range(0,N)):\n",
    "    for j in range(0,N):\n",
    "        ged_distances[0][i,j] = ged(dataset[i],dataset[j],cf=cf, method='Riesen')[0]\n",
    "times.append(time.clock() - start)\n",
    "\n",
    "\n",
    "start = time.clock()\n",
    "for i in tqdm(range(0,N)):\n",
    "    for j in range(0,N):\n",
    "        ged_distances[1][i,j] = ged(dataset[i],dataset[j],cf=cf, method='Neighboorhood')[0]\n",
    "\n",
    "times.append(time.clock() - start)\n",
    "\n",
    "start = time.clock()\n",
    "for i in tqdm(range(0,N)):\n",
    "    for j in range(0,N):\n",
    "        ged_distances[2][i,j] = ged(dataset[i],dataset[j],cf=cf, method='Riesen',solver=lsap_solverHG)[0]\n",
    "times.append(time.clock() - start)\n",
    "\n",
    "start = time.clock()\n",
    "for i in tqdm(range(0,N)):\n",
    "    for j in range(0,N):\n",
    "        ged_distances[3][i,j] = ged(dataset[i],dataset[j],cf=cf, method='Neighboorhood',solver=lsap_solverHG)[0]\n",
    "times.append(time.clock() - start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "autoscroll": false,
    "ein.tags": "worksheet-0",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " method \t mean \t mean \t time\n",
      " Riesen + LSAP \t 37.79903849025053 \t 35.31207262086058 \t 463.300405 \n",
      " Neigh + LSAP \t 36.2281047508137 \t 33.85869987159963 \t 521.7821730000001 \n",
      " Riesen + LSAPE \t 35.95508973095643 \t 34.10092866314312 \t 319.83455500000014 \n",
      " Neigh + LSAPE \t 34.5005822807489 \t 32.5735614679447 \t 350.48029599999995 \n"
     ]
    }
   ],
   "source": [
    "print(\" method \\t mean \\t mean \\t time\")\n",
    "data = list()\n",
    "for i in range(0,len(ged_distances)):\n",
    "    ged_ = np.minimum(ged_distances[i],ged_distances[i].transpose())\n",
    "    print(\" {} \\t {} \\t {} \\t {} \".format(methods[i], np.mean(ged_distances[i]),np.mean(ged_), times[i]))\n"
   ]
  },
  {
   "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.5.2"
  },
  "name": "py-graph_test.ipynb"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
