Skip to content

Commit

Permalink
Fix another bug with the LabKit importer.
Browse files Browse the repository at this point in the history
This time it was due to confusion between a map from spot ID to spots,
and a map from label value to spot.
I fixed it by rewriting everything in terms of label values in the
labeling. So we do not need to keep track of the spot ID anymore. The
labels could be anything. They are still equal to spot ID + 1, because
it is convenient to debug.
  • Loading branch information
tinevez committed Jul 19, 2024
1 parent 8ccd3ee commit 2f9583e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
27 changes: 13 additions & 14 deletions src/main/java/fiji/plugin/trackmate/gui/editor/LabkitImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ public LabkitImporter(
* the time-point in the TrackMate model that corresponds to the
* index image.
* @param the
* map of spots (vs their ID) that were written in the previous
* index image.
* map of spots (vs the label value in the previous labeling)
* that were written in the previous index image.
*/
public void reimport(
final RandomAccessibleInterval< T > novelIndexImg,
final RandomAccessibleInterval< T > previousIndexImg,
final int currentTimePoint,
final Map< Integer, Spot > previousSpotIDs )
final Map< Integer, Spot > previousSpotLabels )
{
// Collect ids of spots that have been modified. id = index - 1
final Set< Integer > modifiedIDs = getModifiedIDs( novelIndexImg, previousIndexImg );
final int nModified = modifiedIDs.size();
// Collect labels corresponding to spots that have been modified.
final Set< Integer > modifiedLabels = getModifiedLabels( novelIndexImg, previousIndexImg );
final int nModified = modifiedLabels.size();
if ( nModified == 0 )
return;

Expand All @@ -103,16 +103,15 @@ public void reimport(
{
/*
* Get all the spots present in the new image, as a map against the
* label in the novel index image. This label value corresponds to
* the ids of the spots in the previous index image (label = id+1).
* label in the novel index image.
*/
final Map< Integer, List< Spot > > novelSpots = getSpots( novelIndexImg );

// Update model for those spots.
for ( final int id : modifiedIDs )
for ( final int labelValue : modifiedLabels )
{
final Spot previousSpot = previousSpotIDs.get( Integer.valueOf( id ) );
final List< Spot > novelSpotList = novelSpots.get( Integer.valueOf( id ) + 1 );
final Spot previousSpot = previousSpotLabels.get( labelValue );
final List< Spot > novelSpotList = novelSpots.get( labelValue );
if ( previousSpot == null )
{
/*
Expand Down Expand Up @@ -218,7 +217,7 @@ private void addNewSpot( final Iterable< Spot > novelSpotList, final int current
}
}

private final Set< Integer > getModifiedIDs(
private final Set< Integer > getModifiedLabels(
final RandomAccessibleInterval< T > novelIndexImg,
final RandomAccessibleInterval< T > previousIndexImg )
{
Expand All @@ -232,8 +231,8 @@ private final Set< Integer > getModifiedIDs(
return;
if ( ci != pi )
{
modifiedIDs.add( Integer.valueOf( pi - 1 ) );
modifiedIDs.add( Integer.valueOf( ci - 1 ) );
modifiedIDs.add( Integer.valueOf( pi ) );
modifiedIDs.add( Integer.valueOf( ci ) );
}
} );
modifiedIDs.remove( Integer.valueOf( -1 ) );
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/fiji/plugin/trackmate/gui/editor/LabkitLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected void launch( final boolean singleTimePoint )
final DatasetInputImage input = makeInput( imp, singleTimePoint );
final Pair< Labeling, Map< Integer, Spot > > pair = makeLabeling( imp, trackmate.getModel().getSpots(), singleTimePoint );
final Labeling labeling = pair.getA();
final Map< Integer, Spot > spotIDs = pair.getB();
final Map< Integer, Spot > spotLabels = pair.getB();

// Make a labeling model from it.
final Context context = TMUtils.getContext();
Expand All @@ -122,14 +122,14 @@ protected void launch( final boolean singleTimePoint )
labkit.onCloseListeners().addListener( () -> {
@SuppressWarnings( "unchecked" )
final RandomAccessibleInterval< T > indexImg = ( RandomAccessibleInterval< T > ) model.imageLabelingModel().labeling().get().getIndexImg();
reimport( indexImg, previousIndexImg, spotIDs, dt );
reimport( indexImg, previousIndexImg, spotLabels, dt );
} );
}

private void reimport(
final RandomAccessibleInterval< T > indexImg,
final RandomAccessibleInterval< T > previousIndexImg,
final Map< Integer, Spot > spotIDs,
final Map< Integer, Spot > spotLabels,
final double dt )
{
new Thread( "TrackMate-LabKit-Importer-thread" )
Expand Down Expand Up @@ -201,7 +201,7 @@ public void run()
{
final RandomAccessibleInterval< T > novelIndexImgThisFrame = Views.hyperSlice( indexImg, timeDim, t );
final RandomAccessibleInterval< T > previousIndexImgThisFrame = Views.hyperSlice( previousIndexImg, timeDim, t );
reimporter.reimport( novelIndexImgThisFrame, previousIndexImgThisFrame, t, spotIDs );
reimporter.reimport( novelIndexImgThisFrame, previousIndexImgThisFrame, t, spotLabels );
log.setProgress( t / ( double ) nTimepoints );
}
log.setStatus( "" );
Expand All @@ -211,7 +211,7 @@ public void run()
{
// Only one.
final int localT = Math.max( 0, currentTimePoint );
reimporter.reimport( indexImg, previousIndexImg, localT, spotIDs );
reimporter.reimport( indexImg, previousIndexImg, localT, spotLabels );
}
}
catch ( final Exception e )
Expand Down Expand Up @@ -308,7 +308,8 @@ private final DatasetInputImage makeInput( final ImagePlus imp, final boolean si
* @param singleTimePoint
* if <code>true</code> we only annotate one time-point.
* @return the pair of: A. a new {@link Labeling}, B. the map of spots that
* were written in the labeling.
* were written in the labeling. The keys are the label value in the
* labeling.
*/
private Pair< Labeling, Map< Integer, Spot > > makeLabeling( final ImagePlus imp, final SpotCollection spots, final boolean singleTimePoint )
{
Expand Down Expand Up @@ -375,18 +376,18 @@ private Pair< Labeling, Map< Integer, Spot > > makeLabeling( final ImagePlus imp
final ImgPlus< UnsignedIntType > lblImgPlus = new ImgPlus<>( lblImg, "LblImg", axes, calibration );

// Write spots in it with index = id + 1 and build a map index -> spot.
final Map< Integer, Spot > spotIDs = new HashMap<>();
final Map< Integer, Spot > spotLabels = new HashMap<>();
if ( singleTimePoint )
{
processFrame( lblImgPlus, spots, currentTimePoint, spotIDs, origin );
processFrame( lblImgPlus, spots, currentTimePoint, spotLabels, origin );
}
else
{
final int timeDim = lblImgPlus.dimensionIndex( Axes.TIME );
for ( int t = 0; t < imp.getNFrames(); t++ )
{
final ImgPlus< UnsignedIntType > lblImgPlusThisFrame = ImgPlusViews.hyperSlice( lblImgPlus, timeDim, t );
processFrame( lblImgPlusThisFrame, spots, t, spotIDs, origin );
processFrame( lblImgPlusThisFrame, spots, t, spotLabels, origin );
}
}
final Labeling labeling = Labeling.fromImg( lblImgPlus );
Expand All @@ -396,25 +397,25 @@ private Pair< Labeling, Map< Integer, Spot > > makeLabeling( final ImagePlus imp
for ( final Label label : labeling.getLabels() )
{
final String name = label.name();
final int index = Integer.parseInt( name );
final Spot spot = spotIDs.get( index );
final int labelVal = Integer.parseInt( name );
final Spot spot = spotLabels.get( labelVal );
if ( spot == null )
{
System.out.println( "Spot is null for index " + index + "!!" ); // DEBUG
System.out.println( "Spot is null for label " + labelVal + "!!" ); // DEBUG
continue;
}
label.setName( spot.getName() );
label.setColor( new ARGBType( colorGen.color( spot ).getRGB() ) );
}

return new ValuePair<>( labeling, spotIDs );
return new ValuePair<>( labeling, spotLabels );
}

private final void processFrame(
final ImgPlus< UnsignedIntType > lblImgPlus,
final SpotCollection spots,
final int t,
final Map< Integer, Spot > spotIDs,
final Map< Integer, Spot > spotLabels,
final long[] origin )
{
// If we have a single timepoint, don't use -1 to retrieve spots.
Expand All @@ -426,7 +427,7 @@ private final void processFrame(
{
final int index = spot.ID() + 1;
SpotUtil.iterable( spot, lblImgPlus ).forEach( p -> p.set( index ) );
spotIDs.put( index, spot );
spotLabels.put( index, spot );
}
}
else
Expand All @@ -445,7 +446,7 @@ private final void processFrame(

final int index = spot.ID() + 1;
SpotUtil.iterable( spot, lblImgPlus ).forEach( p -> p.set( index ) );
spotIDs.put( index, spot );
spotLabels.put( index, spot );
}
}
}
Expand Down

0 comments on commit 2f9583e

Please sign in to comment.