From 2d3ada19e57323fac56342ee8bcab512cf36cc5b Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 23 Dec 2014 16:24:40 -0600 Subject: [PATCH] Allow for no exceptions to be raised and cleanup Allow for no exceptions to be raised Add some helpful comments Remove doctest skips Remove TODO note Cleanup and skip failing doctest --- TODO.txt | 2 +- skimage/_shared/_warnings.py | 7 +++++-- skimage/_shared/testing.py | 12 ++++++------ skimage/_shared/utils.py | 1 - skimage/io/_io.py | 2 +- skimage/measure/_ccomp.pyx | 2 +- skimage/measure/fit.py | 6 +++--- skimage/morphology/watershed.py | 6 +++--- skimage/transform/hough_transform.py | 4 ++-- skimage/util/tests/test_dtype.py | 16 ++++------------ 10 files changed, 26 insertions(+), 32 deletions(-) diff --git a/TODO.txt b/TODO.txt index 13823d08..d6d95586 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. Also remove doctest skip in _ccomp.pyx `label`. + SciPy's labelling. * 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 fd2c06a0..ffac12e9 100644 --- a/skimage/_shared/_warnings.py +++ b/skimage/_shared/_warnings.py @@ -85,12 +85,15 @@ def expected_warnings(matching): 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. + warning is found. You can pass an empty regex as on of the matches + to allow for no matches: "\A\Z". """ with all_warnings() as w: + # enter context yield w - remaining = [m for m in matching] + # exited user context, check the recorded warnings + remaining = [m for m in matching if not '\A\Z' in m.split('|')] for warn in w: found = False for match in matching: diff --git a/skimage/_shared/testing.py b/skimage/_shared/testing.py index e5ca2f87..0f556acf 100644 --- a/skimage/_shared/testing.py +++ b/skimage/_shared/testing.py @@ -117,7 +117,7 @@ def color_check(plugin, fmt='png'): testing.assert_allclose(img2.astype(np.uint8), r2) img3 = img_as_float(img) - with expected_warnings(['precision loss']): + with expected_warnings(['precision loss|unclosed file']): r3 = roundtrip(img3, plugin, fmt) testing.assert_allclose(r3, img) @@ -129,12 +129,12 @@ def color_check(plugin, fmt='png'): r4 = roundtrip(img4, plugin, fmt) testing.assert_allclose(r4, img4) else: - with expected_warnings(['sign loss']): + with expected_warnings(['sign loss|precision loss|unclosed file']): r4 = roundtrip(img4, plugin, fmt) testing.assert_allclose(r4, img_as_ubyte(img4)) img5 = img_as_uint(img) - with expected_warnings(['precision loss']): + with expected_warnings(['precision loss|unclosed file']): r5 = roundtrip(img5, plugin, fmt) testing.assert_allclose(r5, img) @@ -154,7 +154,7 @@ def mono_check(plugin, fmt='png'): testing.assert_allclose(img2.astype(np.uint8), r2) img3 = img_as_float(img) - with expected_warnings(['precision loss']): + with expected_warnings(['precision|unclosed file|\A\Z']): r3 = roundtrip(img3, plugin, fmt) if r3.dtype.kind == 'f': testing.assert_allclose(img3, r3) @@ -165,11 +165,11 @@ def mono_check(plugin, fmt='png'): img4 = img_as_int(img) if fmt.lower() in (('tif', 'tiff')): img4 -= 100 - with expected_warnings(['sign loss']): + with expected_warnings(['sign loss|\A\Z']): r4 = roundtrip(img4, plugin, fmt) testing.assert_allclose(r4, img4) else: - with expected_warnings(['sign loss']): + with expected_warnings(['precision loss|sign loss|unclosed file']): r4 = roundtrip(img4, plugin, fmt) testing.assert_allclose(r4, img_as_uint(img4)) diff --git a/skimage/_shared/utils.py b/skimage/_shared/utils.py index 7be979bc..43fda35d 100644 --- a/skimage/_shared/utils.py +++ b/skimage/_shared/utils.py @@ -163,4 +163,3 @@ def assert_nD(array, ndim, arg_name='image'): ndim = [ndim] if not array.ndim in ndim: raise ValueError(msg % (arg_name, '-or-'.join([str(n) for n in ndim]))) - diff --git a/skimage/io/_io.py b/skimage/io/_io.py index 1ebb3d59..06da7adb 100644 --- a/skimage/io/_io.py +++ b/skimage/io/_io.py @@ -193,7 +193,7 @@ def show(): >>> import skimage.io as io >>> for i in range(4): - ... io.imshow(np.random.rand(50, 50)) # doctest: +SKIP + ... io.imshow(np.random.rand(50, 50)) >>> io.show() # doctest: +SKIP ''' diff --git a/skimage/measure/_ccomp.pyx b/skimage/measure/_ccomp.pyx index ac75601a..56b31a9f 100644 --- a/skimage/measure/_ccomp.pyx +++ b/skimage/measure/_ccomp.pyx @@ -430,7 +430,7 @@ def label(input, neighbors=None, background=None, return_num=False, ... [1, 1, 5], ... [0, 0, 0]]) - >>> print(label(x, background=0)) # doctest: +SKIP + >>> print(label(x, background=0)) [[ 0 -1 -1] [ 0 0 1] [-1 -1 -1]] diff --git a/skimage/measure/fit.py b/skimage/measure/fit.py index b4799a0b..7de90291 100644 --- a/skimage/measure/fit.py +++ b/skimage/measure/fit.py @@ -619,10 +619,10 @@ def ransac(data, model_class, min_samples, residual_threshold, Estimate ellipse model using RANSAC: - >>> ransac_model, inliers = ransac(data, EllipseModel, 5, 3, max_trials=50) # doctest: +SKIP - >>> ransac_model.params # doctest: +SKIP + >>> ransac_model, inliers = ransac(data, EllipseModel, 5, 3, max_trials=50) + >>> ransac_model.params array([ 20.12762373, 29.73563063, 4.81499637, 10.4743584 , 0.05217117]) - >>> inliers # doctest: +SKIP + >>> inliers array([False, False, False, False, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, diff --git a/skimage/morphology/watershed.py b/skimage/morphology/watershed.py index 783e69b3..5e4a9156 100644 --- a/skimage/morphology/watershed.py +++ b/skimage/morphology/watershed.py @@ -119,9 +119,9 @@ def watershed(image, markers, connectivity=None, offset=None, mask=None): >>> from skimage.feature import peak_local_max >>> local_maxi = peak_local_max(distance, labels=image, ... footprint=np.ones((3, 3)), - ... indices=False) # doctest: +SKIP - >>> markers = ndimage.label(local_maxi)[0] # doctest: +SKIP - >>> labels = watershed(-distance, markers, mask=image) # doctest: +SKIP + ... indices=False) + >>> markers = ndimage.label(local_maxi)[0] + >>> labels = watershed(-distance, markers, mask=image) The algorithm works also for 3-D images, and can be used for example to separate overlapping spheres. diff --git a/skimage/transform/hough_transform.py b/skimage/transform/hough_transform.py index 167a4da5..cbb4caa6 100644 --- a/skimage/transform/hough_transform.py +++ b/skimage/transform/hough_transform.py @@ -49,8 +49,8 @@ def hough_line_peaks(hspace, angles, dists, min_distance=9, min_angle=10, >>> rr, cc = line(0, 14, 14, 0) >>> img[cc, rr] = 1 >>> hspace, angles, dists = hough_line(img) - >>> hspace, angles, dists = hough_line_peaks(hspace, angles, dists) # doctest: +SKIP - >>> len(angles) # doctest: +SKIP + >>> hspace, angles, dists = hough_line_peaks(hspace, angles, dists) + >>> len(angles) 2 """ diff --git a/skimage/util/tests/test_dtype.py b/skimage/util/tests/test_dtype.py index 4c1f2a43..612c43e6 100644 --- a/skimage/util/tests/test_dtype.py +++ b/skimage/util/tests/test_dtype.py @@ -30,12 +30,8 @@ def test_range(): (img_as_uint, np.uint16), (img_as_ubyte, np.ubyte)]: - try: - with expected_warnings(['precision loss|sign loss']): - y = f(x) - except ValueError as e: - if not 'No warning raised' in str(e): - raise + with expected_warnings(['precision loss|sign loss|\A\Z']): + y = f(x) omin, omax = dtype_range[dt] @@ -67,12 +63,8 @@ def test_range_extra_dtypes(): imin, imax = dtype_range_extra[dtype_in] x = np.linspace(imin, imax, 10).astype(dtype_in) - try: - with expected_warnings(['precision loss|sign loss']): - y = convert(x, dt) - except ValueError as e: - if not 'No warning raised' in str(e): - raise + with expected_warnings(['precision loss|sign loss|\A\Z']): + y = convert(x, dt) omin, omax = dtype_range_extra[dt] yield (_verify_range,