Skip to content Skip to sidebar Skip to footer

Networkx With Errors From Adding Nodes And Edges

This is a simple python program to generate a network graph. Everything was fine when I placed the data inside the program but when I decided to place the data in two input files,

Solution 1:

The format expected by add_edges_from is a list of tuples, in its most basic form, as a list of (u,v) pairs to connect.

Your files don't have the data in an appropriate form so networkx doesn't know what to do with them. If the text is exactly as you wrote in the "edges" variable, then below is one way to massage it into the right kind of list. You can do similar for the node processing, though this only needs a list of elements, not a list of tuples so is more straightforward.

withopen("edgefile.txt") as data_file:
    data_edges = data_file.read()

# split on the comma, assuming this divides elements, remove the curly braces and quotes
elems = ([f.strip(" {}'") for f in data_edges.strip().split(',')])
# use zip to turn the flat list into a lst of pairs
edge_list = zip(elems[::2], elems[1::2])

# now we are in a form that nx.add_edges_from can handle
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edge_list)

You should read the docs on reading in graphs here: https://networkx.github.io/documentation/stable/reference/readwrite/index.html

which describes how to read from various standard graph formats.


EDIT following qu in comment:

The reason that you have many "unexpected" nodes in the graph is because nx.add_nodes_from takes an iterable type, and when you read the whole file into a text variable, iterating over that string takes one character at at time. This produces the single-character nodes like 0, ', and \n. So we can fix it by parsing the string into a list, and iterating over a list gives one element, like '0000000002'.

Here is an example:

# assume that the file describing nodes is read into this string:
node_txt = "'0000000002', '0000000101', '0000000111', '0000000200', '0000000502', '0000000600', '0000001000', '0000001001', '0000001069', '0000001253', '0000001462', '0000003013', '0000003200', '0000004100', '0000004305', '0000005100', '0000005460', '0000006600', '0000010021', '0000010101', '0000010200', '0000010314', '0000012000', '0000012151', '0000012600', '0000015201', '0000016100', '0000017002', '0000020002', '0000020050', '0000020100', '0000021001', '0000022044', '0000022100'\n"

G1 = nx.Graph()
G1.add_nodes_from(node_txt)
print(G1.nodes())
print(set(node_txt))
# output of these two commands shows that the node names are 1 char each:>>> [' ', "'", '\n', ',', '1', '0', '3', '2', '5', '4', '7', '6', '9']
>>> set([' ', "'", '\n', ',', '1', '0', '3', '2', '5', '4', '7', '6', '9'])

# reference: what we really wanted    
node_list = ['0000000002', '0000000101', '0000000111', '0000000200', '0000000502', '0000000600', '0000001000', '0000001001', '0000001069', '0000001253', '0000001462', '0000003013', '0000003200', '0000004100', '0000004305', '0000005100', '0000005460', '0000006600', '0000010021', '0000010101', '0000010200', '0000010314', '0000012000', '0000012151', '0000012600', '0000015201', '0000016100', '0000017002', '0000020002', '0000020050', '0000020100', '0000021001', '0000022044', '0000022100']

G2  = nx.Graph()
G2.add_nodes_from(node_list)
print(G2.nodes())
print(set(node_list))

So how to transform node_txt into the form of node_list? We follow the same process as described above for the edges - this one is a bit simpler.

elems = [f.strip(" '") for f in node_txt.strip().split(',')]
print(elems == node_list)
# output: True -> so here we recovered the node names correctly from node_txt

Post a Comment for "Networkx With Errors From Adding Nodes And Edges"