user1295112
user1295112

Reputation: 1063

Storing and Accessing node attributes python networkx

I have a network of nodes created using python networkx. i want to store information in nodes such that i can access the information later based on the node label (the name of the node) and the field that in which the information has been stored (like node attributes). the information stored can be a string or a number I wish to do so in a manner such that if xyz is a node:

then I want to save two or three fields having strings like the date of birth of xyz dob=1185, the place of birth of xyz pob=usa, and the day of birth of xyz dayob=monday.

I know that i can use G.add_node has the attribute dictionary field in it...but I can't seem to access it for a particular field. if there is any other way i would appreciate it.

i then want to compare xyz with other nodes in the networks having the same information in common. i.e. intersection of node xyz with node abc based on date of bith, place of birth and day of birth

e.g for if nodes xyz and abc have an edge print their respective dobs, their pobs and their dayobs

Upvotes: 96

Views: 143525

Answers (5)

Marine Galantin
Marine Galantin

Reputation: 2279

Apparently now

G.node[1]['name'] = 'alpha'

is no longer valid in the current version of NetworkX - after version 2.4 included, Thanks Ben for adding the reference to it. Instead, you should use:

G.nodes[1]['name'] = 'alpha'

I found this information in the NetworkX documentation: NetworkX Graph Nodes Documentation.

Here's an example of how you can add and access node attributes:

import networkx as nx

# Create a graph
G = nx.Graph()

# Add a node with attributes
G.add_node('xyz', dob=1185, pob='usa', dayob='monday')
G.add_node('abc', dob=1185, pob='usa', dayob='monday')

# Access node attributes
dob_xyz = G.nodes['xyz']['dob']
pob_xyz = G.nodes['xyz']['pob']
dayob_xyz = G.nodes['xyz']['dayob']

print(f"XYZ - DOB: {dob_xyz}, POB: {pob_xyz}, Day of Birth: {dayob_xyz}")

# Compare nodes based on attributes
for node in G.nodes:
    if (G.nodes[node]['dob'] == dob_xyz and
        G.nodes[node]['pob'] == pob_xyz and
        G.nodes[node]['dayob'] == dayob_xyz):
        print(f"Node {node} has the same DOB, POB, and Day of Birth as XYZ")

Upvotes: 7

Neil
Neil

Reputation: 8634

To add attributes as dictionary you can do the following

g.add_node('node_id', **{"attr1": "val1", "attr2": "val2"})

p.s. if you don't add ** you'll get exception: TypeError: add_node() takes 2 positional arguments but 3 were given

Upvotes: 0

Joel
Joel

Reputation: 23897

Additionally, you don't have to just assign the attributes when the node is added. Even after it's been added you can still set them directly.

import networkx as nx
G=nx.Graph()
G.add_edge(1,2)
#see comment below code for recent versions of networkx.
G.nodes[1]['name'] = 'alpha'
G.nodes[2]['name'] = 'omega'

G.nodes[1]['name']
> 'alpha'

Note: For versions before 2.4, use G.node[] instead of G.nodes[]. See deprecation notes.

You can also use set_node_attributes (documentation) which will let you set an attribute for multiple nodes at the same time:

edit

#use the next line if it's networkx version 1.x
#nx.set_node_attributes(G, 'cost', {1:3.5, 2:56})

#use the next line if it's networkx version 2.x
#nx.set_node_attributes(G, {1:3.5, 2:56}, 'cost')

#or for something that works for 1.x or 2.x
nx.set_node_attributes(G, values = {1:3.5, 2:56}, name='cost')

G.node[1]['cost']
> 3.5

Similar approaches can be used to set edge attributes.

Upvotes: 56

Maehler
Maehler

Reputation: 6331

As you say, it's just a matter of adding the attributes when adding the nodes to the graph

G.add_node('abc', dob=1185, pob='usa', dayob='monday')

or as a dictionary

G.add_node('abc', {'dob': 1185, 'pob': 'usa', 'dayob': 'monday'})

To access the attributes, just access them as you would with any dictionary

G.node['abc']['dob'] # 1185
G.node['abc']['pob'] # usa
G.node['abc']['dayob'] # monday

You say you want to look at attributes for connected nodes. Here's a small example on how that could be accomplished.

for n1, n2 in G.edges_iter():
    print G.node[n1]['dob'], G.node[n2]['dob']
    print G.node[n1]['pob'], G.node[n2]['pob']
    # Etc.

As of networkx 2.0, G.edges_iter() has been replaced with G.edges(). This also applies to nodes. We set data=True to access attributes. The code is now:

for n1, n2 in list(G.edges(data=True)):
    print G.node[n1]['dob'], G.node[n2]['dob']
    print G.node[n1]['pob'], G.node[n2]['pob']
    # Etc.

NOTE: In networkx 2.4, G.node[] has been replaced with G.nodes[].

Upvotes: 99

severinsimmler
severinsimmler

Reputation: 103

As of networkx v2.0, you can use:

import networkx as nx

G = nx.Graph()
G.add_node('abc', dob=1185, pob='usa', dayob='monday')
nx.get_node_attributes(G, 'dob')
> {'abc': 1185}

You can access this dictionary as usual:

{'abc': 1185}['abc']
> 1185

Upvotes: 9

Related Questions