读取igraph和NetworkX中缺少数据的GML文件时出错

2024-04-25 20:37:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组网络,其中包含一些缺少的节点属性数据。我需要能够访问igraph、NetworkX和Gephi中的这些网络。我知道我可以在GML中将缺少的值记录为NAN,igraph会理解它,但NetworkX不会(有此错误消息:networkx.exception.NetworkXError: expected an int, float, string or '[', found 'NAN' at (16, 9)

GML文件如下所示(称为“export.GML”):

graph [
  directed 1
  node [
    id 0
    label "1"
    age 1
  ]
  node [
    id 1
    label "2"
    age 2
  ]
  node [
    id 2
    label "3"
    age "NA"
  ]
  edge [
    source 0
    target 1
  ]
  edge [
    source 1
    target 2
  ]
  edge [
    source 2
    target 0
  ]
]

我可以在Python的igraph中打开此文件,直到最近我还可以在R中打开类似的文件。到今天为止,我首先在Python中得到的是:

>>> import igraph
>>> g = igraph.read("export.gml")
>>> g.vs["age"]
['1', '2', 'NA']

然后在R中:

R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(igraph)

Attaching package: ‘igraph’

The following objects are masked from ‘package:stats’:

    decompose, spectrum

The following object is masked from ‘package:base’:

    union

> igraph_version()
[1] "1.2.6"
> g <- read_graph("export.gml", format = "gml")

 *** caught segfault ***
address (nil), cause 'memory not mapped'

Traceback:
 1: read.graph.gml(file, ...)
 2: read_graph("export.gml", format = "gml")

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 

有没有办法在igraph中打开此文件?或者,有没有办法让NetworkX正确解释GML文件中的NAN