write graph with attribute dictionary

717 views
Skip to first unread message

Juan Pablo Alperin

unread,
Nov 10, 2011, 10:45:12 PM11/10/11
to networkx-discuss
I have a DiGraph that I am reading from an edge list and to which I
want to add a bunch of attributes. I successfully read the edge list,
I read the file that has my attributes, and I can add those to my
nodes. So far, so good.

My problem is that I cannot seem to write the graph to disk again.
Here is my code:

fieldlist=[attr1, attr2, attr3]
attrReader = csv.DictReader(open('data/attributes.csv', 'rb'),
fieldnames=fieldlist)
for row in attrReader:
id=int(row['id'])
if G.has_node(id):
for key in fieldlist:
G[id][key] = row[key]

nx.write_adjlist(G, 'data/adj-list-with-attr.txt', comments='#',
delimiter="\t")

But when I look in my file, it has:
1 2 3 4 attr1 attr2 attr3
Not what I was expecting:
1 2 3 4 {'attr1': "value1", 'attr2': "value2" ... etc.

I have tried to write with other formats:
nx.write_gexf(G, 'data/adj-list-with-attr.txt')
but it gives the error:
AttributeError: 'str' object has no attribute 'copy'

Other formats give the same thing

Any tips?

Dan Schult

unread,
Nov 11, 2011, 8:43:48 AM11/11/11
to networkx...@googlegroups.com
You've tripped over our exposure of the graph structure wart.
To assign attributes to a node, instead of:

> G[id][key] = row[key]
You should use
> G.node[id][key] = row[key]
or #even if node already exists
> G.add_node(id, key = row[key])

The syntax G[u][v]=thing creates an entry in the adjacency list data structure that doesn't match the graph data structure. So at that point the data structure has been corrupted. While we provide G[u][v] to report edge attributes, they should not be assigned to without using data structure dependent syntax.

To add an edge attrbute to an edge that already exists:
> G[id][nbr_id][key] = row[key]
or # works whether edge exists or not
> G.add_edge(id, nbr_id, key = row[key])


Dan

> --
> You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
> To post to this group, send email to networkx...@googlegroups.com.
> To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.
>

Juan Pablo Alperin

unread,
Nov 12, 2011, 2:28:31 PM11/12/11
to networkx-discuss
Aaah. Thanks Dan. I should have followed the examples in the tutorial
a little closer.

Thanks again,

Juan

Juan Pablo Alperin

unread,
Nov 12, 2011, 3:44:09 PM11/12/11
to networkx-discuss
Dan,

I wrote too soon! I tried that out and your notation definitely helps.
The graph is being created properly now!

However, I still cannot seem to write the adjlist with the attributes.
When I do
nx.write_adjlist(G, 'data/adj-list-with-attr.txt', comments='#',
delimiter="\t")

it only writes the adjacency list, but does not have any of the
attributes.

When I try to write in GEXF of Pajek, I am getting encoding errors
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcf in position
58: ordinal not in range(128)

even though my file and content is all UTF-8 encoded.

More help?

Thanks

Juan

On Nov 11, 5:43 am, Dan Schult <dsch...@colgate.edu> wrote:

Dan Schult

unread,
Nov 12, 2011, 4:06:33 PM11/12/11
to networkx...@googlegroups.com
Which version of NetworkX (and python) do you have?
The latest write_gexf() has an argument called encoding
which defaults to utf-8. If your version is fairly old it might
need to be updated. Or maybe there is a bug, but it sounds
like the code is trying to use ascii codec, so probably an old
version.

print networkx.__version__

Juan Pablo Alperin

unread,
Nov 12, 2011, 8:50:34 PM11/12/11
to networkx-discuss
I'm using Python 2.7 and (i think) NetworkX 1.4. I installed
everything with MacPorts.

Any ideas on why write_adjlist is not writing the attributes?

Dan Schult

unread,
Nov 12, 2011, 9:48:00 PM11/12/11
to networkx...@googlegroups.com
The doc_string for write_adjlist says it doesn't write node or edge data
only the adjacency list. For gexf, version 1.5 improved that module
and one improvement was handling unicode better.

You should probably get a newer version. Or, if you're up
for learning a little more python, you could take the code for
write_adjlist() which isn't too long and make a custom writer
for your application.

Dan

Juan Pablo Alperin

unread,
Nov 12, 2011, 10:07:13 PM11/12/11
to networkx...@googlegroups.com
Thanks for the reply Dan. Will try this out on another machine (I'm happy with macports and don't want to start having multiple versions of things installed). By the way, I filed a request for macports to update to 1.5


Juan

Reply all
Reply to author
Forward
0 new messages