From f52f2c1779b82aac79055850b8a999e4b8d71e49 Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Thu, 6 Nov 2014 10:27:43 +1100 Subject: [PATCH 1/2] Raise meaningful error in reconstruct w bad method If a method other than 'erosion' or 'dilation' was passed, an `UnboundLocalVariable` error for `pad_value` was raised, which made it difficult to determine the source of the error. Now, an eloquent `ValueError` is raised instead. See #1218. --- skimage/morphology/greyreconstruct.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/skimage/morphology/greyreconstruct.py b/skimage/morphology/greyreconstruct.py index bce26d6b..ec7aa182 100644 --- a/skimage/morphology/greyreconstruct.py +++ b/skimage/morphology/greyreconstruct.py @@ -152,6 +152,9 @@ def reconstruction(seed, mask, method='dilation', selem=None, offset=None): pad_value = np.min(seed) elif method == 'erosion': pad_value = np.max(seed) + else: + raise ValueError("Reconstruction method can be one of 'erosion' " + "or 'dilation'. Got '%s'." % method) images = np.ones(dims) * pad_value images[[0] + inside_slices] = seed images[[1] + inside_slices] = mask From 39d042e4ba158802530f05d73b74f38751f74f43 Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Thu, 6 Nov 2014 10:29:43 +1100 Subject: [PATCH 2/2] Test correct error for wrong method in reconstruct --- skimage/morphology/tests/test_reconstruction.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/skimage/morphology/tests/test_reconstruction.py b/skimage/morphology/tests/test_reconstruction.py index f5678c08..df9ddf8f 100644 --- a/skimage/morphology/tests/test_reconstruction.py +++ b/skimage/morphology/tests/test_reconstruction.py @@ -97,6 +97,12 @@ def test_invalid_selem(): reconstruction(seed, mask, selem=np.ones((3, 3))) +def test_invalid_method(): + seed = np.array([0, 8, 8, 8, 8, 8, 8, 8, 8, 0]) + mask = np.array([0, 3, 6, 2, 1, 1, 1, 4, 2, 0]) + assert_raises(ValueError, reconstruction, seed, mask, method='foo') + + if __name__ == '__main__': from numpy import testing testing.run_module_suite()