Skip to content

Commit 5665944

Browse files
author
Stefan Hahmann
committed
React quicker on cancel operation
1 parent 0dca4d6 commit 5665944

5 files changed

Lines changed: 35 additions & 8 deletions

File tree

src/main/java/org/mastodon/mamut/detection/DeepLearningDetector.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import net.imglib2.view.IntervalView;
4242
import net.imglib2.view.Views;
4343

44+
import org.mastodon.mamut.util.ApposeProcess;
4445
import org.mastodon.mamut.util.ByteFormatter;
4546
import org.mastodon.mamut.detection.util.SpimImageProperties;
4647
import org.mastodon.mamut.model.ModelGraph;
@@ -82,6 +83,8 @@ public abstract class DeepLearningDetector extends AbstractSpotDetectorOp
8283
*/
8384
private static final int MAX_SIZE_IN_BYTES = Integer.MAX_VALUE - 1;
8485

86+
protected ApposeProcess apposeProcess;
87+
8588
@Override
8689
public void compute( final List< SourceAndConverter< ? > > sources, final ModelGraph graph )
8790
{
@@ -310,6 +313,15 @@ public void cancel( final String reason )
310313
{
311314
// this is a workaround to avoid a null pointer exception during the cancel operation
312315
detector = new DummyDetectorOp();
316+
try
317+
{
318+
if ( apposeProcess != null )
319+
apposeProcess.cancel();
320+
}
321+
catch ( InterruptedException e )
322+
{
323+
logger.info( "Interrupted while cancelling detector: {}", e.getMessage() );
324+
}
313325
super.cancel( reason );
314326
}
315327

src/main/java/org/mastodon/mamut/detection/cellpose/Cellpose3Detector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ && checkParameter( settings, KEY_GPU_ID, Integer.class, errorHolder )
103103
{
104104
try (Cellpose3 cellpose = new Cellpose3( ( Cellpose3.ModelType ) settings.get( KEY_MODEL_TYPE ) ))
105105
{
106+
this.apposeProcess = cellpose;
106107
boolean is3D = is3D( image );
107108
cellpose.set3D( is3D );
108109
cellpose.setCellProbThreshold( ( double ) settings.get( KEY_CELL_PROBABILITY_THRESHOLD ) );

src/main/java/org/mastodon/mamut/detection/cellpose/Cellpose4Detector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ && checkParameter( settings, KEY_GPU_ID, Integer.class, errorHolder )
9494
{
9595
try (Cellpose4 cellpose = new Cellpose4())
9696
{
97+
this.apposeProcess = cellpose;
9798
cellpose.set3D( is3D( image ) );
9899
cellpose.setCellProbThreshold( ( double ) settings.get( KEY_CELL_PROBABILITY_THRESHOLD ) );
99100
cellpose.setFlowThreshold( ( double ) settings.get( KEY_FLOW_THRESHOLD ) );

src/main/java/org/mastodon/mamut/detection/stardist/StarDistDetector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ protected boolean validateSettings( StringBuilder errorHolder )
105105
{
106106
try (StarDist starDist = new StarDist( ( StarDist.ModelType ) settings.get( KEY_MODEL_TYPE ) ))
107107
{
108+
this.apposeProcess = starDist;
108109
boolean isData3D = is3D( image );
109110
Boolean isModelType2D = starDist.getModelType().is2D();
110111
if ( isModelType2D != null )

src/main/java/org/mastodon/mamut/util/ApposeProcess.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.function.Consumer;
1616

1717
import org.apache.commons.lang3.time.StopWatch;
18+
import org.apposed.appose.Builder;
1819
import org.apposed.appose.Environment;
1920
import org.apposed.appose.Service;
2021
import org.apposed.appose.TaskEvent;
@@ -46,6 +47,8 @@ public abstract class ApposeProcess implements AutoCloseable
4647

4748
protected final Map< String, Object > inputs;
4849

50+
protected Service.Task currentTask;
51+
4952
protected ApposeProcess() throws IOException
5053
{
5154
this.stopWatch = StopWatch.createStarted();
@@ -101,8 +104,9 @@ private Environment setUpEnv()
101104
writer.write( content );
102105
}
103106
logEnvFile( envFile );
104-
environment = org.apposed.appose.Appose.file( envFile, "environment.yml" )
105-
.subscribeProgress( ( title, cur, max ) -> logger.info( "{}: {}/{}", title, cur, max ) )
107+
Builder envBuilder = org.apposed.appose.Appose.file( envFile, "environment.yml" );
108+
environment =
109+
envBuilder.subscribeProgress( ( title, cur, max ) -> logger.info( "{}: {}/{}", title, cur, max ) )
106110
.subscribeOutput( msg -> {
107111
if ( !msg.isEmpty() )
108112
logger.info( msg );
@@ -139,6 +143,9 @@ protected Consumer< TaskEvent > getTaskListener( final StopWatch stopWatch, fina
139143
case FAILURE:
140144
logger.error( "Task failed with error: {}. Time elapsed: {}", task.error, stopWatch.formatSplitTime() );
141145
break;
146+
case CRASH:
147+
logger.error( "Task crashed with error. Error: {}. Time elapsed: {}", task.error, stopWatch.formatSplitTime() );
148+
break;
142149
default:
143150
logger.warn( "Unhandled task event: {}.", taskEvent.responseType );
144151
break;
@@ -201,21 +208,26 @@ protected Service.Task runScript() throws IOException
201208
}
202209

203210
String script = generateScript();
204-
Service.Task task = pythonWorker.task( script, inputs, null );
211+
currentTask = pythonWorker.task( script, inputs, null );
205212
stopWatch.split();
206213
if ( logger.isInfoEnabled() )
207214
logger.info( "Created python task. Time elapsed: {}", stopWatch.formatSplitTime() );
208-
task.listen( getTaskListener( stopWatch, task ) );
209-
if ( isPythonTaskInterrupted( task ) )
215+
currentTask.listen( getTaskListener( stopWatch, currentTask ) );
216+
if ( isPythonTaskInterrupted( currentTask ) )
210217
return null;
211218

212219
// Verify that it worked.
213-
if ( task.status != Service.TaskStatus.COMPLETE )
214-
throw new org.mastodon.mamut.detection.PythonRuntimeException( "Python task failed with error: " + task.error );
220+
if ( currentTask.status != Service.TaskStatus.COMPLETE )
221+
throw new PythonRuntimeException( "Python task failed with error: " + currentTask.error );
215222

216223
stopWatch.split();
217224
if ( logger.isInfoEnabled() )
218225
logger.info( "Python task completed. Total time {}", stopWatch.formatSplitTime() );
219-
return task;
226+
return currentTask;
227+
}
228+
229+
public void cancel() throws InterruptedException
230+
{
231+
pythonWorker.kill();
220232
}
221233
}

0 commit comments

Comments
 (0)