2015-03-13 47 views
2

我有以下代碼:如何使Networkx產生GML文件,排序節點

#!/usr/bin/env python 
import networkx as nx 
def main(): 
    """docstring for main""" 
    outerdict = {"A":["a1","a2","a3"], "B":["b1","b2","b3","b5","b6", "b7"], "C":["c2","c3"], "D":["d1","d2","d3"]} 
    keynode = "Z" 

    colorlist = [ "#beaed4", "#fdc086", "#ffff99", "#386cb0","#f0027f"] 
    G = nx.Graph() 
    G.add_node(keynode,graphics={"fill":"#7fc97f","w":27,"h":27, "d":27}) 
    for i,ky in enumerate(outerdict): 
     nodes = outerdict[ky] 
     nodecol = colorlist[i] 
     print ky, nodecol 
     for node in nodes: 
      G.add_node(node,graphics={"fill":nodecol,"w":27,"h":27,"d":27}) 
      G.add_edge(keynode, node, value = 10) 

    outfile = "test.gml" 
    nx.write_gml(G,outfile) 


if __name__ == '__main__': 
    main() 

它生成以下文件GML:

graph [ 
    node [ 
    id 0 
    label "b6" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#ffff99" 
    ] 
    ] 
    node [ 
    id 1 
    label "a1" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#beaed4" 
    ] 
    ] 
    node [ 
    id 2 
    label "b5" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#ffff99" 
    ] 
    ] 
    node [ 
    id 3 
    label "a3" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#beaed4" 
    ] 
    ] 
    node [ 
    id 4 
    label "a2" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#beaed4" 
    ] 
    ] 
    node [ 
    id 5 
    label "b7" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#ffff99" 
    ] 
    ] 
    node [ 
    id 6 
    label "b1" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#ffff99" 
    ] 
    ] 
    node [ 
    id 7 
    label "b2" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#ffff99" 
    ] 
    ] 
    node [ 
    id 8 
    label "b3" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#ffff99" 
    ] 
    ] 
    node [ 
    id 9 
    label "c3" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#fdc086" 
    ] 
    ] 
    node [ 
    id 10 
    label "c2" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#fdc086" 
    ] 
    ] 
    node [ 
    id 11 
    label "Z" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#7fc97f" 
    ] 
    ] 
    node [ 
    id 12 
    label "d2" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#386cb0" 
    ] 
    ] 
    node [ 
    id 13 
    label "d3" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#386cb0" 
    ] 
    ] 
    node [ 
    id 14 
    label "d1" 
    graphics [ 
     h 27 
     d 27 
     w 27 
     fill "#386cb0" 
    ] 
    ] 
    edge [ 
    source 0 
    target 11 
    value 10 
    ] 
    edge [ 
    source 1 
    target 11 
    value 10 
    ] 
    edge [ 
    source 2 
    target 11 
    value 10 
    ] 
    edge [ 
    source 3 
    target 11 
    value 10 
    ] 
    edge [ 
    source 4 
    target 11 
    value 10 
    ] 
    edge [ 
    source 5 
    target 11 
    value 10 
    ] 
    edge [ 
    source 6 
    target 11 
    value 10 
    ] 
    edge [ 
    source 7 
    target 11 
    value 10 
    ] 
    edge [ 
    source 8 
    target 11 
    value 10 
    ] 
    edge [ 
    source 9 
    target 11 
    value 10 
    ] 
    edge [ 
    source 10 
    target 11 
    value 10 
    ] 
    edge [ 
    source 11 
    target 12 
    value 10 
    ] 
    edge [ 
    source 11 
    target 13 
    value 10 
    ] 
    edge [ 
    source 11 
    target 14 
    value 10 
    ] 
] 

注意,節點不按順序排列。例如a1介於b1b5之間。這使得情節是這樣的:

enter image description here

我希望圖什麼是根據環路和初始數據結構outderdict依次順序。因此,黃色節點將與其他黃色等順序連在一起。

如何使用Networkx對GML文件進行排序?其他解決方案(例如Igraph)只要在Python框架內受歡迎。

+0

你不能使用ordereddict嗎?按鍵順序將隨機使用標準字典,否則在添加節點之前,您必須首先對字典中的值進行排序。 – EdChum 2015-03-13 09:27:15

+0

@EdChum:每種顏色(黃色,藍色,紫色等)對應一個鍵。它不必訂購。我們想要的是每個鍵的**成員**應該一起排列,例如,所有黃色節點不應該與紫色節點交替。由於成員包含在**列表**中,因此應該默認按順序排列。但創建GML時'networkx'不知道順序。 – pdubois 2015-03-13 13:13:17

回答

3

從2015年1月1日開始包含在NetworkX中的有序圖數據結構可用。OrderedGraph類將按照添加的順序從NetworkX數據結構輸出節點和邊。
您需要獲得最新的開發版本https://github.com/networkx/networkx/以便以下工作。

import networkx as nx 
outerdict = {"A":["a1","a2","a3"], "B":["b1","b2","b3","b5","b6", "b7"], "C":["c2","c3"], "D":["d1","d2","d3"]} 
keynode = "Z" 
colorlist = [ "#beaed4", "#fdc086", "#ffff99", "#386cb0","#f0027f"] 
G = nx.OrderedGraph() 
G.add_node(keynode,graphics={"fill":"#7fc97f","w":27,"h":27, "d":27}) 
for i,ky in enumerate(outerdict): 
    nodes = outerdict[ky] 
    nodecol = colorlist[i] 
    print ky, nodecol 
    for node in nodes: 
     G.add_node(node,graphics={"fill":nodecol,"w":27,"h":27,"d":27}) 
     G.add_edge(keynode, node, value = 10) 
outfile = "test.gml" 
nx.write_gml(G,outfile) 

注意:不是你已經初始化它你的「outerdict」數據結構可能會出現在不同的順序,當你遍歷「歷數(outerdict)」。如果節點「A」,「B」,「C」和「D」的順序對您很重要,那麼您需要改變它們被添加到圖中的方式,以確保它們按照該順序插入。