python - networkx DiGraph 属性错误 self._succ

标签 python networkx attributeerror directed-graph

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

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

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)的问题).我经历了the migration guide cited in other questionsmahmodel_road_only.py 中没有发现我需要更改的内容。我还检查了 digraph.py 文件,发现 self._succ 被定义了。我还检查了 get_graph() 的定义,如下所示,它调用了 networkx,但没有发现任何明显的问题。

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 版本的问题?如果没有,您可以推荐哪些后续步骤进行调试?

最佳答案

我相信您的问题与 AttributeError: 'DiGraph' object has no attribute '_node' 中的问题类似

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

您现在已将其打开,并将 networkx 2.x 中的工具应用于该图表。但是这些工具假定它是一个 networkx 2.x 有向图,具有 2.x 有向图所期望的所有属性。特别是它希望为节点定义 _succ,这是 1.x DiGraph 没有的。

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

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

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

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

#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 有向图,其边和节点具有 G 所具有的任何属性。 networkx 2.x 命令应该适用于它。

额外的长期解决方案 联系其他研究人员并警告他/她该代码示例现在已过时。

关于python - networkx DiGraph 属性错误 self._succ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016596/

相关文章:

python - numpy 子类不会接受来自 python 继承类的 __new__ 参数

打开pyxl+load_workbook+AttributeError : 'NoneType' object has no attribute 'date1904'

python - 如何改进 pygame 中 3d 空间的模拟?

python - Flask 应用仅显示 1 张图像,不显示其他图像

python - 如何在查找 2 个列表的差异时保持输出列表的顺序

python - 重命名节点时 NetworkX 中的 Bipartite 无法正常工作

javascript - 如何使 svg 交互以收集对描绘元素的评论/注释

python - 计算属于巨型组件的节点的比例(python,networkx)

python - Django/DRF 应用程序收到 AttributeError : 'function' object has no attribute 'get_extra_actions'

python - 在 Pandas 中查询 HDF5