From bea57315fbcf6d9ba289de7523366b62648b1755 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 24 Dec 2014 09:10:11 -0600 Subject: [PATCH] Update docstring and fix pyamg warning check --- skimage/_shared/_warnings.py | 13 ++++++++++--- skimage/segmentation/tests/test_random_walker.py | 13 ++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/skimage/_shared/_warnings.py b/skimage/_shared/_warnings.py index ffac12e9..a5fdd4cb 100644 --- a/skimage/_shared/_warnings.py +++ b/skimage/_shared/_warnings.py @@ -84,9 +84,16 @@ 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. You can pass an empty regex as on of the matches - to allow for no matches: "\A\Z". + pattern(s). + Raises a ValueError if any match was not found or an unexpected + warning was raised. + Allows for three types of behaviors: "and", "or", and "optional" matches. + This is done to accomodate different build enviroments or loop conditions + that may produce different warnings. The behaviors can be combined. + If you pass multiple patterns, you get an orderless "and", where all of the + warnings must be raised. + If you use the "|" operator in a pattern, you can catch one of several warnings. + Finally, you can use "|\A\Z" in a pattern to signify it as optional. """ with all_warnings() as w: diff --git a/skimage/segmentation/tests/test_random_walker.py b/skimage/segmentation/tests/test_random_walker.py index d925592c..7b250051 100644 --- a/skimage/segmentation/tests/test_random_walker.py +++ b/skimage/segmentation/tests/test_random_walker.py @@ -2,13 +2,8 @@ import numpy as np from skimage.segmentation import random_walker from skimage.transform import resize from skimage._shared._warnings import expected_warnings -from skimage._shared.version_requirements import is_installed - -if is_installed('pyamg'): - PYAMG_EXPECTED_WARNING = [] -else: - PYAMG_EXPECTED_WARNING = ['pyamg'] +PYAMG_EXPECTED_WARNING = 'pyamg|\A\Z' def make_2d_syntheticdata(lx, ly=None): @@ -99,11 +94,11 @@ def test_2d_cg_mg(): lx = 70 ly = 100 data, labels = make_2d_syntheticdata(lx, ly) - with expected_warnings('%s|\A\Z' % PYAMG_EXPECTED_WARNING): + with expected_warnings([PYAMG_EXPECTED_WARNING]): labels_cg_mg = random_walker(data, labels, beta=90, mode='cg_mg') assert (labels_cg_mg[25:45, 40:60] == 2).all() assert data.shape == labels.shape - with expected_warnings(PYAMG_EXPECTED_WARNING): + with expected_warnings([PYAMG_EXPECTED_WARNING]): full_prob = random_walker(data, labels, beta=90, mode='cg_mg', return_full_prob=True) assert (full_prob[1, 25:45, 40:60] >= @@ -118,7 +113,7 @@ def test_types(): data, labels = make_2d_syntheticdata(lx, ly) data = 255 * (data - data.min()) // (data.max() - data.min()) data = data.astype(np.uint8) - with expected_warnings(PYAMG_EXPECTED_WARNING): + with expected_warnings([PYAMG_EXPECTED_WARNING]): labels_cg_mg = random_walker(data, labels, beta=90, mode='cg_mg') assert (labels_cg_mg[25:45, 40:60] == 2).all() assert data.shape == labels.shape