From a4e4e57ba509fa3fbfa45bcc22b8a93c0c2de33e Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 23 Dec 2014 07:27:45 -0600 Subject: [PATCH] Create new expected_warnings helper and some cleanup Add new helper function for expected warnings during test Indentation cleanups and avoid skipping doctests if possible. --- TODO.txt | 2 +- skimage/_shared/_warnings.py | 40 +++++++++++++++++++ skimage/measure/_ccomp.pyx | 4 +- skimage/measure/_regionprops.py | 8 ++-- skimage/morphology/tests/test_ccomp.py | 6 +-- skimage/novice/__init__.py | 2 +- .../segmentation/tests/test_random_walker.py | 4 +- 7 files changed, 53 insertions(+), 13 deletions(-) diff --git a/TODO.txt b/TODO.txt index d6d95586..13823d08 100644 --- a/TODO.txt +++ b/TODO.txt @@ -15,7 +15,7 @@ Version 0.13 Version 0.12 ------------ * Change `label` to mark background as 0, not -1, which is consistent with - SciPy's labelling. + SciPy's labelling. Also remove doctest skip in _ccomp.pyx `label`. * Remove `skimage.morphology.label` from `skimage.morphology.__init__`--it now lives in `skimage.measure.label`. * Remove deprecated `reverse_map` parameter of `skimage.transform.warp` diff --git a/skimage/_shared/_warnings.py b/skimage/_shared/_warnings.py index c4cd0a68..cf9df84a 100644 --- a/skimage/_shared/_warnings.py +++ b/skimage/_shared/_warnings.py @@ -4,6 +4,7 @@ from contextlib import contextmanager import sys import warnings import inspect +import re @contextmanager @@ -61,3 +62,42 @@ def all_warnings(): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") yield w + + +@contextmanager +def expected_warnings(matching): + """Context for use in testing to catch known warnings matching regexes + + Parameters + ---------- + matching : list of strings or compiled regexes + Regexes for the desired warning to catch + + + Examples + -------- + >>> from skimage import data, img_as_ubyte, img_as_float + >>> with expected_warnings(['precision loss']): + ... d = img_as_ubyte(img_as_float(data.coins())) + + Notes + ----- + Uses `all_warnings` to ensure all warnings are raised. + Upon exiting, it checks the recorded warnings for the desired matching + string. Raises a warning if the match was not found or an Unexpected + warning is found. + + """ + with all_warnings() as w: + yield w + remaining = matching + for warn in w: + found = False + for match in matching: + if re.search(match, str(warn.message)) is not None: + found = True + remaining.remove(match) + if not found: + raise ValueError('Unexpected warning: %s' % str(warn.message)) + if len(remaining) > 0: + raise ValueError('No warning raised matching: "%s"' % remaining[0]) diff --git a/skimage/measure/_ccomp.pyx b/skimage/measure/_ccomp.pyx index a73815e5..ac75601a 100644 --- a/skimage/measure/_ccomp.pyx +++ b/skimage/measure/_ccomp.pyx @@ -416,12 +416,12 @@ def label(input, neighbors=None, background=None, return_num=False, [0 1 0] [0 0 1]] >>> from skimage.measure import label - >>> print(label(x, neighbors=4)) # doctest: +SKIP + >>> print(label(x, connectivity=1)) [[0 1 1] [2 3 1] [2 2 4]] - >>> print(label(x, neighbors=8)) # doctest: +SKIP + >>> print(label(x, connectivity=2)) [[0 1 1] [1 0 1] [1 1 0]] diff --git a/skimage/measure/_regionprops.py b/skimage/measure/_regionprops.py index cf2bfc41..66a8734f 100644 --- a/skimage/measure/_regionprops.py +++ b/skimage/measure/_regionprops.py @@ -473,13 +473,13 @@ def regionprops(label_image, intensity_image=None, cache=True): >>> from skimage import data, util >>> from skimage.morphology import label >>> img = util.img_as_ubyte(data.coins()) > 110 - >>> label_img = label(img) # doctest: +SKIP - >>> props = regionprops(label_img) # doctest: +SKIP + >>> label_img = label(img, connectivity=img.ndim) + >>> props = regionprops(label_img) >>> # centroid of first labeled object - >>> props[0].centroid # doctest: +SKIP + >>> props[0].centroid (22.729879860483141, 81.912285234465827) >>> # centroid of first labeled object - >>> props[0]['centroid'] # doctest: +SKIP + >>> props[0]['centroid'] (22.729879860483141, 81.912285234465827) """ diff --git a/skimage/morphology/tests/test_ccomp.py b/skimage/morphology/tests/test_ccomp.py index 1a321a08..569b0e38 100644 --- a/skimage/morphology/tests/test_ccomp.py +++ b/skimage/morphology/tests/test_ccomp.py @@ -12,9 +12,9 @@ BG = -1 def label(*args, **kwargs): - """Wrap the label function to avoid deprecation warning""" - with all_warnings(): - return _label(*args, **kwargs) + """Wrap the label function to avoid deprecation warning""" + with all_warnings(): + return _label(*args, **kwargs) class TestConnectedComponents: diff --git a/skimage/novice/__init__.py b/skimage/novice/__init__.py index 68ca6aa7..029d9a64 100644 --- a/skimage/novice/__init__.py +++ b/skimage/novice/__init__.py @@ -42,7 +42,7 @@ True 451 Changing `size` resizes the picture. ->>> picture.size = (45, 30) # doctest: +SKIP +>>> picture.size = (45, 30) You can iterate over pixels, which have RGB values between 0 and 255, and know their location in the picture. diff --git a/skimage/segmentation/tests/test_random_walker.py b/skimage/segmentation/tests/test_random_walker.py index 0bb5b8d8..ed68cb0f 100644 --- a/skimage/segmentation/tests/test_random_walker.py +++ b/skimage/segmentation/tests/test_random_walker.py @@ -81,7 +81,7 @@ def test_2d_cg(): assert data.shape == labels.shape with all_warnings(): # cg mode full_prob = random_walker(data, labels, beta=90, mode='cg', - return_full_prob=True) + return_full_prob=True) assert (full_prob[1, 25:45, 40:60] >= full_prob[0, 25:45, 40:60]).all() assert data.shape == labels.shape @@ -245,7 +245,7 @@ def test_spacing_1(): # Test with `spacing` kwarg # First, anisotropic along Y - with all_warnings(): # using cd mode + with all_warnings(): # using cg mode labels_aniso = random_walker(data_aniso, labels_aniso, mode='cg', spacing=(1., 2., 1.)) assert (labels_aniso[13:17, 26:34, 13:17] == 2).all()