|
2 | 2 | This module provides functionalities to visualize the Mapper graph based on |
3 | 3 | pyvis. |
4 | 4 | """ |
| 5 | +import math |
5 | 6 |
|
6 | 7 | from pyvis.network import Network |
7 | 8 |
|
8 | 9 | import matplotlib.pyplot as plt |
9 | 10 | import matplotlib.colors as mcolors |
10 | 11 |
|
| 12 | +import plotly.graph_objects as go |
| 13 | +import plotly.io as pio |
| 14 | + |
11 | 15 | from tdamapper.core import ( |
12 | 16 | aggregate_graph, |
13 | 17 | ) |
14 | 18 |
|
15 | 19 |
|
| 20 | +_EDGE_WIDTH = 0.75 |
| 21 | + |
| 22 | +_EDGE_COLOR = '#777' |
| 23 | + |
| 24 | + |
| 25 | +def __fmt(x, max_len=3): |
| 26 | + fmt = f'.{max_len}g' |
| 27 | + return f'{x:{fmt}}' |
| 28 | + |
| 29 | + |
| 30 | +def _colorbar(width, height, cmap, cmin, cmax, title): |
| 31 | + colorbar_fig = go.Figure() |
| 32 | + colorbar_fig.add_trace(go.Scatter( |
| 33 | + x=[None], y=[None], |
| 34 | + mode='markers', |
| 35 | + marker=dict( |
| 36 | + colorscale=cmap, |
| 37 | + cmin=cmin, |
| 38 | + cmax=cmax, |
| 39 | + colorbar=dict( |
| 40 | + title=title, |
| 41 | + thickness=20, |
| 42 | + len=1.0, |
| 43 | + ), |
| 44 | + ), |
| 45 | + )) |
| 46 | + colorbar_fig.update_layout( |
| 47 | + xaxis=dict(visible=False), |
| 48 | + yaxis=dict(visible=False), |
| 49 | + margin=dict(l=50, r=50, t=0, b=0), |
| 50 | + #width=width, |
| 51 | + height=height, |
| 52 | + plot_bgcolor='rgba(0,0,0,0)', |
| 53 | + paper_bgcolor='rgba(0,0,0,0)', |
| 54 | + ) |
| 55 | + return colorbar_fig |
| 56 | + |
| 57 | + |
| 58 | +def _combine(network, colorbar): |
| 59 | + network_html = network.generate_html() |
| 60 | + colorbar_html = pio.to_html(colorbar, include_plotlyjs='cdn', full_html=False) |
| 61 | + combined_html = f""" |
| 62 | + <!DOCTYPE html> |
| 63 | + <html> |
| 64 | + <head> |
| 65 | + <title>Network with Colorbar</title> |
| 66 | + <style> |
| 67 | + body {{ |
| 68 | + display: flex; |
| 69 | + flex-direction: row; |
| 70 | + margin: 0; |
| 71 | + padding: 0; |
| 72 | + height: 100vh; |
| 73 | + }} |
| 74 | + .network {{ |
| 75 | + flex: 3; |
| 76 | + }} |
| 77 | + </style> |
| 78 | + </head> |
| 79 | + <body> |
| 80 | + <div class="network"> |
| 81 | + {network_html} |
| 82 | + </div> |
| 83 | + <div class="colorbar"> |
| 84 | + {colorbar_html} |
| 85 | + </div> |
| 86 | + </body> |
| 87 | + </html> |
| 88 | + """ |
| 89 | + return combined_html |
| 90 | + |
| 91 | + |
16 | 92 | def plot_pyvis( |
17 | 93 | mapper_plot, |
18 | 94 | notebook, |
@@ -81,10 +157,10 @@ def _compute_net( |
81 | 157 |
|
82 | 158 | def _size(node): |
83 | 159 | if max_node_size == min_node_size: |
84 | | - node_size_norm = 12 |
| 160 | + node_size_norm = 25.0 |
85 | 161 | else: |
86 | 162 | node_size = int(nodes[node]['size']) |
87 | | - node_size_norm = (node_size - min_node_size) / (max_node_size - min_node_size) * 25 |
| 163 | + node_size_norm = 25.0 * math.sqrt(node_size / max_node_size) |
88 | 164 | return int(round(node_size_norm)) |
89 | 165 |
|
90 | 166 | def _color(node): |
@@ -112,20 +188,26 @@ def _blend_color(source, target): |
112 | 188 | node_id = int(node) |
113 | 189 | node_size = _size(node) |
114 | 190 | node_color = _color(node) |
| 191 | + node_stats = __fmt(node_colors[node]) |
| 192 | + node_label = f'color: {node_stats}\nnode: {node_id}\nsize: {node_size}' |
115 | 193 | node_pos = mapper_plot.positions[node] |
116 | 194 | net.add_node( |
117 | 195 | node_id, |
118 | 196 | label=node_id, |
119 | 197 | size=node_size, |
120 | 198 | color=node_color, |
121 | | - x=node_pos[0] * 1000.0, |
122 | | - y=node_pos[1] * 1000.0, |
| 199 | + title=node_label, |
| 200 | + x=node_pos[0] * width, |
| 201 | + y=node_pos[1] * height, |
123 | 202 | ) |
124 | 203 |
|
125 | 204 | for edge in graph.edges: |
126 | 205 | source_id = int(edge[0]) |
127 | 206 | target_id = int(edge[1]) |
128 | | - edge_color = _blend_color(edge[0], edge[1]) |
129 | | - net.add_edge(source_id, target_id, color=edge_color) |
| 207 | + #edge_color = _blend_color(edge[0], edge[1]) |
| 208 | + edge_color = _EDGE_COLOR |
| 209 | + edge_width = _EDGE_WIDTH |
| 210 | + edge_width = 1.5 |
| 211 | + net.add_edge(source_id, target_id, color=edge_color, width=edge_width) |
130 | 212 |
|
131 | 213 | return net |
0 commit comments