Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nx.write_gexf in Python for graphs that have dict data on nodes and edges

I have a graph constructed using the networkx package in Python that has data attributes associated with both nodes and edges. These attributes are dictionaries (or lists) themselves with nested dictionaries. I can't figure out how to write this graph to .gexf format (or .graphml etc) because of the data type.

Is there a way to get write_gexf to parse these data types to XML? or is there some other workaround?

Here's an example:

1 import networkx as nx
2
3 G = nx.graph()
4 G.add_node(0, attr1 = { 'name1' : 'Alice', 'name2' : 'Bob' }, attr2 = 5)
5 G.add_node(0, attr1 = { 'name1' : 'Calvin', 'name2' : 'Hobbes' }, attr2 = 6)
6 G.add_edge(0,1, likes = ['milk', 'oj'])
7
8 nx.write_gefx(G,"test.gefx")

which gives an error:

Traceback (most recent call last):
File "so_write_gefx.py", line 8, in <module>
nx.write_gexf(G,"test.gexf")
...
line 378, in add_attributes
for val,start,end in v:
ValueError: too many values to unpack
like image 799
Rob Avatar asked Jun 21 '13 20:06

Rob


1 Answers

The GEXF format only specifies a set if fairly simple data types. In your example you are setting the edge data attribute "likes" to be a list

G.add_edge(0,1, likes = ['milk', 'oj'])

which is not handled by the GEXF writer.

If you stick to strings, numbers, etc then you won't encounter this issue.

like image 194
Aric Avatar answered Oct 29 '22 03:10

Aric