mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-09 06:23:40 +08:00
Catch expected NumPy/SciPy warning over deprecated np.rank
This commit is contained in:
@@ -3,7 +3,11 @@ from skimage.segmentation import random_walker
|
||||
from skimage.transform import resize
|
||||
from skimage._shared._warnings import expected_warnings
|
||||
|
||||
# older versions of scipy raise a warning with new NumPy because they use
|
||||
# numpy.rank() instead of arr.ndim or numpy.linalg.matrix_rank.
|
||||
SCIPY_EXPECTED = 'numpy.linalg.matrix_rank|\A\Z'
|
||||
PYAMG_EXPECTED_WARNING = 'pyamg|\A\Z'
|
||||
PYAMG_SCIPY_EXPECTED = SCIPY_EXPECTED + '|' + PYAMG_EXPECTED_WARNING
|
||||
|
||||
|
||||
def make_2d_syntheticdata(lx, ly=None):
|
||||
@@ -77,11 +81,11 @@ def test_2d_cg():
|
||||
lx = 70
|
||||
ly = 100
|
||||
data, labels = make_2d_syntheticdata(lx, ly)
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
labels_cg = random_walker(data, labels, beta=90, mode='cg')
|
||||
assert (labels_cg[25:45, 40:60] == 2).all()
|
||||
assert data.shape == labels.shape
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
full_prob = random_walker(data, labels, beta=90, mode='cg',
|
||||
return_full_prob=True)
|
||||
assert (full_prob[1, 25:45, 40:60] >=
|
||||
@@ -94,7 +98,7 @@ def test_2d_cg_mg():
|
||||
lx = 70
|
||||
ly = 100
|
||||
data, labels = make_2d_syntheticdata(lx, ly)
|
||||
expected = 'scipy.sparse.sparsetools|%s' % PYAMG_EXPECTED_WARNING
|
||||
expected = 'scipy.sparse.sparsetools|%s' % PYAMG_SCIPY_EXPECTED
|
||||
with expected_warnings([expected]):
|
||||
labels_cg_mg = random_walker(data, labels, beta=90, mode='cg_mg')
|
||||
assert (labels_cg_mg[25:45, 40:60] == 2).all()
|
||||
@@ -114,7 +118,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_SCIPY_EXPECTED]):
|
||||
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
|
||||
@@ -148,7 +152,7 @@ def test_3d():
|
||||
n = 30
|
||||
lx, ly, lz = n, n, n
|
||||
data, labels = make_3d_syntheticdata(lx, ly, lz)
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
labels = random_walker(data, labels, mode='cg')
|
||||
assert (labels.reshape(data.shape)[13:17, 13:17, 13:17] == 2).all()
|
||||
assert data.shape == labels.shape
|
||||
@@ -162,7 +166,7 @@ def test_3d_inactive():
|
||||
old_labels = np.copy(labels)
|
||||
labels[5:25, 26:29, 26:29] = -1
|
||||
after_labels = np.copy(labels)
|
||||
with expected_warnings(['"cg" mode|CObject type']):
|
||||
with expected_warnings(['"cg" mode|CObject type' + '|' + SCIPY_EXPECTED]):
|
||||
labels = random_walker(data, labels, mode='cg')
|
||||
assert (labels.reshape(data.shape)[13:17, 13:17, 13:17] == 2).all()
|
||||
assert data.shape == labels.shape
|
||||
@@ -173,11 +177,11 @@ def test_multispectral_2d():
|
||||
lx, ly = 70, 100
|
||||
data, labels = make_2d_syntheticdata(lx, ly)
|
||||
data = data[..., np.newaxis].repeat(2, axis=-1) # Expect identical output
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
multi_labels = random_walker(data, labels, mode='cg',
|
||||
multichannel=True)
|
||||
assert data[..., 0].shape == labels.shape
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
single_labels = random_walker(data[..., 0], labels, mode='cg')
|
||||
assert (multi_labels.reshape(labels.shape)[25:45, 40:60] == 2).all()
|
||||
assert data[..., 0].shape == labels.shape
|
||||
@@ -189,11 +193,11 @@ def test_multispectral_3d():
|
||||
lx, ly, lz = n, n, n
|
||||
data, labels = make_3d_syntheticdata(lx, ly, lz)
|
||||
data = data[..., np.newaxis].repeat(2, axis=-1) # Expect identical output
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
multi_labels = random_walker(data, labels, mode='cg',
|
||||
multichannel=True)
|
||||
assert data[..., 0].shape == labels.shape
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
single_labels = random_walker(data[..., 0], labels, mode='cg')
|
||||
assert (multi_labels.reshape(labels.shape)[13:17, 13:17, 13:17] == 2).all()
|
||||
assert (single_labels.reshape(labels.shape)[13:17, 13:17, 13:17] == 2).all()
|
||||
@@ -220,7 +224,7 @@ def test_spacing_0():
|
||||
lz // 4 - small_l // 8] = 2
|
||||
|
||||
# Test with `spacing` kwarg
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
labels_aniso = random_walker(data_aniso, labels_aniso, mode='cg',
|
||||
spacing=(1., 1., 0.5))
|
||||
|
||||
@@ -248,7 +252,7 @@ def test_spacing_1():
|
||||
|
||||
# Test with `spacing` kwarg
|
||||
# First, anisotropic along Y
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
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()
|
||||
@@ -268,7 +272,7 @@ def test_spacing_1():
|
||||
lz // 2 - small_l // 4] = 2
|
||||
|
||||
# Anisotropic along X
|
||||
with expected_warnings(['"cg" mode']):
|
||||
with expected_warnings(['"cg" mode' + '|' + SCIPY_EXPECTED]):
|
||||
labels_aniso2 = random_walker(data_aniso,
|
||||
labels_aniso2,
|
||||
mode='cg', spacing=(2., 1., 1.))
|
||||
|
||||
Reference in New Issue
Block a user