1

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.

Tomáš Hons
  • 145
  • 7
  • The [github issue](https://github.com/networkx/networkx/issues/4377). The [documentation](https://networkx.org/documentation/latest/reference/generated/networkx.classes.function.edge_subgraph.html#networkx.classes.function.edge_subgraph) is now corrected, i.e. `SubGraph View` is the true type of returned object from `edge_subgraph`. – Tomáš Hons Jan 06 '21 at 08:27

0 Answers0