i have attached numpy-array to nodes in a networkx graph. How to store the graph in gexf-format on disk? (without the numpy vector, as its just something intermediate...)
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)
However, this breaks. I'm new to python, but to me it seems like the ndarray is not serializable?! So, how to tell networkx to ignore that node attribute?
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'>
I solved this by removing the property "vector" from the data items:
for (n,d) in G.nodes(data=True):
del d["vector"]
full 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.