Skip to content

Commit

Permalink
do not enforce C-contiguity on input to binary_morphology
Browse files Browse the repository at this point in the history
  • Loading branch information
grlee77 committed Oct 2, 2020
1 parent 34cb08d commit 911eec8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
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

0 comments on commit 911eec8

Please sign in to comment.