Skip to content

Commit 29b448e

Browse files
committed
Run ruff format, lint
1 parent 5543ec1 commit 29b448e

31 files changed

Lines changed: 807 additions & 568 deletions

docs/notebooks/Classification with persistence images.ipynb

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"import matplotlib.pyplot as plt\n",
2929
"\n",
3030
"from ripser import Rips\n",
31-
"from persim import PersImage\n",
3231
"from persim import PersistenceImager"
3332
]
3433
},
@@ -51,17 +50,23 @@
5150
"N_per_class = int(N / 2)\n",
5251
"N_in_class = 400\n",
5352
"\n",
53+
"\n",
5454
"def noise(N, scale):\n",
5555
" return scale * np.random.random((N, 2))\n",
5656
"\n",
57+
"\n",
5758
"def circle(N, scale, offset):\n",
58-
" return offset + scale * datasets.make_circles(n_samples=N, factor=0.4, noise=0.05)[0]\n",
59-
" \n",
59+
" return (\n",
60+
" offset + scale * datasets.make_circles(n_samples=N, factor=0.4, noise=0.05)[0]\n",
61+
" )\n",
62+
"\n",
63+
"\n",
6064
"just_noise = [noise(N_in_class, 150) for _ in range(N_per_class)]\n",
6165
"\n",
6266
"half = int(N_in_class / 2)\n",
63-
"with_circle = [np.concatenate((circle(half, 50, 70), noise(half, 150)))\n",
64-
" for _ in range(N_per_class)]\n",
67+
"with_circle = [\n",
68+
" np.concatenate((circle(half, 50, 70), noise(half, 150))) for _ in range(N_per_class)\n",
69+
"]\n",
6570
"\n",
6671
"datas = []\n",
6772
"datas.extend(just_noise)\n",
@@ -93,17 +98,17 @@
9398
"source": [
9499
"# Visualize the data\n",
95100
"fig, axs = plt.subplots(1, 2)\n",
96-
"fig.set_size_inches(10,5)\n",
101+
"fig.set_size_inches(10, 5)\n",
97102
"\n",
98-
"xs, ys = just_noise[0][:,0], just_noise[0][:,1]\n",
103+
"xs, ys = just_noise[0][:, 0], just_noise[0][:, 1]\n",
99104
"axs[0].scatter(xs, ys)\n",
100105
"axs[0].set_title(\"Example noise dataset\")\n",
101-
"axs[0].set_aspect('equal', 'box')\n",
106+
"axs[0].set_aspect(\"equal\", \"box\")\n",
102107
"\n",
103-
"xs_, ys_ = with_circle[0][:,0], with_circle[0][:,1]\n",
108+
"xs_, ys_ = with_circle[0][:, 0], with_circle[0][:, 1]\n",
104109
"axs[1].scatter(xs_, ys_)\n",
105110
"axs[1].set_title(\"Example noise with circle dataset\")\n",
106-
"axs[1].set_aspect('equal', 'box')\n",
111+
"axs[1].set_aspect(\"equal\", \"box\")\n",
107112
"\n",
108113
"fig.tight_layout()"
109114
]
@@ -155,7 +160,7 @@
155160
}
156161
],
157162
"source": [
158-
"plt.figure(figsize=(12,6))\n",
163+
"plt.figure(figsize=(12, 6))\n",
159164
"plt.subplot(121)\n",
160165
"\n",
161166
"rips.plot(diagrams_h1[0], show=False)\n",
@@ -234,16 +239,16 @@
234239
}
235240
],
236241
"source": [
237-
"plt.figure(figsize=(15,7.5))\n",
242+
"plt.figure(figsize=(15, 7.5))\n",
238243
"\n",
239244
"for i in range(4):\n",
240-
" ax = plt.subplot(240+i+1)\n",
245+
" ax = plt.subplot(240 + i + 1)\n",
241246
" pimgr.plot_image(imgs[i], ax)\n",
242247
" plt.title(\"PI of $H_1$ for noise\")\n",
243248
"\n",
244249
"for i in range(4):\n",
245-
" ax = plt.subplot(240+i+5)\n",
246-
" pimgr.plot_image(imgs[-(i+1)], ax)\n",
250+
" ax = plt.subplot(240 + i + 5)\n",
251+
" pimgr.plot_image(imgs[-(i + 1)], ax)\n",
247252
" plt.title(\"PI of $H_1$ for circle w/ noise\")"
248253
]
249254
},
@@ -260,7 +265,9 @@
260265
"metadata": {},
261266
"outputs": [],
262267
"source": [
263-
"X_train, X_test, y_train, y_test = train_test_split(imgs_array, labels, test_size=0.40, random_state=42)"
268+
"X_train, X_test, y_train, y_test = train_test_split(\n",
269+
" imgs_array, labels, test_size=0.40, random_state=42\n",
270+
")"
264271
]
265272
},
266273
{

docs/notebooks/Differentiation with Persistence Landscapes.ipynb

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@
3737
"metadata": {},
3838
"outputs": [],
3939
"source": [
40-
"import numpy as np\n",
4140
"import random\n",
4241
"\n",
4342
"from ripser import ripser\n",
4443
"from persim.landscapes import (\n",
45-
" PersLandscapeApprox, \n",
46-
" average_approx, \n",
47-
" snap_pl, \n",
48-
" plot_landscape, \n",
49-
" plot_landscape_simple\n",
44+
" PersLandscapeApprox,\n",
45+
" average_approx,\n",
46+
" snap_pl,\n",
47+
" plot_landscape,\n",
48+
" plot_landscape_simple,\n",
5049
")\n",
5150
"from tadasets import dsphere"
5251
]
@@ -100,15 +99,19 @@
10099
"sph3_pl1 = []\n",
101100
"\n",
102101
"for _ in range(num_runs):\n",
103-
" sph2 = dsphere(n=num_pts, d=2)/1.3333 # sample points, scaling appropriately\n",
104-
" sph2_dgm = ripser(sph2, maxdim=2)['dgms'] # compute PH0, PH1, PH2\n",
105-
" sph2_pl = PersLandscapeApprox(dgms=sph2_dgm, hom_deg=1) # compute persistence landscape\n",
102+
" sph2 = dsphere(n=num_pts, d=2) / 1.3333 # sample points, scaling appropriately\n",
103+
" sph2_dgm = ripser(sph2, maxdim=2)[\"dgms\"] # compute PH0, PH1, PH2\n",
104+
" sph2_pl = PersLandscapeApprox(\n",
105+
" dgms=sph2_dgm, hom_deg=1\n",
106+
" ) # compute persistence landscape\n",
106107
" sph2_pl1.append(sph2_pl)\n",
107-
" \n",
108-
" sph3 = dsphere(n=num_pts, d=3)/1.3581 # sample points, scaling appropriately\n",
109-
" sph3_dgm = ripser(sph3, maxdim=2)['dgms'] # compute PH0, PH1, PH2\n",
110-
" sph3_pl = PersLandscapeApprox(dgms=sph3_dgm, hom_deg=1) # compute persistence landscape\n",
111-
" sph3_pl1.append(sph3_pl)\n"
108+
"\n",
109+
" sph3 = dsphere(n=num_pts, d=3) / 1.3581 # sample points, scaling appropriately\n",
110+
" sph3_dgm = ripser(sph3, maxdim=2)[\"dgms\"] # compute PH0, PH1, PH2\n",
111+
" sph3_pl = PersLandscapeApprox(\n",
112+
" dgms=sph3_dgm, hom_deg=1\n",
113+
" ) # compute persistence landscape\n",
114+
" sph3_pl1.append(sph3_pl)"
112115
]
113116
},
114117
{
@@ -138,7 +141,7 @@
138141
"source": [
139142
"avg_sph2 = average_approx(sph2_pl1)\n",
140143
"avg_sph3 = average_approx(sph3_pl1)\n",
141-
"print(avg_sph2, '\\n') \n",
144+
"print(avg_sph2, \"\\n\")\n",
142145
"print(avg_sph3)"
143146
]
144147
},
@@ -169,7 +172,7 @@
169172
"true_diff_pl = avg_sph2_snapped - avg_sph3_snapped\n",
170173
"significance = true_diff_pl.sup_norm()\n",
171174
"\n",
172-
"print(f'The threshold for significance is {significance}.')"
175+
"print(f\"The threshold for significance is {significance}.\")"
173176
]
174177
},
175178
{
@@ -201,9 +204,11 @@
201204
],
202205
"source": [
203206
"# view figures in notebook\n",
204-
"# %matplotlib inline \n",
207+
"# %matplotlib inline\n",
205208
"\n",
206-
"plot_landscape_simple(avg_sph2_snapped,title='Average $PL_1$ for $S^2$.',depth_range=range(13))"
209+
"plot_landscape_simple(\n",
210+
" avg_sph2_snapped, title=\"Average $PL_1$ for $S^2$.\", depth_range=range(13)\n",
211+
")"
207212
]
208213
},
209214
{
@@ -226,7 +231,9 @@
226231
}
227232
],
228233
"source": [
229-
"plot_landscape_simple(avg_sph3_snapped,title='Average $PL_1$ for $S^2$.',depth_range=range(13))"
234+
"plot_landscape_simple(\n",
235+
" avg_sph3_snapped, title=\"Average $PL_1$ for $S^2$.\", depth_range=range(13)\n",
236+
")"
230237
]
231238
},
232239
{
@@ -249,7 +256,9 @@
249256
}
250257
],
251258
"source": [
252-
"plot_landscape_simple(true_diff_pl,title='Difference of average PLs',depth_range=range(13))"
259+
"plot_landscape_simple(\n",
260+
" true_diff_pl, title=\"Difference of average PLs\", depth_range=range(13)\n",
261+
")"
253262
]
254263
},
255264
{
@@ -280,7 +289,7 @@
280289
}
281290
],
282291
"source": [
283-
"plot_landscape(true_diff_pl,title='Difference of average PLs')"
292+
"plot_landscape(true_diff_pl, title=\"Difference of average PLs\")"
284293
]
285294
},
286295
{
@@ -310,24 +319,27 @@
310319
"sig_count = 0\n",
311320
"\n",
312321
"for shuffle in range(num_perms):\n",
313-
" A_indices = random.sample(range(2*num_runs),num_runs)\n",
314-
" B_indices = [_ for _ in range(2*num_runs) if _ not in A_indices]\n",
315-
" \n",
322+
" A_indices = random.sample(range(2 * num_runs), num_runs)\n",
323+
" B_indices = [_ for _ in range(2 * num_runs) if _ not in A_indices]\n",
324+
"\n",
316325
" A_pl = [comb_pl[i] for i in A_indices]\n",
317326
" B_pl = [comb_pl[j] for j in B_indices]\n",
318-
" \n",
327+
"\n",
319328
" A_avg = average_approx(A_pl)\n",
320329
" B_avg = average_approx(B_pl)\n",
321-
" [A_avg_sn, B_avg_sn] = snap_pl([A_avg,B_avg])\n",
322-
" \n",
330+
" [A_avg_sn, B_avg_sn] = snap_pl([A_avg, B_avg])\n",
331+
"\n",
323332
" shuff_diff = A_avg_sn - B_avg_sn\n",
324-
" if (shuff_diff.sup_norm() >= significance): sig_count += 1\n",
333+
" if shuff_diff.sup_norm() >= significance:\n",
334+
" sig_count += 1\n",
325335
"\n",
326-
"pval = sig_count/num_perms\n",
336+
"pval = sig_count / num_perms\n",
327337
"\n",
328-
"print(f'There were {sig_count} shuffles out of {num_perms} that',\n",
329-
" 'were more significant than the true labelling. Thus, the',\n",
330-
" f'p-value is {pval}.')"
338+
"print(\n",
339+
" f\"There were {sig_count} shuffles out of {num_perms} that\",\n",
340+
" \"were more significant than the true labelling. Thus, the\",\n",
341+
" f\"p-value is {pval}.\",\n",
342+
")"
331343
]
332344
},
333345
{

0 commit comments

Comments
 (0)