简体   繁体   English

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

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

When creating a graph in NetworkX and exporting to GraphML, the keys automatically get turned into "d%i" .在 NetworkX 中创建图形并导出到 GraphML 时,键会自动变成"d%i" For example:例如:

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")

makes something like做类似的东西

<?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">

...

I'm looking to make the keys keep the same name as the as attr.name as specified in the <key> tag:我希望使键的名称与<key>标记中指定的attr.name保持相同的名称:

  <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>

...

Someone asked the same question on the Google forums but was told to change how GraphML is read by a Gremlin server, not how it is written using NetworkX.有人在 Google 论坛上问过同样的问题,但被告知要更改 Gremlin 服务器读取 GraphML 的方式,而不是使用 NetworkX 编写它的方式。

Is there a way to make nx.write_graphml() or nx.generate_graphml() create files using labelV , age , ... instead of d0 , d1 , ... for the id= in <key> tags?有没有办法让nx.write_graphml()nx.generate_graphml()使用labelVage ,...而不是d0d1 ,...为<key>标签中的id=创建文件?

In NetworkX 2.5 this is now possible with named_key_ids :在 NetworkX 2.5 中,现在可以使用named_key_ids

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NetworkX在读写GML时出现label和id问题 - NetworkX problem with label and id when reading and writing GML igraph未在Python中将属性写入graphml - igraph not writing attribute to graphml in Python Networkx-导出带有边缘标签,高度和宽度属性,自定义图像的graphml - Networkx - exporting graphml with edge labels, height and width attributes, custom images 如何在没有NetworkX密钥的情况下访问节点属性? - How to access node attributes without key in networkx? 通过networkx读取时如何保持图形形状 - How to keep graph shape when read it by networkx 将图形从networkx转换为pytorch几何时如何保留节点顺序? - How to retain node ordering when converting graph from networkx to pytorch geometric? 使用networkx.union()时,从networkx.Graph继承的类失败 - class inherit from networkx.Graph fails when using networkx.union() 从不同目录调用文件时如何指定导入路径? - How to specify an import path when the file is called from different directories? Python如何从networkx图中获取生成的邻接矩阵的标签? - Python how to get labels of a generated adjacency matrix from networkx graph? 如何从已经使用networkx创建的多个节点添加多个边? - How to add multiple edges from multiple nodes that are already created with networkx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM