From 1b905d4cefc68aba03aa511557b9700742c61720 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 23 Dec 2014 10:21:06 -0600 Subject: [PATCH] More cleanup and updating of tests Clean up setup_test and add a teardown_test method Implement new setup/teardown in novice tests Fix warning handling in pil_plugin Update rank tests --- skimage/_shared/testing.py | 29 ++++++++++++++++------- skimage/filters/rank/tests/__init__.py | 11 +++++++-- skimage/filters/rank/tests/test_rank.py | 31 ++++++++++++++----------- skimage/filters/tests/__init__.py | 11 +++++++-- skimage/io/_plugins/pil_plugin.py | 8 +++---- skimage/novice/tests/__init__.py | 11 +++++++-- 6 files changed, 69 insertions(+), 32 deletions(-) diff --git a/skimage/_shared/testing.py b/skimage/_shared/testing.py index 44ffb62d..fd109462 100644 --- a/skimage/_shared/testing.py +++ b/skimage/_shared/testing.py @@ -179,14 +179,27 @@ def mono_check(plugin, fmt='png'): def setup_test(): - warnings.simplefilter('error') - with all_warnings(): - from scipy import signal, ndimage, special, optimize, linalg - from scipy.io import loadmat - from skimage import filter, viewer, data - data.moon() - if os.environ.get('TRAVIS_PYTHON_VERSION', None) == '2.7': - warnings.simplefilter('default') + """Default package level setup routine for skimage tests. + + Import packages known to raise errors, and then + force warnings to raise errors. + """ + warnings.simplefilter('default') + from scipy import signal, ndimage, special, optimize, linalg + from scipy.io import loadmat + from skimage import filter, viewer, data + # trigger PIL warnings + data.moon() + warnings.simplefilter('error') + + +def teardown_test(): + """Default package level teardown routine for skimage tests. + + Restore warnings to default behavior + """ + warnings.simplefilter('default') + if __name__ == '__main__': color_check('pil') diff --git a/skimage/filters/rank/tests/__init__.py b/skimage/filters/rank/tests/__init__.py index c098c64d..9c11267a 100644 --- a/skimage/filters/rank/tests/__init__.py +++ b/skimage/filters/rank/tests/__init__.py @@ -1,2 +1,9 @@ -from skimage._shared.testing import setup_test -setup_test() +from skimage._shared.testing import setup_test, teardown_test + + +def setup(): + setup_test() + + +def tearDown(): + teardown_test() diff --git a/skimage/filters/rank/tests/test_rank.py b/skimage/filters/rank/tests/test_rank.py index d245d3f4..5cf5a091 100644 --- a/skimage/filters/rank/tests/test_rank.py +++ b/skimage/filters/rank/tests/test_rank.py @@ -7,18 +7,15 @@ 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) +from skimage._shared._warnings import expected_warnings def test_all(): - with all_warnings(): # precision loss + with expected_warnings(['precision loss', 'non-integer']): check_all() def check_all(): - np.random.seed(0) image = np.random.rand(25, 25) selem = morphology.disk(1) refs = np.load(os.path.join(skimage.data_dir, "rank_filter_tests.npz")) @@ -158,7 +155,11 @@ def test_bitdepth(): for i in range(5): image = np.ones((100, 100), dtype=np.uint16) * 255 * 2 ** i - with all_warnings(): # bit depth + if i > 3: + expected = ["Bitdepth of"] + else: + expected = [] + with expected_warnings(expected): rank.mean_percentile(image=image, selem=elem, mask=mask, out=out, shift_x=0, shift_y=0, p0=.1, p1=.9) @@ -269,7 +270,7 @@ def test_compare_ubyte_vs_float(): for method in methods: func = getattr(rank, method) out_u = func(image_uint, disk(3)) - with all_warnings(): # precision loss + with expected_warnings(['precision loss']): out_f = func(image_float, disk(3)) assert_equal(out_u, out_f) @@ -282,9 +283,8 @@ def test_compare_8bit_unsigned_vs_signed(): image = img_as_ubyte(data.camera()) image[image > 127] = 0 image_s = image.astype(np.int8) - with all_warnings(): # precision loss + with expected_warnings(['sign loss', 'precision loss']): image_u = img_as_ubyte(image_s) - assert_equal(image_u, img_as_ubyte(image_s)) methods = ['autolevel', 'bottomhat', 'equalize', 'gradient', 'maximum', @@ -294,7 +294,7 @@ def test_compare_8bit_unsigned_vs_signed(): for method in methods: func = getattr(rank, method) - with all_warnings(): # sign loss + with expected_warnings(['sign loss', 'precision loss']): out_u = func(image_u, disk(3)) out_s = func(image_s, disk(3)) assert_equal(out_u, out_s) @@ -486,11 +486,11 @@ 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) - with all_warnings(): # bitdepth + with expected_warnings(['Bitdepth of 11']): assert(np.max(rank.entropy(data, selem)) == 12) # make sure output is of dtype double - with all_warnings(): # bitdepth + with expected_warnings(['Bitdepth of 11']): out = rank.entropy(data, np.ones((16, 16), dtype=np.uint8)) assert out.dtype == np.double @@ -522,12 +522,15 @@ def test_16bit(): for bitdepth in range(17): value = 2 ** bitdepth - 1 image[10, 10] = value - with all_warnings(): # bitdepth + if bitdepth > 11: + expected = ['Bitdepth of %s' % (bitdepth - 1)] + else: + expected = [] + with expected_warnings(expected): 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(): image = np.zeros((21, 21), dtype=np.uint16) selem = np.ones((3, 3), dtype=np.uint8) diff --git a/skimage/filters/tests/__init__.py b/skimage/filters/tests/__init__.py index c098c64d..9c11267a 100644 --- a/skimage/filters/tests/__init__.py +++ b/skimage/filters/tests/__init__.py @@ -1,2 +1,9 @@ -from skimage._shared.testing import setup_test -setup_test() +from skimage._shared.testing import setup_test, teardown_test + + +def setup(): + setup_test() + + +def tearDown(): + teardown_test() diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 3146cbbc..cdaea687 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -7,7 +7,7 @@ from PIL import Image from skimage.util import img_as_ubyte, img_as_uint from skimage.external.tifffile import ( imread as tif_imread, imsave as tif_imsave) -from skimage._shared.utils import all_warnings +from skimage._shared._warnings import expected_warnings def imread(fname, dtype=None, img_num=None, **kwargs): @@ -48,13 +48,13 @@ def imread(fname, dtype=None, img_num=None, **kwargs): im = Image.open(fname) try: # this will raise an IOError if the file is not readable - im.getdata()[0] + with expected_warnings(['unclosed file']): + im.getdata()[0] except IOError: site = "http://pillow.readthedocs.org/en/latest/installation.html#external-libraries" raise ValueError('Could not load "%s"\nPlease see documentation at: %s' % (fname, site)) else: - with all_warnings(): # PIL resource warnings - return pil_to_ndarray(im, dtype=dtype, img_num=img_num) + return pil_to_ndarray(im, dtype=dtype, img_num=img_num) def pil_to_ndarray(im, dtype=None, img_num=None): diff --git a/skimage/novice/tests/__init__.py b/skimage/novice/tests/__init__.py index c098c64d..9c11267a 100644 --- a/skimage/novice/tests/__init__.py +++ b/skimage/novice/tests/__init__.py @@ -1,2 +1,9 @@ -from skimage._shared.testing import setup_test -setup_test() +from skimage._shared.testing import setup_test, teardown_test + + +def setup(): + setup_test() + + +def tearDown(): + teardown_test()