-
Notifications
You must be signed in to change notification settings - Fork 11
feat(sanitize): central non-contiguous label-ID sanitation #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c7b287a
feat(sanitize): central non-contiguous label-ID sanitation
timtreis 37cde59
refactor(sanitize): leaner policy, bool support, fix overlap coverage
timtreis 8633bab
perf(sanitize): cheaper contiguity check (max + bincount, not unique)
timtreis 186dd4b
perf(featurizer): sanitize each mask once, call raw feature functions
timtreis c9d2922
test(sanitize): trim to the essentials
timtreis b21be05
refactor: drop per-function label-normalization now centralized
timtreis 01279d4
docs(primitives): fix stale segment.py cross-ref in numba kernel docs…
timtreis 7ba1d0f
docs(readme): non-contiguous labels now handled internally
timtreis 5046b5e
refactor(sanitize): sanitize only at entry points, not in raw funcs
timtreis c44c44d
fix(typing): satisfy numpy 2.5 stubs in costes + radial distribution
timtreis 90f2eed
refactor(sanitize): trim duplicated docstrings, tighten sanitizer
timtreis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Relabel non-contiguous mask label IDs (gaps or arbitrary values) to contiguous | ||
| ``1..N`` without mutating the caller's array, applied at the entry points. See | ||
| :func:`sanitize`. | ||
| """ | ||
|
|
||
| import functools | ||
| import inspect | ||
| from typing import Callable | ||
|
|
||
| import numpy | ||
| from numpy.typing import NDArray | ||
| from skimage.segmentation import relabel_sequential | ||
|
|
||
| # Argument names that hold the label image across the ``get_*`` functions. | ||
| _MASK_PARAMS = ("masks", "labels", "mask") | ||
|
|
||
|
|
||
| def sanitize_masks(masks: NDArray) -> tuple[NDArray, NDArray[numpy.int64]]: | ||
| """Relabel positive labels to contiguous ``1..N``. | ||
|
|
||
| Returns ``(clean, ids)`` where ``clean`` has labels ``1..N`` (the input is | ||
| returned unchanged when already contiguous) and ``ids[i]`` is the original | ||
| label of rank ``i + 1``. Never mutates the input. Raises ``ValueError`` for | ||
| non-integer (non-boolean) or negative labels. | ||
| """ | ||
| if masks.dtype != bool and not numpy.issubdtype(masks.dtype, numpy.integer): | ||
| raise ValueError(f"labels must be an integer array, got dtype {masks.dtype!r}") | ||
| if masks.min(initial=0) < 0: | ||
| raise ValueError("labels must be non-negative") | ||
| mx = int(masks.max(initial=0)) | ||
| if mx == 0: | ||
| return masks, numpy.empty(0, dtype=numpy.int64) | ||
| # bincount is faster than unique but needs a bounded range; fall back for huge labels. | ||
| if mx <= masks.size: | ||
| ids = numpy.flatnonzero(numpy.bincount(masks.ravel(), minlength=mx + 1)) | ||
| else: | ||
| ids = numpy.unique(masks) | ||
| ids = ids[ids > 0].astype(numpy.int64) | ||
| if ids.size == mx: | ||
| # already 1..N: no copy (cast only a bool mask up to an integer dtype) | ||
| return (masks if masks.dtype != bool else masks.astype(numpy.intp)), ids | ||
| clean, _forward, _inverse = relabel_sequential(masks) | ||
| return clean, ids | ||
|
|
||
|
|
||
| def sanitize(func: Callable) -> Callable: | ||
| """Wrap a ``get_*`` function to relabel its label argument (named in | ||
| :data:`_MASK_PARAMS`) to ``1..N`` before the call; functions with no such | ||
| argument are returned unchanged. Use this only to call a raw measurement | ||
| function directly with gapped IDs — the entry points already sanitize. | ||
| """ | ||
| sig = inspect.signature(func) | ||
| param = next((name for name in _MASK_PARAMS if name in sig.parameters), None) | ||
| if param is None: | ||
| return func | ||
|
|
||
| @functools.wraps(func) | ||
| def wrapper(*args, **kwargs): | ||
| bound = sig.bind(*args, **kwargs) | ||
| bound.arguments[param], _ids = sanitize_masks(bound.arguments[param]) | ||
| return func(*bound.args, **bound.kwargs) | ||
|
|
||
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.