从 NetworkX 编写 GraphML 时如何指定 <key> id?

How to specify the <key> id when writing GraphML from NetworkX?

在 NetworkX 中创建图形并导出到 GraphML 时,键会自动 get turned into "d%i"。例如:

import networkx as nx

G = nx.DiGraph()   # or DiGraph, MultiGraph, MultiDiGraph, etc

G.add_node(1, labelV="person", age=29, name="marko")
G.add_node(2, labelV="person", age=27, name="vadas")
G.add_node(3, labelV="software", name="lop", lang="java")
G.add_node(4, labelV="person", age=32, name="josh")
G.add_node(5, labelV="software", name="ripple", lang="java")
G.add_node(6, labelV="person", age=35, name="peter")

G.add_edge(1, 2, labelE="knows", weight=1.0)
G.add_edge(1, 4, labelE="knows", weight=1.0)
G.add_edge(1, 3, labelE="created", weight=0.4)
G.add_edge(4, 5, labelE="created", weight=1.0)
G.add_edge(4, 3, labelE="created", weight=0.4)
G.add_edge(6, 3, labelE="created", weight=0.2)

nx.write_graphml(G, "networkx_tinkergraph_modern.xml")

制作类似

的东西
<?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 attr.name="weight" attr.type="double" for="edge" id="d5"/>
<key attr.name="labelE" attr.type="string" for="edge" id="d4"/>
<key attr.name="lang" attr.type="string" for="node" id="d3"/>
<key attr.name="name" attr.type="string" for="node" id="d2"/>
<key attr.name="age" attr.type="long" for="node" id="d1"/>
<key attr.name="labelV" attr.type="string" for="node" id="d0"/>
<graph edgedefault="directed"><node id="1">
  <data key="d0">person</data>
  <data key="d1">29</data>
  <data key="d2">marko</data>
</node>
<node id="2">
  <data key="d0">person</data>
  <data key="d1">27</data>
  <data key="d2">vadas</data>
</node>
<node id="3">

...

我希望使密钥与 attr.name 保持相同的名称,如 <key> 标签中指定的:

  <key id="labelV" for="node" attr.name="labelV" attr.type="string"/>
  <key id="name" for="node" attr.name="name" attr.type="string"/>
  <key id="lang" for="node" attr.name="lang" attr.type="string"/>
  <key id="age" for="node" attr.name="age" attr.type="int"/>
  <key id="labelE" for="edge" attr.name="labelE" attr.type="string"/>
  <key id="weight" for="edge" attr.name="weight" attr.type="double"/>
  <graph id="G" edgedefault="directed">
    <node id="1">
      <data key="labelV">person</data>
      <data key="name">marko</data>
      <data key="age">29</data>
    </node>
    <node id="2">
      <data key="labelV">person</data>
      <data key="name">vadas</data>
      <data key="age">27</data>
    </node>
    <node id="3">
      <data key="labelV">software</data>
      <data key="name">lop</data>
      <data key="lang">java</data>
    </node>

...

有人在 the Google forums 上问了同样的问题,但被告知要更改 GraphML 由 Gremlin 服务器读取的方式,而不是使用 NetworkX 编写的方式。

有没有办法让 nx.write_graphml()nx.generate_graphml() 使用 labelVage、...而不是 d0、[= 创建文件22=], ... <key> 标签中的 id=?

在 NetworkX 2.5 中,现在可以使用 named_key_ids:

nx.write_graphml(G, "networkx_tinkergraph_modern.xml", named_key_ids=True)