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.
This commit is contained in:
Steven Silvester
2014-12-23 16:50:18 -06:00
parent 782ba46a4c
commit a4e4e57ba5
7 changed files with 53 additions and 13 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ Version 0.13
Version 0.12 Version 0.12
------------ ------------
* Change `label` to mark background as 0, not -1, which is consistent with * 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 * Remove `skimage.morphology.label` from `skimage.morphology.__init__`--it now
lives in `skimage.measure.label`. lives in `skimage.measure.label`.
* Remove deprecated `reverse_map` parameter of `skimage.transform.warp` * Remove deprecated `reverse_map` parameter of `skimage.transform.warp`
+40
View File
@@ -4,6 +4,7 @@ from contextlib import contextmanager
import sys import sys
import warnings import warnings
import inspect import inspect
import re
@contextmanager @contextmanager
@@ -61,3 +62,42 @@ def all_warnings():
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always") warnings.simplefilter("always")
yield w 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])
+2 -2
View File
@@ -416,12 +416,12 @@ def label(input, neighbors=None, background=None, return_num=False,
[0 1 0] [0 1 0]
[0 0 1]] [0 0 1]]
>>> from skimage.measure import label >>> from skimage.measure import label
>>> print(label(x, neighbors=4)) # doctest: +SKIP >>> print(label(x, connectivity=1))
[[0 1 1] [[0 1 1]
[2 3 1] [2 3 1]
[2 2 4]] [2 2 4]]
>>> print(label(x, neighbors=8)) # doctest: +SKIP >>> print(label(x, connectivity=2))
[[0 1 1] [[0 1 1]
[1 0 1] [1 0 1]
[1 1 0]] [1 1 0]]
+4 -4
View File
@@ -473,13 +473,13 @@ def regionprops(label_image, intensity_image=None, cache=True):
>>> from skimage import data, util >>> from skimage import data, util
>>> from skimage.morphology import label >>> from skimage.morphology import label
>>> img = util.img_as_ubyte(data.coins()) > 110 >>> img = util.img_as_ubyte(data.coins()) > 110
>>> label_img = label(img) # doctest: +SKIP >>> label_img = label(img, connectivity=img.ndim)
>>> props = regionprops(label_img) # doctest: +SKIP >>> props = regionprops(label_img)
>>> # centroid of first labeled object >>> # centroid of first labeled object
>>> props[0].centroid # doctest: +SKIP >>> props[0].centroid
(22.729879860483141, 81.912285234465827) (22.729879860483141, 81.912285234465827)
>>> # centroid of first labeled object >>> # centroid of first labeled object
>>> props[0]['centroid'] # doctest: +SKIP >>> props[0]['centroid']
(22.729879860483141, 81.912285234465827) (22.729879860483141, 81.912285234465827)
""" """
+3 -3
View File
@@ -12,9 +12,9 @@ BG = -1
def label(*args, **kwargs): def label(*args, **kwargs):
"""Wrap the label function to avoid deprecation warning""" """Wrap the label function to avoid deprecation warning"""
with all_warnings(): with all_warnings():
return _label(*args, **kwargs) return _label(*args, **kwargs)
class TestConnectedComponents: class TestConnectedComponents:
+1 -1
View File
@@ -42,7 +42,7 @@ True
451 451
Changing `size` resizes the picture. 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, You can iterate over pixels, which have RGB values between 0 and 255,
and know their location in the picture. and know their location in the picture.
@@ -81,7 +81,7 @@ def test_2d_cg():
assert data.shape == labels.shape assert data.shape == labels.shape
with all_warnings(): # cg mode with all_warnings(): # cg mode
full_prob = random_walker(data, labels, beta=90, mode='cg', 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] >= assert (full_prob[1, 25:45, 40:60] >=
full_prob[0, 25:45, 40:60]).all() full_prob[0, 25:45, 40:60]).all()
assert data.shape == labels.shape assert data.shape == labels.shape
@@ -245,7 +245,7 @@ def test_spacing_1():
# Test with `spacing` kwarg # Test with `spacing` kwarg
# First, anisotropic along Y # 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', labels_aniso = random_walker(data_aniso, labels_aniso, mode='cg',
spacing=(1., 2., 1.)) spacing=(1., 2., 1.))
assert (labels_aniso[13:17, 26:34, 13:17] == 2).all() assert (labels_aniso[13:17, 26:34, 13:17] == 2).all()