Skip to content

Commit

Permalink
The detection preview is cancelable.
Browse files Browse the repository at this point in the history
Provided that the detector that is called is cancelable.
  • Loading branch information
tinevez committed Mar 28, 2024
1 parent f513366 commit 3ea93bf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 30 deletions.
77 changes: 49 additions & 28 deletions src/main/java/fiji/plugin/trackmate/util/DetectionPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class DetectionPreview

private final DetectionPreviewPanel panel;

private TrackMate trackmate;

protected DetectionPreview(
final Model model,
final Settings settings,
Expand All @@ -66,6 +68,7 @@ protected DetectionPreview(
detectionSettingsSupplier.get(),
currentFrameSupplier.get(),
thresholdKey ) );
panel.btnCancel.addActionListener( l -> cancel() );
}

public DetectionPreviewPanel getPanel()
Expand All @@ -86,7 +89,9 @@ protected void preview(
final int frame,
final String thresholdKey )
{
panel.btnPreview.setEnabled( false );
panel.btnPreview.setVisible( false );
panel.btnCancel.setVisible( true );
panel.btnCancel.setEnabled( true );
Threads.run( "TrackMate preview detection thread", () ->
{
try
Expand Down Expand Up @@ -115,7 +120,8 @@ protected void preview(
}
finally
{
panel.btnPreview.setEnabled( true );
panel.btnPreview.setVisible( true );
panel.btnCancel.setVisible( false );
}
} );
}
Expand Down Expand Up @@ -163,38 +169,53 @@ protected Pair< Model, Double > runPreviewDetection(
lSettings.detectorFactory = detectorFactory;
lSettings.detectorSettings = new HashMap<>( detectorSettings );

// Does this detector have a THRESHOLD parameter?
final boolean hasThreshold = ( thresholdKey != null ) && ( detectorSettings.containsKey( thresholdKey ) );
final double threshold;
if ( hasThreshold )
{
threshold = ( ( Double ) detectorSettings.get( thresholdKey ) ).doubleValue();
lSettings.detectorSettings.put( thresholdKey, Double.valueOf( Double.NEGATIVE_INFINITY ) );
}
else
this.trackmate = new TrackMate( lSettings );

try
{
threshold = Double.NaN;
}
// Does this detector have a THRESHOLD parameter?
final boolean hasThreshold = ( thresholdKey != null ) && ( detectorSettings.containsKey( thresholdKey ) );
final double threshold;
if ( hasThreshold )
{
threshold = ( ( Double ) detectorSettings.get( thresholdKey ) ).doubleValue();
lSettings.detectorSettings.put( thresholdKey, Double.valueOf( Double.NEGATIVE_INFINITY ) );
}
else
{
threshold = Double.NaN;
}

// Execute preview.
final TrackMate trackmate = new TrackMate( lSettings );
trackmate.getModel().setLogger( panel.logger );
// Execute preview.
trackmate.getModel().setLogger( panel.logger );

final boolean detectionOk = trackmate.execDetection();
if ( !detectionOk )
final boolean detectionOk = trackmate.execDetection();
if ( !detectionOk )
{
panel.logger.error( trackmate.getErrorMessage() );
return null;
}

if ( hasThreshold )
// Filter by the initial threshold value.
trackmate.getModel().getSpots().filter( new FeatureFilter( Spot.QUALITY, threshold, true ) );
else
// Make them all visible.
trackmate.getModel().getSpots().setVisible( true );

return new ValuePair< Model, Double >( trackmate.getModel(), Double.valueOf( threshold ) );
}
finally
{
panel.logger.error( trackmate.getErrorMessage() );
return null;
this.trackmate = null;
}
}

if ( hasThreshold )
// Filter by the initial threshold value.
trackmate.getModel().getSpots().filter( new FeatureFilter( Spot.QUALITY, threshold, true ) );
else
// Make them all visible.
trackmate.getModel().getSpots().setVisible( true );

return new ValuePair< Model, Double >( trackmate.getModel(), Double.valueOf( threshold ) );
private void cancel()
{
panel.btnCancel.setEnabled( false );
if ( null != trackmate )
trackmate.cancel( "User canceled preview" );
}

protected void updateModelAndHistogram( final Model targetModel, final Model sourceModel, final int frame, final double threshold )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package fiji.plugin.trackmate.util;

import static fiji.plugin.trackmate.gui.Fonts.SMALL_FONT;
import static fiji.plugin.trackmate.gui.Icons.CANCEL_ICON;
import static fiji.plugin.trackmate.gui.Icons.PREVIEW_ICON;

import java.awt.GridBagConstraints;
Expand All @@ -48,12 +49,19 @@ public class DetectionPreviewPanel extends JPanel
+ "get rid of them later."
+ "</html>";

private static final String TOOLTIP_CANCEL = "<html>"
+ "Cancel the current preview."
+ "</html>";

final Logger logger;

final JButton btnPreview;

final JButton btnCancel;

final QualityHistogramChart chart;


public DetectionPreviewPanel( final DoubleConsumer thresholdUpdater, final String axisLabel )
{
final GridBagLayout gridBagLayout = new GridBagLayout();
Expand Down Expand Up @@ -84,12 +92,21 @@ public DetectionPreviewPanel( final DoubleConsumer thresholdUpdater, final Strin

this.btnPreview = new JButton( "Preview", PREVIEW_ICON );
btnPreview.setToolTipText( TOOLTIP_PREVIEW );
btnPreview.setFont( SMALL_FONT );
this.btnCancel = new JButton( "Cancel", CANCEL_ICON );
btnCancel.setToolTipText( TOOLTIP_CANCEL );
btnCancel.setVisible( false );
btnCancel.setFont( SMALL_FONT );

final JPanel btnPanel = new JPanel();
btnPanel.add( btnPreview );
btnPanel.add( btnCancel );

final GridBagConstraints gbcBtnPreview = new GridBagConstraints();
gbcBtnPreview.anchor = GridBagConstraints.NORTHEAST;
gbcBtnPreview.insets = new Insets( 5, 5, 0, 0 );
gbcBtnPreview.gridx = 1;
gbcBtnPreview.gridy = 1;
this.add( btnPreview, gbcBtnPreview );
btnPreview.setFont( SMALL_FONT );
this.add( btnPanel, gbcBtnPreview );
}
}

0 comments on commit 3ea93bf

Please sign in to comment.