Metadata-Version: 2.4
Name: fibrs
Version: 0.6.0
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Environment :: GPU :: NVIDIA CUDA :: 12
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Image Processing
Requires-Dist: numpy>=2
Requires-Dist: scipy-openblas64==0.3.31.188.0 ; sys_platform == 'darwin'
Requires-Dist: nvidia-libnvcomp-cu12>=6,<7 ; (platform_machine == 'AMD64' and sys_platform == 'win32' and extra == 'gpu-zstd') or (platform_machine == 'x86_64' and sys_platform == 'linux' and extra == 'gpu-zstd')
Requires-Dist: nvidia-cudnn-cu12>=9.1,<10 ; (platform_machine == 'AMD64' and sys_platform == 'win32' and extra == 'predict') or (platform_machine == 'x86_64' and sys_platform == 'linux' and extra == 'predict')
Requires-Dist: nvidia-cublas-cu12>=12.1,<13 ; (platform_machine == 'AMD64' and sys_platform == 'win32' and extra == 'predict') or (platform_machine == 'x86_64' and sys_platform == 'linux' and extra == 'predict')
Provides-Extra: gpu-zstd
Provides-Extra: predict
License-File: LICENSE
License-File: NOTICE
License-File: licenses/apache-2.0.txt
License-File: licenses/c-blosc-bitshuffle.txt
License-File: licenses/c-blosc-fastlz.txt
License-File: licenses/c-blosc.txt
License-File: licenses/lz4.txt
License-File: licenses/mimalloc.txt
License-File: licenses/numpy.txt
License-File: licenses/nvidia-gpu-kernels.txt
License-File: licenses/villa-MIT.txt
License-File: licenses/zlib.txt
License-File: licenses/zstd.txt
Summary: Native drop-in for Qual's AVF_SPLINE_GENERATOR 0.2.0: traces AFV V/H fibre predictions into the same .afv
Keywords: vesuvius-challenge,herculaneum,papyrus,fibre-tracing,afv,avf-spline-generator,inference,cuda
Author-email: Praveen Perera <me@praveenperera.com>
License-Expression: Apache-2.0
Requires-Python: >=3.9
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/praveenperera/fibrs

# fibrs

A fast drop-in for `avf-spline-generator` 0.2.0, Qual's AFV fibre tracer
([Qualzz/AVF_SPLINE_GENERATOR](https://github.com/Qualzz/AVF_SPLINE_GENERATOR) @ `bee798f`). Same flags, same
`.afv` output.

- **`fibrs predict`** runs Qual's AFV fibre model V2 on an NVIDIA GPU or an Apple silicon Mac, without PyTorch.
- **`fibrs join`** joins a scroll's Z bands into one `.afv`.
- **`fibrs strips`** runs villa's sheet-strip model over an AFV's fibres on an NVIDIA GPU, without PyTorch.

Fibre sets for whole scrolls, traced with fibrs:
[praveenperera/afv-v2-fibers](https://huggingface.co/datasets/praveenperera/afv-v2-fibers).

## Speed

CT to `.afv` on three 512-plane bands, one RTX 5090, fp32:

| Pipeline | Time |
|---|---|
| villa's prediction + original tracer | about 35,000 s |
| fibrs 0.4.0 | 800 s |

About 44× faster. A whole scroll takes fibrs an estimated 2.6–3 hours, against 80–140.
[Benchmarks](https://github.com/praveenperera/fibrs/blob/master/docs/benchmarks.md).

## Install

```sh
pip install fibrs
pip install "fibrs[predict]"   # adds cuDNN and cuBLAS for fibrs predict
```

Install into the environment that has numpy. fibrs uses numpy's OpenBLAS.

Standalone binaries are on the releases page, or:

```sh
cargo binstall fibrs
cargo install fibrs --locked
```

A standalone binary needs the numpy environment active, or `FIBRS_LAPACK` set.
[Install details](https://github.com/praveenperera/fibrs/blob/master/docs/install.md).

## Trace

```sh
fibrs --predictions P --array semantic --channel-order VH --tracing-predictions P \
  --tracing-array tracing --voxel-size-um 9.362 --storage compressed --threads 4 \
  --output fibers.afv --work-dir work
```

Or from Python: `python -m fibrs ARGS`.

- Uses an NVIDIA GPU when available, otherwise the CPU. Force one with `--device cpu` or `--device cuda`.
- Output is the same at any `--threads`.
- Resume an interrupted run with `--resume`.
- `--tiles-yx NY NX` traces a dense window as tiles, to use less memory.

To make an existing `avf-spline-generator` run fibrs:

```sh
scripts/install-dropin.sh VENV            # install
scripts/install-dropin.sh --restore VENV  # undo
```

[Tracing details](https://github.com/praveenperera/fibrs/blob/master/docs/tracing.md).

## Join

```sh
fibrs join --manifest bands.json --output fibers-scroll.afv --stats fibers-scroll.json
```

```json
{
  "bands": [
    {"afv": "trace/z08448-08960/fibers.afv", "core_z": [8448, 8960], "window_z": [8192, 9216]},
    {"afv": "trace/z08960-09472/fibers.afv", "core_z": [8960, 9472], "window_z": [8704, 9728]}
  ],
  "coordinate_space": "PHerc1545/20250821151648",
  "source_path": "https://vesuvius-challenge-open-data.s3.amazonaws.com/PHerc1545/volumes/....zarr",
  "provenance": {"operation": "halo_band_trace_join", "haloPlanes": 256}
}
```

`core_z` is the planes a band keeps, `window_z` the planes it was traced from.

## Subset

```sh
# the fibres with a point in a box of native level-0 voxels: X0 X1 Y0 Y1 Z0 Z1
fibrs subset --input fibers-scroll.afv --output region.afv --box 2500 3524 3600 4624 8000 9024

# every fibre of 2.5 mm or more, the sheet-strip runner's default minimum
fibrs subset --input fibers-scroll.afv --output long.afv --min-length-mm 2.5
```

Writes a smaller `.afv` that villa's AFV importers read like the whole set. Fibres are kept whole and keep their
ids, and the file keeps the set's uuid, so an imported fibre has the same UUID as in a whole-scroll import.
`--box`, `--min-length-mm` and `--family V|H` combine: a fibre is kept only when it passes each one given. A box
of a 27.6 GB scroll set takes about 20 s and under 1 GB of memory.

## Predict

```sh
# whole array, matching villa's output
fibrs predict --mode villa --ct scroll.zarr/0 --model-dir afv_fiber_9um_v2 --out pred.zarr

# one z band, resumable
fibrs predict --ct scroll.zarr/0 --z0 12032 --z1 12544 --model-dir afv_fiber_9um_v2 --out band.zarr
```

Needs the model from [Qualzz20/afv_fiber_9um_v2](https://huggingface.co/Qualzz20/afv_fiber_9um_v2) and either
Linux or Windows x86-64 with an RTX 30-series or newer GPU, cuDNN 9 and cuBLAS 12, or an Apple silicon Mac on
macOS 26 or newer.
[Predict details](https://github.com/praveenperera/fibrs/blob/master/docs/predict.md).

## Strips

```sh
fibrs strips run --afv PHerc0211_afv_v2.afv --out strips/PHerc0211 --weights fiber_strip_small_v1.safetensors \
  --ct s3://vesuvius-challenge-open-data/PHerc0211/volumes/20250821151803-9.362um-1.2m-113keV-masked.zarr \
  --cache-dir ctcache --umbilicus PHerc0211_umbilicus.json --voxel-size-um 9.362
```

Writes a sheet strip per fibre in the layout of villa's Python runner, about 8× faster on one RTX 5090 (2,095
decisions/s over the whole PHerc0211 scroll). The strips match PyTorch's on the error measures, and a run's output
doesn't depend on its batch size or worker count. Needs Linux x86-64, an RTX 30-series or newer GPU, cuDNN 9, a
weights export and a prefetched CT cache.
[Strips details](https://github.com/praveenperera/fibrs/blob/master/docs/strips.md).

## Build

```sh
cargo build --release
FIBRS_LAPACK=/path/to/libscipy_openblas64_.so cargo test --release
```

Rust 1.100+.

## Licence

Apache-2.0. See `NOTICE` and `licenses/`.

