From 227211fec7725c316dfd6041b19768e9f4996341 Mon Sep 17 00:00:00 2001 From: arokem Date: Wed, 23 Dec 2015 14:22:57 -0800 Subject: [PATCH 1/3] DOC: Small one. The input to this one needs to be 2D. --- skimage/morphology/convex_hull.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/morphology/convex_hull.py b/skimage/morphology/convex_hull.py index bb9ca734..5cb675a3 100644 --- a/skimage/morphology/convex_hull.py +++ b/skimage/morphology/convex_hull.py @@ -85,7 +85,7 @@ def convex_hull_object(image, neighbors=8): Parameters ---------- - image : ndarray + image : (M, N) array Binary input image. neighbors : {4, 8}, int Whether to use 4- or 8-connectivity. From 436d0e8ca385f25a2857139f81ab22a6aa3fa1bf Mon Sep 17 00:00:00 2001 From: arokem Date: Wed, 23 Dec 2015 14:57:44 -0800 Subject: [PATCH 2/3] TST: Test error handling in convex_hull, convex_hull_object. Also, a few PEP8 fixes in both module and tests. --- skimage/morphology/convex_hull.py | 9 ++++-- skimage/morphology/tests/test_convex_hull.py | 29 ++++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/skimage/morphology/convex_hull.py b/skimage/morphology/convex_hull.py index 5cb675a3..31b92a47 100644 --- a/skimage/morphology/convex_hull.py +++ b/skimage/morphology/convex_hull.py @@ -1,11 +1,12 @@ -__all__ = ['convex_hull_image', 'convex_hull_object'] - +"""Convex Hull.""" import numpy as np from ..measure._pnpoly import grid_points_in_poly from ._convex_hull import possible_hull from ..measure._label import label from ..util import unique_rows +__all__ = ['convex_hull_image', 'convex_hull_object'] + try: from scipy.spatial import Delaunay except ImportError: @@ -33,6 +34,8 @@ def convex_hull_image(image): .. [1] http://blogs.mathworks.com/steve/2011/10/04/binary-image-convex-hull-algorithm-notes/ """ + if image.ndim > 2: + raise ValueError("Input must be a 2D image") if Delaunay is None: raise ImportError("Could not import scipy.spatial.Delaunay, " @@ -104,6 +107,8 @@ def convex_hull_object(image, neighbors=8): convex_hull_image separately on each object. """ + if image.ndim > 2: + raise ValueError("Input must be a 2D image") if neighbors != 4 and neighbors != 8: raise ValueError('Neighbors must be either 4 or 8.') diff --git a/skimage/morphology/tests/test_convex_hull.py b/skimage/morphology/tests/test_convex_hull.py index c0cc954e..a33f3fb2 100644 --- a/skimage/morphology/tests/test_convex_hull.py +++ b/skimage/morphology/tests/test_convex_hull.py @@ -31,20 +31,24 @@ def test_basic(): assert_array_equal(convex_hull_image(image), expected) + # Test that an error is raised on passing a 3D image: + image3d = np.empty((5, 5, 5)) + assert_raises(ValueError, convex_hull_object, image3d) + @skipif(not scipy_spatial) def test_qhull_offset_example(): - nonzeros = (([1367, 1368, 1368, 1368, 1369, 1369, 1369, 1369, 1369, 1370, 1370, - 1370, 1370, 1370, 1370, 1370, 1371, 1371, 1371, 1371, 1371, 1371, - 1371, 1371, 1371, 1372, 1372, 1372, 1372, 1372, 1372, 1372, 1372, - 1372, 1373, 1373, 1373, 1373, 1373, 1373, 1373, 1373, 1373, 1374, - 1374, 1374, 1374, 1374, 1374, 1374, 1375, 1375, 1375, 1375, 1375, - 1376, 1376, 1376, 1377]), - ([151, 150, 151, 152, 149, 150, 151, 152, 153, 148, 149, 150, 151, - 152, 153, 154, 147, 148, 149, 150, 151, 152, 153, 154, 155, 146, - 147, 148, 149, 150, 151, 152, 153, 154, 146, 147, 148, 149, 150, - 151, 152, 153, 154, 147, 148, 149, 150, 151, 152, 153, 148, 149, - 150, 151, 152, 149, 150, 151, 150])) + nonzeros = (([1367, 1368, 1368, 1368, 1369, 1369, 1369, 1369, 1369, 1370, + 1370, 1370, 1370, 1370, 1370, 1370, 1371, 1371, 1371, 1371, + 1371, 1371, 1371, 1371, 1371, 1372, 1372, 1372, 1372, 1372, + 1372, 1372, 1372, 1372, 1373, 1373, 1373, 1373, 1373, 1373, + 1373, 1373, 1373, 1374, 1374, 1374, 1374, 1374, 1374, 1374, + 1375, 1375, 1375, 1375, 1375, 1376, 1376, 1376, 1377]), + ([151, 150, 151, 152, 149, 150, 151, 152, 153, 148, 149, 150, + 151, 152, 153, 154, 147, 148, 149, 150, 151, 152, 153, 154, + 155, 146, 147, 148, 149, 150, 151, 152, 153, 154, 146, 147, + 148, 149, 150, 151, 152, 153, 154, 147, 148, 149, 150, 151, + 152, 153, 148, 149, 150, 151, 152, 149, 150, 151, 150])) image = np.zeros((1392, 1040), dtype=bool) image[nonzeros] = True expected = image.copy() @@ -139,6 +143,9 @@ def test_object(): assert_raises(ValueError, convex_hull_object, image, 7) + # Test that an error is raised on passing a 3D image: + image3d = np.empty((5, 5, 5)) + assert_raises(ValueError, convex_hull_object, image3d) if __name__ == "__main__": np.testing.run_module_suite() From 9fb5f1333d90cea512ad8cdf72f57a97eb5a251b Mon Sep 17 00:00:00 2001 From: arokem Date: Wed, 23 Dec 2015 15:36:35 -0800 Subject: [PATCH 3/3] TST: Test both functions(!). --- skimage/morphology/tests/test_convex_hull.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/morphology/tests/test_convex_hull.py b/skimage/morphology/tests/test_convex_hull.py index a33f3fb2..1a41b6c8 100644 --- a/skimage/morphology/tests/test_convex_hull.py +++ b/skimage/morphology/tests/test_convex_hull.py @@ -33,7 +33,7 @@ def test_basic(): # Test that an error is raised on passing a 3D image: image3d = np.empty((5, 5, 5)) - assert_raises(ValueError, convex_hull_object, image3d) + assert_raises(ValueError, convex_hull_image, image3d) @skipif(not scipy_spatial)