|
| 1 | +"""Benchmark and equivalence check for relabel_sequential. |
| 2 | +
|
| 3 | +Compares bioimage_cpp.segmentation.relabel_sequential against: |
| 4 | +- skimage.segmentation.relabel_sequential |
| 5 | +- vigra.analysis.relabelConsecutive |
| 6 | +
|
| 7 | +Not part of the pytest suite. Run from the repository root, e.g.: |
| 8 | +
|
| 9 | + python development/segmentation/check_relabel_sequential.py --repeats 5 |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import argparse |
| 15 | +from statistics import median |
| 16 | +from time import perf_counter |
| 17 | +from typing import Callable |
| 18 | + |
| 19 | +import numpy as np |
| 20 | + |
| 21 | +import bioimage_cpp as bic |
| 22 | + |
| 23 | + |
| 24 | +def make_problem(shape: tuple[int, ...], n_labels: int, *, seed: int) -> np.ndarray: |
| 25 | + """Build a random label field with roughly ``n_labels`` distinct non-zero labels. |
| 26 | +
|
| 27 | + The label values themselves are sparse — drawn from a range larger than |
| 28 | + ``n_labels`` — to exercise the sorted-unique remapping logic. About 10% of |
| 29 | + pixels are background (0). |
| 30 | + """ |
| 31 | + rng = np.random.default_rng(seed) |
| 32 | + value_range = max(n_labels * 4, 16) |
| 33 | + label_field = rng.integers(1, value_range, size=shape, dtype=np.int64).astype( |
| 34 | + np.uint32 |
| 35 | + ) |
| 36 | + background_mask = rng.random(size=shape) < 0.1 |
| 37 | + label_field[background_mask] = 0 |
| 38 | + return np.ascontiguousarray(label_field) |
| 39 | + |
| 40 | + |
| 41 | +def run_bioimage_cpp(label_field: np.ndarray) -> np.ndarray: |
| 42 | + relabeled, _, _ = bic.segmentation.relabel_sequential(label_field) |
| 43 | + return relabeled |
| 44 | + |
| 45 | + |
| 46 | +def run_skimage(label_field: np.ndarray) -> np.ndarray: |
| 47 | + from skimage.segmentation import relabel_sequential as sk_relabel |
| 48 | + |
| 49 | + relabeled, _, _ = sk_relabel(label_field) |
| 50 | + return np.asarray(relabeled) |
| 51 | + |
| 52 | + |
| 53 | +def run_vigra(label_field: np.ndarray) -> np.ndarray: |
| 54 | + import vigra |
| 55 | + |
| 56 | + # vigra.analysis.relabelConsecutive(labels, start_label=1, keep_zeros=True) |
| 57 | + # returns (relabeled, max_new_label, mapping_dict). The relabeled array is |
| 58 | + # the first element. We pass start_label=1 and keep_zeros=True to match |
| 59 | + # skimage's default behavior. |
| 60 | + relabeled, _, _ = vigra.analysis.relabelConsecutive( |
| 61 | + label_field.astype(np.uint32), start_label=1, keep_zeros=True |
| 62 | + ) |
| 63 | + return np.asarray(relabeled) |
| 64 | + |
| 65 | + |
| 66 | +def time_calls( |
| 67 | + fn: Callable[[np.ndarray], np.ndarray], |
| 68 | + label_field: np.ndarray, |
| 69 | + repeats: int, |
| 70 | +) -> tuple[list[float], np.ndarray]: |
| 71 | + # warm-up |
| 72 | + result = fn(label_field) |
| 73 | + timings: list[float] = [] |
| 74 | + for _ in range(repeats): |
| 75 | + start = perf_counter() |
| 76 | + result = fn(label_field) |
| 77 | + timings.append(perf_counter() - start) |
| 78 | + return timings, result |
| 79 | + |
| 80 | + |
| 81 | +def check_consecutive(relabeled: np.ndarray, *, offset: int = 1) -> bool: |
| 82 | + unique = np.unique(relabeled) |
| 83 | + non_pass = unique[unique >= offset] |
| 84 | + if non_pass.size == 0: |
| 85 | + return True |
| 86 | + return bool(np.array_equal(non_pass, np.arange(offset, offset + non_pass.size))) |
| 87 | + |
| 88 | + |
| 89 | +def report_one( |
| 90 | + name: str, |
| 91 | + label_field: np.ndarray, |
| 92 | + repeats: int, |
| 93 | + bic_timings: list[float], |
| 94 | + bic_result: np.ndarray, |
| 95 | +) -> None: |
| 96 | + print(f"\n=== {name} ===") |
| 97 | + print( |
| 98 | + f"shape={label_field.shape}, dtype={label_field.dtype}, " |
| 99 | + f"distinct labels in input={int(np.unique(label_field).size)}" |
| 100 | + ) |
| 101 | + bic_median = median(bic_timings) |
| 102 | + print(f"bioimage-cpp median runtime: {bic_median * 1000:.3f} ms") |
| 103 | + print( |
| 104 | + f"bioimage-cpp produces consecutive labels: " |
| 105 | + f"{check_consecutive(bic_result)}" |
| 106 | + ) |
| 107 | + |
| 108 | + try: |
| 109 | + sk_timings, sk_result = time_calls(run_skimage, label_field, repeats) |
| 110 | + sk_median = median(sk_timings) |
| 111 | + agrees = bool(np.array_equal(sk_result, bic_result)) |
| 112 | + print(f"skimage median runtime: {sk_median * 1000:.3f} ms") |
| 113 | + print(f"skimage / bioimage-cpp ratio: {sk_median / bic_median:.3f}x") |
| 114 | + print(f"skimage and bioimage-cpp agree on relabeled array: {agrees}") |
| 115 | + except ImportError: |
| 116 | + print("skimage not available, skipping skimage comparison") |
| 117 | + |
| 118 | + try: |
| 119 | + vi_timings, vi_result = time_calls(run_vigra, label_field, repeats) |
| 120 | + vi_median = median(vi_timings) |
| 121 | + # vigra preserves first-occurrence order, not sorted order; we expect |
| 122 | + # disagreement on the specific label values but agreement on the |
| 123 | + # partition the labels induce. |
| 124 | + agrees = bool(np.array_equal(vi_result, bic_result)) |
| 125 | + same_partition = bool( |
| 126 | + np.unique(vi_result).size == np.unique(bic_result).size |
| 127 | + ) |
| 128 | + print(f"vigra median runtime: {vi_median * 1000:.3f} ms") |
| 129 | + print(f"vigra / bioimage-cpp ratio: {vi_median / bic_median:.3f}x") |
| 130 | + print(f"vigra and bioimage-cpp produce identical relabeled array: {agrees}") |
| 131 | + print( |
| 132 | + f"vigra and bioimage-cpp produce same number of unique labels: " |
| 133 | + f"{same_partition}" |
| 134 | + ) |
| 135 | + except ImportError: |
| 136 | + print("vigra not available, skipping vigra comparison") |
| 137 | + |
| 138 | + |
| 139 | +def main() -> None: |
| 140 | + parser = argparse.ArgumentParser(description=__doc__) |
| 141 | + parser.add_argument("--repeats", type=int, default=5) |
| 142 | + parser.add_argument("--seed", type=int, default=0) |
| 143 | + args = parser.parse_args() |
| 144 | + |
| 145 | + cases = [ |
| 146 | + ("2D small label set (1024x1024, ~100 labels)", (1024, 1024), 100), |
| 147 | + ("2D large label set (1024x1024, ~100000 labels)", (1024, 1024), 100_000), |
| 148 | + ("3D small label set (128x128x128, ~100 labels)", (128, 128, 128), 100), |
| 149 | + ( |
| 150 | + "3D large label set (128x128x128, ~100000 labels)", |
| 151 | + (128, 128, 128), |
| 152 | + 100_000, |
| 153 | + ), |
| 154 | + ] |
| 155 | + |
| 156 | + for name, shape, n_labels in cases: |
| 157 | + label_field = make_problem(shape, n_labels, seed=args.seed) |
| 158 | + bic_timings, bic_result = time_calls( |
| 159 | + run_bioimage_cpp, label_field, args.repeats |
| 160 | + ) |
| 161 | + report_one(name, label_field, args.repeats, bic_timings, bic_result) |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + main() |
0 commit comments