简体   繁体   English

如何在 Python 中使用 networkx 读取 GraphML 文件?

[英]How to read a GraphML file using networkx in Python?

I'm new to networkx and Neo4j.我是 networkx 和 Neo4j 的新手。 I have imported a graph database to Neo4j using the 'Import method' because it was a huge graph and it couldn't be loaded.我已经使用“导入方法”将图形数据库导入 Neo4j,因为它是一个巨大的图形并且无法加载。 Now, I want to do some graph analytics using networkx.现在,我想使用 networkx 进行一些图形分析。 So, as I understood that I should do the following steps:因此,据我了解,我应该执行以下步骤:

  1. Export the graph as GraphML format from Neo4j.从 Neo4j 将图形导出为 GraphML 格式。
  2. Read the GraphML file using networkx in Python.使用 Python 中的 networkx 读取 GraphML 文件。

When reading the GraphML file I face this error: Bad GraphML data: no key labels.读取 GraphML 文件时,我遇到此错误:Bad GraphML data: no key labels。 I opened the GraphML file in notepad and I guess Neo4j is creating a wrong GraphML file.我在记事本中打开了 GraphML 文件,我猜 Neo4j 正在创建错误的 GraphML 文件。 I just have one node but it's considering two key labels for nodes.我只有一个节点,但它正在考虑节点的两个关键标签。

below is a snippet of the GraphML file.下面是 GraphML 文件的片段。 Can anyone help me out with this?谁能帮我解决这个问题?

:位置点(-87.9030396611 41.9790708201)

This seems to have been an issue for a while: https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/478 .这似乎是一段时间的问题: https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/478

What networkx complains about is that a node has data that has the key "labels," but there is no key with id "labels" defined. networkx 抱怨的是一个节点的数据具有键“标签”,但没有定义 id“标签”的键。 Thereby the exception.因此例外。

To avoid this, you have (at least) two options.为避免这种情况,您有(至少)两种选择。 You could do as suggested in the issue I linked, paste this to the top section of the graphML file:您可以按照我链接的问题中的建议进行操作,将其粘贴到 graphML 文件的顶部:

<key id="labels" for="node" attr.name="labels"/>

Another option is to set the format to "gephi" in the configuration (shown below).另一种选择是在配置中将格式设置为“gephi”(如下所示)。 When doing so, the data tag that referenced the "labels" is instead referencing the "TYPE" key, which is defined in the key-tags.这样做时,引用“标签”的数据标签将引用“类型”键,该键在键标签中定义。

call apoc.export.graphml.all(<your-path>, {format:"gephi"})

Also, with the default Neo4j export, networkx generates a ton of warnings, which you can remove by setting the useTypes to true.此外,使用默认的 Neo4j 导出,networkx 会生成大量警告,您可以通过将 useTypes 设置为 true 来删除这些警告。

call apoc.export.graphml.all(<your-path>, {format:"gephi", useTypes:true})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM