Getting nodes attributes from a list of properties

1,438 views
Skip to first unread message

Maristela Carvalho de Jesus

unread,
Jan 27, 2022, 2:54:13 PM1/27/22
to networkx-discuss
Hi :), 

I am loading my Neo4J graph 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))

This is a list of properties of one of my nodes (not every node has the same property) 

<id>: 65
Age: 34
Days: 200
Gender: F
Name: 2

I tried to convert it to NumPy array without success.

Any idea how I could get each property of my node  “Age”, “Gender” and so

Thank you in advance!
Stela.



Dan Schult

unread,
Jan 27, 2022, 2:56:25 PM1/27/22
to networkx...@googlegroups.com
This looks like a questions for DGL Graph.
It looks like the attributes are in NetworkX correctly. 
But we don't have control over what DGL is trying to do to get those values.
Best,
Dan Schult

Maristela Carvalho de Jesus

unread,
Jan 27, 2022, 3:06:42 PM1/27/22
to networkx-discuss
Hi Dan, 

Thank you for your reply. Indeed it seems that the attributes are correct in NetworkX. However, since I didn't manage to load them into DGL I was thinking if I could split the node properties in such a way that I would get only one specific attribute. For example, how could I get only the "Age" from the list of properties?

Thank you,
Maristela.

Dan Schult

unread,
Jan 27, 2022, 5:36:16 PM1/27/22
to networkx...@googlegroups.com
Do you mean something like this?:
Age = nx.get_node_attributes(G, "Age")
which returns a dict keyed by node to the value stored for that node as Age.

If you want to remove the attributes except for Age, you can remove each attribute from all the node data dictionaries.  Or you can build a new graph with the nodes and edges and only add the attribute you want.
newG=nx.Graph()
newG.add_nodes_from(G)
newG.add_edges_from(G.edges)
nx.set_node_attributes(newG, Age, nx.get_node_attributes(G, "Age")) 

(untested)


--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/networkx-discuss/9395ed94-113a-414e-a7a4-4c28fde56d80n%40googlegroups.com.

Maristela Carvalho de Jesus

unread,
Jan 28, 2022, 4:17:54 PM1/28/22
to networkx-discuss
Hi Dan,

Thanks again!

This is exactly what I would like to do, but it doesn't work for the "Age" attribute; it only works for properties. It seems like I have a nested dictionary. 
If, I try:
Age = nx.get_node_attributes(G, "Age")
I get an empty list, the same happens with any other attributes. The only way that I can get is with properties:
prop = nx.get_node_attributes(G, "properties")

It seems like the format of my node attributes is not correct, and I can't figure out how to fix it. 

I am attaching the image of the results.

Thank you,
Maristela.


Attr_nx.PNG

luciano Mattar

unread,
Apr 7, 2023, 9:46:24 AM4/7/23
to networkx-discuss
Hi Maristela,
I'm having the same problem as you. Did you get a solution? Could you please share a code example of how to create a network in networkx from a list returned from a query in python driver?
Sincerely
Reply all
Reply to author
Forward
0 new messages