簡體   English   中英

networkx DiGraph 屬性錯誤 self._succ

[英]networkx DiGraph Attribute Error self._succ

上下文:我正在嘗試運行另一位研究人員的代碼 - 它描述了灣區道路網絡的交通模型,該模型容易受到地震災害的影響。 我是 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)的問題。 我瀏覽了其他問題中引用的遷移指南,沒有發現我需要在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

問題:我該如何解決這個問題? 是更改 Python 還是 Networkx 版本的問題? 如果沒有,您可以推薦哪些后續步驟進行調試?

我相信你的問題類似於AttributeError: 'DiGraph' object has no attribute '_node'

問題是被調查的圖表是在 networkx 1.x 中創建的,然后被腌制。 然后該圖具有 networkx 1.x 對象所具有的屬性。 我相信這也發生在你身上。

您現在已經打開它,並將 networkx 2.x 中的工具應用到該圖。 但是這些工具假定它是一個 networkx 2.x 有向圖,具有 2.x 有向圖所期望的所有屬性。 特別是它希望為節點定義_succ ,而 1.x 有向圖沒有。

所以這里有兩種我認為可行的方法:

短期解決方案刪除 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 命令應該適用於它。

額外的長期解決方案聯系其他研究人員並警告他/她代碼示例現在已經過時了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM