Skip to content

Commit f56c92a

Browse files
author
Stefan Hahmann
committed
Fix stack overflow in case of loops in the graph
In case of loops in the graph, more precisely, in the situation, when the last spot of a branch has an outgoing edge to itself, label spots systematically got to a stack overflow situation due to a recursion that does not finish. This commit adds conditions to leave the recursion and extends the unit test to proof, the fix is working
1 parent 6821de5 commit f56c92a

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

src/main/java/org/mastodon/mamut/tomancak/label_systematically/LabelSpotsSystematically.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ private static void renameDescendants( ModelGraph graph, Predicate<Spot> filter,
7878
Spot child1 = edges.next().getTarget(ref2);
7979
Spot child2 = edges.next().getTarget(ref3);
8080
boolean b = correctOrder.test( branchEnd );
81-
renameDescendants(graph, filter, correctOrder, spot, child1, b );
82-
renameDescendants(graph, filter, correctOrder, spot, child2, !b );
81+
if ( !branchEnd.equals( child1 ) ) // NB: edge linking to itself. this normally should not happen but occurs in real data.
82+
renameDescendants( graph, filter, correctOrder, spot, child1, b );
83+
if ( !branchEnd.equals( child2 ) ) // NB: edge linking to itself. this normally should not happen but occurs in real data.
84+
renameDescendants( graph, filter, correctOrder, spot, child2, !b );
8385
}
8486
finally
8587
{

src/test/java/org/mastodon/mamut/tomancak/label_systematically/LabelSpotsSystematicallyTest.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,33 @@ private double[] array( double... values )
8383
}
8484

8585
@Test
86-
public void testRenameCellsAsExternIntern() {
86+
public void testRenameCellsAsExternIntern()
87+
{
8788
ModelGraph graph = new ModelGraph();
88-
Spot a = addSpot(graph, "a", array(2, 2, 2));
89-
Spot a1 = addSpotAsDescendantOf(graph, "2", a, array(1, 2, 2)); // extern
90-
Spot a2 = addSpotAsDescendantOf(graph, "1", a, array(3, 2, 2)); // intern
91-
Spot center = addSpot(graph, "center", array(4, 2, 2));
92-
Spot b = addSpot(graph, "b", array(6, 2, 2));
93-
Spot b2 = addSpotAsDescendantOf(graph, "4", b, array(5, 2, 2)); // intern
94-
Spot b1 = addSpotAsDescendantOf(graph, "3", b, array(7, 2, 2)); // extern
95-
LabelSpotsSystematically.setLabelsBasedOnExternIntern( graph, Collections.singleton( center ), graph.vertices(), true, false);
96-
assertEquals("a1", a1.getLabel());
97-
assertEquals("a2", a2.getLabel());
98-
assertEquals("b1", b1.getLabel());
99-
assertEquals("b2", b2.getLabel());
89+
Spot a = addSpot( graph, "a", array( 2, 2, 2 ) );
90+
Spot a1 = addSpotAsDescendantOf( graph, "2", a, array( 1, 2, 2 ) ); // extern
91+
Spot a2 = addSpotAsDescendantOf( graph, "1", a, array( 3, 2, 2 ) ); // intern
92+
Spot center = addSpot( graph, "center", array( 4, 2, 2 ) );
93+
Spot b = addSpot( graph, "b", array( 6, 2, 2 ) );
94+
Spot b2 = addSpotAsDescendantOf( graph, "4", b, array( 5, 2, 2 ) ); // intern
95+
Spot b1 = addSpotAsDescendantOf( graph, "3", b, array( 7, 2, 2 ) ); // extern
96+
Spot c = addSpot( graph, "c", array( 8, 2, 2 ) );
97+
Spot c2 = addSpotAsDescendantOf( graph, "5", c, array( 9, 2, 2 ) ); // intern
98+
Spot c1 = addSpotAsDescendantOf( graph, "6", c, array( 10, 2, 2 ) ); // extern
99+
Spot c12 = addSpotAsDescendantOf( graph, "7", c1, array( 11, 2, 2 ) ); // intern
100+
Spot c11 = addSpotAsDescendantOf( graph, "8", c1, array( 12, 2, 2 ) ); // extern
101+
Spot c21 = addSpotAsDescendantOf( graph, "9", c2, array( 13, 2, 2 ) );
102+
graph.addEdge( c2, c2 ).init(); // deliberately create a cycle to test the robustness of the algorithm
103+
LabelSpotsSystematically.setLabelsBasedOnExternIntern( graph, Collections.singleton( center ), graph.vertices(), true, false );
104+
assertEquals( "a1", a1.getLabel() );
105+
assertEquals( "a2", a2.getLabel() );
106+
assertEquals( "b1", b1.getLabel() );
107+
assertEquals( "b2", b2.getLabel() );
108+
assertEquals( "c1", c1.getLabel() );
109+
assertEquals( "c2", c2.getLabel() );
110+
assertEquals( "c11", c11.getLabel() );
111+
assertEquals( "c12", c12.getLabel() );
112+
assertEquals( "c21", c21.getLabel() );
100113
}
101114

102115
// Handling:

0 commit comments

Comments
 (0)