将开始和结束属性添加到gexf文件

时间:2015-11-06 19:59:07

标签: python-3.x graph networkx

我有一个.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

我要做的下一件事是向我的文件中的每个节点和每个边添加startend属性。

基本上,我想要这个

<?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模式以及startend属性):

<?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> 代码中设置startend属性

1 个答案:

答案 0 :(得分:0)

遇到同样的问题。 Networkx 2.1仍然不支持,但有一个解决方法:

  1. 照常编写.gexf文件
  2. Download Gephi 0.9.2并打开.gexf文件
  3. 转到数据实验室,然后按“合并列”#39;。现在选择开始和结束列以及&#39;合并策略&#39;:&#39;创建时间间隔&#39;。现在,您的Interval列填充了&lt; [startend]&gt;
  4. 转到文件&gt;出口&gt;图形文件...并选择文件格式:GEXF文件。您的节点现在包含startend属性。