6

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
Rob
  • 587
  • 5
  • 7

1 Answers1

4

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.

Aric
  • 24,511
  • 5
  • 78
  • 77
  • 1
    It's unfortunate that these graph xml formats can't handle deep data types. – Rob Aug 07 '13 at 19:43
  • I guess in principle they can. It would need to be defined as an extension to the standard. – Aric Aug 08 '13 at 20:03