From 95d66ac5a6bd18c4f412f2a878e3e032765827f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sat, 6 Oct 2012 21:34:41 +0200 Subject: [PATCH] Improve test coverage of find_contours --- skimage/measure/find_contours.py | 2 +- skimage/measure/tests/test_find_contours.py | 62 ++++++++++++--------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/skimage/measure/find_contours.py b/skimage/measure/find_contours.py index 68e1d53c..eafbd523 100755 --- a/skimage/measure/find_contours.py +++ b/skimage/measure/find_contours.py @@ -98,7 +98,7 @@ def find_contours(array, level, """ array = np.asarray(array, dtype=np.double) if array.ndim != 2: - raise RuntimeError('Only 2D arrays are supported.') + raise TypeError('Only 2D arrays are supported.') level = float(level) if (fully_connected not in _param_options or positive_orientation not in _param_options): diff --git a/skimage/measure/tests/test_find_contours.py b/skimage/measure/tests/test_find_contours.py index 62b39b3f..cde60e4d 100644 --- a/skimage/measure/tests/test_find_contours.py +++ b/skimage/measure/tests/test_find_contours.py @@ -21,34 +21,37 @@ r = np.sqrt(x**2 + y**2) def test_binary(): - contours = find_contours(a, 0.5) + ref = [[6. , 1.5], + [5. , 1.5], + [4. , 1.5], + [3. , 1.5], + [2. , 1.5], + [1.5, 2. ], + [1.5, 3. ], + [1.5, 4. ], + [1.5, 5. ], + [1.5, 6. ], + [1. , 6.5], + [0.5, 6. ], + [0.5, 5. ], + [0.5, 4. ], + [0.5, 3. ], + [0.5, 2. ], + [0.5, 1. ], + [1. , 0.5], + [2. , 0.5], + [3. , 0.5], + [4. , 0.5], + [5. , 0.5], + [6. , 0.5], + [6.5, 1. ], + [6. , 1.5]] + + contours = find_contours(a, 0.5, positive_orientation='high') assert len(contours) == 1 - assert_array_equal(contours[0], - [[6. , 1.5], - [5. , 1.5], - [4. , 1.5], - [3. , 1.5], - [2. , 1.5], - [1.5, 2. ], - [1.5, 3. ], - [1.5, 4. ], - [1.5, 5. ], - [1.5, 6. ], - [1. , 6.5], - [0.5, 6. ], - [0.5, 5. ], - [0.5, 4. ], - [0.5, 3. ], - [0.5, 2. ], - [0.5, 1. ], - [1. , 0.5], - [2. , 0.5], - [3. , 0.5], - [4. , 0.5], - [5. , 0.5], - [6. , 0.5], - [6.5, 1. ], - [6. , 1.5]]) + assert_array_equal(contours[0][::-1], ref) + + def test_float(): @@ -70,6 +73,11 @@ def test_memory_order(): assert len(contours) == 1 +def test_invalid_input(): + assert_raises(ValueError, find_contours, r, 0.5, 'foo', 'bar') + assert_raises(TypeError, find_contours, r[..., None], 0.5) + + if __name__ == '__main__': from numpy.testing import run_module_suite run_module_suite()