1

将networkx图转换为graphml时遇到以下问题(无法在此处发布所有代码,但这是要点)。NetworkX 版本为 1.6.2

我有一个名为 G 的 networkx 有向图

G = nx.DiGraph()

使用格式(AuthorA,AuthorB,Weight)的作者列表中的加权边缘填充它

G.add_weighted_edges_from(author_list)

我可以在这里导出graphml,它工作正常

nx.write_graphml(G, 'test.graphml')

然后我计算图表上的 pagerank

graph_metric = nx.pagerank_numpy(G, weight='weight')

然后将属性添加到图中的节点

nx.set_node_attributes(G, 'pagerank', graph_metric)

如果我遍历图表,我可以打印出节点名称和 pagerank

for n.d in G.nodes_iter(data=True):
     print n, d

作者A {u'pagerank': 0.0076688948270074164} ... ... ...

但是如果在更新属性后我尝试从图中创建一个 graphml,我会收到以下错误:

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 111, in generate_graphml

writer.add_graph_element(G)

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 305, in add_graph_element

self.add_nodes(G,graph_element)

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 262, in add_nodes

self.add_attributes("node", node_element, data, default)

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 255, in add_attributes

scope=scope, default=default_value)

File "/usr/lib/pymodules/python2.7/networkx/readwrite/graphml.py", line 242, in add_data

raise nx.NetworkXError('GraphML writer does not support '

NetworkXError: GraphML writer does not support dict types as data values.

想法?

4

2 回答 2

1

好吧,它确实告诉您-您不能将字典作为 graphml 属性-它们在大多数情况下需要是数字类型或字符串。

不是答案,但更容易将代码放在这里。找到“坏”节点:

for node in G.node:
    for attrib in G.node[node]:
        if type(G.node[node][attrib]) == dict:
            print node
于 2014-01-13T19:16:54.583 回答
0

我可以使用 pagerank_numpy() 数据保存 GraphMl:

根据 networkx doc 中的函数定义nx.set_node_attributes(G, 'pagerank', graph_metric)更改为。nx.set_node_attributes(G, graph_metric, 'pagerank')

我创建了一个带有权重的图表,计算了 nx.pagerank_numpy()并使用 pagerank 值保存了 GraphMl

代码:

import networkx as nx

G = nx.DiGraph()

G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'G'), ('B', 'F'), ('C', 'G')], weight = 2)
G.add_edges_from([('D', 'B'), ('E', 'C'), ('E', 'F'),('B', 'H')], weight = 3)

graph_metric = nx.pagerank_numpy(G, weight='weight')
nx.set_node_attributes(G, graph_metric, 'pagerank')
nx.write_graphml(G, 'test.graphml')

GraphMl 输出:

<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="d1" for="edge" attr.name="weight" attr.type="int" />
  <key id="d0" for="node" attr.name="pagerank" attr.type="double" />
  <graph edgedefault="directed">
    <node id="A">
      <data key="d0">0.07114273010226772</data>
    </node>
    <node id="B">
      <data key="d0">0.16184971098265896</data>
    </node>
    <node id="C">
      <data key="d0">0.13161405068919527</data>
    </node>
    <node id="G">
      <data key="d0">0.22232103156958644</data>
    </node>
    <node id="F">
      <data key="d0">0.14068474877723428</data>
    </node>
    <node id="D">
      <data key="d0">0.07114273010226768</data>
    </node>
    <node id="E">
      <data key="d0">0.07114273010226768</data>
    </node>
    <node id="H">
      <data key="d0">0.13010226767452204</data>
    </node>
    <edge source="A" target="B">
      <data key="d1">2</data>
    </edge>
    <edge source="A" target="C">
      <data key="d1">2</data>
    </edge>
    <edge source="B" target="G">
      <data key="d1">2</data>
    </edge>
    <edge source="B" target="F">
      <data key="d1">2</data>
    </edge>
    <edge source="B" target="H">
      <data key="d1">3</data>
    </edge>
    <edge source="C" target="G">
      <data key="d1">2</data>
    </edge>
    <edge source="D" target="B">
      <data key="d1">3</data>
    </edge>
    <edge source="E" target="C">
      <data key="d1">3</data>
    </edge>
    <edge source="E" target="F">
      <data key="d1">3</data>
    </edge>
  </graph>
</graphml>
于 2021-05-05T12:09:36.607 回答