I'm trying to save networkx DiGraph by preserving nodes' attributes.
I have tried with nx.write_weighted_edgelist
, nx.write_edgelist
and nx.write_weighted_edgelist
, and after trying (and also looking at https://networkx.org/documentation/networkx-1.10/reference/readwrite.html) I know that both adjacency and edge lists does not preserve nodes' attributes.
Now I have seen also the other options in the networkx link, but I don't understand if other commands preserve attributes, and I need to be sure that it works (my code need to create and save more than 5000 graphs and it takes almost a day to run).
So which is the best way to save a graph and preserve nodes' attributes?
First, note that you are using an outdated version of the NetworkX docs; you should always use the stable version.
One format which is guaranteed to preserve node data is the pickle (although this is deprecated in NetworkX 2.6, it is currently usable):
In [1]: import networkx as nx
In [2]: G = nx.Graph()
In [3]: G.add_node("A", weight=10)
In [4]: nx.write_gpickle(G, "test.gpickle")
In [5]: H = nx.read_gpickle("test.gpickle")
In [6]: H.nodes(data=True)
Out[6]: NodeDataView({'A': {'weight': 10}})
The GML format should also work for most datatypes:
In [8]: nx.write_gml(G, "test.gml")
In [9]: H = nx.read_gml("test.gml")
In [10]: H.nodes(data=True)
Out[10]: NodeDataView({'A': {'weight': 10}})
GEXF works as well:
In [12]: nx.write_gexf(G, "test.gexf")
In [13]: H = nx.read_gexf("test.gexf")
In [14]: H.nodes(data=True)
Out[14]: NodeDataView({'A': {'weight': 10, 'label': 'A'}})
So you have several options and can decide based on performance and support for the specific attribute data you are trying to save.