TST: Test error handling in convex_hull, convex_hull_object.

Also, a few PEP8 fixes in both module and tests.
This commit is contained in:
arokem
2015-12-23 14:57:44 -08:00
committed by Ariel Rokem
parent 227211fec7
commit 436d0e8ca3
2 changed files with 25 additions and 13 deletions
+7 -2
View File
@@ -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.')
+18 -11
View File
@@ -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()