Networkx - 允许重复的节点标签?

时间:2017-04-03 23:58:08

标签: python graph networkx graph-modelling-language

我希望能够让很多节点具有相同的标签 - 在我的特定情况下,每个节点代表一篇新闻文章,并且应该用新闻类别标记。最终,我真正想要的是带有这些标签的GML文件。

这是一个小样本:

Gtest = nx.Graph()
nodes = [0, 1, 2, 3, 4]
labels = {0:"business", 1:"business",2:"sports", 3:"sports", 4:"politics"}

for node in nodes:
    Gtest.add_node(node)

print Gtest.nodes(data=True) 

""" 
this prints:
[(0, {}), (1, {}), (2, {}), (3, {}), (4, {})]

Which is good, I want 5 nodes.
"""

Gtest = nx.relabel_nodes(Gtest, labels)

print Gtest.nodes(data=True)

"""this prints:

[('business', {}), ('politics', {}), ('sports', {})]

There are only 3 nodes.
"""

nx.write_gml(Gtest, "gml/stackoverflow_test", stringizer = None)

"""
This writes the GML file:

graph [
  name "()"
  node [
    id 0
    label "business"
  ]
  node [
    id 1
    label "politics"
  ]
  node [
    id 2
    label "sports"
  ]
]
"""

最终,我试图最终得到GML文件:

graph [
  name "()"
  node [
    id 0
    label "business"
  ]
  node [
    id 1
    label "business"
  ]
  node [
    id 2
    label "sports"
  ]
  node [
    id 3
    label "sports"
  ]
  node [
    id 4
    label "politics"
  ]
]

是否可以为多个节点/生成此输出文件使用相同的标签?

2 个答案:

答案 0 :(得分:3)

以下是如何做到这一点的一个例子:

G = nx.Graph()
G.add_node(1, {'label' : 'foo'})
G.add_node(2, {'label' : 'foo'})
G.nodes(data=True)
#[(1, {'label': 'foo'}), (2, {'label': 'foo'})]
nx.write_gml(G,open("foo.gml","wb"))
  

图[
        节点[
          id 0
          标签1
        ]
        节点[
          id 1
          标签2
        ]
      ]

NB 答案是networkx-1.1。它在2.0或更高版本中不起作用。相反,您可以设置节点attrribute:

nx.set_node_attributes(G, {1: 'foo', 2: 'foo'}, 'label')
G.nodes(data=True)
#NodeDataView({1: {'label': 'foo'}, 2: {'label': 'foo'}})

答案 1 :(得分:0)

您可以通过将自定义函数作为stringizer参数传递给write_gml函数来实现此目的。

以下是生成所需GML文件的代码:

import networkx as nx

class Article:
    def __init__(self, id, category):
        self.id = id
        self.category = category

    def get_category(self):
        return self.category

Gtest = nx.Graph()
nodes = [0, 1, 2, 3, 4]
labels = {0: "business", 1: "business", 2: "sports", 3: "sports", 4: "politics"}

for node in nodes:
    Gtest.add_node(Article(node, labels[node]))

nx.write_gml(Gtest, "articles.gml", stringizer=Article.get_category)

请注意,如果您添加到add_node中的图形的对象是int,float或dict,则不会调用字符串化函数。因此,以上代码对节点使用了自定义类。