Skip to content

Commit

Permalink
Support boolean views on numeric dtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
lagru committed Sep 19, 2019
1 parent 3d565fd commit b2bb144
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
6 changes: 3 additions & 3 deletions skimage/morphology/_close_objects_cy.pyx
Expand Up @@ -60,7 +60,7 @@ def _remove_close_objects(
try:
for i in range(indices.shape[0]):
index_i = indices[i]
if image[index_i] != 1:
if image[index_i] == 0:
continue

in_range = kdtree.query_ball_point(
Expand All @@ -69,7 +69,7 @@ def _remove_close_objects(
for j in in_range:
index_j = indices[j]
if (
image[index_j] == 1
image[index_j] > 0
and labels[index_i] != labels[index_j]
):
_remove_object(
Expand Down Expand Up @@ -121,6 +121,6 @@ cdef inline void _remove_object(
continue
# The algorithm might cross the image edge when two objects are
# neighbors in the raveled view -> check label to avoid that
if image[neighbor] == 1 and labels[neighbor] == label:
if image[neighbor] > 0 and labels[neighbor] == label:
queue_push(queue_ptr, &neighbor)
image[neighbor] = 0
10 changes: 5 additions & 5 deletions skimage/morphology/misc.py
Expand Up @@ -308,14 +308,14 @@ def remove_close_objects(
... image.view(bool), minimal_distance=3, priority=image
... )
>>> result.view(np.uint8)
array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
array([[8, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9],
[8, 8, 8, 0, 0, 0, 0, 0, 0, 9, 9],
[0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]], dtype=uint8)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7]], dtype=uint8)
"""
if not np.can_cast(image, bool, casting="same_kind"):
# Cython doesn't support boolean memoryviews yet
Expand Down
17 changes: 17 additions & 0 deletions skimage/morphology/tests/test_misc.py
Expand Up @@ -280,3 +280,20 @@ def test_priority(self):
)
desired = np.array([[0, 0, 0], [0, 0, 0], [1, 0, 0]], dtype=bool)
assert_array_equal(result, desired)

@pytest.mark.parametrize("dtype", [np.uint8, np.int8])
def test_view_on_byte_sized(self, dtype):
"""Test behavior if image is a view on 1 byte sized numeric dtypes."""
image = np.array([-2, 0, 2], dtype=dtype)

# When using a view, object values don't change
result_view = remove_close_objects(image.view(bool), 2)
desired_view = np.array([-2, 0, 0], dtype=dtype)
assert result_view.dtype is np.dtype(bool)
assert_array_equal(result_view.view(dtype), desired_view)

# When using astype, the object values > 0 are replaced with 1
result_astype = remove_close_objects(image.astype(bool), 2)
desired_astype = np.array([1, 0, 0], dtype=np.uint8)
assert_array_equal(result_astype, result_view)
assert_array_equal(result_astype.view(np.uint8), desired_astype)

0 comments on commit b2bb144

Please sign in to comment.