ganninu93
ganninu93

Reputation: 1601

Creating an iGraph from a neo4j-driver cypher query

I am trying to do something similar to this, but using neo4j-driver instead of py2neo. When I run the following code, I get a list of all the nodes returned by the query, however i graph does not create any nodes.

from igraph import Graph
from neo4j.v1 import GraphDatabase, basic_auth

driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "pass123"))
session = driver.session()

result = session.run("MATCH (a:author)-[r:PUBLISHED]->(p:paper) RETURN a,r,p")

for record in result:
  print(record)

g = Graph.TupleList(result)
print(g)
session.close()

Console result:

<Record a=<Node id=946 labels=set([u'author']) properties={u'name': u'a9', u'id': u'9'}> r=<Relationship id=950 start=946 end=955 type=u'PUBLISHED' properties={}> p=<Node id=955 labels=set([u'paper']) properties={u'year': 2009, u'id': u'9', u'name': u'p9'}>>
<Record a=<Node id=946 labels=set([u'author']) properties={u'name': u'a9', u'id': u'9'}> r=<Relationship id=949 start=946 end=953 type=u'PUBLISHED' properties={}> p=<Node id=953 labels=set([u'paper']) properties={u'year': 2007, u'id': u'7', u'name': u'p7'}>>
IGRAPH UN-- 0 0 --
+ attr: name (v)

Can someone tell me why this doesn't work please?

Upvotes: 0

Views: 753

Answers (1)

William Lyon
William Lyon

Reputation: 8546

Py2neo's cypher.execute method returns an object that is essentially a list of dicts (or named tuples). The neo4j-driver does not. Instead you'll need to iterate through the cursor object returned by session.run and construct a list of tuples to pass into the igraph constructor. The example here is using data from the BuzzFeed Trumpworld graph which just happens to be what I have in Neo4j at the moment, so adapt the query to your needs:

from igraph import Graph
from neo4j.v1 import GraphDatabase, basic_auth

driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "pass123"))
session = driver.session()

result = session.run("MATCH (p1:Person)-[r]->(p2:Person) RETURN p1.name AS name1, p2.name AS name2")
nodelist = []
for record in result:
    nodelist.append((record["name1"], record["name2"]))

nodelist is a list of tuples, that looks like this:

>>> print(nodelist)

[('RUDY GIULIANI', 'WILBUR ROSS'),
('GARY COHN', 'DONALD J. TRUMP'),
('DAN COATS', 'DONALD J. TRUMP'),
('MICHAEL POMPEO', 'DONALD J. TRUMP'),
('OMAROSÉ ONEE MANIGAULT', 'DONALD J. TRUMP'),
('MICK MULVANEY', 'DONALD J. TRUMP'),
('ALEX SHNAIDER', 'DONALD J. TRUMP'),
('MEHMET ALI YALCINDAG', 'DONALD J. TRUMP'),
('MANGAL PRABHAT LODHA', 'DONALD J. TRUMP'),
('ROGER KHAFIF', 'DONALD J. TRUMP')...

Then to instantiate an igraph object:

g = Graph.TupleList(nodelist)

We can run PageRank on this graph and see which node has the highest PageRank as a sanity check:

pg = g.pagerank()
max_pg = max(pg)
[g.vs[idx]["name"] for idx, pg in enumerate(pg) if pg == max_pg]

And the result is:

['DONALD J. TRUMP']

Upvotes: 2

Related Questions