Getting node_attrs from NetworkX Graph (after exporting from Neo4j)

Hi all! :slight_smile:

I am new here, I am trying to create my first graph in DGL and I would need some help, I googled and looked into the older threads but I couldn’t find an answer.

I have a graph in Neo4J and I would like to load it into DGL. For that I first load from Neo4J to NetworkX as per below:


q1 = """
MATCH (n)-[r]->(c) RETURN *
"""
G = nx.MultiDiGraph()
nodes = list(results.graph()._nodes.values())
for node in nodes:
    G.add_node(node.id, labels=node._labels, properties=node._properties)
relationships = list(results.graph()._relationships.values())
for rel in relationships:
    G.add_edge(rel.start_node.id, rel.end_node.id, key=rel.id, type=rel.type, properties=rel._properties)

Now, I am trying to convert my NetworkX graph to DGL graph:

g_dgl = dgl.from_networkx(G, node_attrs, edge_attrs)

However, I am having problems extracting the node_attrs from NetworkX: my nodes have many properties like “Age”, “Gender” and so. When I check my nodes attributes in NetworkX everything is visible, but everything is in a set of properties, so for example I don’t manage to get just the property “Age”:

node_attrs = list(G.nodes(data=True))

I tried to convert it to NumPy array without success.

Any idea how I could load the node_attrs to DGL?

Thank you in advance!
Stela.

DGL’s node attributes and edge attributes must be tensors. It seems that your node/edge attributes are strings, and therefore you will need to encode them yourself (e.g. to integers).

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.