【问题标题】:Networkx with errors from adding nodes and edgesNetworkx 添加节点和边时出错
【发布时间】:2019-03-28 19:02:29
【问题描述】:

这是一个用于生成网络图的简单 python 程序。当我将数据放在程序中时一切都很好,但是当我决定将数据放在两个输入文件中时,事情开始变得有趣。有两个输入文件:节点(或顶点)和边。当我从名为“Step3-Vertices.txt”的输入文件中读取节点信息时,它没有给出任何错误,但附加信息被添加到我没有提供的节点中。以下是附加信息列表: '[', '{', "'", '0', '2', ',', ' ', '6', '8', 'W', '}', '.', '1', '5', '3', '7', '4', 'O', 'X', 'D', ']', '\n'

然后我读入了名为“Step3-Edges.txt”的第二个文件,这次我得到了一个我无法理解的错误消息列表。

错误消息 - 从文件中添加边缘信息时 NETWORKX 失败:

Traceback (most recent call last):
File "step4_test1.py", line 30, in <module>
G.add_edges_from(data_edges)
File "/home/desmond/anaconda3/lib/python3.6/site-packages/networkx/classes/graph.py", line 934, in add_edges_from
"Edge tuple %s must be a 2-tuple or 3-tuple." % (e,))
networkx.exception.NetworkXError: Edge tuple [ must be a 2-tuple or 3-tuple.

有人可以帮帮我吗?

这是我的程序:

""" THIS PROGRAM WORKS PROPERLY WHEN DATA ARE PASSED TO LOCAL VARIABLES CALLED "nodes" and "edges".  THE EXACT DATA ARE ALSO STORED IN TWO FILES: "nodes" in 'Step3-Vertices.txt' and "edges" in 'Step3-Edges.txt'.  PROBLEMS STARTED WHEN NODES AND EDGES ARE READ FROM BOTH FILES.  FIRST, RUN THIS PROGRAM AND IT SHOULD GENERATE A GRAPH.  THEN REPLACE THE "nodes" with "data_nodes" in "G.add_nodes_from" AND THIS WILL GENERATE UNEXPECTED ADDITIONAL NODES WHICH ARE NOT SUPPOSED TO BE THERE.  NEXT, REPLACE THE "edges" with "data_edges" in "G.add_nodes_from" AND ERROR MESSAGES ARE DISPLAYED."""  


import networkx as nx
import matplotlib.pyplot as plt



""" READ NODES INFORMATION FROM FILE """

with open('Step3-Vertices.txt', encoding='utf-8') as data_file:
    data_nodes = data_file.read()

print(data_nodes)

""" READ EDGESS INFORMATION FROM FILE """


with open('Step3-Edges.txt', encoding='utf-8') as data_file:
    data_edges = data_file.read()

print(data_edges)


G=nx.Graph()

"""  PASS NODES INFORMATION TO A VARIABLE CALLED 'nodes'   """

nodes = ['0000000002', '0000000101', '0000000111', '0000000200', '0000000502', '0000000600', '0000001000', '0000001001', '0000001069', '0000001253', '0000001462', '0000003013', '0000003200', '0000004100', '0000004305', '0000005100', '0000005460', '0000006600', '0000010021', '0000010101', '0000010200', '0000010314', '0000012000', '0000012151', '0000012600', '0000015201', '0000016100', '0000017002', '0000020002', '0000020050', '0000020100', '0000021001', '0000022044', '0000022100']


""" PASS EDGES INFORMATION TO A VARIABLE CALLED 'edges'   """

edges = [{'0000000002', '6080022W'}, {'80.015.012.210', '0000000002'}, {'80.015.012.210', '0000000502'}, {'0000012000', '0000000502'},{'0000000101', '012.105.123.127'}, {'0000000111', '2442032O'}, {'105.103.02.110', '0000000111'}, {'0604054X', '0000000200'}, {'100.001.008.002', '0000000200'}, {'0000000502', '1002567D'}, {'208.08.032.1', '0000000502'}]

"""THIS IS WHERE YOU ADD DATA TO THE NODES AND EDGES, BY DEFAULT, LOCAL VARIABLES ARE USED. TO ADD DATA FROM THE INPUT FILES - replace 'nodes' with 'data_nodes' and replace 'edges' with 'data_edges'   """




G.add_nodes_from(nodes)
G.add_edges_from(edges)

print("Nodes of graph: ")
print(G.nodes())


print("Edges of graph: ")
print(G.edges())

###  DRAW A GRAPH  ###

nx.draw(G)
plt.savefig("test1.png") # save as png
plt.show() # display

【问题讨论】:

    标签: python networkx


    【解决方案1】:

    add_edges_from 期望的格式是元组列表,其最基本的形式是要连接的 (u,v) 对列表。

    您的文件没有适当格式的数据,因此 networkx 不知道如何处理它们。如果文本与您在“edges”变量中写的完全一样,那么下面是一种将其按摩到正确类型列表中的方法。您可以对节点处理执行类似操作,但这只需要元素列表,而不是元组列表,因此更直接。

    with open("edgefile.txt") as data_file:
        data_edges = data_file.read()
    
    # split on the comma, assuming this divides elements, remove the curly braces and quotes
    elems = ([f.strip(" {}'") for f in data_edges.strip().split(',')])
    # use zip to turn the flat list into a lst of pairs
    edge_list = zip(elems[::2], elems[1::2])
    
    # now we are in a form that nx.add_edges_from can handle
    G = nx.Graph()
    G.add_nodes_from(nodes)
    G.add_edges_from(edge_list)
    

    您应该在此处阅读有关阅读图表的文档:https://networkx.github.io/documentation/stable/reference/readwrite/index.html

    它描述了如何读取各种标准图形格式。


    编辑在评论中跟随qu:

    您在图中有许多“意外”节点的原因是因为 nx.add_nodes_from 采用可迭代类型,并且当您将整个文件读入文本变量时,迭代该字符串一次需要一个字符。这会产生像0'\n 这样的单字符节点。所以我们可以通过将字符串解析成一个列表来修复它,并且遍历一个列表会得到一个元素,比如'0000000002'

    这是一个例子:

    # assume that the file describing nodes is read into this string:
    node_txt = "'0000000002', '0000000101', '0000000111', '0000000200', '0000000502', '0000000600', '0000001000', '0000001001', '0000001069', '0000001253', '0000001462', '0000003013', '0000003200', '0000004100', '0000004305', '0000005100', '0000005460', '0000006600', '0000010021', '0000010101', '0000010200', '0000010314', '0000012000', '0000012151', '0000012600', '0000015201', '0000016100', '0000017002', '0000020002', '0000020050', '0000020100', '0000021001', '0000022044', '0000022100'\n"
    
    G1 = nx.Graph()
    G1.add_nodes_from(node_txt)
    print(G1.nodes())
    print(set(node_txt))
    # output of these two commands shows that the node names are 1 char each:
    >>> [' ', "'", '\n', ',', '1', '0', '3', '2', '5', '4', '7', '6', '9']
    >>> set([' ', "'", '\n', ',', '1', '0', '3', '2', '5', '4', '7', '6', '9'])
    
    # reference: what we really wanted    
    node_list = ['0000000002', '0000000101', '0000000111', '0000000200', '0000000502', '0000000600', '0000001000', '0000001001', '0000001069', '0000001253', '0000001462', '0000003013', '0000003200', '0000004100', '0000004305', '0000005100', '0000005460', '0000006600', '0000010021', '0000010101', '0000010200', '0000010314', '0000012000', '0000012151', '0000012600', '0000015201', '0000016100', '0000017002', '0000020002', '0000020050', '0000020100', '0000021001', '0000022044', '0000022100']
    
    G2  = nx.Graph()
    G2.add_nodes_from(node_list)
    print(G2.nodes())
    print(set(node_list))
    

    那么如何将node_txt转化为node_list的形式呢?对于边缘,我们遵循与上述相同的过程 - 这个过程更简单一些。

    elems = [f.strip(" '") for f in node_txt.strip().split(',')]
    print(elems == node_list)
    # output: True -> so here we recovered the node names correctly from node_txt
    

    【讨论】:

    • 谢谢。我测试了你的代码,它正在我的程序上运行。顺便问一下,我如何处理由 Networkx 生成的附加节点。当我从名为“Step3-Vertices.txt”的输入文件中读取节点信息时,它没有给出任何错误,但附加信息被添加到我没有提供的节点中。以下是附加信息的列表:'[', '{', "'", '0', '2', ',', ' ', '6', '8', 'W', '}' , '.', '1', '5', '3', '7', '4', 'O', 'X', 'D', ']', '\n'。这些额外的节点对我没有用。我不知道它是从哪里来的。我该如何摆脱它们?
    • 问题同上。我没有尝试猜测节点文件的结构,但是当您使用&lt;file&gt;.read() 时,python 会给您一个字符串。上面的代码将由逗号分隔的元素列表转换为元组列表。对于节点列表,您可能需要执行相同的操作。否则,字符串将在G.add_nodes_from() 中解释为字符列表
    猜你喜欢
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多