将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
AuthorA {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.
思想?
答案 0 :(得分:1)
好吧,它确实告诉你 - 你不能把字典作为graphml属性 - 它们需要是数字类型或字符串,大多数情况下。
不是答案,但更容易将代码放在这里。 找到'坏'节点:
for node in G.node:
for attrib in G.node[node]:
if type(G.node[node][attrib]) == dict:
print node
答案 1 :(得分:0)
我可以使用 pagerank_numpy() 数据保存 GraphMl:
我将 nx.set_node_attributes(G, 'pagerank', graph_metric)
改为 nx.set_node_attributes(G, graph_metric, 'pagerank')
as per the function definition in networkx doc。
我创建了一个带权重的图表,计算了 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>