Skip to content

Commit 9fdbea4

Browse files
Merge pull request #143 from mastodon-sc/cancel-trackastra-linking
Cancel trackastra linking on user request
2 parents ee936c9 + fb01605 commit 9fdbea4

3 files changed

Lines changed: 54 additions & 9 deletions

File tree

src/main/java/org/mastodon/mamut/linking/trackastra/TrackastraLinker.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ public class TrackastraLinker< V extends Vertex< E > & HasTimepoint & RealLocali
4646
public void mutate1( final ReadOnlyGraph< V, E > graph, final SpatioTemporalIndex< V > index )
4747
{
4848
long start = System.currentTimeMillis();
49+
statusService.clearStatus();
50+
statusService.showStatus( "Trackastra linking." );
4951
try
5052
{
5153
List< SingleTimepointRegionProps > regionProps = computeRegionProps( index );
52-
runLinkPrediction( index, regionProps );
54+
if ( !isCanceled() )
55+
runLinkPrediction( index, regionProps );
5356
ok = true;
57+
statusService.clearStatus();
5458
}
5559
catch ( TrackastraLinkingException e )
5660
{
@@ -86,7 +90,7 @@ private List< SingleTimepointRegionProps > computeRegionProps( final SpatioTempo
8690
throw new IllegalArgumentException(
8791
String.format( "Window size (%d) exceeds time range (%d). Adjust window size or time range.", windowSize, timeRange ) );
8892

89-
try (RegionPropsComputation computation = new RegionPropsComputation( logger, model ))
93+
try (RegionPropsComputation computation = new RegionPropsComputation( logger, model, this, this.statusService ))
9094
{
9195
return computation.computeRegionPropsForSource( source, level, Cast.unchecked( index ), minTimepoint, maxTimepoint );
9296
}
@@ -102,7 +106,8 @@ private void runLinkPrediction( final SpatioTemporalIndex< V > index, final List
102106
log.info( "Performing Trackastra link prediction" );
103107
try (RegionProps props = new RegionProps( regionProps );
104108
LinkPrediction prediction =
105-
new LinkPrediction( settings, Cast.unchecked( index ), Cast.unchecked( edgeCreator ), props, logger ))
109+
new LinkPrediction( settings, Cast.unchecked( index ), Cast.unchecked( edgeCreator ), props, logger, this,
110+
statusService ))
106111
{
107112
prediction.predictAndCreateLinks();
108113
}

src/main/java/org/mastodon/mamut/linking/trackastra/appose/computation/LinkPrediction.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.mastodon.mamut.util.ResourceUtils;
3434
import org.mastodon.spatial.SpatioTemporalIndex;
3535
import org.mastodon.tracking.linking.EdgeCreator;
36+
import org.scijava.Cancelable;
37+
import org.scijava.app.StatusService;
3638
import org.slf4j.Logger;
3739
import org.slf4j.LoggerFactory;
3840

@@ -49,8 +51,13 @@ public class LinkPrediction extends ApposeProcess
4951

5052
private final RegionProps regionProps;
5153

54+
private final Cancelable cancelable;
55+
56+
private final StatusService statusService;
57+
5258
public LinkPrediction( final Map< String, Object > settings, final SpatioTemporalIndex< Spot > index,
53-
final EdgeCreator< Spot > edgeCreator, final RegionProps regionProps, final org.scijava.log.Logger uiLogger
59+
final EdgeCreator< Spot > edgeCreator, final RegionProps regionProps, final org.scijava.log.Logger uiLogger,
60+
final Cancelable cancelable, final StatusService statusService
5461
) throws IOException
5562
{
5663
super();
@@ -59,6 +66,8 @@ public LinkPrediction( final Map< String, Object > settings, final SpatioTempora
5966
this.edgeCreator = edgeCreator;
6067
this.regionProps = regionProps;
6168
this.uiLogger = uiLogger;
69+
this.cancelable = cancelable;
70+
this.statusService = statusService;
6271
}
6372

6473
public void predictAndCreateLinks() throws IOException
@@ -80,7 +89,15 @@ public void predictAndCreateLinks() throws IOException
8089
inputs.put( BORDER_DIST, NDArrays.asNDArray( regionProps.borderDists ) );
8190

8291
Service.Task result = runScript();
92+
if ( cancelable.isCanceled() )
93+
{
94+
uiLogger.info( "Link prediction canceled by user.\n" );
95+
return;
96+
}
97+
uiLogger.info( "Link prediction finished. Now writing edges.\n" );
98+
statusService.showProgress( 9, 10 ); // 90% after prediction
8399
writeEdges( result );
100+
statusService.showProgress( 1, 1 ); // 100% after writing edges
84101
}
85102
finally
86103
{
@@ -107,6 +124,11 @@ private void writeEdges( final Service.Task task )
107124

108125
for ( int row = 0; row < rows; row++ )
109126
{
127+
if ( cancelable.isCanceled() )
128+
{
129+
uiLogger.info( "Link creation canceled by user.\n" );
130+
return;
131+
}
110132
int startFrame = getInt( randomAccess, 0, row );
111133
int startId = getInt( randomAccess, 1, row ) - 1; // Trackastra uses 1-based labels
112134
int endFrame = getInt( randomAccess, 2, row );

src/main/java/org/mastodon/mamut/linking/trackastra/appose/computation/RegionPropsComputation.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.text.NumberFormat;
1717
import java.util.ArrayList;
1818
import java.util.List;
19-
import java.util.function.Consumer;
2019

2120
import net.imglib2.RandomAccessibleInterval;
2221
import net.imglib2.appose.NDArrays;
@@ -38,6 +37,8 @@
3837
import org.mastodon.mamut.util.ImgUtils;
3938
import org.mastodon.mamut.util.ResourceUtils;
4039
import org.mastodon.spatial.SpatioTemporalIndex;
40+
import org.scijava.Cancelable;
41+
import org.scijava.app.StatusService;
4142
import org.slf4j.Logger;
4243
import org.slf4j.LoggerFactory;
4344

@@ -51,11 +52,19 @@ public class RegionPropsComputation extends ApposeProcess
5152

5253
private final String model;
5354

54-
public RegionPropsComputation( final org.scijava.log.Logger uiLogger, final String model ) throws IOException
55+
private final Cancelable cancelable;
56+
57+
private final StatusService statusService;
58+
59+
public RegionPropsComputation( final org.scijava.log.Logger uiLogger, final String model, final Cancelable cancelable,
60+
final StatusService statusService )
61+
throws IOException
5562
{
5663
super();
5764
this.uiLogger = uiLogger;
5865
this.model = model;
66+
this.cancelable = cancelable;
67+
this.statusService = statusService;
5968
}
6069

6170
public List< SingleTimepointRegionProps > computeRegionPropsForSource( final Source< ? > source, final int level,
@@ -70,15 +79,23 @@ public List< SingleTimepointRegionProps > computeRegionPropsForSource( final Sou
7079
List< SingleTimepointRegionProps > singleTimepointRegionProps = new ArrayList<>();
7180
for ( int timepoint = minTimepoint; timepoint <= maxTimepoint; timepoint++, done++ )
7281
{
82+
if ( cancelable.isCanceled() )
83+
{
84+
log.info( "Region props computation canceled at timepoint: {}", timepoint );
85+
uiLogger.info( "Region props computation canceled at timepoint: " + timepoint + "\n" );
86+
break;
87+
}
7388
if ( spatioTemporalIndex.getSpatialIndex( timepoint ).isEmpty() )
7489
{
7590
log.info( "No spots. Adding empty region props for timepoint: {}", timepoint );
7691
uiLogger.info( "No spots. Adding empty region props for timepoint: " + timepoint + "\n" );
7792
singleTimepointRegionProps.add( null );
78-
continue;
7993
}
80-
SingleTimepointRegionProps regionProps = computeSingleTimepointProps( source, timepoint, level, spatioTemporalIndex );
81-
singleTimepointRegionProps.add( regionProps );
94+
else
95+
{
96+
SingleTimepointRegionProps regionProps = computeSingleTimepointProps( source, timepoint, level, spatioTemporalIndex );
97+
singleTimepointRegionProps.add( regionProps );
98+
}
8299
formatProgress( done + 1, todo );
83100
}
84101
return singleTimepointRegionProps;
@@ -134,6 +151,7 @@ private SingleTimepointRegionProps computeSingleTimepointProps( final Source< ?
134151
private void formatProgress( final int done, final int todo )
135152
{
136153
double progress = ( double ) done / todo;
154+
statusService.showProgress( ( int ) ( 0.75d * done ), todo ); // reserve 25% for link prediction
137155
NumberFormat percentFormatter = NumberFormat.getPercentInstance();
138156
percentFormatter.setMinimumFractionDigits( 0 );
139157
percentFormatter.setMaximumFractionDigits( 0 );

0 commit comments

Comments
 (0)