Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix ndimage.grey_dilation #199

Merged
merged 3 commits into from

3 participants

@tonysyu

grey_dilation raises an error if given a scalar size or if structure is given but size and footprint aren't. Note that other morphology functions behave correctly for these cases. See original post and ticket #1135 for details.

@tonysyu tonysyu Fix ndimage.grey_dilation.
`grey_dilation` raises an error if given a scalar `size` or if `structure` is given but size and footprint aren't.

Ticket #1135
479ae86
@jjhelmus

Based on some recent discussion on the mailing list it should be noted that this pull also addressed ticket #1281 and ticket #1498.

Two addition that might impove this pull:

  1. Should we raise a RuntimeError if footprint, structure and size are None? Current ndimage.grey_dilation([1]) raises a TypeError at line 1428, which is not as helpful. If this sanity check is added it should also be added to grey_erosion.

  2. Update the doc for grey_dilation and grey_erosion to mention that size is optional if footprint or structure is provided.

tonysyu added some commits
@tonysyu tonysyu Raise user-friendly error when parameters not specified.
Before, when size, footprint, and structure were not specified, functions raised an `IndexError` with no explanation.
4ba06a9
@tonysyu tonysyu Add note to docstring that size is optional.
Specifying `size` is optional if `structure` is specified. This is True for all morphology functions (verified for all altered functions).
2761212
@tonysyu

Thanks Jonathan! I just updated my branch and responded on the mailing list.

@rgommers
Owner

Looks good, thanks Tony and Jonathan.

@rgommers rgommers merged commit 9fdd754 into scipy:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 23, 2012
  1. @tonysyu

    Fix ndimage.grey_dilation.

    tonysyu authored
    `grey_dilation` raises an error if given a scalar `size` or if `structure` is given but size and footprint aren't.
    
    Ticket #1135
Commits on May 8, 2012
  1. @tonysyu

    Raise user-friendly error when parameters not specified.

    tonysyu authored
    Before, when size, footprint, and structure were not specified, functions raised an `IndexError` with no explanation.
  2. @tonysyu

    Add note to docstring that size is optional.

    tonysyu authored
    Specifying `size` is optional if `structure` is specified. This is True for all morphology functions (verified for all altered functions).
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 11 deletions.
  1. +19 −11 scipy/ndimage/morphology.py
  2. +24 −0 scipy/ndimage/tests/test_ndimage.py
View
30 scipy/ndimage/morphology.py
@@ -1163,8 +1163,8 @@ def grey_erosion(input, size = None, footprint = None, structure = None,
Array over which the grayscale erosion is to be computed.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- grayscale erosion. Optional if `footprint` is provided.
+ Shape of a flat and full structuring element used for the grayscale
+ erosion. Optional if `footprint` or `structure` is provided.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -1266,6 +1266,8 @@ def grey_erosion(input, size = None, footprint = None, structure = None,
[0, 0, 0, 0, 0, 0, 0]])
"""
+ if size is None and footprint is None and structure is None:
+ raise ValueError("size, footprint or structure must be specified")
return filters._min_or_max_filter(input, size, footprint, structure,
output, mode, cval, origin, 1)
@@ -1287,8 +1289,8 @@ def grey_dilation(input, size = None, footprint = None, structure = None,
Array over which the grayscale dilation is to be computed.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- grayscale dilation. Optional if `footprint` is provided.
+ Shape of a flat and full structuring element used for the grayscale
+ dilation. Optional if `footprint` or `structure` is provided.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -1406,6 +1408,8 @@ def grey_dilation(input, size = None, footprint = None, structure = None,
[1, 1, 1, 1, 1, 1, 1]])
"""
+ if size is None and footprint is None and structure is None:
+ raise ValueError("size, footprint or structure must be specified")
if structure is not None:
structure = numpy.asarray(structure)
structure = structure[tuple([slice(None, None, -1)] *
@@ -1420,6 +1424,10 @@ def grey_dilation(input, size = None, footprint = None, structure = None,
origin[ii] = -origin[ii]
if footprint is not None:
sz = footprint.shape[ii]
+ elif structure is not None:
+ sz = structure.shape[ii]
+ elif numpy.isscalar(size):
+ sz = size
else:
sz = size[ii]
if not sz & 1:
@@ -1443,8 +1451,8 @@ def grey_opening(input, size = None, footprint = None, structure = None,
Array over which the grayscale opening is to be computed.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- grayscale opening. Optional if `footprint` is provided.
+ Shape of a flat and full structuring element used for the grayscale
+ opening. Optional if `footprint` or `structure` is provided.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -1538,8 +1546,8 @@ def grey_closing(input, size = None, footprint = None, structure = None,
Array over which the grayscale closing is to be computed.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- grayscale closing. Optional if `footprint` is provided.
+ Shape of a flat and full structuring element used for the grayscale
+ closing. Optional if `footprint` or `structure` is provided.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -1635,9 +1643,9 @@ def morphological_gradient(input, size = None, footprint = None,
Array over which to compute the morphlogical gradient.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- mathematical morphology operations. Optional if `footprint`
- is provided. A larger `size` yields a more blurred gradient.
+ Shape of a flat and full structuring element used for the mathematical
+ morphology operations. Optional if `footprint` or `structure` is
+ provided. A larger `size` yields a more blurred gradient.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
View
24 scipy/ndimage/tests/test_ndimage.py
@@ -4662,6 +4662,30 @@ def test_hit_or_miss03(self):
assert_array_almost_equal(expected, out)
+class TestDilateFix:
+
+ def setUp(self):
+ # dilation related setup
+ self.array = numpy.array([[0, 0, 0, 0, 0,],
+ [0, 0, 0, 0, 0,],
+ [0, 0, 0, 1, 0,],
+ [0, 0, 1, 1, 0,],
+ [0, 0, 0, 0, 0,]], dtype=numpy.uint8)
+
+ self.sq3x3 = numpy.ones((3, 3))
+ dilated3x3 = ndimage.binary_dilation(self.array, structure=self.sq3x3)
+ self.dilated3x3 = dilated3x3.view(numpy.uint8)
+
+ def test_dilation_square_structure(self):
+ result = ndimage.grey_dilation(self.array, structure=self.sq3x3)
+ # +1 accounts for difference between grey and binary dilation
+ assert_array_almost_equal(result, self.dilated3x3 + 1)
+
+ def test_dilation_scalar_size(self):
+ result = ndimage.grey_dilation(self.array, size=3)
+ assert_array_almost_equal(result, self.dilated3x3)
+
+
#class NDImageTestResult(unittest.TestResult):
# separator1 = '=' * 70 + '\n'
# separator2 = '-' * 70 + '\n'
Something went wrong with that request. Please try again.