def save_graphml(G, filename='graph.graphml', folder=None):
"""
Save graph as GraphML file to disk.
Parameters
----------
G : networkx multidigraph
filename : string
the name of the graphml file (including file extension)
folder : string
the folder to contain the file, if None, use default data folder
Returns
-------
None
"""
start_time = time.time()
if folder is None:
folder = settings.data_folder
# create a copy and convert all the node/edge attribute values to string or
# it won't save
G_save = G.copy()
for dict_key in G_save.graph:
# convert all the graph attribute values to strings
G_save.graph[dict_key] = make_str(G_save.graph[dict_key])
for _, data in G_save.nodes(data=True):
for dict_key in data:
# convert all the node attribute values to strings
data[dict_key] = make_str(data[dict_key])
for _, _, data in G_save.edges(keys=False, data=True):
for dict_key in data:
# convert all the edge attribute values to strings
data[dict_key] = make_str(data[dict_key])
if not os.path.exists(folder):
os.makedirs(folder)
nx.write_graphml(G_save, '{}/{}'.format(folder, filename))
log('Saved graph "{}" to disk as GraphML at "{}/{}" in {:,.2f} seconds'.format(G_save.name, folder, filename, time.time()-start_time))
评论列表
文章目录