networkx删除节点属性

时间:2017-03-20 21:42:24

标签: python python-2.7 numpy networkx

我已将numpy-array附加到networkx图中的节点。如何在磁盘上以gexf格式存储图形? (没有numpy向量,因为它只是中间的东西......)

def create():
    G = nx.Graph()
    for i in range(256):
        G.add_node(i, vector=np.arange(20))
    for i in range(1,20):
        for j in range(1,256, 10):
            G.add_edge(i,j)

    temp = tempfile.mktemp(suffix=".gexf")
    print("dumping G = (V: %s, E: %s) to disk %s"
        % (len(G.nodes()), len(G.edges()), temp))
    nx.write_gexf(G, temp)

然而,这打破了。我是python的新手,但对我而言,似乎ndarray不可序列化?!那么,如何告诉networkx忽略该节点属性?

File "...lib\site-packages\networkx\readwrite\gexf.py", line 430, in add_attributes
    attr_id = self.get_attr_id(make_str(k), self.xml_type[val_type],
KeyError: <type 'numpy.ndarray'>

2 个答案:

答案 0 :(得分:2)

使用原生pickleh5py等HDF5库来序列化图形对象。例如,你可以这样做:

import pickle

with open("pickle_file", "wb") as f:
    pickle.dump(create(), f)

可以通过以下方式将pickle图形加载回Python:

with open("pickle_file", "rb") as f:
    G = pickle.load(f)

答案 1 :(得分:2)

我通过从数据项中删除属性“vector”解决了这个问题:

for (n,d) in G.nodes(data=True):
    del d["vector"]

完整的MWE:

def create():
    G = nx.Graph()
    for i in range(256):
        G.add_node(i, vector=np.arange(20))
    for i in range(1,20):
        for j in range(1,256, 10):
            G.add_edge(i,j)

    temp = tempfile.mktemp(suffix=".gexf")
    print("dumping G = (V: %s, E: %s) to disk %s"
        % (len(G.nodes()), len(G.edges()), temp))
    for (n,d) in G.nodes(data=True):
        del d["vector"]
    nx.write_gexf(G, temp)