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

查看:258
本文介绍了networkx DiGraph属性错误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.

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

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 .我还检查了如下所示的get_graph()的定义,该定义调用networkx,但是没有发现任何明显的问题.

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?

推荐答案

我相信您的问题与

存在的问题是要研究的图形是在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中.特别是,它期望为1.x DiGraph没有的节点定义_succ.

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() #or Graph for someone else with this problem.

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 DiGraph属性错误self._succ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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