|
| 1 | +"""Multi-input decoder-graph combine operations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Sequence |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | + |
| 10 | +def _validate_label_input(arr: np.ndarray, *, name: str) -> None: |
| 11 | + if not np.issubdtype(arr.dtype, np.integer): |
| 12 | + raise TypeError(f"combine_split {name} must be an integer label array.") |
| 13 | + if np.issubdtype(arr.dtype, np.signedinteger) and arr.size and int(arr.min()) < 0: |
| 14 | + raise ValueError(f"combine_split {name} must not contain negative labels.") |
| 15 | + |
| 16 | + |
| 17 | +def _check_key_space(max_a: int, max_b: int) -> int: |
| 18 | + max_uint64 = int(np.iinfo(np.uint64).max) |
| 19 | + if max_b >= max_uint64: |
| 20 | + raise OverflowError("combine_split pair-key base exceeds uint64 range.") |
| 21 | + base = max_b + 1 |
| 22 | + if max_a > (max_uint64 - max_b) // base: |
| 23 | + raise OverflowError("combine_split pair keys would overflow uint64.") |
| 24 | + return base |
| 25 | + |
| 26 | + |
| 27 | +def combine_split( |
| 28 | + inputs: Sequence[np.ndarray], |
| 29 | + *, |
| 30 | + output_dtype: str | np.dtype = "uint32", |
| 31 | +) -> np.ndarray: |
| 32 | + """Return the background-preserving coarsest common refinement of two labels.""" |
| 33 | + if len(inputs) != 2: |
| 34 | + raise ValueError(f"combine_split expects exactly two inputs, got {len(inputs)}.") |
| 35 | + |
| 36 | + a = np.asarray(inputs[0]) |
| 37 | + b = np.asarray(inputs[1]) |
| 38 | + if a.shape != b.shape: |
| 39 | + raise ValueError( |
| 40 | + f"combine_split inputs must have matching shapes, got {a.shape} and {b.shape}." |
| 41 | + ) |
| 42 | + _validate_label_input(a, name="input 0") |
| 43 | + _validate_label_input(b, name="input 1") |
| 44 | + |
| 45 | + dtype = np.dtype(output_dtype) |
| 46 | + if not np.issubdtype(dtype, np.integer): |
| 47 | + raise TypeError(f"combine_split output_dtype must be an integer dtype, got {dtype}.") |
| 48 | + |
| 49 | + out = np.zeros(a.shape, dtype=dtype) |
| 50 | + fg = (a != 0) & (b != 0) |
| 51 | + if not bool(fg.any()): |
| 52 | + return out |
| 53 | + |
| 54 | + a_fg = a[fg] |
| 55 | + b_fg = b[fg] |
| 56 | + max_a = int(a_fg.max()) |
| 57 | + max_b = int(b_fg.max()) |
| 58 | + base = _check_key_space(max_a, max_b) |
| 59 | + |
| 60 | + key = a_fg.astype(np.uint64, copy=False) |
| 61 | + if key is a_fg: |
| 62 | + key = key.copy() |
| 63 | + key *= np.uint64(base) |
| 64 | + np.add(key, b_fg.astype(np.uint64, copy=False), out=key) |
| 65 | + |
| 66 | + _, inv = np.unique(key, return_inverse=True) |
| 67 | + n_labels = int(inv.max()) + 1 if inv.size else 0 |
| 68 | + if n_labels >= 2**32: |
| 69 | + raise OverflowError("combine_split produced too many labels for uint32 output.") |
| 70 | + dtype_info = np.iinfo(dtype) |
| 71 | + if n_labels > int(dtype_info.max): |
| 72 | + raise OverflowError( |
| 73 | + f"combine_split produced {n_labels} labels, exceeding output dtype {dtype}." |
| 74 | + ) |
| 75 | + |
| 76 | + labels = np.arange(1, n_labels + 1, dtype=dtype) |
| 77 | + out[fg] = labels[inv] |
| 78 | + return out |
| 79 | + |
| 80 | + |
| 81 | +__all__ = ["combine_split"] |
0 commit comments