Which elements from networkx graph might become labels at neo4j graph?

Viewed 445

I have a MultiDiGraph in networkx and I am thinking about exporting it to neo4j. I have already a property for the nodes called ntype and a property for the edges called dtype which fits with the label concept for nodes and edges for neo4j graphs.

Which elements from networkx might become labels at neo4j? How can it be managed?

I add an example of the graph at python and the graph I would like to get when exporting to neo4j.

Here is the code at python

import networkx as nx

G=nx.MultiDiGraph()

G.add_nodes_from([
    (1,{'ntype':'NUMBER', 'color':'blue'}),
    (2,{'ntype':'NUMBER', 'color':'yellow'}),
    ('A',{'ntype':'LETTER', 'state':'done'}),
    ('B',{'ntype':'LETTER', 'state':'pending'})
    ])
G.add_edges_from([
    (1,'A','dir',{'etype':'IMPACTS', 'when':'old'}),
    (1,'A','n_dir',{'etype':'IMPACTS', 'when':'new'}),
    (1,'B','n_dir',{'etype':'ALTS', 'when':'future'}),
    (2,'B','dir',{'etype':'IMPACTS', 'when':'new'}),
    (2,'B','n_dir',{'etype':'IMPACTS', 'when':'old'}),
    (2,'A','n_dir',{'etype':'ALTS', 'when':'new'}),
    ])

And here is the code at neo4j of the graph I would like to have when importing from networkx

CREATE (one:NUMBER {name:1, color:'blue'})
CREATE (two:NUMBER {name:2, color:'yellow'})
CREATE (A:LETTER {name:'A', state:'done'})
CREATE (B:LETTER {name:'B', state:'pending'})
CREATE (one)-[:IMPACTS {k:'dir', when:'old'}]->(A)
CREATE (one)-[:IMPACTS {k:'n_dir', when:'new'}]->(A)
CREATE (one)-[:IMPACTS {k:'n_dir', when:'future'}]->(B)
CREATE (two)-[:ALTS {k:'dir', when:'new'}]->(B)
CREATE (two)-[:IMPACTS {k:'n_dir', when:'old'}]->(B)
CREATE (two)-[:ALTS {k:'n_dir', when:'new'}]->(A)

This is a picture of the the graph

I have tried exporting in networkx to graphml and importing in neo4j as described here enter link description here. I also added the parameters readLabels and storeNodeIds. From the browser:

CALL apoc.import.graphml("test.graphml", {readLabels: true, storeNodeIds:true})

Now I get the labels for the edges but I do not get the labels for the nodes. This is what I get:

enter image description here

1 Answers

I made some reserch and finally I have got a process to be able to export a networkx graph into a neo4j graph managing the labels. Some details have to be considered:

1.- When creating the nodes: the property to become a label at neo4j must be named as labels (notice that it is in plural, do not ask why, it works). Its value must be a string starting with ':'

G.add_nodes_from([(1,{'labels':':NUMBER', 'color':'blue'}), ('A',{'labels':':LETTER', 'state':'done'})])

2.- When creating the edges: the property to become a label at neo4j must be named as label (now it is in singular!!). Its value must be a string starting with ':'

G.add_edges_from([(1,'A','dir',{'label':'IMPACTS', 'when':'old'})])

3.- For MultiDiGraph the k value is not going to be imported. My workarround is to duplicate k as an edge property called, for exampel id.

for u,v,k in G.edges(keys=True):
G[u][v][k]['id']=k

4.- Export networkx graph to graphml using named_key_ids=True

nx.write_graphml(G, 'test.graphml', named_key_ids=True)

5.- Import to neo4j with parameteres readLabels: true and storeNodeIds:true. At neo4j browser you might have:

CALL apoc.import.graphml("test.graphml", {readLabels: true, storeNodeIds:true})

Here I include a graph created in networkx that can be imported to neo4j as described:

import networkx as nx

G=nx.MultiDiGraph()

#creation of nodes with property called 'labels'
G.add_nodes_from([
    (1,{'labels':':NUMBER', 'color':'blue'}),
    (2,{'labels':':NUMBER', 'color':'yellow'}),
    ('A',{'labels':':LETTER', 'state':'done'}),
    ('B',{'labels':':LETTER', 'state':'pending'})
    ])
#creation of edges with property called 'label'
G.add_edges_from([
    (1,'A','dir',{'label':'IMPACTS', 'when':'old'}),
    (1,'A','n_dir',{'label':'IMPACTS', 'when':'new'}),
    (1,'B','n_dir',{'label':'ALTS', 'when':'future'}),
    (2,'B','dir',{'label':'IMPACTS', 'when':'new'}),
    (2,'B','n_dir',{'label':'IMPACTS', 'when':'old'}),
    (2,'A','n_dir',{'label':'ALTS', 'when':'new'}),
    ])
#k is duplicated at every node as a property
for u,v,k in G.edges(keys=True):
    G[u][v][k]['id']=k

nx.write_graphml(G, 'test.graphml', named_key_ids=True)

What is pending? To figure out how to assing to a node or an edge more than one label. Any help will be welcome

Related