py2neo icon indicating copy to clipboard operation
py2neo copied to clipboard

networkx export of the graph

Open geoHeil opened this issue 4 years ago • 4 comments
trafficstars

how can I export the graph to networkx which is obtained by running some cypher query?

geoHeil avatar Apr 29 '21 14:04 geoHeil

Right now, there's no direct way to do this. You would have to graph.run(...).to_subgraph() and then manually iterate through the nodes and relationships to convert them into NetworkX format.

Out of interest, how would you expect to map the node labels and relationship types onto a NetworkX graph?

technige avatar May 05 '21 08:05 technige

Well a simple solution such as https://neo4j.com/labs/apoc/4.1/export/graphml/ what is happening for t.graphml.all would do the job already i.e. having a Networkx MultiDiGraph with an edge attribute relationship_kin / node_kind to store the label /type and then all the attributer as key/value lookups.

geoHeil avatar May 05 '21 17:05 geoHeil

This would be wonderful!

priamai avatar Aug 30 '21 05:08 priamai

hi @geoHeil , i am not familiar with NetworkX but if you just need a graphml export what about a tiny CALL apoc.export.graphml.all(null, {stream:true}) ?

from py2neo import Graph

g = Graph(name="test")

# Create testdata on db1
g.run("CREATE (n:MyNode) SET n.id = 1")
g.run("CREATE (n:MyNode2) SET n.id = 2")

# Extract data from db1
graphml = g.run(
    "CALL apoc.export.graphml.all(null, {stream:true}) yield file, nodes, relationships, properties, data RETURN file, nodes, relationships, properties, data"
).to_data_frame()
print(graphml)

motey avatar Oct 07 '21 15:10 motey