Context:我正在尝试运行另一位研究人员的代码 - 它描述了湾区道路网络的交通模型,该模型容易受到地震灾害的影响。我是 Python 新手,因此非常感谢您帮助调试以下错误。
Issue:当我尝试按照文件中提供的示例数据运行代码时,按照自述文件中的说明进行操作时,出现以下错误。
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)的问题。我经历了其他问题中引用的迁移指南 https://networkx.github.io/documentation/stable/release/migration_guide_from_1.x_to_2.0.html并发现没有什么我需要改变的mahmodel_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
Question: 我该如何解决这个问题?是否需要更改 Python 或 Networkx 版本?如果没有,您建议采取哪些后续步骤进行调试?