简体   繁体   English

为什么来自 networkx 的 read_edgelist 不接受数组/列表作为权重?

[英]Why does read_edgelist from networkx not accept arrays/lists as weights?

I am using Python and the networkx package to read an edgelist from file in order to build a graph.我正在使用 Python 和 networkx 包从文件中读取边列表以构建图形。

My edgelist looks something like this:我的边缘列表看起来像这样:

[(0, 114, {'pts': array([[  1, 822],
       [  1, 821],
       [  2, 820],
       [  3, 819]], dtype=int16), 'weight': 23.38477631085024}),
(1, 110, {'pts': array([[ 1,  3],
       [ 1,  2],
       [ 2,  1]], dtype=int16), 'weight': 18.414213562373096})]

I wrote this edgelist with:我写了这个边缘列表:

nx.write_edgelist(G, 'my_el.edgelist', data=True)

My edges are defined by the start and the end node, and following that, I have my weights.我的边由开始和结束节点定义,然后,我有我的权重。 Each edge has two weights.每条边有两个权重。 The first weight is a array of pixel-coordinates, and the second is a float.第一个权重是像素坐标数组,第二个是浮点数。

The graph was constructed from a skeleton with the ´sknw´ library using the build_sknw function :该图是使用build_sknw 函数从带有“sknw”库的骨架构建的:

def build_sknw(ske, multi=False):
    buf = buffer(ske)
    nbs = neighbors(buf.shape)
    acc = np.cumprod((1,)+buf.shape[::-1][:-1])[::-1]
    mark(buf, nbs)
    pts = np.array(np.where(buf.ravel()==2))[0]
    nodes, edges = parse_struc(buf, pts, nbs, acc)
    return build_graph(nodes, edges, multi)

Now I want to read in this edgelist to build a graph.现在我想读入这个边列表来构建一个图表。 However, Python does not recognize my array of pixel-coords as a single weight-element.但是,Python 不会将我的像素坐标数组识别为单个权重元素。 I've tried nx.read_edgelist('my_el.edgelist', data=True) , which gives me the following error:我试过nx.read_edgelist('my_el.edgelist', data=True) ,这给了我以下错误:

TypeError: Failed to convert edge data (["{'pts':", 'array([[', '1,', '822],']) to dictionary.

nx.read_edgelist('my_el.edgelist', data=['pts', 'weight'] gives me: nx.read_edgelist('my_el.edgelist', data=['pts', 'weight']给我:

IndexError: Edge data ["{'pts':", 'array([[', '1,', '822],'] and data_keys ['pts', 'weight'] are not the same length

and nx.read_edgelist('my_el.edgelist', data=(('pts', int), ('weight', float'))) gives menx.read_edgelist('my_el.edgelist', data=(('pts', int), ('weight', float')))给我

IndexError: Edge data ["{'pts':", 'array([[', '1,', '822],'] and data_keys (('pts', <class 'int'>), ('weight', <class 'float'>)) are not the same length

I assume that the function is having a problem with either the array as a weight, or with the formatting of the my_el.edgelist file, but I do not really know, how to properly solve this issue without any workarounds via conversion to string, or similar.我认为该函数在将数组作为权重或 my_el.edgelist 文件的格式方面存在问题,但我真的不知道如何通过转换为字符串来正确解决此问题而没有任何变通方法,或者相似的。

I'd be thankful if someone can point me in the right direction and help me out with this!如果有人能指出我正确的方向并帮助我解决这个问题,我将不胜感激!

Dealing with numpy arrays seems to be a major problem for networkx.处理 numpy 数组似乎是 networkx 的一个主要问题。 The function that converts my image skeleton to the graph G however seems to be using numpy arrays.然而,将我的图像骨架转换为图形G的函数似乎正在使用 numpy 数组。 Since I prefer not to alter the imported function, a possible workaround that seemed to mitigate the problem when writing the edgelist to file was to change the array type thus:由于我不想更改导入的函数,因此在将 edgelist 写入文件时似乎可以缓解问题的一种可能解决方法是更改​​数组类型:

for (s, e) in G.edges():
    G[s][e]['pts'] = G[s][e]['pts'].tolist()

It might not be the most computationally efficient, and doesn't deal with the root of the problem, but will get the job done, in case anybody encounters a similar issue.它可能不是计算效率最高的,也不会解决问题的根源,但可以完成工作,以防有人遇到类似问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM