我有一个.gexf
文件,其中包含带有ID和标签的节点和边缘。我使用.gexf
从.gml
文件生成了此networkx
文件。这是代码:
import networkx as nx
G = nx.read_gml('data/gml/test.gml') # read in gml file as Graph
nx.write_gexf(G, "output/test.gexf") # write to gexf format
我要做的下一件事是向我的文件中的每个节点和每个边添加start
和end
属性。
基本上,我想要这个:
<?xml version='1.0' encoding='utf-8'?>
<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<graph defaultedgetype="undirected" mode="static">
<nodes>
<node id="clock" label="clock" />
<node id="beach" label="beach" />
<node id="sun" label="sun" />
<node id="sea" label="sea" />
<node id="sand" label="sand" />
<node id="guitar" label="guitar" />
(...)
</nodes>
<edges>
<edge id="0" source="ice" target="shoe" weight="0.9995600294856769" />
<edge id="1" source="ice" target="toothbrush" weight="0.9992457544219484" />
<edge id="1533" source="snake" target="ant" weight="0.9999144063155566" />
(...)
<edge id="1534" source="mosquito" target="jellyfish" weight="0.9994175606336606" />
<edge id="1535" source="ant" target="star" weight="0.9994226236705537" />
</edges>
</graph>
</gexf>
看起来像这样(请注意dynamic
模式以及start
和end
属性):
<?xml version='1.0' encoding='utf-8'?>
<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<graph defaultedgetype="undirected" mode="dynamic">
<nodes>
<node id="clock" label="clock" start="2000-02-20" end="2000-02-22" />
<node id="beach" label="beach" start="2000-02-20" end="2000-02-22" />
<node id="sun" label="sun" start="2000-02-20" end="2000-02-22" />
<node id="sea" label="sea" start="2000-02-20" end="2000-02-22" />
<node id="sand" label="sand" start="2000-02-20" end="2000-02-22" />
<node id="guitar" label="guitar" start="2000-02-20" end="2000-02-22" />
(...)
</nodes>
<edges>
<edge id="0" source="ice" target="shoe" weight="0.9995600294856769" start="2000-02-20" end="2000-02-22" />
<edge id="1" source="ice" target="toothbrush" weight="0.9992457544219484" start="2000-02-20" end="2000-02-22" />
<edge id="1533" source="snake" target="ant" weight="0.9999144063155566" start="2000-02-20" end="2000-02-22" />
(...)
<edge id="1534" source="mosquito" target="jellyfish" weight="0.9994175606336606" start="2000-02-20" end="2000-02-22" />
<edge id="1535" source="ant" target="star" weight="0.9994226236705537" start="2000-02-20" end="2000-02-22" />
</edges>
</graph>
</gexf>
很遗憾,我无法找到任何关于如何编写动态networkx
文件并添加pygexf
和{gexf
以及start
}的文档。 {1}}属性到每个(已存在的)节点和边缘。有人可以帮我这个吗?
更新: 当我使用
end
要设置边缘属性,我会得到正确的输出,例如:
nx.set_edge_attributes(G, 'start', '2000-02-20')
nx.set_edge_attributes(G, 'end', '2000-02-22')
然而,当我这样做时:
<edge id="0" source="great" target="wait" weight="0.998675772419067" start="2000-02-20" end="2000-02-22" />
我明白了:
nx.set_node_attributes(G, 'start','2000-02-20')
nx.set_node_attributes(G, 'end','2000-02-22')
如何在<node id="blue" label="blue">
<attvalues>
<attvalue for="0" value="2000-02-20" />
<attvalue for="1" value="2000-02-22" />
</attvalues>
代码中设置start
和end
属性?
答案 0 :(得分:0)
遇到同样的问题。 Networkx 2.1仍然不支持,但有一个解决方法:
start
,end
]&gt; start
和end
属性。