|
37 | 37 | "metadata": {}, |
38 | 38 | "outputs": [], |
39 | 39 | "source": [ |
40 | | - "import numpy as np\n", |
41 | 40 | "import random\n", |
42 | 41 | "\n", |
43 | 42 | "from ripser import ripser\n", |
44 | 43 | "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", |
50 | 49 | ")\n", |
51 | 50 | "from tadasets import dsphere" |
52 | 51 | ] |
|
100 | 99 | "sph3_pl1 = []\n", |
101 | 100 | "\n", |
102 | 101 | "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", |
106 | 107 | " 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)" |
112 | 115 | ] |
113 | 116 | }, |
114 | 117 | { |
|
138 | 141 | "source": [ |
139 | 142 | "avg_sph2 = average_approx(sph2_pl1)\n", |
140 | 143 | "avg_sph3 = average_approx(sph3_pl1)\n", |
141 | | - "print(avg_sph2, '\\n') \n", |
| 144 | + "print(avg_sph2, \"\\n\")\n", |
142 | 145 | "print(avg_sph3)" |
143 | 146 | ] |
144 | 147 | }, |
|
169 | 172 | "true_diff_pl = avg_sph2_snapped - avg_sph3_snapped\n", |
170 | 173 | "significance = true_diff_pl.sup_norm()\n", |
171 | 174 | "\n", |
172 | | - "print(f'The threshold for significance is {significance}.')" |
| 175 | + "print(f\"The threshold for significance is {significance}.\")" |
173 | 176 | ] |
174 | 177 | }, |
175 | 178 | { |
|
201 | 204 | ], |
202 | 205 | "source": [ |
203 | 206 | "# view figures in notebook\n", |
204 | | - "# %matplotlib inline \n", |
| 207 | + "# %matplotlib inline\n", |
205 | 208 | "\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 | + ")" |
207 | 212 | ] |
208 | 213 | }, |
209 | 214 | { |
|
226 | 231 | } |
227 | 232 | ], |
228 | 233 | "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 | + ")" |
230 | 237 | ] |
231 | 238 | }, |
232 | 239 | { |
|
249 | 256 | } |
250 | 257 | ], |
251 | 258 | "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 | + ")" |
253 | 262 | ] |
254 | 263 | }, |
255 | 264 | { |
|
280 | 289 | } |
281 | 290 | ], |
282 | 291 | "source": [ |
283 | | - "plot_landscape(true_diff_pl,title='Difference of average PLs')" |
| 292 | + "plot_landscape(true_diff_pl, title=\"Difference of average PLs\")" |
284 | 293 | ] |
285 | 294 | }, |
286 | 295 | { |
|
310 | 319 | "sig_count = 0\n", |
311 | 320 | "\n", |
312 | 321 | "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", |
316 | 325 | " A_pl = [comb_pl[i] for i in A_indices]\n", |
317 | 326 | " B_pl = [comb_pl[j] for j in B_indices]\n", |
318 | | - " \n", |
| 327 | + "\n", |
319 | 328 | " A_avg = average_approx(A_pl)\n", |
320 | 329 | " 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", |
323 | 332 | " 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", |
325 | 335 | "\n", |
326 | | - "pval = sig_count/num_perms\n", |
| 336 | + "pval = sig_count / num_perms\n", |
327 | 337 | "\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 | + ")" |
331 | 343 | ] |
332 | 344 | }, |
333 | 345 | { |
|
0 commit comments