/
bolt_util.py
73 lines (64 loc) · 2.3 KB
/
bolt_util.py
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
from .pygraphistry import util
node_id_key = u'_bolt_node_id_key'
node_type_key = u'type'
node_label_prefix_key = u'_lbl_'
start_node_id_key = u'_bolt_start_node_id_key'
end_node_id_key = u'_bolt_end_node_id_key'
relationship_id_key = u'_bolt_relationship_id'
relationship_type_key = u'type'
def is_neotime(v):
try:
return v.__module__ == 'neotime'
except:
return False
def stringify_neotimes(df):
#Otherwise currently encountering a toString error
import neotime
df2 = df.copy()
for c in df.columns:
df2[c] = df[c].apply(lambda v: str(v) if is_neotime(v) else v)
return df2
def to_bolt_driver(driver=None):
if driver is None:
return None
try:
from neo4j import GraphDatabase, Driver
if isinstance(driver, Driver):
return driver
return GraphDatabase.driver(**driver)
except ImportError:
raise BoltSupportModuleNotFound()
def bolt_graph_to_edges_dataframe(graph):
import pandas as pd
df = pd.DataFrame([
util.merge_two_dicts(
{ key: value for (key, value) in relationship.items() },
{
relationship_id_key: relationship.id,
relationship_type_key: relationship.type,
start_node_id_key: relationship.start_node.id,
end_node_id_key: relationship.end_node.id
}
)
for relationship in graph.relationships
])
return stringify_neotimes(df)
def bolt_graph_to_nodes_dataframe(graph):
import pandas as pd
df = pd.DataFrame([
util.merge_two_dicts(
{ key: value for (key, value) in node.items() },
util.merge_two_dicts(
{
node_id_key: node.id,
node_type_key: ",".join(sorted([str(label) for label in node.labels]))
},
{ node_label_prefix_key + str(label): True for label in node.labels }))
for node in graph.nodes
])
return stringify_neotimes(df)
class BoltSupportModuleNotFound(Exception):
def __init__(self):
super(BoltSupportModuleNotFound, self).__init__(
"The neo4j module was not found but is required for pygraphistry bolt support. Try running `!pip install pygraphistry[bolt]`."
)