-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathvisualization_graph.py
More file actions
249 lines (206 loc) · 8.33 KB
/
visualization_graph.py
File metadata and controls
249 lines (206 loc) · 8.33 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
from __future__ import annotations
import warnings
from collections.abc import Iterable
from typing import Optional
from IPython.display import HTML
from pydantic import BaseModel, Field
from pydantic_extra_types.color import Color, ColorType
from .colors import ColorsType, neo4j_colors
from .node import Node, NodeIdType
from .node_size import RealNumber, verify_radii
from .nvl import NVL
from .options import Layout, Renderer, RenderOptions
from .relationship import Relationship
class VisualizationGraph(BaseModel):
"""
A graph to visualize.
"""
nodes: list[Node] = Field(description="The nodes in the graph")
relationships: list[Relationship] = Field(description="The relationships in the graph")
def render(
self,
layout: Optional[Layout] = None,
renderer: Renderer = Renderer.CANVAS,
width: str = "100%",
height: str = "600px",
pan_position: Optional[tuple[float, float]] = None,
initial_zoom: Optional[float] = None,
min_zoom: float = 0.075,
max_zoom: float = 10,
allow_dynamic_min_zoom: bool = True,
max_allowed_nodes: int = 10_000,
) -> HTML:
"""
Render the graph.
Parameters
----------
layout:
The `Layout` to use.
renderer:
The `Renderer` to use.
width:
The width of the rendered graph.
height:
The height of the rendered graph.
pan_position:
The initial pan position.
initial_zoom:
The initial zoom level.
min_zoom:
The minimum zoom level.
max_zoom:
The maximum zoom level.
allow_dynamic_min_zoom:
Whether to allow dynamic minimum zoom level.
max_allowed_nodes:
The maximum allowed number of nodes to render.
"""
num_nodes = len(self.nodes)
if num_nodes > max_allowed_nodes:
raise ValueError(
f"Too many nodes ({num_nodes}) to render. Maximum allowed nodes is set "
f"to {max_allowed_nodes} for performance reasons. It can be increased by "
"overriding `max_allowed_nodes`, but rendering could then take a long time"
)
Renderer.check(renderer, num_nodes)
render_options = RenderOptions(
layout=layout,
renderer=renderer,
pan_X=pan_position[0] if pan_position is not None else None,
pan_Y=pan_position[1] if pan_position is not None else None,
initial_zoom=initial_zoom,
min_zoom=min_zoom,
max_zoom=max_zoom,
allow_dynamic_min_zoom=allow_dynamic_min_zoom,
)
return NVL().render(
self.nodes,
self.relationships,
render_options,
width,
height,
)
def toggle_nodes_pinned(self, pinned: dict[NodeIdType, bool]) -> None:
"""
Toggle whether nodes should be pinned or not.
Parameters
----------
pinned:
A dictionary mapping from node ID to whether the node should be pinned or not.
"""
for node in self.nodes:
node_pinned = pinned.get(node.id)
if node_pinned is None:
continue
node.pinned = node_pinned
def resize_nodes(
self,
sizes: Optional[dict[NodeIdType, RealNumber]] = None,
node_radius_min_max: Optional[tuple[RealNumber, RealNumber]] = (3, 60),
) -> None:
"""
Resize the nodes in the graph.
Parameters
----------
sizes:
A dictionary mapping from node ID to the new size of the node.
If a node ID is not in the dictionary, the size of the node is not changed.
node_radius_min_max:
Minimum and maximum node size radius as a tuple. To avoid tiny or huge nodes in the visualization, the
node sizes are scaled to fit in the given range. If None, the sizes are used as is.
"""
if sizes is None and node_radius_min_max is None:
raise ValueError("At least one of `sizes` and `node_radius_min_max` must be given")
# Gather and verify all node size values we have to work with
all_sizes = {}
for node in self.nodes:
size = None
if sizes is not None:
size = sizes.get(node.id)
if size is not None:
if not isinstance(size, (int, float)):
raise ValueError(f"Size for node '{node.id}' must be a real number, but was {size}")
if size < 0:
raise ValueError(f"Size for node '{node.id}' must be non-negative, but was {size}")
all_sizes[node.id] = size
if size is None:
if node.size is not None:
all_sizes[node.id] = node.size
if node_radius_min_max is not None:
verify_radii(node_radius_min_max)
unscaled_min_size = min(all_sizes.values())
unscaled_max_size = max(all_sizes.values())
unscaled_size_range = float(unscaled_max_size - unscaled_min_size)
new_min_size, new_max_size = node_radius_min_max
new_size_range = new_max_size - new_min_size
if abs(unscaled_size_range) < 1e-6:
default_node_size = new_min_size + new_size_range / 2.0
final_sizes = {id: default_node_size for id in all_sizes}
else:
final_sizes = {
id: new_min_size + new_size_range * ((nz - unscaled_min_size) / unscaled_size_range)
for id, nz in all_sizes.items()
}
else:
final_sizes = all_sizes
for node in self.nodes:
size = final_sizes.get(node.id)
if size is None:
continue
node.size = size
def color_nodes(self, property: str, colors: Optional[ColorsType] = None, override: bool = False) -> None:
"""
Color the nodes in the graph based on a property.
Parameters
----------
property:
The property of the nodes to use for coloring.
colors:
The colors to use for the nodes. If a dictionary is given, it should map from property to color.
If an iterable is given, the colors are used in order.
The default colors are the Neo4j graph colors.
override:
Whether to override existing colors of the nodes, if they have any.
"""
if colors is None:
colors = neo4j_colors
if isinstance(colors, dict):
self._color_nodes_dict(property, colors, override)
else:
self._color_nodes_iter(property, colors, override)
def _color_nodes_dict(self, property: str, colors: dict[str, ColorType], override: bool) -> None:
for node in self.nodes:
color = colors.get(getattr(node, property))
if color is None:
continue
if node.color is not None and not override:
continue
if not isinstance(color, Color):
node.color = Color(color)
else:
node.color = color
def _color_nodes_iter(self, property: str, colors: Iterable[ColorType], override: bool) -> None:
exhausted_colors = False
prop_to_color = {}
colors_iter = iter(colors)
for node in self.nodes:
prop = getattr(node, property)
if prop not in prop_to_color:
next_color = next(colors_iter, None)
if next_color is None:
exhausted_colors = True
colors_iter = iter(colors)
next_color = next(colors_iter)
prop_to_color[prop] = next_color
color = prop_to_color[prop]
if node.color is not None and not override:
continue
if not isinstance(color, Color):
node.color = Color(color)
else:
node.color = color
if exhausted_colors:
warnings.warn(
f"Ran out of colors for property '{property}'. {len(prop_to_color)} colors were needed, but only "
f"{len(set(prop_to_color.values()))} were given, so reused colors"
)