Skip to content

Commit 2de42c9

Browse files
authored
Merge pull request #430 from quantumjot/fix-is-unique-boolean-bug
Fix _is_unique to correctly handle boolean arrays
2 parents efa8cd9 + f637bc3 commit 2de42c9

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎btrack/io/_localization.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
def _is_unique(x: npt.NDArray) -> bool:
2828
"""Check whether a segmentation is equivalent to the labeled version."""
29+
if x.dtype == bool:
30+
return False
2931
return np.array_equal(label(x), x)
3032

3133

‎tests/test_utils.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from btrack import btypes, utils
22
from btrack.constants import DEFAULT_OBJECT_KEYS, Dimensionality
33
from btrack.io import objects_from_array
4+
from btrack.io._localization import _is_unique
45

56
from ._utils import create_test_image, create_test_tracklet
67

@@ -10,6 +11,32 @@
1011
import pytest
1112

1213

14+
def test_is_unique_boolean_array():
15+
"""Test that _is_unique returns False for boolean arrays."""
16+
# Create a simple boolean mask
17+
mask = np.array(
18+
[[True, False, False], [False, True, False], [False, False, True]], dtype=bool
19+
)
20+
assert _is_unique(mask) is False
21+
22+
23+
def test_is_unique_labeled_array():
24+
"""Test that _is_unique returns True for already-labeled arrays."""
25+
# Create a labeled array (each object has a unique integer ID)
26+
labeled = np.array([[1, 0, 0], [0, 2, 0], [0, 0, 3]], dtype=int)
27+
assert _is_unique(labeled) is True
28+
29+
30+
def test_is_unique_unlabeled_array():
31+
"""Test that _is_unique returns False for unlabeled binary arrays."""
32+
# Create a mask with multiple separate objects all having the same
33+
# non-sequential ID (5). After labeling, they should have IDs (1, 2)
34+
unlabeled = np.array(
35+
[[5, 5, 0, 0], [5, 5, 0, 0], [0, 0, 5, 5], [0, 0, 5, 5]], dtype=int
36+
)
37+
assert _is_unique(unlabeled) is False
38+
39+
1340
def _example_segmentation_generator():
1441
for _ in range(10):
1542
img, _centroids = create_test_image()

0 commit comments

Comments
 (0)