neo4j-viz 0.3.1__py3-none-any.whl → 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- neo4j_viz/__init__.py +21 -2
- neo4j_viz/gds.py +71 -13
- neo4j_viz/gql_create.py +46 -12
- neo4j_viz/neo4j.py +44 -9
- neo4j_viz/options.py +97 -2
- neo4j_viz/pandas.py +57 -13
- neo4j_viz/visualization_graph.py +22 -2
- {neo4j_viz-0.3.1.dist-info → neo4j_viz-0.4.0.dist-info}/METADATA +1 -1
- {neo4j_viz-0.3.1.dist-info → neo4j_viz-0.4.0.dist-info}/RECORD +11 -11
- {neo4j_viz-0.3.1.dist-info → neo4j_viz-0.4.0.dist-info}/WHEEL +1 -1
- {neo4j_viz-0.3.1.dist-info → neo4j_viz-0.4.0.dist-info}/top_level.txt +0 -0
neo4j_viz/__init__.py
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
from .node import Node
|
|
2
|
-
from .options import
|
|
2
|
+
from .options import (
|
|
3
|
+
CaptionAlignment,
|
|
4
|
+
Direction,
|
|
5
|
+
ForceDirectedLayoutOptions,
|
|
6
|
+
HierarchicalLayoutOptions,
|
|
7
|
+
Layout,
|
|
8
|
+
Packing,
|
|
9
|
+
Renderer,
|
|
10
|
+
)
|
|
3
11
|
from .relationship import Relationship
|
|
4
12
|
from .visualization_graph import VisualizationGraph
|
|
5
13
|
|
|
6
|
-
__all__ = [
|
|
14
|
+
__all__ = [
|
|
15
|
+
"VisualizationGraph",
|
|
16
|
+
"Node",
|
|
17
|
+
"Relationship",
|
|
18
|
+
"CaptionAlignment",
|
|
19
|
+
"Layout",
|
|
20
|
+
"Renderer",
|
|
21
|
+
"ForceDirectedLayoutOptions",
|
|
22
|
+
"HierarchicalLayoutOptions",
|
|
23
|
+
"Direction",
|
|
24
|
+
"Packing",
|
|
25
|
+
]
|
neo4j_viz/gds.py
CHANGED
|
@@ -2,15 +2,17 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
from itertools import chain
|
|
4
4
|
from typing import Optional
|
|
5
|
+
from uuid import uuid4
|
|
5
6
|
|
|
6
7
|
import pandas as pd
|
|
7
8
|
from graphdatascience import Graph, GraphDataScience
|
|
9
|
+
from pandas import Series
|
|
8
10
|
|
|
9
11
|
from .pandas import _from_dfs
|
|
10
12
|
from .visualization_graph import VisualizationGraph
|
|
11
13
|
|
|
12
14
|
|
|
13
|
-
def
|
|
15
|
+
def _fetch_node_dfs(
|
|
14
16
|
gds: GraphDataScience, G: Graph, node_properties: list[str], node_labels: list[str]
|
|
15
17
|
) -> dict[str, pd.DataFrame]:
|
|
16
18
|
return {
|
|
@@ -21,7 +23,21 @@ def _node_dfs(
|
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
|
|
24
|
-
def
|
|
26
|
+
def _fetch_rel_df(gds: GraphDataScience, G: Graph) -> pd.DataFrame:
|
|
27
|
+
relationship_properties = G.relationship_properties()
|
|
28
|
+
assert isinstance(relationship_properties, Series)
|
|
29
|
+
|
|
30
|
+
relationship_properties_per_type = relationship_properties.tolist()
|
|
31
|
+
property_set: set[str] = set()
|
|
32
|
+
for props in relationship_properties_per_type:
|
|
33
|
+
if props:
|
|
34
|
+
property_set.update(props)
|
|
35
|
+
|
|
36
|
+
if len(property_set) > 0:
|
|
37
|
+
return gds.graph.relationshipProperties.stream(
|
|
38
|
+
G, relationship_properties=list(property_set), separate_property_columns=True
|
|
39
|
+
)
|
|
40
|
+
|
|
25
41
|
return gds.graph.relationships.stream(G)
|
|
26
42
|
|
|
27
43
|
|
|
@@ -31,6 +47,7 @@ def from_gds(
|
|
|
31
47
|
size_property: Optional[str] = None,
|
|
32
48
|
additional_node_properties: Optional[list[str]] = None,
|
|
33
49
|
node_radius_min_max: Optional[tuple[float, float]] = (3, 60),
|
|
50
|
+
max_node_count: int = 10_000,
|
|
34
51
|
) -> VisualizationGraph:
|
|
35
52
|
"""
|
|
36
53
|
Create a VisualizationGraph from a GraphDataScience object and a Graph object.
|
|
@@ -49,10 +66,14 @@ def from_gds(
|
|
|
49
66
|
size_property : str, optional
|
|
50
67
|
Property to use for node size, by default None.
|
|
51
68
|
additional_node_properties : list[str], optional
|
|
52
|
-
Additional properties to include in the visualization node, by default None
|
|
69
|
+
Additional properties to include in the visualization node, by default None which means that all node
|
|
70
|
+
properties will be fetched.
|
|
53
71
|
node_radius_min_max : tuple[float, float], optional
|
|
54
72
|
Minimum and maximum node radius, by default (3, 60).
|
|
55
73
|
To avoid tiny or huge nodes in the visualization, the node sizes are scaled to fit in the given range.
|
|
74
|
+
max_node_count : int, optional
|
|
75
|
+
The maximum number of nodes to fetch from the graph. The graph will be sampled using random walk with restarts
|
|
76
|
+
if its node count exceeds this number.
|
|
56
77
|
"""
|
|
57
78
|
node_properties_from_gds = G.node_properties()
|
|
58
79
|
assert isinstance(node_properties_from_gds, pd.Series)
|
|
@@ -61,7 +82,9 @@ def from_gds(
|
|
|
61
82
|
if size_property is not None and size_property not in actual_node_properties:
|
|
62
83
|
raise ValueError(f"There is no node property '{size_property}' in graph '{G.name()}'")
|
|
63
84
|
|
|
64
|
-
if additional_node_properties is
|
|
85
|
+
if additional_node_properties is None:
|
|
86
|
+
additional_node_properties = actual_node_properties
|
|
87
|
+
else:
|
|
65
88
|
for prop in additional_node_properties:
|
|
66
89
|
if prop not in actual_node_properties:
|
|
67
90
|
raise ValueError(f"There is no node property '{prop}' in graph '{G.name()}'")
|
|
@@ -69,14 +92,40 @@ def from_gds(
|
|
|
69
92
|
node_properties = set()
|
|
70
93
|
if additional_node_properties is not None:
|
|
71
94
|
node_properties.update(additional_node_properties)
|
|
72
|
-
|
|
73
95
|
if size_property is not None:
|
|
74
96
|
node_properties.add(size_property)
|
|
75
|
-
|
|
76
97
|
node_properties = list(node_properties)
|
|
77
|
-
|
|
98
|
+
|
|
99
|
+
node_count = G.node_count()
|
|
100
|
+
if node_count > max_node_count:
|
|
101
|
+
sampling_ratio = float(max_node_count) / node_count
|
|
102
|
+
sample_name = f"neo4j-viz_sample_{uuid4()}"
|
|
103
|
+
G_fetched, _ = gds.graph.sample.rwr(sample_name, G, samplingRatio=sampling_ratio, nodeLabelStratification=True)
|
|
104
|
+
else:
|
|
105
|
+
G_fetched = G
|
|
106
|
+
|
|
107
|
+
property_name = None
|
|
108
|
+
try:
|
|
109
|
+
# Since GDS does not allow us to only fetch node IDs, we add the degree property
|
|
110
|
+
# as a temporary property to ensure that we have at least one property to fetch
|
|
111
|
+
if len(actual_node_properties) == 0:
|
|
112
|
+
property_name = f"neo4j-viz_property_{uuid4()}"
|
|
113
|
+
gds.degree.mutate(G_fetched, mutateProperty=property_name)
|
|
114
|
+
node_properties = [property_name]
|
|
115
|
+
|
|
116
|
+
node_dfs = _fetch_node_dfs(gds, G_fetched, node_properties, G_fetched.node_labels())
|
|
117
|
+
rel_df = _fetch_rel_df(gds, G_fetched)
|
|
118
|
+
finally:
|
|
119
|
+
if G_fetched.name() != G.name():
|
|
120
|
+
G_fetched.drop()
|
|
121
|
+
elif property_name is not None:
|
|
122
|
+
gds.graph.nodeProperties.drop(G_fetched, node_properties=[property_name])
|
|
123
|
+
|
|
78
124
|
for df in node_dfs.values():
|
|
79
125
|
df.rename(columns={"nodeId": "id"}, inplace=True)
|
|
126
|
+
if property_name is not None and property_name in df.columns:
|
|
127
|
+
df.drop(columns=[property_name], inplace=True)
|
|
128
|
+
rel_df.rename(columns={"sourceNodeId": "source", "targetNodeId": "target"}, inplace=True)
|
|
80
129
|
|
|
81
130
|
node_props_df = pd.concat(node_dfs.values(), ignore_index=True, axis=0).drop_duplicates()
|
|
82
131
|
if size_property is not None:
|
|
@@ -89,12 +138,21 @@ def from_gds(
|
|
|
89
138
|
df.rename(columns={"labels": "__labels"}, inplace=True)
|
|
90
139
|
df["labels"] = lbl
|
|
91
140
|
|
|
92
|
-
|
|
93
|
-
|
|
141
|
+
node_labels_df = pd.concat([df[["id", "labels"]] for df in node_dfs.values()], ignore_index=True, axis=0)
|
|
142
|
+
node_labels_df = node_labels_df.groupby("id").agg({"labels": list})
|
|
94
143
|
|
|
95
|
-
node_df = node_props_df.merge(
|
|
144
|
+
node_df = node_props_df.merge(node_labels_df, on="id")
|
|
96
145
|
|
|
97
|
-
|
|
98
|
-
|
|
146
|
+
if "caption" not in actual_node_properties:
|
|
147
|
+
node_df["caption"] = node_df["labels"].astype(str)
|
|
99
148
|
|
|
100
|
-
|
|
149
|
+
try:
|
|
150
|
+
return _from_dfs(node_df, rel_df, node_radius_min_max=node_radius_min_max, rename_properties={"__size": "size"})
|
|
151
|
+
except ValueError as e:
|
|
152
|
+
err_msg = str(e)
|
|
153
|
+
if "column" in err_msg:
|
|
154
|
+
err_msg = err_msg.replace("column", "property")
|
|
155
|
+
if ("'size'" in err_msg) and (size_property is not None):
|
|
156
|
+
err_msg = err_msg.replace("'size'", f"'{size_property}'")
|
|
157
|
+
raise ValueError(err_msg)
|
|
158
|
+
raise e
|
neo4j_viz/gql_create.py
CHANGED
|
@@ -2,6 +2,8 @@ import re
|
|
|
2
2
|
import uuid
|
|
3
3
|
from typing import Any, Optional
|
|
4
4
|
|
|
5
|
+
from pydantic import BaseModel, ValidationError
|
|
6
|
+
|
|
5
7
|
from neo4j_viz import Node, Relationship, VisualizationGraph
|
|
6
8
|
|
|
7
9
|
|
|
@@ -252,6 +254,20 @@ def from_gql_create(
|
|
|
252
254
|
node_top_level_keys = Node.all_validation_aliases(exempted_fields=["id"])
|
|
253
255
|
rel_top_level_keys = Relationship.all_validation_aliases(exempted_fields=["id", "source", "target"])
|
|
254
256
|
|
|
257
|
+
def _parse_validation_error(e: ValidationError, entity_type: type[BaseModel]) -> None:
|
|
258
|
+
for err in e.errors():
|
|
259
|
+
loc = err["loc"][0]
|
|
260
|
+
if (loc == "size") and size_property is not None:
|
|
261
|
+
loc = size_property
|
|
262
|
+
if loc == "caption":
|
|
263
|
+
if (entity_type == Node) and (node_caption is not None):
|
|
264
|
+
loc = node_caption
|
|
265
|
+
elif (entity_type == Relationship) and (relationship_caption is not None):
|
|
266
|
+
loc = relationship_caption
|
|
267
|
+
raise ValueError(
|
|
268
|
+
f"Error for {entity_type.__name__.lower()} property '{loc}' with provided input '{err['input']}'. Reason: {err['msg']}"
|
|
269
|
+
)
|
|
270
|
+
|
|
255
271
|
nodes = []
|
|
256
272
|
relationships = []
|
|
257
273
|
alias_to_id = {}
|
|
@@ -267,7 +283,10 @@ def from_gql_create(
|
|
|
267
283
|
anonymous_count += 1
|
|
268
284
|
if alias not in alias_to_id:
|
|
269
285
|
alias_to_id[alias] = str(uuid.uuid4())
|
|
270
|
-
|
|
286
|
+
try:
|
|
287
|
+
nodes.append(Node(id=alias_to_id[alias], **top_level, properties=props))
|
|
288
|
+
except ValidationError as e:
|
|
289
|
+
_parse_validation_error(e, Node)
|
|
271
290
|
|
|
272
291
|
continue
|
|
273
292
|
|
|
@@ -283,7 +302,10 @@ def from_gql_create(
|
|
|
283
302
|
anonymous_count += 1
|
|
284
303
|
if left_alias not in alias_to_id:
|
|
285
304
|
alias_to_id[left_alias] = str(uuid.uuid4())
|
|
286
|
-
|
|
305
|
+
try:
|
|
306
|
+
nodes.append(Node(id=alias_to_id[left_alias], **left_top_level, properties=left_props))
|
|
307
|
+
except ValidationError as e:
|
|
308
|
+
_parse_validation_error(e, Node)
|
|
287
309
|
elif left_alias not in alias_to_id:
|
|
288
310
|
snippet = _get_snippet(query, query.index(left_node))
|
|
289
311
|
raise ValueError(f"Relationship references unknown node alias: '{left_alias}' near: `{snippet}`.")
|
|
@@ -295,7 +317,10 @@ def from_gql_create(
|
|
|
295
317
|
anonymous_count += 1
|
|
296
318
|
if right_alias not in alias_to_id:
|
|
297
319
|
alias_to_id[right_alias] = str(uuid.uuid4())
|
|
298
|
-
|
|
320
|
+
try:
|
|
321
|
+
nodes.append(Node(id=alias_to_id[right_alias], **right_top_level, properties=right_props))
|
|
322
|
+
except ValidationError as e:
|
|
323
|
+
_parse_validation_error(e, Node)
|
|
299
324
|
elif right_alias not in alias_to_id:
|
|
300
325
|
snippet = _get_snippet(query, query.index(right_node))
|
|
301
326
|
raise ValueError(f"Relationship references unknown node alias: '{right_alias}' near: `{snippet}`.")
|
|
@@ -313,15 +338,20 @@ def from_gql_create(
|
|
|
313
338
|
if "type" in props:
|
|
314
339
|
props["__type"] = props["type"]
|
|
315
340
|
props["type"] = rel_type
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
341
|
+
|
|
342
|
+
try:
|
|
343
|
+
relationships.append(
|
|
344
|
+
Relationship(
|
|
345
|
+
id=rel_id,
|
|
346
|
+
source=alias_to_id[left_alias],
|
|
347
|
+
target=alias_to_id[right_alias],
|
|
348
|
+
**top_level,
|
|
349
|
+
properties=props,
|
|
350
|
+
)
|
|
323
351
|
)
|
|
324
|
-
|
|
352
|
+
except ValidationError as e:
|
|
353
|
+
_parse_validation_error(e, Relationship)
|
|
354
|
+
|
|
325
355
|
continue
|
|
326
356
|
|
|
327
357
|
snippet = part[:30]
|
|
@@ -346,6 +376,10 @@ def from_gql_create(
|
|
|
346
376
|
|
|
347
377
|
VG = VisualizationGraph(nodes=nodes, relationships=relationships)
|
|
348
378
|
if (node_radius_min_max is not None) and (size_property is not None):
|
|
349
|
-
|
|
379
|
+
try:
|
|
380
|
+
VG.resize_nodes(node_radius_min_max=node_radius_min_max)
|
|
381
|
+
except TypeError:
|
|
382
|
+
loc = "size" if size_property is None else size_property
|
|
383
|
+
raise ValueError(f"Error for node property '{loc}'. Reason: must be a numerical value")
|
|
350
384
|
|
|
351
385
|
return VG
|
neo4j_viz/neo4j.py
CHANGED
|
@@ -4,12 +4,21 @@ from typing import Optional, Union
|
|
|
4
4
|
|
|
5
5
|
import neo4j.graph
|
|
6
6
|
from neo4j import Result
|
|
7
|
+
from pydantic import BaseModel, ValidationError
|
|
7
8
|
|
|
8
9
|
from neo4j_viz.node import Node
|
|
9
10
|
from neo4j_viz.relationship import Relationship
|
|
10
11
|
from neo4j_viz.visualization_graph import VisualizationGraph
|
|
11
12
|
|
|
12
13
|
|
|
14
|
+
def _parse_validation_error(e: ValidationError, entity_type: type[BaseModel]) -> None:
|
|
15
|
+
for err in e.errors():
|
|
16
|
+
loc = err["loc"][0]
|
|
17
|
+
raise ValueError(
|
|
18
|
+
f"Error for {entity_type.__name__.lower()} property '{loc}' with provided input '{err['input']}'. Reason: {err['msg']}"
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
13
22
|
def from_neo4j(
|
|
14
23
|
result: Union[neo4j.graph.Graph, Result],
|
|
15
24
|
size_property: Optional[str] = None,
|
|
@@ -50,14 +59,30 @@ def from_neo4j(
|
|
|
50
59
|
all_node_field_aliases = Node.all_validation_aliases()
|
|
51
60
|
all_rel_field_aliases = Relationship.all_validation_aliases()
|
|
52
61
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
62
|
+
try:
|
|
63
|
+
nodes = [
|
|
64
|
+
_map_node(node, all_node_field_aliases, size_property, caption_property=node_caption)
|
|
65
|
+
for node in graph.nodes
|
|
66
|
+
]
|
|
67
|
+
except ValueError as e:
|
|
68
|
+
err_msg = str(e)
|
|
69
|
+
if ("'size'" in err_msg) and (size_property is not None):
|
|
70
|
+
err_msg = err_msg.replace("'size'", f"'{size_property}'")
|
|
71
|
+
elif ("'caption'" in err_msg) and (node_caption is not None):
|
|
72
|
+
err_msg = err_msg.replace("'caption'", f"'{node_caption}'")
|
|
73
|
+
raise ValueError(err_msg)
|
|
74
|
+
|
|
56
75
|
relationships = []
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
76
|
+
try:
|
|
77
|
+
for rel in graph.relationships:
|
|
78
|
+
mapped_rel = _map_relationship(rel, all_rel_field_aliases, caption_property=relationship_caption)
|
|
79
|
+
if mapped_rel:
|
|
80
|
+
relationships.append(mapped_rel)
|
|
81
|
+
except ValueError as e:
|
|
82
|
+
err_msg = str(e)
|
|
83
|
+
if ("'caption'" in err_msg) and (relationship_caption is not None):
|
|
84
|
+
err_msg = err_msg.replace("'caption'", f"'{relationship_caption}'")
|
|
85
|
+
raise ValueError(err_msg)
|
|
61
86
|
|
|
62
87
|
VG = VisualizationGraph(nodes, relationships)
|
|
63
88
|
|
|
@@ -102,7 +127,12 @@ def _map_node(
|
|
|
102
127
|
properties["__labels"] = properties["labels"]
|
|
103
128
|
properties["labels"] = labels
|
|
104
129
|
|
|
105
|
-
|
|
130
|
+
try:
|
|
131
|
+
viz_node = Node(**top_level_fields, properties=properties)
|
|
132
|
+
except ValidationError as e:
|
|
133
|
+
_parse_validation_error(e, Node)
|
|
134
|
+
|
|
135
|
+
return viz_node
|
|
106
136
|
|
|
107
137
|
|
|
108
138
|
def _map_relationship(
|
|
@@ -135,4 +165,9 @@ def _map_relationship(
|
|
|
135
165
|
properties["__type"] = properties["type"]
|
|
136
166
|
properties["type"] = rel.type
|
|
137
167
|
|
|
138
|
-
|
|
168
|
+
try:
|
|
169
|
+
viz_rel = Relationship(**top_level_fields, properties=properties)
|
|
170
|
+
except ValidationError as e:
|
|
171
|
+
_parse_validation_error(e, Relationship)
|
|
172
|
+
|
|
173
|
+
return viz_rel
|
neo4j_viz/options.py
CHANGED
|
@@ -2,10 +2,10 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import warnings
|
|
4
4
|
from enum import Enum
|
|
5
|
-
from typing import Any, Optional
|
|
5
|
+
from typing import Any, Optional, Union
|
|
6
6
|
|
|
7
7
|
import enum_tools.documentation
|
|
8
|
-
from pydantic import BaseModel, Field
|
|
8
|
+
from pydantic import BaseModel, Field, ValidationError, model_validator
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
@enum_tools.documentation.document_enum
|
|
@@ -33,6 +33,69 @@ class Layout(str, Enum):
|
|
|
33
33
|
GRID = "grid"
|
|
34
34
|
|
|
35
35
|
|
|
36
|
+
@enum_tools.documentation.document_enum
|
|
37
|
+
class Direction(str, Enum):
|
|
38
|
+
"""
|
|
39
|
+
The direction in which the layout should be oriented
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
LEFT = "left"
|
|
43
|
+
RIGHT = "right"
|
|
44
|
+
UP = "up"
|
|
45
|
+
DOWN = "down"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@enum_tools.documentation.document_enum
|
|
49
|
+
class Packing(str, Enum):
|
|
50
|
+
"""
|
|
51
|
+
The packing method to be used
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
BIN = "bin"
|
|
55
|
+
STACK = "stack"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class HierarchicalLayoutOptions(BaseModel, extra="forbid"):
|
|
59
|
+
"""
|
|
60
|
+
The options for the hierarchical layout.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
direction: Optional[Direction] = None
|
|
64
|
+
packaging: Optional[Packing] = None
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class ForceDirectedLayoutOptions(BaseModel, extra="forbid"):
|
|
68
|
+
"""
|
|
69
|
+
The options for the force-directed layout.
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
gravity: Optional[float] = None
|
|
73
|
+
simulationStopVelocity: Optional[float] = None
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
LayoutOptions = Union[HierarchicalLayoutOptions, ForceDirectedLayoutOptions]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def construct_layout_options(layout: Layout, options: dict[str, Any]) -> Optional[LayoutOptions]:
|
|
80
|
+
if not options:
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
if layout == Layout.FORCE_DIRECTED:
|
|
84
|
+
try:
|
|
85
|
+
return ForceDirectedLayoutOptions(**options)
|
|
86
|
+
except ValidationError as e:
|
|
87
|
+
_parse_validation_error(e, ForceDirectedLayoutOptions)
|
|
88
|
+
elif layout == Layout.HIERARCHICAL:
|
|
89
|
+
try:
|
|
90
|
+
return HierarchicalLayoutOptions(**options)
|
|
91
|
+
except ValidationError as e:
|
|
92
|
+
_parse_validation_error(e, ForceDirectedLayoutOptions)
|
|
93
|
+
|
|
94
|
+
raise ValueError(
|
|
95
|
+
f"Layout options only supported for layouts `{Layout.FORCE_DIRECTED}` and `{Layout.HIERARCHICAL}`, but was `{layout}`"
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
36
99
|
@enum_tools.documentation.document_enum
|
|
37
100
|
class Renderer(str, Enum):
|
|
38
101
|
"""
|
|
@@ -72,6 +135,9 @@ class RenderOptions(BaseModel, extra="allow"):
|
|
|
72
135
|
"""
|
|
73
136
|
|
|
74
137
|
layout: Optional[Layout] = None
|
|
138
|
+
layout_options: Optional[Union[HierarchicalLayoutOptions, ForceDirectedLayoutOptions]] = Field(
|
|
139
|
+
None, serialization_alias="layoutOptions"
|
|
140
|
+
)
|
|
75
141
|
renderer: Optional[Renderer] = None
|
|
76
142
|
|
|
77
143
|
pan_X: Optional[float] = Field(None, serialization_alias="panX")
|
|
@@ -84,5 +150,34 @@ class RenderOptions(BaseModel, extra="allow"):
|
|
|
84
150
|
min_zoom: Optional[float] = Field(None, serialization_alias="minZoom", description="The minimum zoom level allowed")
|
|
85
151
|
allow_dynamic_min_zoom: Optional[bool] = Field(None, serialization_alias="allowDynamicMinZoom")
|
|
86
152
|
|
|
153
|
+
@model_validator(mode="after")
|
|
154
|
+
def check_layout_options_match(self) -> RenderOptions:
|
|
155
|
+
if self.layout_options is None:
|
|
156
|
+
return self
|
|
157
|
+
|
|
158
|
+
if self.layout == Layout.HIERARCHICAL and not isinstance(self.layout_options, HierarchicalLayoutOptions):
|
|
159
|
+
raise ValueError("layout_options must be of type HierarchicalLayoutOptions for hierarchical layout")
|
|
160
|
+
if self.layout == Layout.FORCE_DIRECTED and not isinstance(self.layout_options, ForceDirectedLayoutOptions):
|
|
161
|
+
raise ValueError("layout_options must be of type ForceDirectedLayoutOptions for force-directed layout")
|
|
162
|
+
return self
|
|
163
|
+
|
|
87
164
|
def to_dict(self) -> dict[str, Any]:
|
|
88
165
|
return self.model_dump(exclude_none=True, by_alias=True)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _parse_validation_error(e: ValidationError, entity_type: type[BaseModel]) -> None:
|
|
169
|
+
for err in e.errors():
|
|
170
|
+
loc = err["loc"][0]
|
|
171
|
+
if err["type"] == "missing":
|
|
172
|
+
raise ValueError(
|
|
173
|
+
f"Mandatory `{entity_type.__name__}` parameter '{loc}' is missing. Expected one of {entity_type.model_fields[loc].validation_alias.choices} to be present" # type: ignore
|
|
174
|
+
)
|
|
175
|
+
elif err["type"] == "extra_forbidden":
|
|
176
|
+
raise ValueError(
|
|
177
|
+
f"Unexpected `{entity_type.__name__}` parameter '{loc}' with provided input '{err['input']}'. "
|
|
178
|
+
f"Allowed parameters are: {', '.join(entity_type.model_fields.keys())}"
|
|
179
|
+
)
|
|
180
|
+
else:
|
|
181
|
+
raise ValueError(
|
|
182
|
+
f"Error for `{entity_type.__name__}` parameter '{loc}' with provided input '{err['input']}'. Reason: {err['msg']}"
|
|
183
|
+
)
|
neo4j_viz/pandas.py
CHANGED
|
@@ -4,6 +4,7 @@ from collections.abc import Iterable
|
|
|
4
4
|
from typing import Optional, Union
|
|
5
5
|
|
|
6
6
|
from pandas import DataFrame
|
|
7
|
+
from pydantic import BaseModel, ValidationError
|
|
7
8
|
|
|
8
9
|
from .node import Node
|
|
9
10
|
from .relationship import Relationship
|
|
@@ -12,19 +13,54 @@ from .visualization_graph import VisualizationGraph
|
|
|
12
13
|
DFS_TYPE = Union[DataFrame, Iterable[DataFrame]]
|
|
13
14
|
|
|
14
15
|
|
|
16
|
+
def _parse_validation_error(e: ValidationError, entity_type: type[BaseModel]) -> None:
|
|
17
|
+
for err in e.errors():
|
|
18
|
+
loc = err["loc"][0]
|
|
19
|
+
if err["type"] == "missing":
|
|
20
|
+
raise ValueError(
|
|
21
|
+
f"Mandatory {entity_type.__name__.lower()} column '{loc}' is missing. Expected one of {entity_type.model_fields[loc].validation_alias.choices} to be present" # type: ignore
|
|
22
|
+
)
|
|
23
|
+
else:
|
|
24
|
+
raise ValueError(
|
|
25
|
+
f"Error for {entity_type.__name__.lower()} column '{loc}' with provided input '{err['input']}'. Reason: {err['msg']}"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
15
29
|
def _from_dfs(
|
|
16
|
-
node_dfs: DFS_TYPE,
|
|
30
|
+
node_dfs: Optional[DFS_TYPE],
|
|
17
31
|
rel_dfs: DFS_TYPE,
|
|
18
32
|
node_radius_min_max: Optional[tuple[float, float]] = (3, 60),
|
|
19
33
|
rename_properties: Optional[dict[str, str]] = None,
|
|
20
34
|
) -> VisualizationGraph:
|
|
35
|
+
relationships = _parse_relationships(rel_dfs, rename_properties=rename_properties)
|
|
36
|
+
|
|
37
|
+
if node_dfs is None:
|
|
38
|
+
has_size = False
|
|
39
|
+
node_ids = set()
|
|
40
|
+
for rel in relationships:
|
|
41
|
+
node_ids.add(rel.source)
|
|
42
|
+
node_ids.add(rel.target)
|
|
43
|
+
nodes = [Node(id=id) for id in node_ids]
|
|
44
|
+
else:
|
|
45
|
+
nodes, has_size = _parse_nodes(node_dfs, rename_properties=rename_properties)
|
|
46
|
+
|
|
47
|
+
VG = VisualizationGraph(nodes=nodes, relationships=relationships)
|
|
48
|
+
|
|
49
|
+
if node_radius_min_max is not None and has_size:
|
|
50
|
+
VG.resize_nodes(node_radius_min_max=node_radius_min_max)
|
|
51
|
+
|
|
52
|
+
return VG
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _parse_nodes(node_dfs: DFS_TYPE, rename_properties: Optional[dict[str, str]]) -> tuple[list[Node], bool]:
|
|
21
56
|
if isinstance(node_dfs, DataFrame):
|
|
22
57
|
node_dfs_iter: Iterable[DataFrame] = [node_dfs]
|
|
58
|
+
elif node_dfs is None:
|
|
59
|
+
node_dfs_iter = []
|
|
23
60
|
else:
|
|
24
61
|
node_dfs_iter = node_dfs
|
|
25
62
|
|
|
26
63
|
all_node_field_aliases = Node.all_validation_aliases()
|
|
27
|
-
all_rel_field_aliases = Relationship.all_validation_aliases()
|
|
28
64
|
|
|
29
65
|
has_size = True
|
|
30
66
|
nodes = []
|
|
@@ -41,14 +77,23 @@ def _from_dfs(
|
|
|
41
77
|
key = rename_properties[key]
|
|
42
78
|
properties[key] = value
|
|
43
79
|
|
|
44
|
-
|
|
80
|
+
try:
|
|
81
|
+
nodes.append(Node(**top_level, properties=properties))
|
|
82
|
+
except ValidationError as e:
|
|
83
|
+
_parse_validation_error(e, Node)
|
|
84
|
+
|
|
85
|
+
return nodes, has_size
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _parse_relationships(rel_dfs: DFS_TYPE, rename_properties: Optional[dict[str, str]]) -> list[Relationship]:
|
|
89
|
+
all_rel_field_aliases = Relationship.all_validation_aliases()
|
|
45
90
|
|
|
46
91
|
if isinstance(rel_dfs, DataFrame):
|
|
47
92
|
rel_dfs_iter: Iterable[DataFrame] = [rel_dfs]
|
|
48
93
|
else:
|
|
49
94
|
rel_dfs_iter = rel_dfs
|
|
95
|
+
relationships: list[Relationship] = []
|
|
50
96
|
|
|
51
|
-
relationships = []
|
|
52
97
|
for rel_df in rel_dfs_iter:
|
|
53
98
|
for _, row in rel_df.iterrows():
|
|
54
99
|
top_level = {}
|
|
@@ -61,18 +106,16 @@ def _from_dfs(
|
|
|
61
106
|
key = rename_properties[key]
|
|
62
107
|
properties[key] = value
|
|
63
108
|
|
|
64
|
-
|
|
109
|
+
try:
|
|
110
|
+
relationships.append(Relationship(**top_level, properties=properties))
|
|
111
|
+
except ValidationError as e:
|
|
112
|
+
_parse_validation_error(e, Relationship)
|
|
65
113
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if node_radius_min_max is not None and has_size:
|
|
69
|
-
VG.resize_nodes(node_radius_min_max=node_radius_min_max)
|
|
70
|
-
|
|
71
|
-
return VG
|
|
114
|
+
return relationships
|
|
72
115
|
|
|
73
116
|
|
|
74
117
|
def from_dfs(
|
|
75
|
-
node_dfs: DFS_TYPE,
|
|
118
|
+
node_dfs: Optional[DFS_TYPE],
|
|
76
119
|
rel_dfs: DFS_TYPE,
|
|
77
120
|
node_radius_min_max: Optional[tuple[float, float]] = (3, 60),
|
|
78
121
|
) -> VisualizationGraph:
|
|
@@ -85,8 +128,9 @@ def from_dfs(
|
|
|
85
128
|
|
|
86
129
|
Parameters
|
|
87
130
|
----------
|
|
88
|
-
node_dfs: Union[DataFrame, Iterable[DataFrame]]
|
|
131
|
+
node_dfs: Optional[Union[DataFrame, Iterable[DataFrame]]]
|
|
89
132
|
DataFrame or iterable of DataFrames containing node data.
|
|
133
|
+
If None, the nodes will be created from the source and target node ids in the rel_dfs.
|
|
90
134
|
rel_dfs: Union[DataFrame, Iterable[DataFrame]]
|
|
91
135
|
DataFrame or iterable of DataFrames containing relationship data.
|
|
92
136
|
node_radius_min_max : tuple[float, float], optional
|
neo4j_viz/visualization_graph.py
CHANGED
|
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import warnings
|
|
4
4
|
from collections.abc import Iterable
|
|
5
|
-
from typing import Any, Callable, Hashable, Optional
|
|
5
|
+
from typing import Any, Callable, Hashable, Optional, Union
|
|
6
6
|
|
|
7
7
|
from IPython.display import HTML
|
|
8
8
|
from pydantic_extra_types.color import Color, ColorType
|
|
@@ -11,7 +11,13 @@ from .colors import NEO4J_COLORS_CONTINUOUS, NEO4J_COLORS_DISCRETE, ColorSpace,
|
|
|
11
11
|
from .node import Node, NodeIdType
|
|
12
12
|
from .node_size import RealNumber, verify_radii
|
|
13
13
|
from .nvl import NVL
|
|
14
|
-
from .options import
|
|
14
|
+
from .options import (
|
|
15
|
+
Layout,
|
|
16
|
+
LayoutOptions,
|
|
17
|
+
Renderer,
|
|
18
|
+
RenderOptions,
|
|
19
|
+
construct_layout_options,
|
|
20
|
+
)
|
|
15
21
|
from .relationship import Relationship
|
|
16
22
|
|
|
17
23
|
|
|
@@ -42,6 +48,7 @@ class VisualizationGraph:
|
|
|
42
48
|
def render(
|
|
43
49
|
self,
|
|
44
50
|
layout: Optional[Layout] = None,
|
|
51
|
+
layout_options: Union[dict[str, Any], LayoutOptions, None] = None,
|
|
45
52
|
renderer: Renderer = Renderer.CANVAS,
|
|
46
53
|
width: str = "100%",
|
|
47
54
|
height: str = "600px",
|
|
@@ -60,6 +67,8 @@ class VisualizationGraph:
|
|
|
60
67
|
----------
|
|
61
68
|
layout:
|
|
62
69
|
The `Layout` to use.
|
|
70
|
+
layout_options:
|
|
71
|
+
The `LayoutOptions` to use.
|
|
63
72
|
renderer:
|
|
64
73
|
The `Renderer` to use.
|
|
65
74
|
width:
|
|
@@ -92,8 +101,19 @@ class VisualizationGraph:
|
|
|
92
101
|
|
|
93
102
|
Renderer.check(renderer, num_nodes)
|
|
94
103
|
|
|
104
|
+
if not layout:
|
|
105
|
+
layout = Layout.FORCE_DIRECTED
|
|
106
|
+
if not layout_options:
|
|
107
|
+
layout_options = {}
|
|
108
|
+
|
|
109
|
+
if isinstance(layout_options, dict):
|
|
110
|
+
layout_options_typed = construct_layout_options(layout, layout_options)
|
|
111
|
+
else:
|
|
112
|
+
layout_options_typed = layout_options
|
|
113
|
+
|
|
95
114
|
render_options = RenderOptions(
|
|
96
115
|
layout=layout,
|
|
116
|
+
layout_options=layout_options_typed,
|
|
97
117
|
renderer=renderer,
|
|
98
118
|
pan_X=pan_position[0] if pan_position is not None else None,
|
|
99
119
|
pan_Y=pan_position[1] if pan_position is not None else None,
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
neo4j_viz/__init__.py,sha256=
|
|
1
|
+
neo4j_viz/__init__.py,sha256=Q-VZlJe3_kAow_-F_-9RsHCQfbOfv5on26YD9ihw27o,504
|
|
2
2
|
neo4j_viz/colors.py,sha256=IvOCTmCu7WTMna_wNLZ3GrThTwFyIoKtNkmZYDLdYac,6694
|
|
3
|
-
neo4j_viz/gds.py,sha256=
|
|
4
|
-
neo4j_viz/gql_create.py,sha256=
|
|
5
|
-
neo4j_viz/neo4j.py,sha256=
|
|
3
|
+
neo4j_viz/gds.py,sha256=Cu-0RDi6YtWiuWP1N06h5P0zd5mQx0z5cafr0fs2K24,6637
|
|
4
|
+
neo4j_viz/gql_create.py,sha256=K33cT6dOj8eJPGNNJXiXlCfLIzNxTwcW4n_2AG3_zaY,14751
|
|
5
|
+
neo4j_viz/neo4j.py,sha256=_bZkj1XQKazueRM7Ti0pIPdSI8fVd432mZbQsxFO2yw,6112
|
|
6
6
|
neo4j_viz/node.py,sha256=MiLoghsn2NLs_iV65NuW7u3iaxP8MTKoNy6La9TdreY,3886
|
|
7
7
|
neo4j_viz/node_size.py,sha256=c_sMtQSD8eJ_6Y0Kr6ku0LOs9VoEDxfYCUUzUWZ-1Xo,1197
|
|
8
8
|
neo4j_viz/nvl.py,sha256=ZN3tyWar9ugR88r5N6txW3ThfNEWOt5A1KzrrRnLKwk,5262
|
|
9
|
-
neo4j_viz/options.py,sha256=
|
|
10
|
-
neo4j_viz/pandas.py,sha256=
|
|
9
|
+
neo4j_viz/options.py,sha256=eOpiLcIfFvUiPoozyT44F9MHGRkqCfBZFmh0u_6DfwY,6400
|
|
10
|
+
neo4j_viz/pandas.py,sha256=Ajn77VRcen4lqrVQp0O9yUG5bZ7QkjiCIpjj0UFCD0w,5162
|
|
11
11
|
neo4j_viz/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
12
|
neo4j_viz/relationship.py,sha256=sRgGjNzlqt6wPmiB3WXBTxR_W_5Z40VofpHUZvkRS1I,4143
|
|
13
|
-
neo4j_viz/visualization_graph.py,sha256=
|
|
13
|
+
neo4j_viz/visualization_graph.py,sha256=Fizmzm8p4p3Fbd3gsiTymkXgI3crpdmr5ipv4gFv3SY,13760
|
|
14
14
|
neo4j_viz/resources/icons/screenshot.svg,sha256=Ns9Yi2Iq4lIaiFvzc0pXBmjxt4fcmBO-I4cI8Xiu1HE,311
|
|
15
15
|
neo4j_viz/resources/icons/zoom-in.svg,sha256=PsO5yFkA1JnGM2QV_qxHKG13qmoR-RrlWARpaXNp5qU,415
|
|
16
16
|
neo4j_viz/resources/icons/zoom-out.svg,sha256=OQRADAoe2bxbCeFufg6W22nR41q5NlI8QspT9l5pXUw,400
|
|
17
17
|
neo4j_viz/resources/nvl_entrypoint/base.js,sha256=QfFjqd7Uo2F-plgFlPfLzUcjqzTzqUh533VaBemJhLI,1816247
|
|
18
18
|
neo4j_viz/resources/nvl_entrypoint/styles.css,sha256=JjeTSB9OJT2KMfb8yFUUMLMG7Rzrf3o60hSCD547zTk,1123
|
|
19
|
-
neo4j_viz-0.
|
|
20
|
-
neo4j_viz-0.
|
|
21
|
-
neo4j_viz-0.
|
|
22
|
-
neo4j_viz-0.
|
|
19
|
+
neo4j_viz-0.4.0.dist-info/METADATA,sha256=J3KTYHc4S8w50RqS4VbTaOQ4EaAWjGqyzVQn9zfU7OY,7074
|
|
20
|
+
neo4j_viz-0.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
21
|
+
neo4j_viz-0.4.0.dist-info/top_level.txt,sha256=jPUM3z8MOtxqDanc2VzqkxG4HJn8aaq4S7rnCFNk_Vs,10
|
|
22
|
+
neo4j_viz-0.4.0.dist-info/RECORD,,
|
|
File without changes
|