Skip to content

Add exact blockwise topological thinning and sheetness; shuffle block order - #102

Merged
davidackerman merged 15 commits into
mainfrom
blockwise-thinning
Nov 6, 2026
Merged

davidackerman merged 15 commits into
mainfrom
blockwise-thinning

Conversation

@davidackerman

@davidackerman davidackerman commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

@stuarteberg

Summary

  • topological-thinning: blockwise Lee thinning to curve skeletons, or to medial surfaces with medial_surface: true, for objects such as ER that span the volume, which per-object skeletonize can't hold in memory.
    • The kernel is adapted from scikit-image's BSD implementation, with the blockwise scheme of COSEM's SparkTopologicalThinning, whose own kernel derives from GPL Fiji code.
    • Lee's sequential re-check visits the 8 parity subfields of the dataset's voxel index in turn, so one iteration reaches at most 48 voxels (6 directions × 8 subfields). A pass of k = iterations_per_pass iterations with a 48k+2 voxel halo therefore matches whole-volume thinning. The re-check also tests Euler invariance, so no object loses its last voxel (scikit-image's can).
    • A pass rethins only blocks with deletions in or near them. A block whose face voxels can no longer change is thinned to completion without a halo and not revisited.
    • Each object is thinned as if it were alone and keeps its ID. Thinning works on the voxel grid, so it ignores anisotropy.
  • sheetness: sheetness and thickness from the medial surface topological-thinning writes, ported from COSEM's SparkCurvature and SparkCalculatePropertiesFromMedialSurface:
    • at each medial-surface voxel, the Descoteaux sheet measure, the largest over COSEM's scales, and the thickness, twice the distance to the nearest voxel outside its object;
    • every other voxel takes the mean sheetness of its nearest medial-surface voxels by paths through its object (COSEM's 2023 method);
    • both distances are exact: a block's read widens until they are, as far as it fits in half its worker's memory (or max_padding_voxels, if set), and the run fails beyond that;
    • it writes a uint8 sheetness volume, sheetness and thickness on the medial surface, and CSVs with one row per object: a summary, volume and surface area by sheetness, and medial-surface voxels by sheetness and thickness.
  • COSEM parity: the thinning kernel and hessian: cosem reproduce COSEM's; the defaults intentionally do not, so default values are not directly comparable with published COSEM results.
    • The Hessian and the geodesic step use each object's own mask.
    • The default Hessian (hessian: exact) takes Gaussian derivatives at scales from one voxel up. COSEM's finite differences score sheets thinner than 3 voxels only 0.3-0.5; with Gaussian derivatives, synthetic sheets score about 0.7-0.87, tubes 4 or more voxels wide 0.12-0.16 and thinner ones up to 0.3.
  • Checks:
    • One-off comparisons with COSEM's jar, not in CI: the thinning kernel is voxel-identical to its Skeletonize3D_ on 680 random volumes, with the same iteration counts and the same blocks found independent; its Hessian matches SparkCurvature.getCurvature to within 1e-5 (the jar computes in float32).
    • Tests: blockwise against whole-volume thinning across block shapes, iterations per pass, roi and mask, including blocks being skipped and retired; the Gaussian-derivative Hessian against scipy's filters; and sheetness failing when a read can't be made exact.
  • Block order: blocks run through compute_blockwise_partitions are now shuffled, with a fixed seed, so a dataset's dense blocks no longer land on a few workers. randomize had been off, and a no-op because repartition keeps order. Results are read back in block order, so outputs don't change.
    • Sheetness packs its blocks into partitions balanced by object voxels and starts the heaviest first (new block_partitions and compute_partitions). On the jrc_c-elegans-dddpc-1 ER on 96 workers this took sheetness from 94 to 30 min with identical results: shuffling took its first step from 20.8 to 13.4 min, and heaviest-first its second from 73.1 to 16.7 min. Other commands were not timed.
    • The skeletonize and split-narrow-bridges waves keep their heaviest-first lists (randomize=False), and multiscale is unchanged.
  • multiscale gets downsample_method: mode_suppress_zero (each window's most common non-zero label, as neuclease's flat_mode_except_zero) and mean_suppress_zero (the mean of its non-zero voxels). topological-thinning's pyramid uses the first: its output is a voxel thin, and on a 256³ crop of a real medial surface plain mode kept no voxels at s1. sheetness pyramids its three volumes with the second.
  • Two new compiled extensions (topological_thinning, geodesic), so installs need a rebuild.
  • Usage, limits and attribution: the README sections "Medial surfaces and sheetness" and "Acknowledgements".

Port COSEM's blockwise Lee thinning (curve skeletons and medial surfaces)
on scikit-image's BSD kernel: the re-check runs over 8 parity subfields of
the dataset's voxel index and tests Euler invariance, so a pass of k
iterations with a 48k+2 voxel halo matches thinning the whole volume.
Blocks are skipped when nothing changed near them and retired once their
faces are final. Objects are thinned as if alone and keep their IDs.

Port COSEM's sheetness and thickness from the medial surface: the Hessian
sheet measure over 12 scales on each object's own mask, thickness as twice
the distance to another label, and each voxel's sheetness from its nearest
medial-surface voxels by geodesic distance within its object.
@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.71180% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.71%. Comparing base (e467e60) to head (0b4602a).
⚠️ Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
src/cellmap_analyze/analyze/sheetness.py 97.00% 9 Missing ⚠️
...rc/cellmap_analyze/process/topological_thinning.py 95.90% 7 Missing ⚠️
src/cellmap_analyze/util/dask_util.py 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #102      +/-   ##
==========================================
+ Coverage   86.51%   87.71%   +1.20%     
==========================================
  Files          27       29       +2     
  Lines        4048     4558     +510     
==========================================
+ Hits         3502     3998     +496     
- Misses        546      560      +14     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

COSEM's central differences of central differences underestimate the
Hessian below a blur of about 2 voxels, so sheets thinner than 3 voxels
scored 0.3-0.5. The default now takes Gaussian derivatives at each scale
(blurring to within a voxel of it, then a sigma-1 derivative kernel at
each point), normalised by sigma squared per axis, with Frangi's noise
constant, and skips scales under a voxel, where thin tubes look flat.
hessian='cosem' keeps COSEM's.
Contiguous db.range partitions put a dataset's dense blocks together, so
a few partitions ran long after the rest: on the c-elegans ER, 73 of 96
workers sat idle at the end of sheetness. Blocks are now shuffled by
default (randomize was a no-op: repartition keeps order), and sheetness
packs its geodesic step by each block's object voxels, heaviest first,
with per-task priorities so the slowest blocks start first.
…ng and retirement

Both sheetness steps now widen a block's read until its distances are exact,
up to max_padding_voxels (default 128) per side, and raise past that. The
fill step used to stop after three widenings and keep what it had; with every
voxel exact, the always-zero Unreached Volume column is gone.

A new thinning test checks that blocks are retired, copied and skipped as
intended and still give the whole-volume result.
A block with object voxels but no medial-surface voxels returned nothing from
the first step, so the second step costed it 0 and started it last. It now
reports its object voxels. The README also notes that thinning works on the
voxel grid, so it ignores anisotropy.
@davidackerman davidackerman changed the title Add blockwise topological thinning and sheetness Add blockwise topological thinning and sheetness; shuffle block order Nov 6, 2026
Skeletons and medial surfaces are a voxel thin, so in a 2x2x2 window the
background always wins the mode: on a 256^3 crop of a real medial surface,
mode kept no voxels at s1. Multiscale gets a nonzero_mode method, which takes
each window's most common non-zero label, and topological-thinning's pyramid
uses it.
Renames nonzero_mode to mode_suppress_zero, after neuclease's
flat_mode_except_zero, and adds mean_suppress_zero: the mean of each window's
non-zero voxels. With multiscale: true, sheetness pyramids its three volumes
by it, so thin ER and the values on its medial surface survive the coarse
levels undiluted, as volume rendering needs.
Instead of a fixed 128 voxels per side, a read widens while it fits in half
its dask worker's memory limit (the machine's available memory outside a
worker), from an estimate of each step's bytes per voxel. max_padding_voxels
stays as an optional hard cap. The error says which limit was hit.
With reconstruction: true, sheetness labels every voxel within a
medial-surface voxel's ball, of radius its distance to the edge of its object,
and adds each object's reconstructed volume to the summary CSV. Such a ball
holds only its object's voxels, so what the rebuild misses is detail the
medial surface lost. It is drawn in the geodesic step, whose reads already
reach every ball that reaches the block.
@davidackerman davidackerman changed the title Add blockwise topological thinning and sheetness; shuffle block order Add exact blockwise topological thinning and sheetness; shuffle block order Nov 6, 2026
@davidackerman
davidackerman merged commit 8a7e68e into main Nov 6, 2026
2 checks passed
@davidackerman
davidackerman deleted the blockwise-thinning branch November 6, 2026 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant