Given this initial graph:
import networkx as nx
G=nx.MultiGraph()
fromnodes=[0,0,1,1,1,1,1,2,3,4,5,5,5,7,8,9,10]
tonodes=[1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
dupedgeind=0
for x,y in zip(fromnodes,tonodes):
if G.has_edge(x,y):
dupedgeind=dupedgeind+1
G.add_edge(x,y,key=dupedgeind)
else:
dupedgeind=0
G.add_edge(x,y,key=dupedgeind)
Can anyone recreate this error?
pos=nx.nx_agraph.pygraphviz_layout(G,prog='sfdp')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\networkx\drawing\nx_agraph.py", line 262, in pygraphviz_layout
A=to_agraph(G)
File "C:\Python27\lib\site-packages\networkx\drawing\nx_agraph.py", line 155, in to_agraph
A.add_edge(u,v,key=str(key),**str_edgedata)
File "C:\Python27\lib\site-packages\pygraphviz\agraph.py", line 484, in add_edge
eh = gv.agedge(self.handle, uh, vh, key, _Action.find)
KeyError: 'agedge: no key'
The problem has something to do with the call to graphviz's agedge function, it seems to not like the format of the key
parameter; when I change (line 480 of agraph.py
):
...
eh = gv.agedge(self.handle, uh, vh, key , _Action.create)
...
to
...
eh = gv.agedge(self.handle, uh, vh, "a_string" , _Action.create)
...
it no longer fails (but loses the key labels).
Is there an obvious way to fix this (so that key
parameter values are retained)? - Nothing I try seems to work.
What are the most sensible next debugging steps?
From here, it appears that the c
agedge function (which I can't see as it's in a .pyd binary) has the following format:
*agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
where the char *name
is the key.
I cannot work out why it won't accept a str
dtype as in the initial error.
Note versions:
networkx - 1.11, pygraphviz - 1.3.1 (installed from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygraphviz) Python 2.7 (32bit - isntalled via python(x,y)) on Windows 7 (64-bit), GraphViz - 2.38
I have also seen this issue crop up in these questions:
Hierarchical graph with parallel edges
How to draw parallel edges in Networkx / Graphviz
UPDATE 1
I have tried adjusting the key
input to the agedge function to a number of variants of char arrays (e.g. (ct.c_char_p * len(key))(key)
(ct is ctypes module) based on this). This changes the error to:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\networkx\drawing\nx_agraph.py", line 262, in pygraphviz_layout
A=to_agraph(G)
File "C:\Python27\lib\site-packages\networkx\drawing\nx_agraph.py", line 155, in to_agraph
A.add_edge(u,v,str(key),**str_edgedata)
File "C:\Python27\lib\site-packages\pygraphviz\agraph.py", line 482, in add_edge
eh = gv.agedge(self.handle, uh, vh, (ct.c_char_p * len(key))(key), _Action.create)
TypeError: in method 'agedge', argument 4 of type 'char *'
UPDATE 2
I can get it to run (but not return a multigraph) if I do this:
In agraph.py
replacing the line
eh = gv.agedge(self.handle, uh, vh, key , _Action.create)
with
try:
# new
if key==0:
eh = gv.agedge(self.handle, uh, vh, str(0), _Action.create)
else:
eh = gv.agedge(self.handle, uh, vh, str(1), _Action.create)
I don't know why just casting to a string str(key)
does not work.
UPDATE 3 - EDIT with the function
Found the function here - https://github.com/ellson/graphviz/blob/master/lib/cgraph/edge.c
Agedge_t *agedge(Agraph_t * g, Agnode_t * t, Agnode_t * h, char *name,
int cflag)
{
Agedge_t *e;
IDTYPE id;
int have_id;
have_id = agmapnametoid(g, AGEDGE, name, &id, FALSE);
if (have_id || ((name == NILstr) && (NOT(cflag) || agisstrict(g)))) {
/* probe for pre-existing edge */
Agtag_t key;
key = Tag;
if (have_id) {
key.id = id;
key.objtype = AGEDGE;
} else {
key.id = key.objtype = 0;
}
/* might already exist locally */
e = agfindedge_by_key(g, t, h, key);
if ((e == NILedge) && agisundirected(g))
e = agfindedge_by_key(g, h, t, key);
if (e)
return e;
if (cflag) {
e = agfindedge_by_key(agroot(g), t, h, key);
if ((e == NILedge) && agisundirected(g))
e = agfindedge_by_key(agroot(g), h, t, key);
if (e) {
subedge(g,e);
return e;
}
}
}
UPDATE 4:
The source of the error is within this pygraphviz file, graphviz_wrap.c, line 3921:
SWIGINTERN PyObject *_wrap_agedge(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
Agraph_t *arg1 = (Agraph_t *) 0 ;
Agnode_t *arg2 = (Agnode_t *) 0 ;
Agnode_t *arg3 = (Agnode_t *) 0 ;
char *arg4 = (char *) 0 ;
int arg5 ;
void *argp1 = 0 ;
int res1 = 0 ;
void *argp2 = 0 ;
int res2 = 0 ;
void *argp3 = 0 ;
int res3 = 0 ;
int res4 ;
char *buf4 = 0 ;
int alloc4 = 0 ;
int val5 ;
int ecode5 = 0 ;
PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ;
PyObject * obj2 = 0 ;
PyObject * obj3 = 0 ;
PyObject * obj4 = 0 ;
Agedge_t *result = 0 ;
if (!PyArg_ParseTuple(args, char*)"OOOOO:agedge",&obj0,&obj1,&obj2,&obj3,&obj4)) SWIG_fail;
res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_Agraph_t, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '""agedge" "', argument " "1"" of type '" "Agraph_t *""'");
}
arg1 = (Agraph_t *)(argp1);
res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_Agnode_t, 0 | 0 );
if (!SWIG_IsOK(res2)) {
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "agedge" "', argument " "2"" of type '" "Agnode_t *""'");
}
arg2 = (Agnode_t *)(argp2);
res3 = SWIG_ConvertPtr(obj2, &argp3,SWIGTYPE_p_Agnode_t, 0 | 0 );
if (!SWIG_IsOK(res3)) {
SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "agedge" "', argument " "3"" of type '" "Agnode_t *""'");
}
arg3 = (Agnode_t *)(argp3);
res4 = SWIG_AsCharPtrAndSize(obj3, &buf4, NULL, &alloc4);
if (!SWIG_IsOK(res4)) {
SWIG_exception_fail(SWIG_ArgError(res4), "in method '" "agedge" "', argument " "4"" of type '" "char *""'");
}
arg4 = (char *)(buf4);
ecode5 = SWIG_AsVal_int(obj4, &val5);
if (!SWIG_IsOK(ecode5)) {
SWIG_exception_fail(SWIG_ArgError(ecode5), "in method '" "agedge" "', argument " "5"" of type '" "int""'");
}
arg5 = (int)(val5);
{
result = (Agedge_t *)agedge(arg1,arg2,arg3,arg4,arg5);
if (!result) {
PyErr_SetString(PyExc_KeyError,"agedge: no key");
return NULL;
}
}
resultobj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Agedge_t, 0 | 0 );
if (alloc4 == SWIG_NEWOBJ) free((char*)buf4);
return resultobj;
fail:
if (alloc4 == SWIG_NEWOBJ) free((char*)buf4);
return NULL;
}
Or, it's within this one, graphviz.i, line 68.
Either way, it seems like the error string "agedge: no key" is returned if agedge
fails for any reason... Perhaps it's something to do with SWIG.
Try changing the variable name from "key" to something else like "temp_key". I mean that it is possible that you (or any previously imported module) has declared a non-string type "key" variable before...?
Apparently if running :
eh = gv.agedge(self.handle, uh, vh, key , _Action.create)
fails but running:
eh = gv.agedge(self.handle, uh, vh, "key" , _Action.create)
give you no issue, it could only be relative to the "key" variable type.. did you try this:
eh = gv.agedge(self.handle, uh, vh, str(key) , _Action.create)
or
eh = gv.agedge(self.handle, uh, vh, unicode(key) , _Action.create)
Integrate str()/unicode() in your orginal code give:
import networkx as nx
G=nx.MultiGraph()
fromnodes=[0,0,1,1,1,1,1,2,3,4,5,5,5,7,8,9,10]
tonodes=[1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
dupedgeind=0
for x,y in zip(fromnodes,tonodes):
if G.has_edge(x,y):
dupedgeind=dupedgeind+1
G.add_edge(x,y,key=str(dupedgeind))
#G.add_edge(x,y,key=unicode(dupedgeind))
else:
dupedgeind=0
G.add_edge(x,y,key=str(dupedgeind))
#G.add_edge(x,y,key=unicode(dupedgeind))
both (str & unicode version) works fine on Linux.
Best regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With