networkx 有向图属性错误 self._succ [英] networkx DiGraph Attribute Error self._succ

查看:30
本文介绍了networkx 有向图属性错误 self._succ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我正在尝试运行另一位研究人员的代码 - 它描述了湾区道路网络的交通模型,该模型易受地震危害.我是 Python 新手,因此非常感谢您帮助调试以下错误.

Context: I'm trying to run another researcher's code - it describes a traffic model for the Bay Area road network, which is subject to seismic hazard. I'm new to Python and therefore would really appreciate some help debugging the following error.

问题:当我按照 README 中的说明尝试运行随文件提供的示例数据的代码时,出现以下错误.

Issue: When I try to run the code for the sample data provided with the file, following the instructions in the README, I get the following error.

DN0a226926:quick_traffic_model gitanjali$ python mahmodel_road_only.py
You are considering 2 ground-motion intensity maps.
You are considering 1743 different site locations.
You are considering 2 different damage maps (1 per ground-motion intensity map).
Traceback (most recent call last):
  File "mahmodel_road_only.py", line 288, in <module>
main()
  File "mahmodel_road_only.py", line 219, in main
  G = get_graph()
  File "mahmodel_road_only.py", line 157, in get_graph
  G = add_superdistrict_centroids(G)
  File "mahmodel_road_only.py", line 46, in add_superdistrict_centroids
  G.add_node(str(1000000 + i))
  File "/Library/Python/2.7/site-packages/networkx-2.0-py2.7.egg/networkx/classes/digraph.py", line 412, in add_node
if n not in self._succ:
  AttributeError: 'DiGraph' object has no attribute '_succ'

调试:根据其他一些问题,此错误似乎源于 networkx 版本(我使用的是 2.0)或 Python 版本(我使用的是 2.7.10).我浏览了其他问题中引用的迁移指南mahmodel_road_only.py 中没有发现我需要更改的内容.我还检查了 digraph.py 文件,发现定义了 self._succ.我还检查了调用networkx的get_graph()的定义,如下所示,但没有看到任何明显的问题.

Debugging: Based on some other questions, it seems like this error stems from an issue with the networkx version (I'm using 2.0) or the Python version (I'm using 2.7.10). I went through the migration guide cited in other questions and found nothing that I needed to change in mahmodel_road_only.py. I also checked the digraph.py file and found that self._succ is defined. I also checked the definition of get_graph(), shown below, which calls networkx, but didn't see any obvious issues.

def get_graph():
  import networkx
  '''loads full mtc highway graph with dummy links and then adds a few 
  fake centroidal nodes for max flow and traffic assignment'''
G = networkx.read_gpickle("input/graphMTC_CentroidsLength3int.gpickle")
G = add_superdistrict_centroids(G)
assert not G.is_multigraph() # Directed! only one edge between nodes
G = networkx.freeze(G) #prevents edges or nodes to be added or deleted
return G

问题:我该如何解决这个问题?是更改 Python 或 Networkx 版本的问题吗?如果没有,您可以推荐哪些后续步骤进行调试?

Question: How can I resolve this problem? Is it a matter of changing the Python or Networkx versions? If not, what next steps could you recommend for debugging?

推荐答案

我相信你的问题与 AttributeError: 'DiGraph' 对象没有属性 '_node'

问题在于所调查的图是在 networkx 1.x 中创建的,然后被腌制.该图然后具有 networkx 1.x 对象具有的属性.我相信这也发生在你身上.

The issue there is that the graph being investigated was created in networkx 1.x and then pickled. The graph then has the attributes that a networkx 1.x object has. I believe this happened for you as well.

您现在已经打开了它,并且正在将 networkx 2.x 中的工具应用到该图.但是这些工具假定它是一个 networkx 2.x DiGraph,具有 2.x DiGraph 中预期的所有属性.特别是它期望为节点定义 _succ,而 1.x DiGraph 没有.

You've now opened it and you're applying tools from networkx 2.x to that graph. But those tools assume that it's a networkx 2.x DiGraph, with all the attributes expected in a 2.x DiGraph. In particular it expects _succ to be defined for a node, which a 1.x DiGraph does not have.

所以这里有两种我认为可行的方法:

So here are two approaches that I believe will work:

短期解决方案删除 networkx 2.x 并替换为 networkx 1.11.

Short term solution Remove networkx 2.x and replace with networkx 1.11.

这不是最佳选择,因为 networkx 2.x 更强大.此外,在 2.x 和 1.x 中编写的代码(遵循您提到的迁移指南)在 1.x 中效率较低(例如,1.x 代码在某些地方使用列表和2.x 代码使用生成器).

This is not optimal because networkx 2.x is more powerful. Also code that has been written to work in both 2.x and 1.x (following the migration guide you mentioned) will be less efficient in 1.x (for example there will be places where the 1.x code is using lists and the 2.x code is using generators).

长期解决方案将 1.x 图形转换为 2.x 图形(我无法轻松测试,因为目前我的计算机上没有 1.x - 如果有人尝试此操作,请发表评论说这是否有效以及是否您的网络已加权):

Long term solution Convert the 1.x graph into a 2.x graph (I can't test easily as I don't have 1.x on my computer at the moment - If anyone tries this, please leave a comment saying whether this works and whether your network was weighted):

#you need commands here to load the 1.x graph G
#
import networkx as nx   #networkx 2.0
H = nx.DiGraph() #if it's a DiGraph()
#H=nx.Graph() #if it's a typical networkx Graph().

H.add_nodes_from(G.nodes(data=True))
H.add_edges_from(G.edges(data=True))

data=True 用于确保保留任何边/节点权重.H 现在是 networkx 2.x DiGraph,边和节点具有 G 的任何属性.networkx 2.x 命令应该可以处理它.

The data=True is used to make sure that any edge/node weights are preserved. H is now a networkx 2.x DiGraph, with the edges and nodes having whatever attributes G had. The networkx 2.x commands should work on it.

奖励长期解决方案联系其他研究人员并警告他/她代码示例现已过时.

Bonus longer term solution Contact the other researcher and warn him/her that the code example is now out of date.

这篇关于networkx 有向图属性错误 self._succ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆