-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathplot.py
More file actions
569 lines (527 loc) · 20.2 KB
/
plot.py
File metadata and controls
569 lines (527 loc) · 20.2 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
"""
This module provides functionalities to visualize the Mapper graph.
"""
import igraph as ig
import networkx as nx
import numpy as np
from tdamapper._common import deprecated
from tdamapper.plot_backends.plot_matplotlib import plot_matplotlib
from tdamapper.plot_backends.plot_plotly import plot_plotly, plot_plotly_update
from tdamapper.plot_backends.plot_pyvis import plot_pyvis
class MapperPlot:
"""
Class for generating and visualizing the Mapper graph.
This class creates a metric embedding of the Mapper graph in 2D or 3D and
converts it into a plot.
:param graph: The precomputed Mapper graph to be embedded. This can be
obtained by calling :func:`tdamapper.core.mapper_graph` or
:func:`tdamapper.core.MapperAlgorithm.fit_transform`.
:type graph: :class:`networkx.Graph`, required
:param dim: The dimension of the graph embedding (2 or 3).
:type dim: int
:param iterations: The number of iterations used to construct the graph
embedding. Defaults to 50.
:type iterations: int, optional
:param seed: The random seed used to construct the graph embedding.
Defaults to None.
:type seed: int, optional
:param layout_engine: The engine used to compute the graph layout in the
specified dimensions. Possible values are 'igraph' and 'networkx'.
Defaults to 'igraph'.
:type layout_engine: str, optional
"""
def __init__(
self,
graph,
dim,
iterations=50,
seed=None,
layout_engine="igraph",
):
self.graph = graph
self.dim = dim
self.iterations = iterations
self.seed = seed
self.layout_engine = layout_engine
self.positions = self._compute_pos()
def _compute_pos(self):
if self.layout_engine == "igraph":
return self._compute_pos_ig()
elif self.layout_engine == "networkx":
return self._compute_pos_nx()
else:
raise ValueError(
f"Unknown engine {self.layout_engine}. "
"Only possible values are 'igraph' and 'networkx'"
)
def _compute_pos_nx(self):
return nx.spring_layout(
self.graph,
dim=self.dim,
seed=self.seed,
iterations=self.iterations,
)
def _compute_pos_ig(self):
if self.graph.number_of_nodes() == 0:
return {}
rng = np.random.default_rng(self.seed)
random_pos = rng.random((len(self.graph.nodes()), self.dim))
graph_ig = ig.Graph.from_networkx(self.graph)
if self.dim == 2:
layout = graph_ig.layout_fruchterman_reingold(
niter=self.iterations,
seed=random_pos,
)
pos = {
node: (layout[i][0], layout[i][1])
for i, node in enumerate(self.graph.nodes())
}
elif self.dim == 3:
layout = graph_ig.layout_fruchterman_reingold_3d(
niter=self.iterations,
seed=random_pos,
)
pos = {
node: (layout[i][0], layout[i][1], layout[i][2])
for i, node in enumerate(self.graph.nodes())
}
return pos
def plot_matplotlib(
self,
colors,
node_size=1,
agg=np.nanmean,
title=None,
cmap="jet",
width=512,
height=512,
):
"""
Draw a static plot using Matplotlib.
:param colors: An array of values that determine the color of each
node in the graph, useful for highlighting different features of
the data.
:type colors: array-like of shape (n,) or list-like of size n
:param node_size: A scaling factor for node size. Defaults to 1.
:type node_size: float, optional
:param agg: A function used to aggregate the `colors` array over the
points within a single node. The final color of each node is
obtained by mapping the aggregated value with the colormap `cmap`.
Defaults to `numpy.nanmean`.
:type agg: Callable, optional
:param title: The title to be displayed alongside the figure.
:type title: str, optional
:param cmap: The name of a colormap used to map `colors` data values,
aggregated by `agg`, to actual RGBA colors. Defaults to 'jet'.
:type cmap: str, optional
:param width: The desired width of the figure in pixels. Defaults to
512.
:type width: int, optional
:param height: The desired height of the figure in pixels. Defaults to
512
:type height: int, optional
:return: A static matplotlib figure that can be displayed on screen
and notebooks.
:rtype: :class:`matplotlib.figure.Figure`,
:class:`matplotlib.axes.Axes`
"""
return plot_matplotlib(
self,
colors=colors,
node_size=node_size,
agg=agg,
title=title,
cmap=cmap,
width=width,
height=height,
)
def plot_plotly(
self,
colors,
node_size=1,
agg=np.nanmean,
title=None,
cmap="jet",
width=None,
height=None,
):
"""
Draw an interactive plot using Plotly.
:param colors: An array of values that determine the color of each
node in the graph, useful for highlighting different features of
the data.
:type colors: array-like of shape (n,) or list-like of size n
:param node_size: A scaling factor for node size. When node_size is a
list, the figure will display a slider with the specified values.
Defaults to 1.
:type node_size: int, float or list, optional
:param agg: A function used to aggregate the `colors` array over the
points within a single node. The final color of each node is
obtained by mapping the aggregated value with the colormap `cmap`.
Defaults to `numpy.nanmean`.
:type agg: Callable, optional
:param title: The title for the colormap. When colors has shape (n, m)
and title is a list of string, each item will be used as title for
its corresponding colormap.
:type title: str, list[str], optional
:param cmap: The name of a colormap used to map `colors` data values,
aggregated by `agg`, to actual RGBA colors. Defaults to 'jet'.
:type cmap: str, optional
:param width: The desired width of the figure in pixels.
:type width: int, optional
:param height: The desired height of the figure in pixels.
:type height: int, optional
:return: An interactive Plotly figure that can be displayed on screen
and notebooks. For 3D embeddings, the figure requires a WebGL
context to be shown.
:rtype: :class:`plotly.graph_objects.Figure`
"""
return plot_plotly(
self,
colors=colors,
node_size=node_size,
agg=agg,
title=title,
cmap=cmap,
width=width,
height=height,
)
def plot_plotly_update(
self,
fig,
colors=None,
node_size=None,
agg=None,
title=None,
cmap=None,
width=None,
height=None,
):
"""
Draw an interactive plot using Plotly on a previously rendered figure.
This is typically faster than calling `MapperPlot.plot_plotly` on a
new set of parameters.
:param fig: A Plotly Figure object obtained by calling the method
`MapperPlot.plot_plotly`.
:type fig: :class:`plotly.graph_objects.Figure`
:param colors: An array of values that determine the color of each
node in the graph, useful for highlighting different features of
the data. Defaults to None.
:type colors: array-like of shape (n,) or list-like of size n, optional
:param node_size: A scaling factor for node size. Defaults to None.
:type node_size: float, optional
:param agg: A function used to aggregate the `colors` array over the
points within a single node. The final color of each node is
obtained by mapping the aggregated value with the colormap `cmap`.
Defaults to None.
:type agg: Callable, optional
:param title: The title to be displayed alongside the figure. Defaults
to None.
:type title: str, optional
:param cmap: The name of a colormap used to map `colors` data values,
aggregated by `agg`, to actual RGBA colors. Defaults to None.
:type cmap: str, optional
:param width: The desired width of the figure in pixels. Defaults to
None.
:type width: int, optional
:param height: The desired height of the figure in pixels. Defaults to
None.
:type height: int, optional
:return: An interactive Plotly figure that can be displayed on screen
and notebooks. For 3D embeddings, the figure requires a WebGL
context to be shown.
:rtype: :class:`plotly.graph_objects.Figure`
"""
return plot_plotly_update(
self,
fig,
colors=colors,
node_size=node_size,
agg=agg,
title=title,
cmap=cmap,
width=width,
height=height,
)
def plot_pyvis(
self,
output_file,
colors,
node_size=1,
agg=np.nanmean,
title=None,
cmap="jet",
width=512,
height=512,
):
"""
Draw an interactive HTML plot using PyVis.
:param output_file: The path where the html file is written.
:type output_file: str
:type colors: array-like of shape (n,) or list-like of size n
:param colors: An array of values that determine the color of each
node in the graph, useful for highlighting different features of
the data.
:type colors: array-like of shape (n,) or list-like of size n
:param node_size: A scaling factor for node size. Defaults to 1.
:type node_size: float, optional
:param agg: A function used to aggregate the `colors` array over the
points within a single node. The final color of each node is
obtained by mapping the aggregated value with the colormap `cmap`.
Defaults to `numpy.nanmean`.
:type agg: Callable, optional
:param title: The title to be displayed alongside the figure. Defaults
to None.
:type title: str, optional
:param cmap: The name of a colormap used to map `colors` data values,
aggregated by `agg`, to actual RGBA colors. Defaults to 'jet'.
:type cmap: str, optional
:param width: The desired width of the figure in pixels. Defaults to
512.
:type width: int, optional
:param height: The desired height of the figure in pixels. Defaults to
512.
:type height: int, optional
"""
return plot_pyvis(
self,
output_file=output_file,
colors=colors,
node_size=node_size,
agg=agg,
title=title,
cmap=cmap,
width=width,
height=height,
)
class MapperLayoutInteractive:
"""
**DEPRECATED**: This class is deprecated and will be removed in a future
release. Use :class:`tdamapper.plot.MapperPlot`.
Class for generating and visualizing the Mapper graph.
This class creates a metric embedding of the Mapper graph in 2D or 3D and
converts it into a Plotly figure suitable for interactive display.
:param graph: The precomputed Mapper graph to be embedded. This can be
obtained by calling :func:`tdamapper.core.mapper_graph` or
:func:`tdamapper.core.MapperAlgorithm.fit_transform`.
:type graph: :class:`networkx.Graph`, required
:param dim: The dimension of the graph embedding (2 or 3).
:type dim: int
:param seed: The random seed used to construct the graph embedding.
:type seed: int, optional (default: 42)
:param iterations: The number of iterations used to construct the graph
embedding.
:type iterations: int, optional (default: 50)
:param colors: An array of values that determine the color of each node in
the graph, useful for highlighting different features of the data.
:type colors: array-like of shape (n,) or list-like of size n
:param agg: A function used to aggregate the `colors` array over the
points within a single node. The final color of each node is
obtained by mapping the aggregated value with the colormap `cmap`.
Defaults to `numpy.nanmean`.
:type agg: Callable, optional
:param title: The title to be displayed alongside the figure.
:type title: str, optional
:param width: The desired width of the figure in pixels.
:type width: int, optional (default: 512)
:param height: The desired height of the figure in pixels.
:type height: int, optional (default: 512)
:param cmap: The name of a colormap used to map `color` data values,
aggregated by `agg`, to actual RGBA colors.
:type cmap: str, optional
"""
@deprecated(
"This class is deprecated and will be removed in a future release. "
"Use tdamapper.plot.MapperPlot."
)
def __init__(
self,
graph,
dim,
seed=42,
iterations=50,
colors=None,
agg=np.nanmean,
title=None,
width=512,
height=512,
cmap="jet",
):
self._graph = graph
self._dim = dim
self._iterations = iterations
self._seed = seed
self._mapper_plot = MapperPlot(
graph=self._graph,
dim=self._dim,
iterations=self._iterations,
seed=self._seed,
)
self._colors = colors
self._agg = agg
self._title = title
self._width = width
self._height = height
self._cmap = cmap
self._fig = self._mapper_plot.plot_plotly(
colors=self._colors,
agg=self._agg,
title=self._title,
width=self._width,
height=self._height,
cmap=self._cmap,
)
def update(
self,
seed=None,
iterations=None,
colors=None,
agg=None,
title=None,
width=None,
height=None,
cmap=None,
):
"""
Update the figure.
This method modifies the figure returned by the `plot` function. After
calling this method, the figure will be updated according to the
supplied parameters.
:param seed: The random seed used to construct the graph embedding.
:type seed: int, optional
:param iterations: The number of iterations used to construct the
graph embedding.
:type iterations: int, optional
:param colors: An array of values that determine the color of each
node in the graph, useful for highlighting different features of
the data.
:type colors: array-like of shape (n,) or list-like of size n
:param agg: A function used to aggregate the `colors` data when
multiple points are mapped to a single node. The final color of
each node is obtained by mapping the aggregated value with the
colormap `cmap`. Defaults to None.
:type agg: Callable, optional
:param title: The title to be displayed alongside the figure.
:type title: str, optional
:param width: The desired width of the figure in pixels.
:type width: int, optional
:param height: The desired height of the figure in pixels.
:type height: int, optional
:param cmap: The name of a colormap used to map `color` data values,
aggregated by `agg`, to actual RGBA colors.
:type cmap: str, optional
"""
_update_pos = False
if seed is not None:
self._seed = seed
_update_pos = True
if iterations is not None:
self._iterations = iterations
_update_pos = True
if _update_pos:
self._mapper_plot = MapperPlot(
graph=self._graph,
dim=self._dim,
iterations=self._iterations,
seed=self._seed,
)
self._mapper_plot.plot_plotly_update(
self._fig,
colors=colors,
agg=agg,
title=title,
width=width,
height=height,
cmap=cmap,
)
def plot(self):
"""
Plot the Mapper graph.
:return: An interactive Plotly figure that can be displayed on screen
and notebooks. For 3D embeddings, the figure requires a WebGL
context to be shown.
:rtype: :class:`plotly.graph_objects.Figure`
"""
return self._fig
class MapperLayoutStatic:
"""
**DEPRECATED**: This class is deprecated and will be removed in a future
release. Use :class:`tdamapper.plot.MapperPlot`.
Class for generating and visualizing the Mapper graph.
This class creates a metric embedding of the Mapper graph in 2D and
converts it into a matplotlib figure suitable for static display.
:param graph: The precomputed Mapper graph to be embedded. This can be
obtained by calling :func:`tdamapper.core.mapper_graph` or
:func:`tdamapper.core.MapperAlgorithm.fit_transform`.
:type graph: :class:`networkx.Graph`, required
:param dim: The dimension of the graph embedding (only 2 is supported, for
compatibility).
:type dim: int
:param seed: The random seed used to construct the graph embedding.
:type seed: int, optional (default: 42)
:param iterations: The number of iterations used to construct the graph
embedding.
:type iterations: int, optional (default: 50)
:param colors: An array of values that determine the color of each node in
the graph, useful for highlighting different features of the data.
:type colors: array-like of shape (n,) or list-like of size n
:param agg: A function used to aggregate the `colors` array over the
points within a single node. The final color of each node is
obtained by mapping the aggregated value with the colormap `cmap`.
Defaults to `numpy.nanmean`.
:type agg: Callable, optional
:param title: The title to be displayed alongside the figure.
:type title: str, optional
:param width: The desired width of the figure in pixels.
:type width: int, optional (default: 512)
:param height: The desired height of the figure in pixels.
:type height: int, optional (default: 512)
:param cmap: The name of a colormap used to map `color` data values,
aggregated by `agg`, to actual RGBA colors.
:type cmap: str, optional
"""
@deprecated(
"This class is deprecated and will be removed in a future release. "
"Use tdamapper.plot.MapperPlot."
)
def __init__(
self,
graph,
dim,
seed=42,
iterations=50,
colors=None,
agg=np.nanmean,
title=None,
width=512,
height=512,
cmap="jet",
):
self._colors = colors
self._agg = agg
self._title = title
self._width = width
self._height = height
self._cmap = cmap
self.mapper_plot = MapperPlot(
graph=graph,
dim=dim,
iterations=iterations,
seed=seed,
)
def plot(self):
"""
Plot the Mapper graph.
:return: A static matplotlib figure that can be displayed on screen
and notebooks.
:rtype: :class:`matplotlib.figure.Figure`,
:class:`matplotlib.axes.Axes`
"""
return self.mapper_plot.plot_matplotlib(
colors=self._colors,
agg=self._agg,
title=self._title,
width=self._width,
height=self._height,
cmap=self._cmap,
)