Skip to content

Feature: Serialize contraction hierarchy (save/load CH) #197

@jamescollinharky

Description

@jamescollinharky

Problem

Pandana builds a contraction hierarchy (CH) in the C++ backend when you create a Network. Only the graph is persisted: save_hdf5() saves nodes and edges, but not the CH. So every from_hdf5() rebuilds the contraction hierarchy from scratch, which can take a long time on large networks.

Proposed solution

Add the ability to save and load the precomputed CH so that reloading a network can skip the contraction step.

New/updated API:

  • network.save_ch(base_path) — write the CH to binary files (base_path_0.bin, base_path_1.bin, … for each impedance).
  • Network.from_hdf5(filename, ch_path=base_path) — load the graph from HDF5 and, when ch_path is given, load the CH from those files instead of running contraction.

Example:

# After building a network once
network.save_hdf5("graph.h5")
network.save_ch("ch")

# Later: load without re-contracting
network = pandana.Network.from_hdf5("graph.h5", ch_path="ch")

Optional extension: make Network picklable (__getstate__ / __setstate__) so the full network (graph + CH) can be serialized with pickle.dump / pickle.load.

Implementation notes

  • CH lives in the C++ layer (ContractionHierarchies in libch). The change adds SaveContractedEdges(path) and LoadPreprocessing(path) there, plus Python/Cython wiring and the save_ch / from_hdf5(ch_path=) API.
  • Binary format: header (node count, edge count) plus one record per contracted edge (source, target, distance, direction flags, etc.). No version field in the format yet.

I have a working prototype (patch + tests + design doc) and can open a PR against dev if this direction fits the project. Happy to adjust the API or scope based on maintainer feedback.


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions