diff --git a/scikits/image/graph/tests/test_trace_path.py b/scikits/image/graph/tests/test_trace_path.py new file mode 100644 index 00000000..a10770cb --- /dev/null +++ b/scikits/image/graph/tests/test_trace_path.py @@ -0,0 +1,71 @@ +import numpy as np +from numpy.testing import * + +from scikits.image.graph import trace_path + +a = np.ones((8,8), dtype=np.float32) +a[1:-1, 1] = 0 +a[1, 1:-1] = 0 + +## array([[ 1., 1., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 0., 0., 0., 0., 0., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 0., 1., 1., 1., 1., 1., 1.], +## [ 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32) + +def test_basic(): + costs, return_path = trace_path(a, (1, 6), [(7, 2)]) + assert_array_equal(costs, + [[ 1., 1., 1., 1., 1., 1., 1., 1.], + [ 1., 0., 0., 0., 0., 0., 0., 1.], + [ 1., 0., 1., 1., 1., 1., 1., 1.], + [ 1., 0., 1., 2., 2., 2., 2., 2.], + [ 1., 0., 1., 2., 3., 3., 3., 3.], + [ 1., 0., 1., 2., 3., 4., 4., 4.], + [ 1., 0., 1., 2., 3., 4., 5., 5.], + [ 1., 1., 1., 2., 3., 4., 5., 6.]]) + + assert_array_equal(return_path, + [[(1, 6), + (1, 5), + (1, 4), + (1, 3), + (1, 2), + (2, 1), + (3, 1), + (4, 1), + (5, 1), + (6, 1), + (7, 2)]]) + +def test_no_diagonal(): + costs, path = trace_path(a, (1, 6), [(7, 2)], diagonal_steps=False) + assert_array_equal(costs, + [[ 2., 1., 1., 1., 1., 1., 1., 2.], + [ 1., 0., 0., 0., 0., 0., 0., 1.], + [ 1., 0., 1., 1., 1., 1., 1., 2.], + [ 1., 0., 1., 2., 2., 2., 2., 3.], + [ 1., 0., 1., 2., 3., 3., 3., 4.], + [ 1., 0., 1., 2., 3., 4., 4., 5.], + [ 1., 0., 1., 2., 3., 4., 5., 6.], + [ 2., 1., 2., 3., 4., 5., 6., 7.]]) + assert_array_equal(path, + [[(1, 6), + (1, 5), + (1, 4), + (1, 3), + (1, 2), + (1, 1), + (2, 1), + (3, 1), + (4, 1), + (5, 1), + (6, 1), + (6, 2), + (7, 2)]]) + +if __name__ == "__main__": + run_module_suite()