|
| 1 | +"""Minimal serial harness for blockwise TEASAR and exact stitching. |
| 2 | +
|
| 3 | +This is deliberately development-only orchestration. It performs no I/O, |
| 4 | +parallel scheduling, retries, or artifact serialization; those responsibilities |
| 5 | +belong to downstream block-processing systems. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from collections.abc import Sequence |
| 11 | + |
| 12 | +import numpy as np |
| 13 | + |
| 14 | +import bioimage_cpp as bic |
| 15 | + |
| 16 | + |
| 17 | +dist = bic.skeleton.distributed |
| 18 | + |
| 19 | + |
| 20 | +def _processing_blocks(volume: np.ndarray, block_shape: Sequence[int]): |
| 21 | + blocking = bic.utils.Blocking([0, 0, 0], list(volume.shape), list(block_shape)) |
| 22 | + for block_id in range(blocking.number_of_blocks): |
| 23 | + core = blocking.get_block(block_id) |
| 24 | + begin = [int(value) for value in core.begin] |
| 25 | + end = [int(value) for value in core.end] |
| 26 | + faces = [] |
| 27 | + for axis in range(3): |
| 28 | + if blocking.get_neighbor_id(block_id, axis, lower=True) >= 0: |
| 29 | + faces.append((axis, "low")) |
| 30 | + if blocking.get_neighbor_id(block_id, axis, lower=False) >= 0: |
| 31 | + faces.append((axis, "high")) |
| 32 | + end[axis] += 1 |
| 33 | + slices = tuple(slice(begin[axis], end[axis]) for axis in range(3)) |
| 34 | + yield blocking, block_id, np.ascontiguousarray(volume[slices]), begin, faces |
| 35 | + |
| 36 | + |
| 37 | +def _unique_coordinates(parts: list[np.ndarray]) -> np.ndarray: |
| 38 | + if not parts: |
| 39 | + return np.empty((0, 3), dtype=np.int64) |
| 40 | + return np.unique(np.concatenate(parts, axis=0), axis=0) |
| 41 | + |
| 42 | + |
| 43 | +def _unique_target_map(parts: list[dict[int, np.ndarray]]) -> dict[int, np.ndarray]: |
| 44 | + labels = sorted({label for part in parts for label in part}) |
| 45 | + return { |
| 46 | + label: _unique_coordinates([part[label] for part in parts if label in part]) |
| 47 | + for label in labels |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +def _assert_matching_interfaces(blocking, per_face) -> None: |
| 52 | + for block_id in range(blocking.number_of_blocks): |
| 53 | + for axis in range(3): |
| 54 | + neighbor = blocking.get_neighbor_id(block_id, axis, lower=False) |
| 55 | + if neighbor < 0: |
| 56 | + continue |
| 57 | + left = per_face[(block_id, axis, "high")] |
| 58 | + right = per_face[(neighbor, axis, "low")] |
| 59 | + if isinstance(left, dict): |
| 60 | + if left.keys() != right.keys(): |
| 61 | + raise AssertionError( |
| 62 | + f"target labels disagree across blocks {block_id}/{neighbor}" |
| 63 | + ) |
| 64 | + for label in left: |
| 65 | + np.testing.assert_array_equal(left[label], right[label]) |
| 66 | + else: |
| 67 | + np.testing.assert_array_equal(left, right) |
| 68 | + |
| 69 | + |
| 70 | +def run_blockwise_binary( |
| 71 | + mask: np.ndarray, |
| 72 | + block_shape: Sequence[int], |
| 73 | + *, |
| 74 | + spacing=(1.0, 1.0, 1.0), |
| 75 | + remove_cycles: bool = False, |
| 76 | +): |
| 77 | + """Run the complete binary block pipeline serially and return a lattice graph.""" |
| 78 | + fragments = [] |
| 79 | + per_face = {} |
| 80 | + blocking = None |
| 81 | + for blocking, block_id, block, origin, faces in _processing_blocks( |
| 82 | + np.asarray(mask), block_shape |
| 83 | + ): |
| 84 | + face_targets = [] |
| 85 | + for axis, side in faces: |
| 86 | + targets = dist.block_border_targets( |
| 87 | + block, |
| 88 | + [(axis, side)], |
| 89 | + origin=origin, |
| 90 | + spacing=spacing, |
| 91 | + number_of_threads=1, |
| 92 | + ) |
| 93 | + per_face[(block_id, axis, side)] = targets |
| 94 | + face_targets.append(targets) |
| 95 | + targets = _unique_coordinates(face_targets) |
| 96 | + fragments.append( |
| 97 | + dist.block_teasar( |
| 98 | + block, |
| 99 | + open_faces=faces, |
| 100 | + origin=origin, |
| 101 | + required_targets=targets, |
| 102 | + spacing=spacing, |
| 103 | + number_of_threads=1, |
| 104 | + ) |
| 105 | + ) |
| 106 | + if blocking is None: |
| 107 | + raise ValueError("mask must be a three-dimensional array") |
| 108 | + _assert_matching_interfaces(blocking, per_face) |
| 109 | + merged = dist.merge_block_skeletons(fragments) |
| 110 | + if remove_cycles: |
| 111 | + merged = dist.minimum_spanning_forest(merged, spacing=spacing) |
| 112 | + return merged |
| 113 | + |
| 114 | + |
| 115 | +def run_blockwise_labels( |
| 116 | + labels: np.ndarray, |
| 117 | + block_shape: Sequence[int], |
| 118 | + *, |
| 119 | + background: int = 0, |
| 120 | + spacing=(1.0, 1.0, 1.0), |
| 121 | + remove_cycles: bool = False, |
| 122 | +): |
| 123 | + """Run the labeled block pipeline serially and return lattice graphs by label.""" |
| 124 | + fragment_maps = [] |
| 125 | + per_face = {} |
| 126 | + blocking = None |
| 127 | + for blocking, block_id, block, origin, faces in _processing_blocks( |
| 128 | + np.asarray(labels), block_shape |
| 129 | + ): |
| 130 | + face_targets = [] |
| 131 | + for axis, side in faces: |
| 132 | + targets = dist.block_border_targets_labels( |
| 133 | + block, |
| 134 | + [(axis, side)], |
| 135 | + origin=origin, |
| 136 | + background=background, |
| 137 | + spacing=spacing, |
| 138 | + number_of_threads=1, |
| 139 | + ) |
| 140 | + per_face[(block_id, axis, side)] = targets |
| 141 | + face_targets.append(targets) |
| 142 | + targets = _unique_target_map(face_targets) |
| 143 | + fragment_maps.append( |
| 144 | + dist.block_teasar_labels( |
| 145 | + block, |
| 146 | + open_faces=faces, |
| 147 | + origin=origin, |
| 148 | + required_targets=targets, |
| 149 | + background=background, |
| 150 | + spacing=spacing, |
| 151 | + number_of_threads=1, |
| 152 | + ) |
| 153 | + ) |
| 154 | + if blocking is None: |
| 155 | + raise ValueError("labels must be a three-dimensional array") |
| 156 | + _assert_matching_interfaces(blocking, per_face) |
| 157 | + merged = dist.merge_block_skeleton_maps(fragment_maps) |
| 158 | + if remove_cycles: |
| 159 | + merged = { |
| 160 | + label: dist.minimum_spanning_forest(fragment, spacing=spacing) |
| 161 | + for label, fragment in merged.items() |
| 162 | + } |
| 163 | + return merged |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + example = np.zeros((17, 17, 25), dtype=np.uint8) |
| 168 | + example[8, 8, 2:23] = 1 |
| 169 | + result = run_blockwise_binary(example, (9, 9, 8), remove_cycles=True) |
| 170 | + print( |
| 171 | + f"stitched {len(result[0])} lattice vertices and {len(result[1])} edges " |
| 172 | + "from serial processing blocks" |
| 173 | + ) |
0 commit comments