forked from neo4j-contrib/gds-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgds.py
More file actions
358 lines (301 loc) · 12.9 KB
/
gds.py
File metadata and controls
358 lines (301 loc) · 12.9 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
from graphdatascience import GraphDataScience
import uuid
from contextlib import contextmanager
import logging
logger = logging.getLogger("mcp_server_neo4j_gds")
@contextmanager
def projected_graph_from_params(gds, node_labels=None, undirected=False, **kwargs):
"""
Project a graph from the database, delegating to projected_graph.
Supports both:
- Positional node_labels as the 2nd argument.
- nodeLabels/relTypes passed either directly as kwargs or nested under a 'kwargs' dict.
Returns the projected GDS graph. The caller is responsible for dropping it
(e.g., gds.graph.drop(G)).
"""
# Unwrap if the caller passed a nested 'kwargs' dict
params = (
kwargs.get("kwargs")
if "kwargs" in kwargs and isinstance(kwargs.get("kwargs"), dict)
else kwargs
)
# Prefer explicit node_labels positional arg, fall back to params["nodeLabels"]
nodeLabels = params.get("nodeLabels", node_labels)
relTypes = params.get("relTypes")
with projected_graph(
gds, node_labels=nodeLabels, relationship_types=relTypes, undirected=undirected
) as G:
yield G
@contextmanager
def projected_graph(gds, node_labels=None, relationship_types=None, undirected=False):
"""
Project a graph from the database.
Args:
gds: GraphDataScience instance
node_labels: specifies node labels to project. If empty, all nodes are projected. Default is []
relationship_types: specifies relationship types to project. If empty, all relationships are projected. Default is [].
undirected: If True, project as undirected graph. Default is False (directed).
"""
if relationship_types is None:
relationship_types = []
if node_labels is None:
node_labels = []
graph_name = f"temp_graph_{uuid.uuid4().hex[:8]}"
try:
# Get relationship properties (non-string)
rel_properties = get_relationship_properties_keys(gds, relationship_types)
valid_rel_properties = validate_rel_properties(
gds, rel_properties, node_labels, relationship_types
)
rel_prop_map = ", ".join(f"{prop}: r.{prop}" for prop in valid_rel_properties)
# Get node properties and validate to see which are compatible with GDS
node_properties = get_node_properties_keys(gds, node_labels)
valid_node_projection_properties = validate_node_properties(
gds, node_properties
)
node_prop_map_source = create_source_projection_properties(
valid_node_projection_properties
)
node_prop_map_target = create_target_projection_properties(
valid_node_projection_properties
)
logger.info(f"Node property map source: '{node_prop_map_source}'")
logger.info(f"Node property map target: '{node_prop_map_target}'")
# Configure graph projection based on undirected parameter
# Create data configuration (node/relationship structure)
data_config_parts = [
"sourceNodeLabels: labels(n)",
"targetNodeLabels: labels(m)",
"relationshipType: type(r)",
]
# create projection query for node/rel entities
match_proj_query = create_projection_query(node_labels, relationship_types)
if node_prop_map_source or node_prop_map_target:
data_config_parts.extend(
[
f"sourceNodeProperties: {{{node_prop_map_source}}}",
f"targetNodeProperties: {{{node_prop_map_target}}}",
]
)
if rel_prop_map:
data_config_parts.append(f"relationshipProperties: {{{rel_prop_map}}}")
data_config = ", ".join(data_config_parts)
# Create additional configuration
additional_config_parts = []
if undirected:
additional_config_parts.append("undirectedRelationshipTypes: ['*']")
additional_config = (
", ".join(additional_config_parts) if additional_config_parts else ""
)
# Use separate data and additional configuration parameters
if additional_config:
project_query = f"""
{match_proj_query}
WITH n, r, m
RETURN gds.graph.project(
$graph_name,
n,
m,
{{{data_config}}},
{{{additional_config}}}
)
"""
logger.info(f"Project query: '{project_query}'")
G, _ = gds.graph.cypher.project(
project_query,
graph_name=graph_name,
)
else:
projection_query = f"""
{match_proj_query}
WITH n, r, m
RETURN gds.graph.project(
$graph_name,
n,
m,
{{{data_config}}}
)
"""
logger.info(f"Projection query: '{projection_query}'")
G, _ = gds.graph.cypher.project(
projection_query,
graph_name=graph_name,
)
yield G
finally:
gds.graph.drop(graph_name)
def get_node_labels(gds: GraphDataScience):
query = """
MATCH (n)
WITH DISTINCT labels(n) AS labels
UNWIND labels AS label
WITH DISTINCT label
RETURN COLLECT(label) AS labels
"""
df = gds.run_cypher(query)
if df.empty:
return []
return df["labels"].iloc[0]
def count_nodes(gds: GraphDataScience):
with projected_graph(gds) as G:
return G.node_count()
def get_node_properties_keys(gds: GraphDataScience, node_labels=None):
if node_labels is None:
node_labels = []
nodelabels_query = create_node_cypher_match_query(node_labels)
property_extractor = """
WITH keys(properties(n)) AS prop_keys_list
UNWIND prop_keys_list AS prop_keys
WITH DISTINCT prop_keys
RETURN COLLECT(prop_keys) AS properties_keys
"""
query = nodelabels_query + property_extractor
df = gds.run_cypher(query)
if df.empty:
return []
return df["properties_keys"].iloc[0]
def get_relationship_properties_keys(gds: GraphDataScience, relationshipTypes=None):
if relationshipTypes is None:
relationshipTypes = []
rel_query = create_relationship_cypher_match_query([], relationshipTypes)
property_extractor = """
WITH keys(properties(r)) AS prop_keys_list
UNWIND prop_keys_list AS prop_keys
WITH DISTINCT prop_keys
RETURN COLLECT(prop_keys) AS properties_keys
"""
query = rel_query + property_extractor
df = gds.run_cypher(query)
if df.empty:
return []
return df["properties_keys"].iloc[0]
def get_relationship_types(gds: GraphDataScience, node_labels=None):
if node_labels is None:
node_labels = []
type_extractor = """
WITH type(r) AS type
WITH DISTINCT type
RETURN COLLECT(type) AS relationship_types
"""
query = create_relationship_cypher_match_query(node_labels, []) + type_extractor
df = gds.run_cypher(query)
if df.empty:
return []
return df["relationship_types"].iloc[0]
def create_projection_query(node_labels, rel_types):
node_query = create_node_cypher_match_query(node_labels)
rel_query = create_relationship_cypher_match_query(node_labels, rel_types)
match_proj_query = f"{node_query} OPTIONAL {rel_query}"
return match_proj_query
def create_node_cypher_match_query(node_labels):
if len(node_labels) > 0:
match_query = f"MATCH (n) WHERE ANY(l IN labels(n) WHERE l IN {node_labels})"
else:
match_query = "MATCH (n)"
return match_query
def create_relationship_cypher_match_query(node_labels, relationship_types):
label_constraint = f"ANY(l IN labels(n) WHERE l IN {node_labels}) AND ANY(l IN labels(m) WHERE l IN {node_labels})"
type_constraint = f"type(r) IN {relationship_types}"
match_query = "MATCH (n)-[r]->(m)"
if len(node_labels) > 0 and len(relationship_types) > 0:
return match_query + f" WHERE {label_constraint} AND {type_constraint}"
elif len(relationship_types) > 0:
return match_query + f" WHERE {type_constraint}"
elif len(node_labels) > 0:
return match_query + f" WHERE {label_constraint}"
else:
return match_query
def validate_rel_properties(
gds: GraphDataScience, rel_properties, node_labels=None, rel_types=None
):
if rel_types is None:
rel_types = []
if node_labels is None:
node_labels = []
match_rel_query = create_relationship_cypher_match_query(node_labels, rel_types)
valid_rel_properties = {}
for i in range(len(rel_properties)):
pi = gds.run_cypher(
f"""
{match_rel_query}
WITH r.{rel_properties[i]} AS prop
WHERE prop IS NOT NULL
WITH
CASE
WHEN prop IS :: FLOAT THEN 1
WHEN prop IS :: INTEGER THEN 1
ELSE 2
END AS INVALID_PROP_TYPE
RETURN distinct(INVALID_PROP_TYPE)
"""
)
if pi.shape[0] == 1 and int(pi["INVALID_PROP_TYPE"][0]) == 1:
valid_rel_properties[rel_properties[i]] = f"toFloat(r.{rel_properties[i]}"
return valid_rel_properties
def validate_node_properties(gds: GraphDataScience, node_properties, node_labels=None):
if node_labels is None:
node_labels = []
projectable_properties = {}
for i in range(len(node_properties)):
# Check property types and whether all values are whole numbers
match_node_query = create_node_cypher_match_query(node_labels)
type_check = gds.run_cypher(
f"""
{match_node_query}
WITH n.{node_properties[i]} AS prop
WHERE prop IS NOT NULL
RETURN
prop IS :: LIST<FLOAT NOT NULL> AS IS_LIST_FLOAT,
prop IS :: LIST<INTEGER NOT NULL> AS IS_LIST_INTEGER,
prop IS :: INTEGER AS IS_INTEGER,
prop IS :: FLOAT AS IS_FLOAT,
CASE
WHEN prop IS :: FLOAT THEN null
WHEN prop IS :: INTEGER THEN null
WHEN prop IS :: LIST<FLOAT NOT NULL> THEN null
WHEN prop IS :: LIST<INTEGER NOT NULL> THEN null
ELSE 1
END AS INVALID_PROP_TYPE
LIMIT 10
"""
)
if not type_check.empty:
has_invalids = len(type_check["INVALID_PROP_TYPE"].dropna()) > 0
if not has_invalids: # all properties are ok
has_ints = any(type_check["IS_INTEGER"])
has_floats = any(type_check["IS_FLOAT"])
has_lists_float = any(type_check["IS_LIST_FLOAT"])
has_lists_int = any(type_check["IS_LIST_INTEGER"])
has_lists = has_lists_float or has_lists_int
has_nums = has_ints or has_floats
if has_nums and not has_lists:
if has_floats:
projectable_properties[node_properties[i]] = "FLOAT"
else:
projectable_properties[node_properties[i]] = "INTEGER"
if has_lists and not has_nums:
if has_lists_float:
projectable_properties[node_properties[i]] = "FLOAT_LIST"
else:
projectable_properties[node_properties[i]] = "INTEGER_LIST"
return projectable_properties
def create_source_projection_properties(projectable_properties):
return create_node_projection_properties(projectable_properties, "n")
def create_target_projection_properties(projectable_properties):
return create_node_projection_properties(projectable_properties, "m")
def create_node_projection_properties(projectable_properties, variable):
valid_node_properties = {}
for prop in projectable_properties:
property_type = projectable_properties[prop]
if property_type == "FLOAT_LIST":
valid_node_properties[prop] = f"toFloatList({variable}. {prop})"
elif property_type == "FLOAT":
valid_node_properties[prop] = f"toFloat({variable}. {prop})"
elif property_type == "INTEGER_LIST" or property_type == "INTEGER":
valid_node_properties[prop] = f"{variable}. {prop}"
else:
raise "should never end up here"
node_prop_map = ", ".join(
f"{prop}: {expr}" for prop, expr in valid_node_properties.items()
)
return node_prop_map