Add demo ipynb
[persistence.git] / 2Dpers.ipynb
diff --git a/2Dpers.ipynb b/2Dpers.ipynb
new file mode 100644 (file)
index 0000000..898a094
--- /dev/null
@@ -0,0 +1,152 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from scipy import misc\n",
+    "import numpy as np\n",
+    "import matplotlib.pyplot as plt\n",
+    "\n",
+    "from mpl_toolkits.mplot3d import Axes3D\n",
+    "\n",
+    "import imagepers"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "im = np.loadtxt('paws.numpy')\n",
+    "im = misc.imresize(im, 2.0)\n",
+    "im = im * (255 / np.max(im))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "g0 = imagepers.persistence(im)\n",
+    "g0"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "fig = plt.figure()\n",
+    "plt.imshow(im, interpolation=\"nearest\")\n",
+    "plt.colorbar()\n",
+    "xx, yy = np.mgrid[0:im.shape[0], 0:im.shape[1]]\n",
+    "\n",
+    "#fig = plt.figure()\n",
+    "#plt.contourf(xx, yy, im, np.arange(0, 255, 20))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "fig = plt.figure()\n",
+    "ax = fig.add_subplot(111)\n",
+    "ax.set_title(\"Peristence diagram\")\n",
+    "ax.plot([0,255], [0,255], '-', c='grey')\n",
+    "for i, homclass in enumerate(g0):\n",
+    "    p_birth, bl, pers, p_death = homclass\n",
+    "    if pers <= 1.0:\n",
+    "        continue\n",
+    "    \n",
+    "    x, y = bl, bl-pers\n",
+    "    ax.plot([x], [y], '.', c='b')\n",
+    "    ax.text(x, y+2, str(i+1), color='b')\n",
+    "ax.set_xlabel(\"Birth level\")\n",
+    "ax.set_ylabel(\"Death level\")\n",
+    "ax.set_xlim((-5,260))\n",
+    "ax.set_ylim((-5,260))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "fig = plt.figure()\n",
+    "ax = fig.add_subplot(111)\n",
+    "ax.set_title(\"Loci of births\")\n",
+    "for i, homclass in enumerate(g0):\n",
+    "    p_birth, bl, pers, p_death = homclass\n",
+    "    if pers <= 20.0:\n",
+    "        continue\n",
+    "    y, x = p_birth\n",
+    "    ax.plot([x], [y], '.', c='b')\n",
+    "    ax.text(x, y+0.25, str(i+1), color='b')\n",
+    "    \n",
+    "ax.set_xlim((0,im.shape[1]))\n",
+    "ax.set_ylim((0,im.shape[0]))\n",
+    "plt.gca().invert_yaxis()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "fig = plt.figure()\n",
+    "ax = fig.gca(projection='3d')\n",
+    "ax.plot_surface(xx, yy, im ,rstride=1, cstride=1, cmap=plt.cm.jet,\n",
+    "        linewidth=0)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "plt.show()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python3.5",
+   "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.5"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 1
+}