NetworkX之輸入檔案GML

NO IMAGE

之前看過networkx,igraph都說支援GML格式檔案輸入形式,直到今天想再Nepidemix load進檔案進行分析,發現Nepidemix 對load輸入檔案要求是GML和gpickle,其餘的不支援。所以查閱了下GML究竟是什麼。

GML(Graph Modelling Language):
There are many different programs that work with graphs but almost all of them use their own file format. As a consequence, exchanging graphs between different programs is almost impossible. Simple tasks like exchange of data, externally reproducible results or a common benchmark suite are much harder than neccessary.
Therefore, we have developed a new file format for the Graphlet system: GML. GML supports attaching arbitrary information to graphs, nodes and edges, and is therefore able to emulate almost every other format.
原文連結 可以看出GML 是網路資料的一個統一標準,相當於度量衡的作用。GML支援關於圖,點,邊的任意資料的附加,因此可以模擬任意格式的資料。
networkx 提供了函式 可以讀GML格式的資料 或者將你load進去的普通網路轉換成GML格式的檔案。連結請click

read_gml(path,[,encoding, relabel]) 讀GML
write_gml(G,path)寫網路G進GML檔案
parse_gml(lines[,relael]) 從字串中解析GML圖
generate_gml(G)  Generate a single entry of the graph G in GML format

舉個栗子:
我的原始資料是這樣,存在一個GML.txt裡
a

結果:投影的GML檔案是這樣:

graph [
node [
id 0
label "1"
]
node [
id 1
label "3"
]
node [
id 2
label "2"
]
edge [
source 0
target 1
]
edge [
source 0
target 2
]
]