How do I pickle and unpickle a list of MultiDiGraphs created with NetworkX?

Viewed 2317

I have a list of MultiDiGraphs (directed graphs that allow more than one edge between each pair of nodes) that I have created using NetworkX. I have stored these graphs in a list. Now, I would like to pickle this list, so I can use it in the future -I know I could use other methods of storage but I am particularly interested in this one-.

I would also like to be able to load the pickled information into another list of graphs if I want to use it in the future.

Could anyone tell me how to pickle and unpickle this kind of object? Thanks.

2 Answers

I use cPickle for python2 (it's faster) but pickle for python3, so import accordingly depending on what python you use.

import cPickle as pickle  # python2
import pickle  # python3
import networkx as nx

# Make graph
G = nx.MultiDiGraph()
G.add_edges_from([(1,2),(1,2),(3,4),(1,3)])

# Dump graph
with open("/path/to/file/multigraph.p", 'wb') as f:
    pickle.dump(G, f)

# Load graph
with open("/path/to/file/multigraph.p", 'rb') as f:  # notice the r instead of w
    G_loaded = pickle.load(f)

As simple as that.

[EDIT] To pickle.dump a list of graphs:

# Dump List of graphs

l = [G, G_loaded]
with open("/path/to/file/multigraph.p", 'wb') as f:
    pickle.dump(l, f)

# Load List of graphs

with open("/path/to/file/multigraph.p", 'rb') as f:
    l_load = pickle.load(f)

Now l_load[0] contains multigraph G.

Not sure whether the networkx intra-library util methods were available back in 2017, but for current (2020) reference, you can use:

gpickle module in networkx.readwrite

Something like the pseudocode below. Under the hood, the method is essentially doing the same thing, i.e., calling pickle.dummp(list, path)

# Gs : List[nx.MultiGraph] = [gs1, gs2,...,gsT]
out_fp = f'{data_dir}/{dataset}/graphs'
logger.info(f'| saving nx.Graphs  outputs to {out_fp}/{output_prefix}_*.gpickle')
nx.write_gpickle(Gs, f"{out_fp}/{output_prefix}_Gs.gpickle")
Related