FIX: Shortcut output or catch trivial cases in random_walker

Also:
* New tests to cover these new checks
* Improvements to docstrings and user warnings
* Generalize handling of `sampling` in accordance with docstring
* Some extra whitespace to improve readability
This commit is contained in:
Josh Warner (Mac)
2014-04-18 18:47:20 -05:00
parent 7161175e43
commit 7847287f46
2 changed files with 95 additions and 27 deletions
@@ -255,6 +255,48 @@ def test_spacing_1():
assert (labels_aniso2[26:34, 13:17, 13:17] == 2).all()
def test_trivial_cases():
# When all voxels are labeled
img = np.ones((10, 10))
labels = np.ones((10, 10))
pass_through = random_walker(img, labels)
np.testing.assert_array_equal(pass_through, labels)
# When all voxels are labeled AND return_full_prob is True
labels[:, :5] = 3
expected = np.concatenate(((labels == 1)[..., np.newaxis],
(labels == 3)[..., np.newaxis]), axis=2)
test = random_walker(img, labels, return_full_prob=True)
np.testing.assert_array_equal(test, expected)
def test_bad_inputs():
# Too few dimensions
img = np.ones(10)
labels = np.arange(10)
np.testing.assert_raises(ValueError, random_walker, img, labels)
np.testing.assert_raises(ValueError,
random_walker, img, labels, multichannel=True)
# Too many dimensions
img = np.random.normal(size=(3, 3, 3, 3, 3))
labels = np.arange(3 ** 5).reshape(img.shape)
np.testing.assert_raises(ValueError, random_walker, img, labels)
np.testing.assert_raises(ValueError,
random_walker, img, labels, multichannel=True)
# Spacing incorrect length
img = np.random.normal(size=(10, 10))
labels = np.zeros((10, 10))
labels[2, 4] = 2
labels[6, 8] = 5
np.testing.assert_raises(ValueError,
random_walker, img, labels, spacing=(1,))
# Spacing contains unacceptable information
np.testing.assert_raises(
ValueError, random_walker, img, labels, spacing=(1, 'chickens'))
if __name__ == '__main__':
from numpy import testing
testing.run_module_suite()
np.testing.run_module_suite()