I am trying to pickle networkx's edge_subgraph and I does not work.
Here is the code
import networkx as nx
import pickle as pkl
G = nx.binomial_graph(100, 0.1)
significant_edges = [(u,v) for u,v in G.edges() if G.degree[u] == G.degree[v]]
subgraph = nx.edge_subgraph(G, significant_edges)
with open('subgraph.pkl', 'wb') as f:
pkl.dump(subgraph, f)
AttributeError: Can't pickle local object 'show_edges.<locals>.<lambda>'
I checked it also with node subgraph, ie. subgraph
, and that works.
It can be fixed by initializing new graph.
subgraph = nx.Graph(nx.edge_subgraph(G, significant_edges))
But I would like to know what in networkx mechanics causes this error. Also, a more elegant way to sidestep this error.
I suspect that it is causes by the fact, that this edge supgraph is not a proper graph itself and is tight with the original graph. Maybe it is necessary to create a new graph.
Any help appreciated, thank you.