Skip to content

Commit

Permalink
feat(dust): add between thresholds denoted (lower,upper)
Browse files Browse the repository at this point in the history
  • Loading branch information
william-silversmith committed Dec 19, 2024
1 parent f58a882 commit a7e2946
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
27 changes: 27 additions & 0 deletions automated_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,33 @@ def test_dust_retain_all():
)
assert np.all(recovered == labels)

@pytest.mark.parametrize("dtype", TEST_TYPES)
@pytest.mark.parametrize("connectivity", (6,18,26))
@pytest.mark.parametrize("order", ("C", "F"))
@pytest.mark.parametrize("in_place", (False, True))
@pytest.mark.parametrize("invert", (False, True))
def test_dust_between_threshold(
dtype, connectivity, order, in_place, invert
):
labels = np.zeros((100,100,10), dtype=np.uint8, order=order)
labels[:5,:5,:1] = 1
labels[20:40,20:40,:] = 2
labels[10:17,10:17,:] = 3
recovered = cc3d.dust(
labels,
threshold=(25, 491),
connectivity=connectivity,
in_place=in_place,
invert=invert,
)

res = [ int(x) for x in np.unique(recovered) ]

if invert:
assert res == [0,2]
else:
assert res == [0,1,3]

@pytest.mark.parametrize("dtype", INT_TYPES)
@pytest.mark.parametrize("connectivity", (6,18,26))
def test_dust_random(dtype, connectivity):
Expand Down
24 changes: 16 additions & 8 deletions cc3d/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import (
Dict, Union, Tuple, Iterator,
Dict, Union, Tuple, List, Iterator,
Sequence, Optional, Any, BinaryIO
)

Expand All @@ -19,7 +19,7 @@

def dust(
img:np.ndarray,
threshold:Union[int,float],
threshold:Union[int,float,Tuple[int,int],Tuple[float,float],List[int],List[float]],
connectivity:int = 26,
in_place:bool = False,
binary_image:bool = False,
Expand All @@ -32,15 +32,18 @@ def dust(
can be read as a verb "to dust" the image.

img: 2D or 3D image
threshold: discard components smaller than this in voxels
threshold:
(int) discard components smaller than this in voxels
(tuple/list) keep components in range [lower, upper)
connectivity: cc3d connectivity to use
in_place: whether to modify the input image or perform
dust
precomputed_ccl: for performance, avoid computing a CCL
pass since the input is already a CCL output from this
library.
invert: switch the operation from less than threshold to
greater than or equal to threshold.
invert: switch the threshold direction. For scalar input,
this means less than converts to greater than or equal to,
for ranged input, switch from between to outside of range.

Returns: dusted image
"""
Expand All @@ -63,9 +66,14 @@ def dust(
mask_sizes = stats["voxel_counts"]
del stats

to_mask = [
i for i in range(1, N+1) if mask_sizes[i] < threshold
]
if isinstance(threshold, (tuple, list)):
to_mask = [
i for i in range(1, N+1) if not (threshold[0] <= mask_sizes[i] < threshold[1])
]
else:
to_mask = [
i for i in range(1, N+1) if mask_sizes[i] < threshold
]

if len(to_mask) == 0:
return img
Expand Down

0 comments on commit a7e2946

Please sign in to comment.