-
Notifications
You must be signed in to change notification settings - Fork 2
/
plotting.py
394 lines (342 loc) · 14.2 KB
/
plotting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""Plot functions """
import numpy as np
import plotly.graph_objs as gobj
from giotto.diagrams._utils import _subdiagrams
def plot_point_cloud(point_cloud, dimension=None):
"""Plot the first 2 or 3 coordinates of the point cloud.
This function will not work on 1-dimensional arrays.
Parameters
----------
point_cloud : ndarray, shape (n_samples, n_dimensions)
Data points to be represented in a 2D or 3D scatter plot. Only the
first 2 or 3 dimensions will be considered for plotting.
dimension : int or None, default : ``None``
This parameter sets the dimension of the resulting plot. If ``None``,
the dimension will be chosen between 2 and 3 depending on
``n_dimensions`` see Input).
"""
if dimension is None:
dimension = np.min((3, point_cloud.shape[1]))
# Check consistency between point_cloud and dimension
if point_cloud.shape[1] < dimension:
raise ValueError("Not enough dimensions available in the input point"
"cloud.")
if dimension == 2:
layout = {
"title": "Point Cloud",
"width": 800,
"height": 800,
"xaxis1": {
"title": "First coordinate",
"side": "bottom",
"type": "linear",
"ticks": "outside",
"anchor": "x1",
"showline": True,
"zeroline": True,
"showexponent": "all",
"exponentformat": "e"
},
"yaxis1": {
"title": "Second coordinate",
"side": "left",
"type": "linear",
"ticks": "outside",
"anchor": "y1",
"showline": True,
"zeroline": True,
"showexponent": "all",
"exponentformat": "e"
},
"plot_bgcolor": "white"
}
fig = gobj.Figure(layout=layout)
fig.update_xaxes(zeroline=True, linewidth=1, linecolor='black',
mirror=False)
fig.update_yaxes(zeroline=True, linewidth=1, linecolor='black',
mirror=False)
fig.add_trace(gobj.Scatter(x=point_cloud[:, 0],
y=point_cloud[:, 1],
mode='markers',
marker=dict(size=4,
color=list(range(
point_cloud.shape[0])),
colorscale='Viridis',
opacity=0.8)))
fig.show()
elif dimension == 3:
scene = {
"xaxis": {
"title": "First coordinate",
"type": "linear",
"showexponent": "all",
"exponentformat": "e"
},
"yaxis": {
"title": "Second coordinate",
"type": "linear",
"showexponent": "all",
"exponentformat": "e"
},
"zaxis": {
"title": "Third coordinate",
"type": "linear",
"showexponent": "all",
"exponentformat": "e"
}
}
fig = gobj.Figure()
fig.update_layout(scene=scene, title="Point cloud")
fig.add_trace(gobj.Scatter3d(x=point_cloud[:, 0],
y=point_cloud[:, 1],
z=point_cloud[:, 2],
mode='markers',
marker=dict(size=4,
color=list(range(
point_cloud.shape[0])),
colorscale='Viridis',
opacity=0.8)))
fig.show()
else:
raise ValueError("The value of the dimension is different from 2 or 3")
def plot_diagram(diagram, homology_dimensions=None):
"""Plot a single persistence diagram.
Parameters
----------
diagram : ndarray, shape (n_points, 3)
The persistence diagram to plot, where the third dimension along axis 1
contains homology dimensions, and the other two contain (birth, death)
pairs to be used as coordinates in the two-dimensional plot.
homology_dimensions : list of int or None, default: ``None``
Homology dimensions which will appear on the plot. If ``None``, all
homology dimensions which appear in `diagram` will be plotted.
"""
if homology_dimensions is None:
homology_dimensions = np.unique(diagram[:, 2])
maximum_persistence = np.where(np.isinf(diagram), -np.inf, diagram).max()
layout = {
"title": "Persistence diagram",
"width": 500,
"height": 500,
"xaxis1": {
"title": "Birth",
"side": "bottom",
"type": "linear",
"range": [0, 1.1 * maximum_persistence],
"ticks": "outside",
"anchor": "y1",
"showline": True,
"zeroline": True,
"showexponent": "all",
"exponentformat": "e"
},
"yaxis1": {
"title": "Death",
"side": "left",
"type": "linear",
"range": [0, 1.1 * maximum_persistence],
"ticks": "outside",
"anchor": "x1",
"showline": True,
"zeroline": True,
"showexponent": "all",
"exponentformat": "e"
},
"plot_bgcolor": "white"
}
fig = gobj.Figure(layout=layout)
fig.update_xaxes(zeroline=True, linewidth=1, linecolor='black',
mirror=False)
fig.update_yaxes(zeroline=True, linewidth=1, linecolor='black',
mirror=False)
fig.add_trace(gobj.Scatter(x=np.array([-100 * maximum_persistence,
100 * maximum_persistence]),
y=np.array([-100 * maximum_persistence,
100 * maximum_persistence]),
mode='lines',
line=dict(dash='dash', width=1, color='black'),
showlegend=False, hoverinfo='none'))
for i, dimension in enumerate(homology_dimensions):
name = f"H{int(dimension)}"
subdiagram = _subdiagrams(np.asarray([diagram]), [dimension],
remove_dim=True)[0]
diff = (subdiagram[:, 1] != subdiagram[:, 0])
subdiagram = subdiagram[diff]
fig.add_trace(gobj.Scatter(x=subdiagram[:, 0], y=subdiagram[:, 1],
mode='markers', name=name))
fig.show()
def plot_landscapes(landscapes, homology_dimensions=None, samplings=None):
"""Plot landscapes by homology dimension.
Parameters
----------
landscapes : ndarray, shape (n_homology_dimension, n_layers, n_values)
Collection of ``n_homology_dimension`` discretised persistence
landscapes. Each landscape contains ``n_layers`` layers. Entry i along
axis 0 should be the persistence landscape in homology dimension i.
homology_dimensions : list of int or None, default: ``None``
Homology dimensions for which the Betti curves should be plotted.
If ``None``, all available dimensions will be used.
samplings : ndarray, shape (n_homology_dimension, n_layers, n_values), \
default: ``None``
For each homology dimension, (filtration parameter) values to be used
on the x-axis against the corresponding values in `landscapes` on
the y-axis. If ``None``, the samplings will start at 0 with step 1.
"""
if homology_dimensions is None:
homology_dimensions = np.arange(0, landscapes.shape[0])
if samplings is None:
samplings = np.arange(0, landscapes.shape[2])
layout = {
"xaxis1": {
"side": "bottom",
"type": "linear",
"ticks": "outside",
"anchor": "y1",
"showline": True,
"zeroline": True,
"showexponent": "all",
"exponentformat": "e"
},
"yaxis1": {
"side": "left",
"type": "linear",
"ticks": "outside",
"anchor": "x1",
"showline": True,
"zeroline": True,
"showexponent": "all",
"exponentformat": "e"
},
"plot_bgcolor": "white"
}
for i, dimension in enumerate(homology_dimensions):
layout_dim = layout.copy()
layout_dim['title'] = f"Persistence landscape for homology dimension" \
f"{int(dimension)}"
fig = gobj.Figure(layout=layout_dim)
fig.update_xaxes(zeroline=True, linewidth=1, linecolor='black',
mirror=False)
fig.update_yaxes(zeroline=True, linewidth=1, linecolor='black',
mirror=False)
n_layers = landscapes.shape[1]
for layer in range(n_layers):
fig.add_trace(gobj.Scatter(x=samplings,
y=landscapes[i, layer, :],
mode='lines', showlegend=False,
hoverinfo='none',
name=f"layer {layer + 1}"))
fig.show()
def plot_betti_curves(betti_curves, homology_dimensions=None, samplings=None):
"""Plot the Betti curves of a single persistence diagram by homology
dimension.
Parameters
----------
betti_curves : ndarray, shape (n_homology_dimension, n_values)
Collection of ``n_homology_dimension`` discretised Betti curves.
Entry i along axis 0 should be the Betti curve in homology dimension i.
homology_dimensions : list of int or None, default: ``None``
Homology dimensions for which the Betti curves should be plotted.
If ``None``, all available dimensions will be used.
samplings : ndarray, shape (n_homology_dimension, n_values), \
default: ``None``
For each homology dimension, (filtration parameter) values to be used
on the x-axis against the corresponding values in `betti_curves` on
the y-axis. If ``None``, the samplings will start at 0 with step 1.
"""
if homology_dimensions is None:
homology_dimensions = np.arange(0, betti_curves.shape[0])
if samplings is None:
samplings = np.arange(0, betti_curves.shape[1])
layout = {
"title": "Betti curves",
"xaxis1": {
"title": "Epsilon",
"side": "bottom",
"type": "linear",
"ticks": "outside",
"anchor": "x1",
"showline": True,
"zeroline": True,
"showexponent": "all",
"exponentformat": "e"
},
"yaxis1": {
"title": "Betti number",
"side": "left",
"type": "linear",
"ticks": "outside",
"anchor": "y1",
"showline": True,
"zeroline": True,
"showexponent": "all",
"exponentformat": "e"
},
"plot_bgcolor": "white"
}
fig = gobj.Figure(layout=layout)
fig.update_xaxes(zeroline=True, linewidth=1, linecolor='black',
mirror=False)
fig.update_yaxes(zeroline=True, linewidth=1, linecolor='black',
mirror=False)
for i, dimension in enumerate(homology_dimensions):
fig.add_trace(gobj.Scatter(x=samplings,
y=betti_curves[i, :],
mode='lines', showlegend=False,
hoverinfo='none'))
return fig
def plot_betti_surfaces(betti_curves, samplings=None,
homology_dimensions=None):
"""Plots the Betti surfaces (Betti numbers against time and filtration
parameter) by homology dimension.
Parameters
----------
betti_curves : ndarray, shape (n_samples, n_homology_dimensions, \
n_values)
``n_samples`` collections of discretised Betti curves. There are
``n_homology_dimension`` curves in each collection. Index i along axis
1 should yield all Betti curves in homology dimension i.
homology_dimensions : list of int or None, default: ``None``
Homology dimensions for which the Betti surfaces should be plotted.
If ``None``, all available dimensions will be used.
samplings : ndarray, shape (n_homology_dimension, n_values), \
default: ``None``
For each homology dimension, (filtration parameter) values to be used
on the x-axis against the corresponding values in `betti_curves` on the
y-axis. If ``None``, the samplings will start at 0 with step 1.
"""
if homology_dimensions is None:
homology_dimensions = np.arange(0, betti_curves.shape[1])
if samplings is None:
samplings = np.arange(0, betti_curves.shape[2])
scene = {
"xaxis": {
"title": "Epsilon",
"type": "linear",
"showexponent": "all",
"exponentformat": "e"
},
"yaxis": {
"title": "Time",
"type": "linear",
"showexponent": "all",
"exponentformat": "e"
},
"zaxis": {
"title": "Betti number",
"type": "linear",
"showexponent": "all",
"exponentformat": "e"
}
}
if betti_curves.shape[0] == 1:
plot_betti_curves(betti_curves[0], samplings, homology_dimensions)
else:
for i, dimension in enumerate(homology_dimensions):
fig = gobj.Figure()
fig.update_layout(scene=scene, title=f"Betti surface for homology "
f"dimension {int(dimension)}")
fig.add_trace(gobj.Surface(x=samplings,
y=np.arange(betti_curves.shape[0]),
z=betti_curves[:, i, :],
connectgaps=True, hoverinfo='none'))
fig.show()