skm_tools.persistence

Save networks to disk and load them again.

This module only reads and writes files; it keeps no state between calls.

Attributes

FORMATS

Functions

save_graph(g, path[, format])

Save a network to a file.

load_graph(path[, format])

Load a network saved with save_graph().

Module Contents

skm_tools.persistence.FORMATS = ('pickle', 'json')
skm_tools.persistence.save_graph(g, path, format='pickle')

Save a network to a file.

Parameters:
  • g (networkx.Graph) – Network to save.

  • path (str or pathlib.Path) – File to write (overwritten if it exists).

  • format ({"pickle", "json"}) –

    • "pickle" (default): fastest, keeps everything (graph type, attribute types), but Python-only, and unsafe to load from untrusted sources.

    • "json": portable and safe, keeps list attributes; tuples and sets become lists (see skm_tools.serialize.graph_to_dict(), also for its limits).

    On the full CKN (26k nodes, 899k edges), pickle took 2 s to save, 2 s to load and 101 MB; JSON 15 s, 6 s and 193 MB. For other network tools (e.g. Cytoscape), see skm_tools.cytoscape_utils, or networkx’s own writers.

skm_tools.persistence.load_graph(path, format='pickle')

Load a network saved with save_graph().

Parameters:
  • path (str or pathlib.Path) – File to read.

  • format ({"pickle", "json"}) – Format the file was saved in. Only load pickle files you trust: unpickling can run arbitrary code.

Returns:

The network, of the type it was saved as.

Return type:

networkx.Graph