From ff4e3bd0bd810099eb7da3bc9c707cedc5997f59 Mon Sep 17 00:00:00 2001 From: John Wiggins Date: Fri, 28 Aug 2015 20:37:26 +0200 Subject: [PATCH 1/4] Raise an error for negative sigma values in guassian_filter. --- skimage/filters/_gaussian.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/skimage/filters/_gaussian.py b/skimage/filters/_gaussian.py index 6bc12574..a02b6bdb 100644 --- a/skimage/filters/_gaussian.py +++ b/skimage/filters/_gaussian.py @@ -74,6 +74,10 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0, array([[ 0.05855018, 0.09653293, 0.05855018], [ 0.09653293, 0.15915589, 0.09653293], [ 0.05855018, 0.09653293, 0.05855018]]) + >>> gaussian_filter(a, sigma=-1) + Traceback (most recent call last): + ... + ValueError: Sigma values less than zero are not valid >>> # Several modes are possible for handling boundaries >>> gaussian_filter(a, sigma=1, mode='reflect') array([[ 0.08767308, 0.12075024, 0.08767308], @@ -93,6 +97,9 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0, "3D image with last dimension of length 3.") warnings.warn(RuntimeWarning(msg)) multichannel = True + if np.any(np.asarray(sigma) < 0.0): + msg = "Sigma values less than zero are not valid" + raise ValueError(msg) if multichannel: # do not filter across channels if not isinstance(sigma, coll.Iterable): From c05108da3687bf6854bf391e1d84add5c0062141 Mon Sep 17 00:00:00 2001 From: John Wiggins Date: Fri, 28 Aug 2015 21:03:01 +0200 Subject: [PATCH 2/4] Replace a doc test with some regular unit tests. --- skimage/filters/_gaussian.py | 4 ---- skimage/filters/tests/test_gaussian.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/skimage/filters/_gaussian.py b/skimage/filters/_gaussian.py index a02b6bdb..643dca05 100644 --- a/skimage/filters/_gaussian.py +++ b/skimage/filters/_gaussian.py @@ -74,10 +74,6 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0, array([[ 0.05855018, 0.09653293, 0.05855018], [ 0.09653293, 0.15915589, 0.09653293], [ 0.05855018, 0.09653293, 0.05855018]]) - >>> gaussian_filter(a, sigma=-1) - Traceback (most recent call last): - ... - ValueError: Sigma values less than zero are not valid >>> # Several modes are possible for handling boundaries >>> gaussian_filter(a, sigma=1, mode='reflect') array([[ 0.08767308, 0.12075024, 0.08767308], diff --git a/skimage/filters/tests/test_gaussian.py b/skimage/filters/tests/test_gaussian.py index c88ee1f8..59a0dfaf 100644 --- a/skimage/filters/tests/test_gaussian.py +++ b/skimage/filters/tests/test_gaussian.py @@ -1,8 +1,23 @@ +from nose.tools import raises import numpy as np from skimage.filters._gaussian import gaussian_filter from skimage._shared._warnings import expected_warnings +@raises(ValueError) +def test_negative_sigma(): + a = np.zeros((3, 3)) + a[1, 1] = 1. + gaussian_filter(a, -1.0) + + +@raises(ValueError) +def test_multiple_negative_sigma(): + a = np.zeros((3, 3)) + a[1, 1] = 1. + gaussian_filter(a, [-1.0, 1.0]) + + def test_null_sigma(): a = np.zeros((3, 3)) a[1, 1] = 1. From a0fc75c7cb335264f46ee1a88f83624734dc4bfe Mon Sep 17 00:00:00 2001 From: John Wiggins Date: Fri, 28 Aug 2015 21:10:39 +0200 Subject: [PATCH 3/4] Don't use two lines to raise an exception with a short message. --- skimage/filters/_gaussian.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/skimage/filters/_gaussian.py b/skimage/filters/_gaussian.py index 643dca05..85b572ca 100644 --- a/skimage/filters/_gaussian.py +++ b/skimage/filters/_gaussian.py @@ -94,8 +94,7 @@ def gaussian_filter(image, sigma, output=None, mode='nearest', cval=0, warnings.warn(RuntimeWarning(msg)) multichannel = True if np.any(np.asarray(sigma) < 0.0): - msg = "Sigma values less than zero are not valid" - raise ValueError(msg) + raise ValueError("Sigma values less than zero are not valid") if multichannel: # do not filter across channels if not isinstance(sigma, coll.Iterable): From 6e6d243dbdb91c70bb3cd26567e9ee877e245d23 Mon Sep 17 00:00:00 2001 From: John Wiggins Date: Fri, 28 Aug 2015 21:19:20 +0200 Subject: [PATCH 4/4] Use assert_raises instead of @raises. --- skimage/filters/tests/test_gaussian.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/skimage/filters/tests/test_gaussian.py b/skimage/filters/tests/test_gaussian.py index 59a0dfaf..14c95d0e 100644 --- a/skimage/filters/tests/test_gaussian.py +++ b/skimage/filters/tests/test_gaussian.py @@ -1,21 +1,16 @@ -from nose.tools import raises import numpy as np +from numpy.testing import assert_raises from skimage.filters._gaussian import gaussian_filter from skimage._shared._warnings import expected_warnings -@raises(ValueError) def test_negative_sigma(): a = np.zeros((3, 3)) a[1, 1] = 1. - gaussian_filter(a, -1.0) - - -@raises(ValueError) -def test_multiple_negative_sigma(): - a = np.zeros((3, 3)) - a[1, 1] = 1. - gaussian_filter(a, [-1.0, 1.0]) + assert_raises(ValueError, gaussian_filter, a, sigma=-1.0) + assert_raises(ValueError, gaussian_filter, a, sigma=[-1.0, 1.0]) + assert_raises(ValueError, gaussian_filter, a, + sigma=np.asarray([-1.0, 1.0])) def test_null_sigma():