Skip to content

digitopo Python API

Design record, for the maintainers. This specification was agreed on 2026-11-26; the guide it refers to is the book Du pixel à la mesure (Alain Lebret), whose topology recipes use digitopo, and the lots are the order of the work. Lots 1 to 3 are implemented and give the same results as pink26 point by point; the API of pink26 is complete (see "API parity with pink26").

Principles

  1. Same public API as pink26. Same submodules, function names, parameter names and return types. A recipe of the guide must run with from digitopo import skeleton or from pink26 import skeleton without any other change, and pink26 serves as the oracle of the tests without an adapter. The names inherited from Pink are kept (for example inhibit, not keep).
  2. Measurements are part of digitopo (module measure), with no dependency on skan.
  3. The needs of the guide decide the order of the work (lots 1 to 3 below).

Conventions

  • Input: any array_like; every nonzero value is an object point. Points outside the array belong to the background.
  • Output: bool for masks, uint8 for codes, int32 for labels. No function modifies its input.
  • connectivity is mandatory, as in pink26: 4 or 8 in 2D, 6 or 26 in 3D (8 or 80 in 4D, where available). The background uses the complementary connectivity. 18 is refused. The documentation gives the scikit-image equivalents: connectivity=1 is 4 (2D) or 6 (3D), and connectivity=ndim is 8 (2D) or 26 (3D).
  • Small connectivities by duality: a point is (4, 8)-simple (resp. (6, 26)-simple) in X if and only if it is (8, 4)-simple (resp. (26, 6)-simple) in the complement of X plus the point. The (8, 4) and (26, 6) test of Couprie and Bertrand (2009, Theorem 15) is therefore enough.
  • spacing (physical step per axis) is used by the measurements only, never by the topology.
  • Errors: as in pink26. TypeError for a wrong dtype or a non-integer argument; ValueError for an invalid value (wrong dimension or shape, a connectivity that does not exist in the dimension, a value out of range); DigitopoError, a subclass of ValueError (pink26's Pink26Error), for an input that digitopo cannot process although it is valid in principle (connectivity 18 where an operator does not handle it, a variant that Pink does not provide, an image too large for the core, a distance that overflows, a malformed file).

Types

Type Role
PointType(IntEnum) BACKGROUND, SIMPLE, MULTIPLE, SEPARATING, ISOLATED, INTERIOR, OTHER: same codes as pink26
PointClass3D(IntEnum) Types A to I of Malandain, Bertrand and Ayache (1993), same codes as pink26
Topology(NamedTuple) objects, holes (holes in 2D, tunnels in 3D), cavities (0 in 2D), euler
DigitopoError(ValueError) Input that digitopo cannot process (see Errors above)

Lot 1: recipes already in the guide

Recipe ids are stable; printed numbers are those of the 2026-11-26 build.

Function Returns Recipe
topology.topological_numbers(image, connectivity) (T, T_bar), two uint8 arrays recipe-157 (Fiche 103)
topology.simple_points(image, connectivity) bool mask recipe-157 (Fiche 103)
topology.point_types(image, connectivity) uint8 array of PointType recipe-157 (Fiche 103)
topology.point_classes_3d(image, connectivity) uint8 array of PointClass3D 3D skeletons
topology.end_points(image, connectivity) bool mask recipe-70, recipe-76 (Fiches 104, 105)
topology.curve_points(image, connectivity) bool mask recipe-70 (Fiche 104)
topology.junction_points(image, connectivity) bool mask recipe-70, recipe-76 (Fiches 104, 105)
topology.topology(image, connectivity) Topology recipe-158 (Fiche 107)
topology.euler_characteristic(image, connectivity) int recipe-158 (Fiche 107)
skeleton.ultimate_skeleton(image, connectivity, priority=None, inhibit=None) bool mask recipe-70 (Fiche 104)
skeleton.curvilinear_skeleton(image, connectivity, priority=None, inhibit=None) bool mask recipe-70, recipe-83 (Fiches 104, 106)

priority defaults to the squared Euclidean distance to the background, which gives a centred skeleton comparable to the medial axis of recipe-76 (Fiche 105), without changing the topology.

Lot 2: topology section to be written (implemented)

Function Returns Use
skeleton.parallel_skeleton(image, connectivity, shape="ultimate", symmetric=True, max_steps=None, inhibit=None) bool mask Critical kernels: symmetric skeletons, curves or surfaces in 3D
watershed.topological_watershed(image, connectivity) uint8 array Compared with recipe-38 and recipe-39 (Fiches 101, 102)
watershed.watershed_lines(image, markers, connectivity) bool mask Watershed lines from markers
evaluation.topology_errors(prediction, reference, connectivity) Topology of the differences Evaluating a segmentation, including one produced by a neural network
evaluation.cl_dice(prediction, reference, connectivity) float Overlap of networks (vessels, roots, roads)

Notes on lot 2:

  • parallel_skeleton gives pink26's results: Pink's lskelMK2, lskelNK2b and lskelCK2 in 2D, and pink26's corrected lskelMK3, lskelAMK3, lskelCK3a, lskelACK3a and lskelSK3 in 3D. Connectivity 8 or 26 only; the combinations that pink26 does not provide raise DigitopoError.
  • topological_watershed and watershed_lines give pink26's results (Pink's lwshedtopo with pink26's second pass), with connectivity 4 or 8 in 2D and 6, 18 or 26 in 3D. The image has no frame, unlike the topology and skeleton functions.
  • interop and watershed.watershed_labels have no equivalent in pink26. They follow the conventions of scikit-image, which the rest of the API does not: a connectivity from 1 to ndim (connectivity, skimage_connectivity), float images in [0, 1] (as_uint8, and the keyword-only convert=True of the grey-level functions, whose default keeps pink26's uint8 rule), labelled markers and labelled basins (watershed_labels). The connectivity stays mandatory everywhere: a default would differ from one function of scikit-image to another.
  • evaluation has no equivalent in pink26. topology_errors returns signed differences (prediction minus reference) of global counts. cl_dice follows Shit et al. (CVPR 2021) with digitopo's curvilinear skeletons: the symmetric parallel one for connectivity 8 or 26, the sequential one for 4 or 6.

Lot 3: from topology to measurement (implemented)

Function Returns Use
measure.branches(skeleton, connectivity, spacing=None, thin=True) Branches dataclass Physical lengths with diagonal steps and the spacing of volumes (chapter 12)
grayscale.homotopic_thinning(image, connectivity, condition=None, max_steps=None) uint8 array Simplifying a grey-level image without changing its topology
grayscale.homotopic_thickening(image, connectivity, condition=None, max_steps=None) uint8 array The dual: raising constructible points
grayscale.homotopic_kernel(image, connectivity, condition=None) uint8 array Thinning until stability
grayscale.lambda_kernel(image, connectivity, lambda_, condition=None) uint8 array Kernel that also lowers peaks and passes of contrast at most lambda

API parity with pink26 (complete)

Since 2026-11-27 digitopo provides every public function of pink26, with the same modules, names, parameters, defaults, dtypes, errors and messages (DigitopoError for Pink26Error), and the same results point by point (bytes for files). The last functions added:

Function Returns Notes
distance.distance_map(image, kind="squared_euclidean") int32 array Kinds squared_euclidean (2D, 3D), chamfer, d4, d8 (2D), d6, d18, d26 (3D); the outside is background; DigitopoError if a value does not fit in 32 bits
io.read(path) uint8, int32, float32 or float64 array Pink's PGM format (P2, P5, P7, P8, PB, P9, PA, PC, PD), 2D and 3D, parsed in Rust (src/io.rs) with pink26's rules
io.write(path, image) None The bytes of pink26 (P5, P8, P9, PC); bool is written as 0/255
skeleton.zhang_wang_thinning(image, max_steps=None) bool mask Pink's lskelzhangwang (no topology proof)
skeleton.guo_hall_thinning(image, variant=1, max_steps=None) bool mask Pink's lskelguohall, variants 1, 2, 3 (no topology proof)

Notes:

  • The path distances (chamfer, d4 to d26) are shortest paths with the neighbourhoods and weights of Pink's ldist and lchamfrein, computed by raster sweeps until stability; the squared Euclidean distance reuses the Meijster code of the skeletons.
  • io.read follows the C semantics of pink26's reader: a NUL byte ends a header line or an ASCII value, numbers are read like strtoll and strtod (hexadecimal floats, inf, nan(...); the NaN payload rules are those of the host's libc: macOS, verified, or glibc). A message that holds bytes of the file which are not UTF-8 raises the UnicodeDecodeError that pink26 raises.
  • One difference in the messages: a Guo-Hall variant outside the C int is a TypeError in both libraries, but pink26's message is nanobind's.
  • The two classic thinnings are Pink's lskelpar.c on the 2-pixel frame of parallel2d.rs; after the first step, only the points near a removed point are tested again, which gives the same result.

Provenance

The order in which the skeletons examine the points was read in pink26's copy of Pink (lskeletons.c, mcrbt.c, mccodimage.c, CeCILL v2.0): Pink's red-black tree with its reallocations, its direction codes and its neighbour order. digitopo reproduces them so that the results are identical; this part is therefore derived from Pink, which CeCILL v2.1 allows (article 12.3 of CeCILL v2.0). The simplicity test, the topological numbers, the Euler characteristic and the distance map are written from the articles.

Decisions on measure.branches (2026-11-27):

  • Result: a frozen dataclass Branches with NumPy arrays start, end ((n, ndim) coordinates), length ((n,), units of spacing), kind (uint8 codes of BranchKind: TERMINAL 1, INTERNAL 2, ISOLATED 3, CYCLE 4), paths (list of (m, ndim) coordinates) and labels (int32 image: 0 outside, k on branch k - 1, -1 on junctions); len(b) and b.count(kind).
  • Length: raw chain length, the sum of the Euclidean distances between successive point centres, scaled by spacing (no Kulpa-type correction).
  • Nodes: end points (one neighbour) and junctions, a junction being a connected group of points with three neighbours or more. A closed curve without node is one CYCLE branch.
  • Links: m-adjacency (Gonzalez and Woods), extended to 3D: a diagonal link is dropped when another skeleton point lies in the box it spans, so that the staircases of 8- and 26-connected skeletons are chains.
  • thin=True (default): the skeleton is first thinned by ultimate_skeleton with its end points inhibited, which keeps its topology and its end points but removes the points that make it two points thick (symmetric parallel skeletons keep such points); the measured skeleton is returned in Branches.skeleton.
  • The grayscale module gives pink26's results; its Rust code was restructured (one generic implementation for 2D, 3D and both directions, a bucket queue, one byte of flags per point), see the documentation of src/grayscale.rs.

Errors and limits

  • Rust: every public function returns digitopo::Result<T> and checks its arguments (dimension, number of points, connectivity, sizes of the optional images, spacing, markers). digitopo::Error tells why (Dimension, Connectivity, Shape, TooLarge, Argument, NotProvided, Overflow); no input makes the crate panic. The few panics left guard internal invariants (documented in # Panics for Cube::new).
  • Python: the modules check the arguments as pink26 does (same exceptions and messages); an error found by the Rust core raises digitopo.DigitopoError, a ValueError.
  • Empty images (an axis of length 0) give empty results in Rust; the Python functions follow pink26 (some refuse them).
  • Size: digitopo stores point indices in 32 bits, so an image may have up to about 4 x 10^9 points, frame included. pink26's smaller limits come from Pink's C code (2^24 points for the grey-level operators, 2^25 for the watershed, 2^31 pixels for the 2D thinnings); digitopo does not apply them: these calls raise an error in pink26 and give a result in digitopo.

Open points

  • The parallel thinnings run their passes that do not depend on the scan order on all cores (the marking of the simple points, the masks of MK2 and CK2, the isthmus points of CK3a, ACK3a and SK3, the removals): MK2 is 5.7 times faster on 1024^2, MK3 1.6 times on 128^3. The passes whose marks change the next points of the scan stay sequential, in Pink's order. The sequential skeletons compute their initial constraints in parallel in 3D only: in 2D, a parallel pass slowed the sequential loop that follows.