Skip to main content

teasar

TEASAR skeletonisation of villa's surface-track slabs, written in Rust with PyO3 bindings. Its GPU backend is Metal, for Apple GPUs; a pure-Rust CPU reference sits behind the same API. Each backend is a Rust module behind its own Cargo feature (metal), so other GPU backends can sit beside it.

teasar is an independent implementation. It contains no code from Kimimaro or Brook; it was written from its specification, the TEASAR paper and published GPU algorithms.

Credits

  • Sato, Bitter, Bender, Kaufman and Nakajima, "TEASAR: tree-structure extraction algorithm for accurate and robust skeletons", Pacific Graphics 2000, doi:10.1109/PCCGA.2000.883951
  • Kimimaro (seung-lab), the reference whose behaviour this package matches

Install

pip install teasar          # or: uv pip install teasar

PyPI has prebuilt wheels, one abi3 wheel per platform for CPython 3.11 or newer, so installing needs no Rust toolchain:

platform wheel backends
macOS on Apple silicon (arm64) macosx_11_0_arm64 Metal and the CPU reference
Linux x86_64 and arm64 (glibc 2.28 or newer) manylinux_2_28 the CPU reference

The Metal backend needs macOS 26 or newer and an M1 or newer GPU; elsewhere Device() raises BackendUnavailable and the CPU reference, CpuDevice(), still works. Check the install:

python -c "import teasar; print(teasar.check_available())"   # (True, 'Apple M4 Pro') on a supported Mac

Building from source

On any other platform pip builds teasar from PyPI's source distribution, which needs a stable Rust toolchain (rustup; on macOS also the Xcode command line tools, xcode-select --install). To build it yourself:

pip install --no-binary teasar teasar                             # PyPI's source distribution
pip install git+https://github.com/praveenperera/teasar          # or the repository

A source build enables the python and metal features from pyproject.toml. The metal feature is a no-op off macOS, so a Linux build has the CPU reference only.

From Rust

The same code is the teasar crate on crates.io:

cargo add teasar

Its metal feature is on by default and does nothing off macOS; teasar::Device::gpu() opens the Metal backend and teasar::Device::cpu(threads) the CPU reference.

Python API

import teasar as tm

ok, reason = tm.check_available()          # (True, "Apple M4 Pro") or (False, why); never raises
dev = tm.Device()                          # Metal; raises tm.BackendUnavailable elsewhere
cpu = tm.CpuDevice(threads=0)              # the CPU reference, all cores

labels = dev.prepare_labels(binary, 2, 50, 80000, settings="published_0211")
labels = dev.prepare_from_block(block, x_size, slabs, settings="published_0211", downsample=2, min_count=50,
                                max_count=80000)
packed = dev.skeletonize(labels_list, settings="published_0211", anisotropy=(2, 2, 2), dust_threshold=49)
packed = dev.extract(block, x_size, slabs, settings="published_0211", downsample=2, min_count=50, max_count=80000,
                     anisotropy=(2, 2, 2), dust_threshold=49)
dev.last_stats()   # {"wall_s", "gpu_busy_s", "host_threads", "cpu_pieces", "cpu_voxels", "gpu_pieces", ...}
  • binary is a C-contiguous (Z, Y, X) uint8 or bool array; block is a (Z, Y, ceil(x_size / 8)) uint8 array, bit-packed along x with the lowest x in the most significant bit (np.packbits(..., axis=-1, bitorder="big")).
  • slabs is a list of (kind, lo, hi) with kind 'h' (z range), 'vy' (y range) or 'vx' (x range), block-local.
  • Labels equal villa's prepare_cc_labels bit for bit: 6-connected components at full resolution numbered in C-order scan order of each component's first voxel, cropped to a multiple of downsample, max-pooled, and filtered to min_count <= count < max_count pooled voxels.
  • skeletonize and extract take target_order='best' (the default): border targets first, then ordinary targets farthest-first by geodesic distance from the root.
  • target_order='kimimaro' is a compatibility mode that reproduces Kimimaro's skeletons as villa calls it: its face picks, root, target order, path and claim rules. It takes two orders from the running Python interpreter, a fresh built-in set's iteration order and numpy's default argsort, so its output matches only with CPython and numpy versions whose orders equal the tested ones (CPython 3.14.10, numpy 2.5.2). Both backends run its skeleton stage on host threads; the Metal backend runs the prepare stages on the GPU first. A call with a piece whose largest boundary distance exceeds 1,100 raises tm.SomaUnsupported before any path search, with slab set to the first such slab; villa then runs that slab on its CPU path. On the test fixtures its skeletons equal Kimimaro's on 16 synthetic masks, and its leaf and branch-point counts on a 384-slab test box are within 0.7% of Kimimaro's.
  • Each skeleton is in sorted form: vertices in increasing C-order index of their voxel, edges written (lower, higher), edge rows sorted.
  • In target_order='best' the root is the first border target, the penalty is the README's (1 - B / max B)^16 plus D / max D, and invalidation removes the piece voxels strictly inside the ball of radius scale * DBF + const around each path vertex.

Settings

Every method takes a required keyword settings naming one of the two sets villa runs, and every other settings value it passes must equal that set's. The set is never inferred from the values. Anything else raises tm.UnsupportedSettings (a NotImplementedError), naming the field, the value passed and the set's value.

set downsample min_count max_count anisotropy dust_threshold scale const fix_branching h depth vertical thickness
published_0211 2 50 80000 (2, 2, 2) 49 1.0 2.0 True 2 4
villa_default 4 6 10000 (4, 4, 4) 5 1.0 2.0 True 4 4
method checked against the set
prepare_labels downsample, min_count, max_count
prepare_from_block the same, and every slab's thickness
skeletonize anisotropy, dust_threshold, scale, const, fix_branching; fix_borders is True or False
extract all of the above, and every slab's thickness
  • Equality is numeric (2 equals 2.0; anisotropy is any sequence of three numbers), and the bool fields must be bools.
  • A slab's thickness (hi - lo on its axis) must be the set's h depth or vertical thickness. A slab that ends at the block's edge on that axis may be thinner; one thinner than downsample returns an empty Packed from extract and None from prepare_from_block.
  • fix_borders=True turns border targets off for a slab whose pooled shape has two or more axes of length 1, as villa does.

Limits

Input checks run before any work and raise tm.InputTooLarge; the output check raises tm.OutputTooLarge. Both are ValueErrors whose message names the limit.

limit value
block.nbytes 512 MiB
each pooled axis of a slab (or of a skeletonize label array) 32,768 voxels
one slab, full resolution 3 × 2^26 voxels
one slab, pooled 2^24 voxels
one call, pooled voxels summed over its slabs 2^26
the arrays a call returns 512 MiB
  • OutputTooLarge.slab is the index, in the call's slab list (thin slabs included), of the first slab whose result takes the running total over the limit. Rerun the slabs before it in one call and continue from it in a new one.
  • Split a long slab list with the baseline partition: keep the slabs in order and start a new call whenever the next slab would take the call's pooled total over 2^26 (tests/fixtures.py's slab_calls; 6 calls on the 384-slab test box).
  • Calls release the GIL; one call at a time runs on a device. Returned arrays are new and owned by the caller.
  • TEASAR_PROFILE=1 prints per-kernel GPU time and the internal batching to stderr after each Metal call.

Host pieces

The Metal skeletoniser runs the slab stages and every piece of at most 1,000 pooled voxels on the GPU, and traces larger pieces on host threads beside the GPU's path loop, with the CPU reference's code. TEASAR_CPU_THREADS sets the threads, read once per Device (default: the performance cores; the thread driving the GPU is extra). dev.last_stats() reports the split for every call, and TEASAR_PROFILE=1 prints it with the host pieces' time. The output does not depend on the split: every piece's skeleton is the same, bit for bit, on either side. Tuning knobs, for experiments only:

variable default what
TEASAR_CPU_PIECE 1000 pieces over this many pooled voxels run on host threads
TEASAR_GPU_BUDGET 2^16 path-loop work units per GPU lane before it pauses
TEASAR_LARGE_PIECE the size filter's maximum pieces over this many pooled voxels fall back to host threads; none do at the supported settings
TEASAR_CPU_FALLBACK=0 off makes that fallback an error, for tests
TEASAR_SKEL_BATCH_VOXELS 2^23 pooled voxels per internal skeletonize batch
TEASAR_PROFILE_BUCKETS=1 off runs and times the path loop by piece-size bucket (slower)

With TEASAR_PROFILE=1 the profile also counts resume dispatches, pieces paused and pauses by phase, and splits the host pieces into hybrid-split and fallback pieces.

CPU reference contract

The CPU reference returns exactly what the Metal backend returns. Its connected components use a different algorithm (union-find over x-runs) from the GPU's (union-find over the 6-connected pieces of each pooled cell), so each checks the other.

The CPU reference's skeletons equal the Metal backend's bit for bit on the fixture slabs in both target orders, and a testing build's field-level dump (_fields) shows each piece's DBF, B, D, P and root from the GPU path loop equal the CPU's bit for bit, on the test box's slabs and the synthetic masks. In target_order='kimimaro' both backends run the same host code for the skeleton stage.

Development

uv venv .venv && source .venv/bin/activate
uv pip install numpy scipy zarr pytest maturin
maturin develop --uv --release -F python,metal,testing   # testing adds the test hooks

maturin build --release makes the release wheel; like pip install ., it enables python and metal from pyproject.toml, and test_macos_build_has_metal fails if a macOS build lacks the Metal backend.

Tests

pytest tests runs the test suite on the CPU reference, on a testing build. GPU runs set TEASAR_GPU=1 (scripts/gpu_tests.sh, and scripts/gpu_validate.sh under Metal's API and shader validation). The fixture set, villa and the parity harness are not part of this repo, so their paths have no defaults:

export TEASAR_FIXTURES=/path/to/fixtures        # the fixture root, holding data/, ref/ and villa/
export TEASAR_DATA=$TEASAR_FIXTURES/data        # the fixture set's inputs and per-slab fixtures
export TEASAR_VILLA=$TEASAR_FIXTURES/villa      # a villa checkout, for spiral-fitting/fast_tracks.py
export TEASAR_HARNESS=/path/to/parity/harness   # run_candidate.py, compare.py and skel_counts.py

Tests that read the fixture set skip with a message naming TEASAR_DATA when it is unset, and the track checks on the synthetic masks name TEASAR_VILLA. scripts/parity_check.sh, hash_lock.sh and count_check.py run the harness on the test box against the reference in the fixture root's ref/, on the repo's .venv, or with $TEASAR_PYTHON on another interpreter, such as a clean venv with a release wheel installed. The timing scripts (scripts/bench_extract.py, bench_prep.py, prof_skel.py) print last_stats(), summed over each pass's calls, beside every timing; set TEASAR_CPU_THREADS for the host threads.

Licence

MIT, copyright Praveen Perera.

Metadata

Release files for teasar 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for teasar 0.1.1
File Size Uploaded Provenance Actions
teasar-0.1.1.tar.gz 142.2 kB With provenance Details for teasar-0.1.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for teasar 0.1.1
File Interpreter ABI Platform Actions
teasar-0.1.1-cp311-abi3-manylinux_2_28_x86_64.whl CPython 3.11 abi3 Linux glibc 2.28+ x86-64 Details for teasar-0.1.1-cp311-abi3-manylinux_2_28_x86_64.whl
teasar-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl CPython 3.11 abi3 Linux glibc 2.28+ ARM64 Details for teasar-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl
teasar-0.1.1-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for teasar-0.1.1-cp311-abi3-macosx_11_0_arm64.whl

Total release size: 1.9 MB

Release files / teasar-0.1.1.tar.gz

Download URL teasar-0.1.1.tar.gz
Size 142.2 kB
Tags Source
SHA-256 checksum
How to use checksums
559d0f96094ef7a8e22b9dbdfac3e5afbee72d1f01f161cf2523396e793b51c2
BLAKE2b-256 checksum
How to use checksums
d7bbea3164526120008249d652f23cbc52312d9e6fd2119967332dd3436f980c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / teasar-0.1.1-cp311-abi3-manylinux_2_28_x86_64.whl

Download URL teasar-0.1.1-cp311-abi3-manylinux_2_28_x86_64.whl
Size 565.7 kB
Tags CPython 3.11 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
d2ddad9530ec32d0aab0d81cfe6fa95d8533d187c0d907c2f77119fb994be6a2
BLAKE2b-256 checksum
How to use checksums
15e114667a096bd49f4054d37ed40ecf21caa4b1667b307805394c2ce4c3fad9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / teasar-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl

Download URL teasar-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl
Size 530.3 kB
Tags CPython 3.11 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
188af5b8bf4adbc377dd543bd6be69aea5a4c597ad14e244683937ebd879f59f
BLAKE2b-256 checksum
How to use checksums
a2f3e0ca94f5395cf3ba8f2a128c29a8c738174899ca7ca4b0e949df5bf293c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / teasar-0.1.1-cp311-abi3-macosx_11_0_arm64.whl

Download URL teasar-0.1.1-cp311-abi3-macosx_11_0_arm64.whl
Size 659.6 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9fdee4c44fe80a24c1674c3c90d146d8917fe00ef0a29d26f453f86837c63573
BLAKE2b-256 checksum
How to use checksums
4782e4169fc740850cdb9e091127c1c014ce7d8a539f13c9019ea3b98d1435d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for teasar 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 4 files in this release have been uploaded with provenance metadata.

Select from 4 files

Attestation for teasar-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

4 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page