/
neodgl.py
68 lines (53 loc) · 1.84 KB
/
neodgl.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
import neo4j
from neo4j import GraphDatabase
from neo4j.graph import Node, Relationship
import pandas as pd
import dgl
import matplotlib.pyplot as plt
import numpy as np
class edge_list():
"""Class to gather the edge list and create dgl graph"""
def __init__(self, uri: str, password: str) -> None:
self.driver = GraphDatabase.driver(uri, auth=("neo4j", password))
def close(self) -> None:
self.driver.close()
@classmethod
def edge_list(cls, tx) -> any:
query = ("""
MATCH path=(m)--(n)
RETURN ID(m) AS u, ID(n) AS v
""")
result = tx.run(query)
#return a dataframe
return result.data()
def get_edge_list(self) -> any:
result = self.driver.session().write_transaction(self.edge_list)
return pd.DataFrame(result)
def dgl_graph_from_cypher(self, data: pd.DataFrame) -> None:
"""
Takes the whole graph and creates dgl graph
ARGS:
data is the edge list in a pandas df
RETURNS: dgl graph
"""
#THIS IS UNDIRECTED GRAPH
u = data["u"].to_numpy()
v = data["v"].to_numpy()
#Building diagram
return dgl.graph((u, v))
def true_labels(cls, tx):
query = ("""
MATCH (n)
RETURN ID(n) as id, n.team as team
""")
result = tx.run(query)
#return a dataframe
return result.data()
def get_true_labels(self) -> any:
result = self.driver.session().write_transaction(self.true_labels)
return pd.DataFrame(result)
if __name__ == "__main__":
hello = edge_list()
data = hello.dgl_graph_from_cypher(hello.get_edge_list())
print('We have %d nodes.'% data.number_of_nodes())
print('We have %d edges.'% data.number_of_edges())