ipynb: Update
[persistence.git] / 2Dpers.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "from scipy import misc\n",
10 "from PIL import Image\n",
11 "import numpy as np\n",
12 "import matplotlib.pyplot as plt\n",
13 "\n",
14 "from mpl_toolkits.mplot3d import Axes3D\n",
15 "\n",
16 "import imagepers"
17 ]
18 },
19 {
20 "cell_type": "code",
21 "execution_count": null,
22 "metadata": {},
23 "outputs": [],
24 "source": [
25 "im = np.loadtxt('paws.numpy')\n",
26 "print(\"pre shape:\", im.shape)\n",
27 "w, h = im.shape\n",
28 "im = np.array(Image.fromarray(im).resize((h*2, w*2)))\n",
29 "print(\"post shape:\", im.shape)\n",
30 "im = im * (255 / np.max(im))"
31 ]
32 },
33 {
34 "cell_type": "code",
35 "execution_count": null,
36 "metadata": {},
37 "outputs": [],
38 "source": [
39 "g0 = imagepers.persistence(im)\n",
40 "g0"
41 ]
42 },
43 {
44 "cell_type": "code",
45 "execution_count": null,
46 "metadata": {},
47 "outputs": [],
48 "source": [
49 "fig = plt.figure()\n",
50 "plt.imshow(im, interpolation=\"nearest\")\n",
51 "plt.colorbar()\n",
52 "xx, yy = np.mgrid[0:im.shape[0], 0:im.shape[1]]\n",
53 "\n",
54 "#fig = plt.figure()\n",
55 "#plt.contourf(xx, yy, im, np.arange(0, 255, 20))"
56 ]
57 },
58 {
59 "cell_type": "code",
60 "execution_count": null,
61 "metadata": {},
62 "outputs": [],
63 "source": [
64 "fig = plt.figure()\n",
65 "ax = fig.add_subplot(111)\n",
66 "ax.set_title(\"Peristence diagram\")\n",
67 "ax.plot([0,255], [0,255], '-', c='grey')\n",
68 "for i, homclass in enumerate(g0):\n",
69 " p_birth, bl, pers, p_death = homclass\n",
70 " if pers <= 1.0:\n",
71 " continue\n",
72 " \n",
73 " x, y = bl, bl-pers\n",
74 " ax.plot([x], [y], '.', c='b')\n",
75 " ax.text(x, y+2, str(i+1), color='b')\n",
76 "ax.set_xlabel(\"Birth level\")\n",
77 "ax.set_ylabel(\"Death level\")\n",
78 "ax.set_xlim((-5,260))\n",
79 "ax.set_ylim((-5,260))"
80 ]
81 },
82 {
83 "cell_type": "code",
84 "execution_count": null,
85 "metadata": {},
86 "outputs": [],
87 "source": [
88 "fig = plt.figure()\n",
89 "ax = fig.add_subplot(111)\n",
90 "ax.set_title(\"Loci of births\")\n",
91 "for i, homclass in enumerate(g0):\n",
92 " p_birth, bl, pers, p_death = homclass\n",
93 " if pers <= 20.0:\n",
94 " continue\n",
95 " y, x = p_birth\n",
96 " ax.plot([x], [y], '.', c='b')\n",
97 " ax.text(x, y+0.25, str(i+1), color='b')\n",
98 " \n",
99 "ax.set_xlim((0,im.shape[1]))\n",
100 "ax.set_ylim((0,im.shape[0]))\n",
101 "plt.gca().invert_yaxis()"
102 ]
103 },
104 {
105 "cell_type": "code",
106 "execution_count": null,
107 "metadata": {},
108 "outputs": [],
109 "source": [
110 "fig = plt.figure(figsize=(6, 6))\n",
111 "ax = fig.add_subplot(111, projection='3d')\n",
112 "ax.set_zlim(0, 800)\n",
113 "ax.plot_surface(xx, yy, im ,rstride=1, cstride=1, cmap=plt.cm.jet,\n",
114 " linewidth=0)\n",
115 "plt.show()"
116 ]
117 },
118 {
119 "cell_type": "code",
120 "execution_count": null,
121 "metadata": {},
122 "outputs": [],
123 "source": []
124 },
125 {
126 "cell_type": "code",
127 "execution_count": null,
128 "metadata": {},
129 "outputs": [],
130 "source": []
131 }
132 ],
133 "metadata": {
134 "kernelspec": {
135 "display_name": "Python 3 (ipykernel)",
136 "language": "python",
137 "name": "python3"
138 },
139 "language_info": {
140 "codemirror_mode": {
141 "name": "ipython",
142 "version": 3
143 },
144 "file_extension": ".py",
145 "mimetype": "text/x-python",
146 "name": "python",
147 "nbconvert_exporter": "python",
148 "pygments_lexer": "ipython3",
149 "version": "3.11.8"
150 }
151 },
152 "nbformat": 4,
153 "nbformat_minor": 4
154 }