From: Stefan Huber Date: Wed, 23 May 2018 17:13:18 +0000 (+0200) Subject: Add demo ipynb X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=b210d5622c539c64875465bf873e794db9c50cfd;p=persistence.git Add demo ipynb --- diff --git a/2Dpers.ipynb b/2Dpers.ipynb new file mode 100644 index 0000000..898a094 --- /dev/null +++ b/2Dpers.ipynb @@ -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 +} diff --git a/paws.numpy b/paws.numpy new file mode 100644 index 0000000..98eb2be --- /dev/null +++ b/paws.numpy @@ -0,0 +1,98 @@ +# Array shape: (4, 11, 14) + +0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.40 0.40 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 0.00 0.70 1.40 2.20 1.80 0.70 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 1.10 4.00 6.50 4.30 1.80 0.70 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 1.40 6.10 7.20 3.20 0.70 0.40 1.10 1.40 0.40 + +0.00 0.00 0.70 1.40 1.10 0.70 2.20 2.50 1.10 0.40 1.40 6.50 7.20 1.40 + +0.00 0.40 2.90 7.90 5.40 1.40 0.70 0.40 1.10 1.80 2.90 7.90 8.30 1.80 + +0.00 0.00 1.80 5.40 3.20 1.80 4.30 3.60 2.90 6.10 7.60 2.50 1.80 0.40 + +0.00 0.00 0.40 0.70 0.70 2.50 9.00 7.90 3.60 7.90 9.00 2.20 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 1.10 4.70 4.00 1.40 2.90 3.60 0.70 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 0.40 0.70 0.70 0.40 0.40 0.40 0.00 0.00 0.00 + +# New slice + +0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.40 1.10 1.10 0.70 0.40 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.40 2.20 3.60 3.20 2.20 1.10 0.40 0.00 0.00 0.00 + +0.00 0.40 1.10 0.70 0.40 1.10 2.90 5.40 5.00 2.20 0.40 0.00 0.00 0.00 + +0.00 1.10 5.80 4.30 1.10 0.40 1.10 2.50 2.20 1.10 1.10 1.80 0.70 0.00 + +0.00 1.10 5.00 4.30 1.80 1.10 0.40 0.40 0.70 1.80 6.10 8.60 2.90 0.40 + +0.00 0.00 1.10 1.80 5.40 5.80 2.50 3.20 5.00 3.20 4.70 5.40 1.40 0.00 + +0.00 0.00 0.00 1.40 7.20 7.60 4.00 8.60 10.10 3.20 1.10 0.70 0.40 0.00 + +0.00 0.00 0.00 0.40 2.20 2.20 1.80 4.70 6.50 1.80 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 0.40 0.40 0.70 1.10 0.40 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 + +# New slice + +0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.40 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 1.10 1.80 1.80 0.70 0.00 0.00 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.40 2.90 4.70 2.90 0.70 0.00 0.40 0.40 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 1.10 2.90 2.90 0.70 0.70 2.20 2.50 0.70 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.40 0.40 0.40 1.40 6.10 8.30 2.20 0.00 0.00 0.00 + +0.00 0.40 0.70 0.40 0.40 0.40 0.40 1.40 3.20 2.50 0.70 0.00 0.00 0.00 + +0.00 0.40 1.10 0.70 1.40 2.50 2.50 4.70 7.90 3.20 0.40 0.00 0.00 0.00 + +0.00 0.00 0.40 0.40 2.20 5.80 4.00 2.90 8.60 3.60 0.40 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.70 1.80 1.40 0.70 1.80 0.70 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 0.40 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 + +# New slice + +0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 0.40 0.40 0.40 0.00 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.40 1.40 1.40 1.80 0.70 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.40 1.40 4.00 5.40 2.20 0.40 0.00 0.00 0.00 0.00 + +0.00 0.00 0.70 1.10 0.40 1.10 3.20 3.60 1.10 0.00 0.00 0.00 0.00 0.00 + +0.00 0.40 2.90 3.60 1.10 0.40 0.70 0.70 0.40 0.40 0.00 0.00 0.00 0.00 + +0.00 0.40 2.50 3.20 1.80 0.70 0.40 0.40 0.40 1.40 0.70 0.00 0.00 0.00 + +0.00 0.00 0.70 3.60 5.80 2.90 1.40 2.20 1.40 1.80 1.10 0.00 0.00 0.00 + +0.00 0.00 1.10 5.00 6.80 3.20 4.00 6.10 1.80 0.40 0.40 0.00 0.00 0.00 + +0.00 0.00 0.40 1.10 1.80 1.80 4.30 3.20 0.70 0.00 0.00 0.00 0.00 0.00 + +0.00 0.00 0.00 0.00 0.00 0.40 0.70 0.40 0.00 0.00 0.00 0.00 0.00 0.00 + +# New slice +