TrackMate Scripting - no spots or no tracks



Hi all (and I’m guessing I would love for @tinevez to poke around :wink: ),

I’m trying to analyse tracks from an avi file using trackmate from a python script (below). I started from a script found here and modified it to load an avi instead of a tif. Cheers to @DidasW, your example helped a lot.

My problem is, if I don’t swap Z and T like everyone seems to do (and that the Trackmate GUI urges me to do when I load the file from GUI), my scripts detects the spots OK but can’t find any tracks (there is an obvious one, I checked). But if I do swap Z and T, TrackMate doesn’t find any spots at all…

If I convert my avi to an avi stack, same stuff happens.

It’s almost like the track detection guy looks over T and the spots detections guy needs Z, or doesn’t know were to look if I swap.

I’m baffled here, could anyone help me?

My script :

from ij import IJ, ImagePlus, ImageStack, WindowManager
from ij.process import ImageConverter
import sys
import fiji.plugin.trackmate.action.ExportStatsToIJAction as ExportStatsToIJAction
import fiji.plugin.trackmate.action.ExportTracksToXML as ExportTracksToXML
import fiji.plugin.trackmate.detection.DetectorKeys as DetectorKeys
import fiji.plugin.trackmate.detection.DogDetectorFactory as DogDetectorFactory
import fiji.plugin.trackmate.detection.LogDetectorFactory as LogDetectorFactory
import fiji.plugin.trackmate.features.FeatureFilter as FeatureFilter
import fiji.plugin.trackmate.features.FeatureAnalyzer as FeatureAnalyzer
import fiji.plugin.trackmate.features.track.TrackDurationAnalyzer as TrackDurationAnalyzer
import fiji.plugin.trackmate.features.ModelFeatureUpdater as ModelFeatureUpdater
import fiji.plugin.trackmate.features.SpotFeatureCalculator as SpotFeatureCalculator
import as SpotContrastAndSNRAnalyzerFactory
import as SpotContrastAndSNRAnalyzer
import as SpotIntensityAnalyzerFactory
import fiji.plugin.trackmate.features.track.TrackSpeedStatisticsAnalyzer as TrackSpeedStatisticsAnalyzer
import as TmXmlReader
import as TmXmlWriter
import fiji.plugin.trackmate.Logger as Logger
import fiji.plugin.trackmate.Model as Model
import fiji.plugin.trackmate.SelectionModel as SelectionModel
import fiji.plugin.trackmate.Settings as Settings
import fiji.plugin.trackmate.TrackMate as TrackMate
import fiji.plugin.trackmate.tracking.sparselap.SparseLAPTrackerFactory as SparseLAPTrackerFactory
import fiji.plugin.trackmate.tracking.LAPUtils as LAPUtils
import fiji.plugin.trackmate.util.TMUtils as TMUtils
import fiji.plugin.trackmate.visualization.hyperstack.HyperStackDisplayer as HyperStackDisplayer

import fiji.plugin.trackmate.gui.GuiUtils as GuiUtils
print'debug 1';

# Get currently selected image
#**--------------------MODIFY THE FOLDER PATH, '--------------------------**"AVI...", "open=[/home/tdelattre/Bureau/Camera_test_4.avi] first=200 last=300 convertToGray use");"Image Sequence...", "open=[/home/tdelattre/Bureau/Camera_test_4.tif] convert sort");

#imp = IJ.openImage("/home/tdelattre/Bureau/Camera_test_4.tif");;

# change the folder_path to what you need
print'debug 2';
imp = WindowManager.getCurrentImage()
dims = imp.getDimensions();
print 'dimensions';
print dims;
print 'number of dimensions';
print imp.getNDimensions();
#print 'number of channels';
#print imp.getNChannels();
#print imp.getChannel();

imp.setDimensions( dims[ 2 ], dims[ 4 ], dims[ 3 ] );
dims = imp.getDimensions();
print dims;
print'debug 3';


model = Model()
print'debug 4';
# Set logger
settings = Settings()
print settings;
print'debug 5';

# Setting recognition parameters
settings.detectorFactory = LogDetectorFactory()
settings.detectorSettings = { 
    'RADIUS' : 8.0,
    'THRESHOLD' : 3.0,
print'debug 6';

# Setting tracking parameters
settings.trackerFactory = SparseLAPTrackerFactory()
settings.trackerSettings = LAPUtils.getDefaultLAPSettingsMap() #this sets tens of madatory settings
settings.trackerSettings['LINKING_MAX_DISTANCE'] = 3000.0
settings.trackerSettings['MAX_FRAME_GAP']= 5000
settings.trackerSettings['ALLOW_TRACK_MERGING']= True
print'debug 7';

# Start Trackmate.   
trackmate = TrackMate(model, settings)    
ok = trackmate.checkInput()
print 'CheckInput = %s'%ok
if not ok:
ok = trackmate.process()
print 'TrackMate.process = %s'%ok
if not ok:
print'debug 8';

# Write in the logger, found xxx tracks
model.getLogger().log('Found ' + str(model.getTrackModel().nTracks(True)) + ' tracks.')

print'debug 9';    
selectionModel = SelectionModel(model)
displayer =  HyperStackDisplayer(model, selectionModel, imp)# after this, track still not displayed
displayer.render() #track displayed
print'debug 10';
fm = model.getFeatureModel()
#print fm 
f = open('/home/tdelattre/Bureau/test_track.dat','w') # create file to store track info.
for id in model.getTrackModel().trackIDs(True):
    track = model.getTrackModel().trackSpots(id)
    for spot in track:
        sid = spot.ID()
        # Fetch spot features directly from spot. 

imp.getDimensions() sends

[1024, 768, 1, 101, 1] without swapping, [1024, 768, 1, 1, 101] with swapping.
There is 1 channel is the video.

Thanks a lot !


Do these parameters work when you run TrackMate via the GUI?

Note that RADIUS in scripting is really the radius, whereas Estimated blob diameter is the diameter, i.e. radius * 2 (a fact that confuses quite a few people when they switch over to TrackMate scripting, in my experience)1.

[1] See the source code here:


Hi @imagejan,

You nailed it. I made that mistake, and correcting it (radius / 2) solves the problem.
I confess I don’t understand how I was able to detect spots with a radius twice the real size and swapped z/t axis (and why it was a problem for tracks and not for spot detection).
Anyway, it works now, thanks to you :slight_smile:





If you had 101 slices instead of 101 time points, spot detection works in 3D, so you might pick up different spots depending on your x,y,z calibration (and a threshold of 3.0 will mean something entirely different for a 3D Laplacian of Gaussian…)

On the other hand, if you’re left with only 1 frame, there’s no possibility to link tracks :slight_smile:


Ok, it makes sense. Sometimes it’s hard to wraps one’s head around more than 3 dimensions :slight_smile:
Thanks again Jan.
Marking the post as solved.