Skip to content
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

allow non-contiguous array input to binary morphology functions #4058

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions cupyx/scipy/ndimage/morphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ def _binary_erosion(input, structure, iterations, mask, output, border_value,
except TypeError:
raise TypeError('iterations parameter should be an integer')

if not input.flags.c_contiguous:
# TODO: grlee77: is C-contiguity required?
input = cupy.ascontiguousarray(input)
if input.dtype.kind == 'c':
raise TypeError('Complex type not supported')
if structure is None:
Expand All @@ -166,7 +163,6 @@ def _binary_erosion(input, structure, iterations, mask, output, border_value,
if structure.ndim != input.ndim:
raise RuntimeError('structure and input must have same dimensionality')
if not structure.flags.c_contiguous:
# TODO: grlee77: is C-contiguity required?
structure = cupy.ascontiguousarray(structure)
if structure.size < 1:
raise RuntimeError('structure must not be empty')
Expand All @@ -175,8 +171,7 @@ def _binary_erosion(input, structure, iterations, mask, output, border_value,
if mask.shape != input.shape:
raise RuntimeError('mask and input must have equal sizes')
if not mask.flags.c_contiguous:
# TODO: grlee77: current indexing requires C contiguous arrays.
mask = cupy.asacontiguousarray(mask)
mask = cupy.ascontiguousarray(mask)
masked = True
else:
masked = False
Expand Down
31 changes: 31 additions & 0 deletions tests/cupyx_tests/scipy_tests/ndimage_tests/test_morphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,37 @@ def test_binary_erosion_and_dilation(self, xp, scp):
return self._filter(xp, scp, x)


@testing.parameterize(*(
testing.product({
'x_dtype': [numpy.int8, numpy.float32],
'shape': [(16, 24)],
'filter': ['binary_erosion', 'binary_dilation'],
'iterations': [1, 2],
'contiguity': ['C', 'F', 'none']}
))
)
@testing.gpu
@testing.with_requires('scipy')
class BinaryErosionAndDilationContiguity(unittest.TestCase):
def _filter(self, xp, scp, x):
filter = getattr(scp.ndimage, self.filter)
ndim = len(self.shape)
structure = scp.ndimage.generate_binary_structure(ndim, 1)
return filter(x, structure, iterations=self.iterations, mask=None,
output=None, border_value=0, origin=0, brute_force=True)

@testing.numpy_cupy_array_equal(scipy_name='scp')
def test_binary_erosion_and_dilation_input_contiguity(self, xp, scp):
rstate = numpy.random.RandomState(5)
x = rstate.randn(*self.shape) > 0.3
x = xp.asarray(x, dtype=self.x_dtype)
if self.contiguity == 'F':
x = xp.asfortranarray(x)
elif self.contiguity == 'none':
x = x[::2, ::3]
return self._filter(xp, scp, x)


@testing.parameterize(*(
testing.product({
'shape': [(3, 4), (2, 3, 4), (1, 2, 3, 4)],
Expand Down