Setting any edge key attribute as weight

597 views
Skip to first unread message

Stephan Gerhard

unread,
Oct 22, 2009, 8:23:09 AM10/22/09
to networkx-discuss
Hi,

A simple use case, but I don't know how I can do most efficiently.

import networkx
G=networkx.path_graph(2)
G.edge[0][1]={'a': 2, 'b': 4}

Now, for analysis, I want to select one key whose value is used as
weight (key).

Is there a simple way to do this?

Stephan

Aric Hagberg

unread,
Oct 22, 2009, 8:36:42 AM10/22/09
to networkx...@googlegroups.com

One simple solution is to just assign the 'weight' key.

In [1]: import networkx

In [2]: G=networkx.Graph()

In [3]: G.add_edge(0,1,a=2,b=4)

In [4]: G.edges(data=True)
Out[4]: [(0, 1, {'a': 2, 'b': 4})]

In [5]: for u,v,d in G.edges(data=True):
...: G[u][v]['weight']=d['b']
...:
...:

In [6]: G.edges(data=True)
Out[6]: [(0, 1, {'a': 2, 'b': 4, 'weight': 4})]

In [7]: for u,v,d in G.edges(data=True):
G[u][v]['weight']=d['a']
...:
...:

In [9]: G.edges(data=True)
Out[9]: [(0, 1, {'a': 2, 'b': 4, 'weight': 2})]


Aric

darshan

unread,
Oct 29, 2009, 2:21:38 PM10/29/09
to networkx-discuss
I can't get the simple things of networkx to work

G.add_edge gives error.
G.add_edge(0,1,a=2,b=4)
TypeError: add_edge() got an unexpected keyword argument 'a'

Please help

Christopher Ellison

unread,
Oct 29, 2009, 2:36:23 PM10/29/09
to networkx...@googlegroups.com
darshan wrote the following on 10/29/2009 11:21 AM:
> I can't get the simple things of networkx to work
>
> G.add_edge gives error.
> G.add_edge(0,1,a=2,b=4)
> TypeError: add_edge() got an unexpected keyword argument 'a'
>
> Please help
>

Do you have most updated SVN version of NetworkX installed? The above
error suggests that you have an older version.

Chris

darshan

unread,
Oct 29, 2009, 3:27:22 PM10/29/09
to networkx-discuss
I installed it today through synaptics manager on ubuntu.

>>> print networkx.__version__
0.36


On Oct 29, 2:36 pm, Christopher Ellison <celli...@cse.ucdavis.edu>
wrote:

Christopher Ellison

unread,
Oct 29, 2009, 3:33:58 PM10/29/09
to networkx...@googlegroups.com
darshan wrote the following on 10/29/2009 12:27 PM:
> I installed it today through synaptics manager on ubuntu.
>
>>>> print networkx.__version__
> 0.36
>

The version in the Ubuntu repositories is quite old, at least in
comparison to what is on SVN. The new features you are trying to use
require a newer version (something like 1.0rc1). You might try
installing from SVN or easy_install.

Chris

arnegr

unread,
Oct 30, 2009, 11:32:24 AM10/30/09
to networkx-discuss


On Oct 29, 8:33 pm, Christopher Ellison <celli...@cse.ucdavis.edu>
wrote:
Hello,

I'm using version 1.0rc1 and have a similar problem. At least I got
the same error message. When creating a layout using pygraphviz I get:

File "/usr/lib/python2.5/site-packages/networkx/drawing/
nx_agraph.py", line 246, in pygraphviz_layout
A=to_agraph(G)
File "/usr/lib/python2.5/site-packages/networkx/drawing/
nx_agraph.py", line 156, in to_agraph
A.add_edge(u,v,**edgedata)
TypeError: add_edge() got an unexpected keyword argument 'weight'

Thanks for help!

Arne

Aric Hagberg

unread,
Oct 30, 2009, 12:00:41 PM10/30/09
to networkx...@googlegroups.com
On Fri, Oct 30, 2009 at 9:32 AM, arnegr <arn...@uni-bonn.de> wrote:
>
> I'm using version 1.0rc1 and have a similar problem. At least I got
> the same error message. When creating a layout using pygraphviz I get:
>
>  File "/usr/lib/python2.5/site-packages/networkx/drawing/
> nx_agraph.py", line 246, in pygraphviz_layout
>    A=to_agraph(G)
>  File "/usr/lib/python2.5/site-packages/networkx/drawing/
> nx_agraph.py", line 156, in to_agraph
>    A.add_edge(u,v,**edgedata)
>  TypeError: add_edge() got an unexpected keyword argument 'weight'


This is working for me with 1.0rc1. Are you sure you are using
that version? If there is a bug here I'd like to get it fixed.


In [1]: import networkx;networkx.__version__
Out[1]: '1.0rc1'

In [2]: import pygraphviz;pygraphviz.__version__
Out[2]: '1.0.dev1489'

In [3]: G=networkx.Graph()

In [4]: G.add_edge(1,2,weight=7)

In [5]: A=networkx.to_agraph(G)

In [6]: A
Out[6]:
strict graph {
1 -- 2 [weight=7];
}

In [7]: pos=networkx.pygraphviz_layout(G)

In [8]: pos
Out[8]: {1: (28.0, 29.0), 2: (100.0, 19.0)}


Aric

darshan

unread,
Oct 30, 2009, 2:54:32 PM10/30/09
to networkx-discuss
thanks!

On Oct 29, 3:33 pm, Christopher Ellison <celli...@cse.ucdavis.edu>
wrote:

arnegr

unread,
Nov 2, 2009, 7:45:47 AM11/2/09
to networkx-discuss
Hi,

i had an older pygraphviz version (0.35). I updated to 0.99.1 but
still have an error, even if it's not the same than before :-)
0.99.1 is the newest version of pygraphviz I can get on python package
index...where do I get your version '1.0.dev1489'?
This is the message ipython gives, when trying your example:

In [1]: import networkx as nx;nx.__version__
Out[1]: '1.0rc1'

In [2]: import pygraphviz;pygraphviz.__version__
Out[2]: '0.99.1'

In [3]: G=nx.Graph()

In [4]: G.add_edge(1,2,weight=7)

In [5]: A=nx.to_agraph(G)
/usr/lib/python2.5/site-packages/pygraphviz-0.99.1-py2.5-linux-
x86_64.egg/pygraphviz/agraph.py:1586: DeprecationWarning: raising a
string exception is deprecated
raise "Attribute value must be a string"
---------------------------------------------------------------------------
Attribute value must be a string Traceback (most recent call
last)

/usr/lib/python2.5/site-packages/networkx/drawing/nx_agraph.py in
to_agraph(N)
154 else:
155 for u,v,edgedata in N.edges_iter(data=True):
--> 156 A.add_edge(u,v,**edgedata)
157
158

/usr/lib/python2.5/site-packages/pygraphviz-0.99.1-py2.5-linux-
x86_64.egg/pygraphviz/agraph.py in add_edge(self, u, v, key, **attr)
374 eh=gv.agedge(self.handle,uh,vh,key,_Action.create)
375 e=Edge(self,eh=eh)
--> 376 e.attr.update(**attr)
377 except KeyError:
378 return None # silent failure for strict graph,
already added

/usr/lib/python2.5/UserDict.py in update(self, other, **kwargs)
158 self[k] = v
159 if kwargs:
--> 160 self.update(kwargs)
161 def get(self, key, default=None):
162 try:

/usr/lib/python2.5/UserDict.py in update(self, other, **kwargs)
150 elif hasattr(other, 'iteritems'): # iteritems saves
memory and lookups
151 for k, v in other.iteritems():
--> 152 self[k] = v
153 elif hasattr(other, 'keys'):
154 for k in other.keys():

/usr/lib/python2.5/site-packages/pygraphviz-0.99.1-py2.5-linux-
x86_64.egg/pygraphviz/agraph.py in __setitem__(self, name, value)
1584 pass
1585 except TypeError:
-> 1586 raise "Attribute value must be a string"
1587
1588 def __getitem__(self, name):

Attribute value must be a string: None

In [6]:

Any idea?Thanks in advance...Arne

arnegr

unread,
Nov 2, 2009, 7:58:33 AM11/2/09
to networkx-discuss
Hi, me again,

i managed to get the newest pygraphviz version from the subversion
system and I'm glad to tell, that at least your example is working for
me now as well! I think my original problem will be solved now as
well...thanks! Arne
Reply all
Reply to author
Forward
0 new messages