Files
scikit-image/scikits/image/graph/tests/test_trace_path.py
T
Stefan van der Walt dc6715bc08 trace_path: Add tests.
2009-11-22 13:20:41 +02:00

72 lines
2.7 KiB
Python

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()