diff --git a/skimage/graph/tests/test_anisotropy.py b/skimage/graph/tests/test_anisotropy.py new file mode 100644 index 00000000..a2d3a240 --- /dev/null +++ b/skimage/graph/tests/test_anisotropy.py @@ -0,0 +1,53 @@ +import skimage.graph.mcp as mcp +from numpy.testing import (assert_array_equal, + assert_almost_equal, + ) + +import numpy as np + +a = np.ones((8, 8), dtype=np.float32) + + +horizontal_ramp = np.array( [[ 0., 1., 2., 3., 4., 5., 6., 7.,], + [ 0., 1., 2., 3., 4., 5., 6., 7.,], + [ 0., 1., 2., 3., 4., 5., 6., 7.,], + [ 0., 1., 2., 3., 4., 5., 6., 7.,], + [ 0., 1., 2., 3., 4., 5., 6., 7.,], + [ 0., 1., 2., 3., 4., 5., 6., 7.,], + [ 0., 1., 2., 3., 4., 5., 6., 7.,], + [ 0., 1., 2., 3., 4., 5., 6., 7.,]]) + +vertical_ramp = np.array( [[ 0., 0., 0., 0., 0., 0., 0., 0.,], + [ 1., 1., 1., 1., 1., 1., 1., 1.,], + [ 2., 2., 2., 2., 2., 2., 2., 2.,], + [ 3., 3., 3., 3., 3., 3., 3., 3.,], + [ 4., 4., 4., 4., 4., 4., 4., 4.,], + [ 5., 5., 5., 5., 5., 5., 5., 5.,], + [ 6., 6., 6., 6., 6., 6., 6., 6.,], + [ 7., 7., 7., 7., 7., 7., 7., 7.,]]) + + +def test_anisotropy(): + + # Create seeds; vertical seeds create a horizonral ramp + seeds_for_horizontal = [(i,0) for i in range(8) ] + seeds_for_vertcal = [(0,i) for i in range(8) ] + + + for sy in range(1, 5): + for sx in range(1,5): + sampling = sy, sx + # Trace horizontally + m1 = mcp.MCP_Geometric(a, sampling=sampling, fully_connected=True) + costs1, traceback = m1.find_costs(seeds_for_horizontal) + # Trace vertically + m2 = mcp.MCP_Geometric(a, sampling=sampling, fully_connected=True) + costs2, traceback = m2.find_costs(seeds_for_vertcal) + + # Check + assert_array_equal(costs1, horizontal_ramp * sx) + assert_array_equal(costs2, vertical_ramp * sy) + + +if __name__ == "__main__": + np.testing.run_module_suite() diff --git a/skimage/graph/tests/test_connect.py b/skimage/graph/tests/test_connect.py new file mode 100644 index 00000000..619ac3b4 --- /dev/null +++ b/skimage/graph/tests/test_connect.py @@ -0,0 +1,83 @@ +import skimage.graph.mcp as mcp +# import stentseg.graph._mcp as mcp +from numpy.testing import (assert_array_equal, + assert_almost_equal, + ) + +import numpy as np + + +a = np.ones((8, 8), dtype=np.float32) + +count = 0 +class MCP(mcp.MCP_Connect): + + def _reset(self): + """ Reset the id map. + """ + mcp.MCP_Connect._reset(self) + self._conn = {} + self._bestconn = {} + + + def create_connection(self, id1, id2, pos1, pos2, cost1, cost2): + # Process data + hash = min(id1, id2), max(id1, id2) + val = min(pos1, pos2), max(pos1, pos2) + cost = min(cost1, cost2) + # Add to total list + self._conn.setdefault(hash, []).append(val) + # Keep track of connection with lowest cost + curcost = self._bestconn.get(hash, (np.inf,))[0] + if cost < curcost: + self._bestconn[hash] = (cost,) + val + + +def test_connections(): + + # Create MCP object with three seed points + mcp = MCP(a) + costs, traceback = mcp.find_costs([ (1,1), (7,7), (1,7) ]) + + # Test that all three seed points are connected + connections = set(mcp._conn.keys()) + assert (0, 1) in connections + assert (1, 2) in connections + assert (0, 2) in connections + + # Test that any two neighbors have only been connected once + for position_tuples in mcp._conn.values(): + n1 = len(position_tuples) + n2 = len(set(position_tuples)) + assert n1 == n2 + + # For seed 0 and 1 + cost, pos1, pos2 = mcp._bestconn[(0,1)] + # Test meeting points + assert (pos1, pos2) == ( (3,3), (4,4) ) + # Test the whole path + path = mcp.traceback(pos1) + list(reversed(mcp.traceback(pos2))) + assert_array_equal(path, + [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)]) + + # For seed 1 and 2 + cost, pos1, pos2 = mcp._bestconn[(1,2)] + # Test meeting points + assert (pos1, pos2) == ( (3,7), (4,7) ) + # Test the whole path + path = mcp.traceback(pos1) + list(reversed(mcp.traceback(pos2))) + assert_array_equal(path, + [(1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7)]) + + # For seed 0 and 2 + cost, pos1, pos2 = mcp._bestconn[(0,2)] + # Test meeting points + assert (pos1, pos2) == ( (1,3), (1,4) ) + # Test the whole path + path = mcp.traceback(pos1) + list(reversed(mcp.traceback(pos2))) + assert_array_equal(path, + [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7)]) + + +if __name__ == "__main__": + np.testing.run_module_suite() \ No newline at end of file diff --git a/skimage/graph/tests/test_mcp.py b/skimage/graph/tests/test_mcp.py index 560f19d0..5c44abad 100644 --- a/skimage/graph/tests/test_mcp.py +++ b/skimage/graph/tests/test_mcp.py @@ -116,8 +116,8 @@ def test_offsets(): m = mcp.MCP(a, offsets=offsets) costs, traceback = m.find_costs([(1, 6)]) assert_array_equal(traceback, - [[-1, -1, -1, -1, -1, -1, -1, -1], - [-1, -1, -1, -1, -1, -1, -1, -1], + [[-2, -2, -2, -2, -2, -2, -2, -2], + [-2, -2, -2, -2, -2, -2, -1, -2], [15, 14, 13, 12, 11, 10, 0, 1], [10, 0, 1, 2, 3, 4, 5, 6], [10, 0, 1, 2, 3, 4, 5, 6], @@ -151,5 +151,6 @@ def _test_random(shape): return a, costs, offsets + if __name__ == "__main__": np.testing.run_module_suite()