def test_detect_corners(self): path = PointSet(3) path.append(10, 2, 0) path.append(11, 3, 0) path.append(12, 4, 0) path.append(13, 5, 0) path.append(14, 6, 0) path.append(15, 7, 0) # top path.append(16, 6, 0) path.append(17, 5, 0) path.append(18, 4, 0) path.append(19, 3, 0) path.append(20, 2, 0) # bottom path.append(21, 3, 0) path.append(22, 4, 0) path.append(23, 5, 0) path.append(24, 6, 0) path.append(25, 7, 0) # top path.append(26, 6, 0) path.append(27, 5, 0) path.append(28, 4, 0) path.append(29, 3, 0) path0 = path for i in range(3): path = path0.copy() path[:, 2] = path[:, i] path[:, i] = 0 # Test that _detect_corners detects the indices correctly I = _detect_corners(path, smoothFactor=1) assert I == [5, 10, 15] # Test that _add_corner_to_edge constructs the graph and splits # the path in the correct way graph = StentGraph() n1, n5 = tuple(path[0].flat), tuple(path[-1].flat) n2, n3, n4 = tuple(path[5].flat), tuple(path[10].flat), tuple( path[15].flat) graph.add_edge(n1, n5, path=path, cost=0, ctvalue=0) _add_corner_to_edge(graph, n1, n5, smoothFactor=1) assert graph.number_of_nodes() == 5 assert graph.number_of_edges() == 4 for n in [n1, n2, n3, n4, n5]: assert n in graph.nodes() path12, path23, path34, path45 = path[0:6], path[5:11], path[ 10:16], path[15:20] if n1 > n2: path12 = np.flipud(path12) if n2 > n3: path23 = np.flipud(path23) if n3 > n4: path34 = np.flipud(path34) if n4 > n5: path45 = np.flipud(path45) assert np.all(graph.edge[n1][n2]['path'] == path12) assert np.all(graph.edge[n2][n3]['path'] == path23) assert np.all(graph.edge[n3][n4]['path'] == path34) assert np.all(graph.edge[n4][n5]['path'] == path45)
def test_pop_nodes(self): # Create dummy paths path1 = PointSet(2) path1.append(1, 11) path1.append(1, 12) # create 4 nodes (6-7-8-9), remove 8 graph = StentGraph() graph.add_edge(6, 7, cost=4, ctvalue=70, path=path1) graph.add_edge(7, 8, cost=2, ctvalue=50, path=path1) graph.add_edge(8, 9, cost=3, ctvalue=60, path=path1) graph0 = graph.copy() # Pop straight line graph = graph0.copy() pop_nodes(graph) assert graph.number_of_nodes() == 2 assert graph.number_of_edges() == 1 assert graph.edge[6][9]['path'].shape[0] == 3 + 1 # Pop cycle graph = graph0.copy() graph.add_edge(9, 6, cost=3, ctvalue=60, path=path1) pop_nodes(graph) assert graph.number_of_nodes() == 1 assert graph.number_of_edges() == 1 n = graph.nodes()[0] assert graph.edge[n][n]['path'].shape[0] == 4 + 1 + 1 # cycle # arbitrary what node stayed around # Pop with one side branch popping graph = graph0.copy() graph.add_edge(7, 2, cost=3, ctvalue=60, path=path1) pop_nodes(graph) assert graph.number_of_nodes() == 4 assert graph.number_of_edges() == 3 assert graph.edge[7][9]['path'].shape[0] == 2 + 1 # Pop with one prevent popping graph = graph0.copy() graph.node[7]['nopop'] = True pop_nodes(graph) assert graph.number_of_nodes() == 3 assert graph.number_of_edges() == 2 assert graph.edge[7][9]['path'].shape[0] == 2 + 1
def test_pop_node(self): # Create paths path1 = PointSet(2) path1.append(1, 11) path1.append(1, 12) path2 = PointSet(2) path2.append(1, 12) path2.append(1, 13) # path12 = PointSet(2) path12.append(1, 11) path12.append(1, 12) path12.append(1, 13) # create 4 nodes (6-7-8-9), remove 8 graph = StentGraph() graph.add_edge(6, 7, cost=4, ctvalue=70) graph.add_edge(7, 8, cost=2, ctvalue=50, path=path1) graph.add_edge(8, 9, cost=3, ctvalue=60, path=path2) # Pop _pop_node(graph, 8) # Check assert graph.number_of_nodes() == 3 assert 8 not in graph.nodes() assert graph.edge[7][9]['ctvalue'] == 50 assert graph.edge[7][9]['cost'] == 5 assert np.all(graph.edge[7][9]['path'] == path12) # create 4 nodes (6-8-7-9), remove 7 graph = StentGraph() graph.add_edge(6, 8, cost=4, ctvalue=70) graph.add_edge(8, 7, cost=2, ctvalue=50, path=np.flipud(path1)) graph.add_edge(7, 9, cost=3, ctvalue=60, path=path2) # Pop _pop_node(graph, 7) # Check assert graph.number_of_nodes() == 3 assert 7 not in graph.nodes() assert graph.edge[8][9]['ctvalue'] == 50 assert graph.edge[8][9]['cost'] == 5 assert np.all(graph.edge[8][9]['path'] == path12) # create 4 nodes (7-8-6-9), remove 8 graph = StentGraph() graph.add_edge(7, 8, cost=4, ctvalue=70, path=np.flipud(path2)) graph.add_edge(8, 6, cost=2, ctvalue=50, path=path1) graph.add_edge(6, 9, cost=3, ctvalue=60) # Pop _pop_node(graph, 8) # Check assert graph.number_of_nodes() == 3 assert 8 not in graph.nodes() assert graph.edge[6][7]['ctvalue'] == 50 assert graph.edge[6][7]['cost'] == 6 assert np.all(graph.edge[6][7]['path'] == path12) # create 3 nodes in a cycle. It should remove all but one graph = StentGraph() graph.add_edge(7, 8, cost=4, ctvalue=70, path=path1) graph.add_edge(8, 9, cost=2, ctvalue=50, path=path2) graph.add_edge(9, 7, cost=3, ctvalue=60, path=path2) # Pop _pop_node(graph, 8) # Check assert graph.number_of_nodes() == 1 assert graph.number_of_edges() == 1 assert 8 not in graph.nodes() n = graph.nodes()[0] assert len(graph.edge[n][n]['path']) == 6 - 1 # create 3 nodes in a cycle, with one subbranch graph = StentGraph() graph.add_edge(7, 8, cost=4, ctvalue=70, path=path1) graph.add_edge(8, 9, cost=2, ctvalue=50, path=path2) graph.add_edge(9, 7, cost=3, ctvalue=60, path=path2) graph.add_edge(7, 4, cost=3, ctvalue=60, path=path2) # Pop _pop_node(graph, 8) # Check assert graph.number_of_nodes() == 2 assert graph.number_of_edges() == 2 assert 8 not in graph.nodes() assert len(graph.edge[7][7]['path']) == 6 - 1