gpt4 book ai didi

python - 在 agraph.py 中将字符串传递给agedge。 networkx 和 pygraphviz 的问题

转载 作者:IT老高 更新时间:2023-10-28 22:09:38 24 4
gpt4 key购买 nike

鉴于此初始图表:

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)

谁能重现这个错误?

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'

问题与调用graphviz的agege函数有关,好像和key参数的格式不一样;当我更改时(agraph.py 的第 480 行):

...
eh = gv.agedge(self.handle, uh, vh, key , _Action.create)
...

...
eh = gv.agedge(self.handle, uh, vh, "a_string" , _Action.create)
...

它不再失败(但丢失了关键标签)。

是否有明显的方法来解决此问题(以便保留 key 参数值) - 我尝试的任何方法似乎都不起作用。

接下来最明智的调试步骤是什么?


来自 here ,看来 c 老化函数(我看不到,因为它在 .pyd 二进制文件中)具有以下格式:

*agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)

其中 char *name 是键。

我无法弄清楚为什么它不会像初始错误那样接受 str dtype。


注释版本:

networkx - 1.11,pygraphviz - 1.3.1 (从 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygraphviz 安装)Windows 7(64 位)上的 Python 2.7(32 位 - 通过 python(x,y) 安装),GraphViz - 2.38

我也看到这个问题出现在这些问题中:


更新 1

我已经尝试将 agege 函数的 key 输入调整为许多 char 数组的变体(例如 (ct.c_char_p * len(key))(key) (ct 是 ctypes 模块)基于 this )。这会将错误更改为:

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 *'

更新 2

如果我这样做,我可以让它运行(但不返回多图):

agraph.py 中替换行

eh = gv.agedge(self.handle, uh, vh, key , _Action.create)

    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)

我不知道为什么只转换为字符串 str(key) 不起作用。

更新 3 - 使用函数编辑

在这里找到函数 - 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;
}
}
}

更新 4:

错误的来源在this内pygraphviz 文件,graphviz_wrap.c,第 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;
}

或者,它在 this one 内,graphviz.i,第 68 行。

无论哪种方式,如果 agege 因任何原因失败,似乎返回错误字符串“agege: no key”......也许这与 SWIG 有关。

最佳答案

尝试将变量名称从“key”更改为“temp_key”等其他名称。我的意思是,您(或任何以前导入的模块)可能在...之前声明了一个非字符串类型的“key”变量?

显然如果运行:

eh = gv.agedge(self.handle, uh, vh, key , _Action.create)

失败但正在运行:

eh = gv.agedge(self.handle, uh, vh, "key" , _Action.create)

没有问题,它只能与“关键”变量类型相关..你试过这个吗:

eh = gv.agedge(self.handle, uh, vh, str(key) , _Action.create)

eh = gv.agege(self.handle, uh, vh, unicode(key) , _Action.create)

将 str()/unicode() 集成到您的原始代码中:

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))

两者(str 和 unicode 版本)都可以在 Linux 上正常运行。

最好的问候

关于python - 在 agraph.py 中将字符串传递给agedge。 networkx 和 pygraphviz 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35610736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com