{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from scipy import misc\n", "from PIL import Image\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", "print(\"pre shape:\", im.shape)\n", "w, h = im.shape\n", "im = np.array(Image.fromarray(im).resize((h*2, w*2)))\n", "print(\"post shape:\", im.shape)\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(figsize=(6, 6))\n", "ax = fig.add_subplot(111, projection='3d')\n", "ax.set_zlim(0, 800)\n", "ax.plot_surface(xx, yy, im ,rstride=1, cstride=1, cmap=plt.cm.jet,\n", " linewidth=0)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.8" } }, "nbformat": 4, "nbformat_minor": 4 }