From 9e8f91930e9b257f0d538e5543c1c5d9e462b5d3 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 23 Dec 2014 16:46:44 -0600 Subject: [PATCH] Handle warnings in several packages Start handling warnings in data, exposure, and draw Add a known_warning decorator and suppress warnings in color pkg Use the existing all_warnings context manager Raise warnings in data Raise warnings in draw Raise warnings in exposure Suppress warnings in exposure tests Add comments about warning suppressions Raise warnings in feature Fix warnings in filter package Add warning handling to graph Handle warnings in io package --- skimage/_shared/testing.py | 32 ++++++++++++------- skimage/feature/tests/__init__.py | 2 ++ skimage/filters/rank/tests/__init__.py | 2 ++ skimage/filters/rank/tests/test_rank.py | 41 +++++++++++++++++-------- skimage/filters/tests/__init__.py | 2 ++ skimage/filters/tests/test_gaussian.py | 4 ++- skimage/filters/thresholding.py | 6 ++-- skimage/graph/tests/__init__.py | 2 ++ skimage/io/_plugins/pil_plugin.py | 10 +++--- skimage/io/tests/__init__.py | 2 ++ skimage/io/tests/test_pil.py | 7 +++-- skimage/io/tests/test_plugin_util.py | 23 +++++++++----- 12 files changed, 91 insertions(+), 42 deletions(-) create mode 100644 skimage/feature/tests/__init__.py create mode 100644 skimage/filters/rank/tests/__init__.py create mode 100644 skimage/filters/tests/__init__.py create mode 100644 skimage/graph/tests/__init__.py create mode 100644 skimage/io/tests/__init__.py diff --git a/skimage/_shared/testing.py b/skimage/_shared/testing.py index 0cf65cb4..1dae36c3 100644 --- a/skimage/_shared/testing.py +++ b/skimage/_shared/testing.py @@ -9,6 +9,7 @@ from skimage import ( data, io, img_as_uint, img_as_float, img_as_int, img_as_ubyte) from numpy import testing import numpy as np +from skimage._shared.utils import all_warnings SKIP_RE = re.compile("(\s*>>>.*?)(\s*)#\s*skip\s+if\s+(.*)$") @@ -115,20 +116,25 @@ def color_check(plugin, fmt='png'): testing.assert_allclose(img2.astype(np.uint8), r2) img3 = img_as_float(img) - r3 = roundtrip(img3, plugin, fmt) + with all_warnings(): # precision loss + r3 = roundtrip(img3, plugin, fmt) testing.assert_allclose(r3, img) - img4 = img_as_int(img) + with all_warnings(): # precision loss + img4 = img_as_int(img) if fmt.lower() in (('tif', 'tiff')): img4 -= 100 - r4 = roundtrip(img4, plugin, fmt) + with all_warnings(): # sign loss + r4 = roundtrip(img4, plugin, fmt) testing.assert_allclose(r4, img4) else: - r4 = roundtrip(img4, plugin, fmt) - testing.assert_allclose(r4, img_as_ubyte(img4)) + with all_warnings(): # sign loss + r4 = roundtrip(img4, plugin, fmt) + testing.assert_allclose(r4, img_as_ubyte(img4)) img5 = img_as_uint(img) - r5 = roundtrip(img5, plugin, fmt) + with all_warnings(): # precision loss + r5 = roundtrip(img5, plugin, fmt) testing.assert_allclose(r5, img) @@ -147,20 +153,24 @@ def mono_check(plugin, fmt='png'): testing.assert_allclose(img2.astype(np.uint8), r2) img3 = img_as_float(img) - r3 = roundtrip(img3, plugin, fmt) + with all_warnings(): # precision loss + r3 = roundtrip(img3, plugin, fmt) if r3.dtype.kind == 'f': testing.assert_allclose(img3, r3) else: testing.assert_allclose(r3, img_as_uint(img)) - img4 = img_as_int(img) + with all_warnings(): # precision loss + img4 = img_as_int(img) if fmt.lower() in (('tif', 'tiff')): img4 -= 100 - r4 = roundtrip(img4, plugin, fmt) + with all_warnings(): # sign loss + r4 = roundtrip(img4, plugin, fmt) testing.assert_allclose(r4, img4) else: - r4 = roundtrip(img4, plugin, fmt) - testing.assert_allclose(r4, img_as_uint(img4)) + with all_warnings(): # sign loss + r4 = roundtrip(img4, plugin, fmt) + testing.assert_allclose(r4, img_as_uint(img4)) img5 = img_as_uint(img) r5 = roundtrip(img5, plugin, fmt) diff --git a/skimage/feature/tests/__init__.py b/skimage/feature/tests/__init__.py new file mode 100644 index 00000000..0922ce59 --- /dev/null +++ b/skimage/feature/tests/__init__.py @@ -0,0 +1,2 @@ +import warnings +warnings.simplefilter('error') diff --git a/skimage/filters/rank/tests/__init__.py b/skimage/filters/rank/tests/__init__.py new file mode 100644 index 00000000..0922ce59 --- /dev/null +++ b/skimage/filters/rank/tests/__init__.py @@ -0,0 +1,2 @@ +import warnings +warnings.simplefilter('error') diff --git a/skimage/filters/rank/tests/test_rank.py b/skimage/filters/rank/tests/test_rank.py index 23745ebf..f2d91ff0 100644 --- a/skimage/filters/rank/tests/test_rank.py +++ b/skimage/filters/rank/tests/test_rank.py @@ -3,15 +3,22 @@ import numpy as np from numpy.testing import run_module_suite, assert_equal, assert_raises import skimage -from skimage import img_as_ubyte, img_as_uint, img_as_float +from skimage import img_as_ubyte, img_as_float from skimage import data, util, morphology from skimage.morphology import cmorph, disk from skimage.filters import rank +from skimage._shared.utils import all_warnings np.random.seed(0) def test_all(): + + with all_warnings(): # precision loss + check_all() + + +def check_all(): image = np.random.rand(25, 25) selem = morphology.disk(1) refs = np.load(os.path.join(skimage.data_dir, "rank_filter_tests.npz")) @@ -151,8 +158,9 @@ def test_bitdepth(): for i in range(5): image = np.ones((100, 100), dtype=np.uint16) * 255 * 2 ** i - r = rank.mean_percentile(image=image, selem=elem, mask=mask, - out=out, shift_x=0, shift_y=0, p0=.1, p1=.9) + with all_warnings(): # bit depth + rank.mean_percentile(image=image, selem=elem, mask=mask, + out=out, shift_x=0, shift_y=0, p0=.1, p1=.9) def test_population(): @@ -261,7 +269,8 @@ def test_compare_ubyte_vs_float(): for method in methods: func = getattr(rank, method) out_u = func(image_uint, disk(3)) - out_f = func(image_float, disk(3)) + with all_warnings(): # precision loss + out_f = func(image_float, disk(3)) assert_equal(out_u, out_f) @@ -273,9 +282,10 @@ def test_compare_8bit_unsigned_vs_signed(): image = img_as_ubyte(data.camera()) image[image > 127] = 0 image_s = image.astype(np.int8) - image_u = img_as_ubyte(image_s) + with all_warnings(): # precision loss + image_u = img_as_ubyte(image_s) - assert_equal(image_u, img_as_ubyte(image_s)) + assert_equal(image_u, img_as_ubyte(image_s)) methods = ['autolevel', 'bottomhat', 'equalize', 'gradient', 'maximum', 'mean', 'subtract_mean', 'median', 'minimum', 'modal', @@ -283,8 +293,10 @@ def test_compare_8bit_unsigned_vs_signed(): for method in methods: func = getattr(rank, method) - out_u = func(image_u, disk(3)) - out_s = func(image_s, disk(3)) + + with all_warnings(): # sign loss + out_u = func(image_u, disk(3)) + out_s = func(image_s, disk(3)) assert_equal(out_u, out_s) @@ -474,10 +486,12 @@ def test_entropy(): selem = np.ones((64, 64), dtype=np.uint8) data = np.tile( np.reshape(np.arange(4096), (64, 64)), (2, 2)).astype(np.uint16) - assert(np.max(rank.entropy(data, selem)) == 12) + with all_warnings(): # bitdepth + assert(np.max(rank.entropy(data, selem)) == 12) # make sure output is of dtype double - out = rank.entropy(data, np.ones((16, 16), dtype=np.uint8)) + with all_warnings(): # bitdepth + out = rank.entropy(data, np.ones((16, 16), dtype=np.uint8)) assert out.dtype == np.double @@ -508,9 +522,10 @@ def test_16bit(): for bitdepth in range(17): value = 2 ** bitdepth - 1 image[10, 10] = value - assert rank.minimum(image, selem)[10, 10] == 0 - assert rank.maximum(image, selem)[10, 10] == value - assert rank.mean(image, selem)[10, 10] == int(value / selem.size) + with all_warnings(): # bitdepth + assert rank.minimum(image, selem)[10, 10] == 0 + assert rank.maximum(image, selem)[10, 10] == value + assert rank.mean(image, selem)[10, 10] == int(value / selem.size) def test_bilateral(): diff --git a/skimage/filters/tests/__init__.py b/skimage/filters/tests/__init__.py new file mode 100644 index 00000000..0922ce59 --- /dev/null +++ b/skimage/filters/tests/__init__.py @@ -0,0 +1,2 @@ +import warnings +warnings.simplefilter('error') diff --git a/skimage/filters/tests/test_gaussian.py b/skimage/filters/tests/test_gaussian.py index 612dd9e4..01e1c12e 100644 --- a/skimage/filters/tests/test_gaussian.py +++ b/skimage/filters/tests/test_gaussian.py @@ -1,5 +1,6 @@ import numpy as np from skimage.filters._gaussian import gaussian_filter +from skimage._shared.utils import all_warnings def test_null_sigma(): @@ -25,7 +26,8 @@ def test_multichannel(): assert np.allclose([a[..., i].mean() for i in range(3)], [gaussian_rgb_a[..., i].mean() for i in range(3)]) # Test multichannel = None - gaussian_rgb_a = gaussian_filter(a, sigma=1, mode='reflect') + with all_warnings(): # multichannel + gaussian_rgb_a = gaussian_filter(a, sigma=1, mode='reflect') # Check that the mean value is conserved in each channel # (color channels are not mixed together) assert np.allclose([a[..., i].mean() for i in range(3)], diff --git a/skimage/filters/thresholding.py b/skimage/filters/thresholding.py index 5a737293..76a725a7 100644 --- a/skimage/filters/thresholding.py +++ b/skimage/filters/thresholding.py @@ -121,7 +121,7 @@ def threshold_otsu(image, nbins=256): >>> thresh = threshold_otsu(image) >>> binary = image <= thresh """ - hist, bin_centers = histogram(image, nbins) + hist, bin_centers = histogram(image.flatten(), nbins) hist = hist.astype(float) # class probabilities for all possible thresholds @@ -176,7 +176,7 @@ def threshold_yen(image, nbins=256): >>> thresh = threshold_yen(image) >>> binary = image <= thresh """ - hist, bin_centers = histogram(image, nbins) + hist, bin_centers = histogram(image.flatten(), nbins) # On blank images (e.g. filled with 0) with int dtype, `histogram()` # returns `bin_centers` containing only one value. Speed up with it. if bin_centers.size == 1: @@ -246,7 +246,7 @@ def threshold_isodata(image, nbins=256, return_all=False): >>> binary = image > thresh """ - hist, bin_centers = histogram(image, nbins) + hist, bin_centers = histogram(image.flatten(), nbins) # image only contains one unique value if len(bin_centers) == 1: diff --git a/skimage/graph/tests/__init__.py b/skimage/graph/tests/__init__.py new file mode 100644 index 00000000..0922ce59 --- /dev/null +++ b/skimage/graph/tests/__init__.py @@ -0,0 +1,2 @@ +import warnings +warnings.simplefilter('error') diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 1b0d9efd..f8414283 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -102,7 +102,7 @@ def pil_to_ndarray(im, dtype=None, img_num=None): dtype = '>u2' if im.mode.endswith('B') else '