Difference between revisions of "Scripting TrackMate"

(make internal links to TrackMate, mention that it is a Jython script, remove semicolons and add whitespace around plusses)
(Put an introduction)
Line 1: Line 1:
 +
== TrackMate scripting principle ==
 +
 +
[[TrackMate_]] can be used out of the GUI, using a scripting language that allows making calls to Java. The most simple way to get started is to use the [[Script Editor]] of Fiji, which takes care of the difficult & boring part for you (such as path). The examples proposed on this page all use Jython, but can be adapted to anything.
 +
 +
Since we are calling the internals of TrackMate, we must get to know a bit of its guts. I have tried to come up with a rational design; though not always successfully. There is 3 main classes to interact with in a script:
 +
 +
* TrackMateModel ([http://fiji.sc/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=src-plugins/TrackMate_/fiji/plugin/trackmate/TrackMateModel.java;hb=HEAD <code>fiji.plugin.trackmate.TrackMateModel</code>]) is the class in charge of <u>storing the data</u>. It cannot do anything to create it. It can help you follow manual modifications you would made in the manual editing mode, interrogate it, ... but it is conceptually just a data recipient.
 +
 +
* Settings ([http://fiji.sc/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=src-plugins/TrackMate_/fiji/plugin/trackmate/Settings.java;hb=HEAD <code>fiji.plugin.trackmate.Settings</code>]) is a part of the model. It is a class storing the fields that will configure TrackMate and pilot how the data is created. As you can see, it is mainly make of public fields.
 +
 +
* TrackMate ([http://fiji.sc/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=src-plugins/TrackMate_/fiji/plugin/trackmate/TrackMate_.java;hb=HEAD <code>fiji.plugin.trackmate.TrackMate_</code>]) is the actual Fiji plugin. When called from the Fiji menu, it launches the GUI. In scripts, we use it to actually <u>perform the analysis tasks</u>, such as generating spots from images, linking them into track, etc... It reads configuration information in the Settings object mentioned above and put the resulting data in the model. 
 +
 +
So getting a working script is all about configuring a proper <code>Settings</code> object and calling <code>exec*</code> methods on a <code>TrackMate_</code> object.
 +
 +
 +
== A full example ==
 +
 +
 
Here is an example of full tracking process, using the easy image found in the [[Getting started with TrackMate|first tutorial]]. The following (Jython) script works as following:
 
Here is an example of full tracking process, using the easy image found in the [[Getting started with TrackMate|first tutorial]]. The following (Jython) script works as following:
  
Line 108: Line 126:
  
 
</source>
 
</source>
 +
 +
 +
[[User:JeanYvesTinevez|JeanYvesTinevez]] 17:35, 20 June 2012 (CEST)

Revision as of 10:35, 20 June 2012

TrackMate scripting principle

TrackMate_ can be used out of the GUI, using a scripting language that allows making calls to Java. The most simple way to get started is to use the Script Editor of Fiji, which takes care of the difficult & boring part for you (such as path). The examples proposed on this page all use Jython, but can be adapted to anything.

Since we are calling the internals of TrackMate, we must get to know a bit of its guts. I have tried to come up with a rational design; though not always successfully. There is 3 main classes to interact with in a script:

  • TrackMateModel (fiji.plugin.trackmate.TrackMateModel) is the class in charge of storing the data. It cannot do anything to create it. It can help you follow manual modifications you would made in the manual editing mode, interrogate it, ... but it is conceptually just a data recipient.
  • Settings (fiji.plugin.trackmate.Settings) is a part of the model. It is a class storing the fields that will configure TrackMate and pilot how the data is created. As you can see, it is mainly make of public fields.
  • TrackMate (fiji.plugin.trackmate.TrackMate_) is the actual Fiji plugin. When called from the Fiji menu, it launches the GUI. In scripts, we use it to actually perform the analysis tasks, such as generating spots from images, linking them into track, etc... It reads configuration information in the Settings object mentioned above and put the resulting data in the model.

So getting a working script is all about configuring a proper Settings object and calling exec* methods on a TrackMate_ object.


A full example

Here is an example of full tracking process, using the easy image found in the first tutorial. The following (Jython) script works as following:

  • It fetches the image from the web
  • It configures settings for segmentation and tracking
  • The model is instantiated, with the settings and imp objects
  • The TrackMate plugin is instantiated with the model object
  • Then the TrackMate object performs all the steps needed.
  • The final results is displayed as an overlay.


import fiji.plugin.trackmate.Settings;
import fiji.plugin.trackmate.TrackMateModel;
import fiji.plugin.trackmate.TrackMate_;
import fiji.plugin.trackmate.Logger;
import fiji.plugin.trackmate.segmentation.DogSegmenter;
import fiji.plugin.trackmate.tracking.SimpleFastLAPTracker;
import fiji.plugin.trackmate.visualization.hyperstack.HyperStackDisplayer;

# Get currently selected image
# imp = WindowManager.getCurrentImage()
imp = IJ.openImage('http://fiji.sc/tinevez/TrackMate/FakeTracks.tif')
imp.show()

#------------------------
# Prepare settings object
#------------------------

settings = fiji.plugin.trackmate.Settings(imp)

# Configure segmenter
settings.segmenter = fiji.plugin.trackmate.segmentation.DogSegmenter()
settings.segmenterSettings = fiji.plugin.trackmate.segmentation.LogSegmenterSettings()
settings.segmenterSettings.expectedRadius = 2.5

# Configure tracker
settings.tracker = fiji.plugin.trackmate.tracking.SimpleFastLAPTracker()
settings.trackerSettings = fiji.plugin.trackmate.tracking.LAPTrackerSettings()

print(str(settings))

#-----------------------------
# Instantiate model and plugin
#-----------------------------

model = fiji.plugin.trackmate.TrackMateModel()
model.setSettings(settings)

trackmate = fiji.plugin.trackmate.TrackMate_(model)

#---------------------
# Execute segmentation
#---------------------

print('Segmenting with parameters:')
print(str(settings.segmenter))
print(str(settings.segmenterSettings))

trackmate.execSegmentation()

print(str(model.getSpots()))

#--------------------------
# Execute initial filtering
#--------------------------

model.setInitialSpotFilterValue(3.6)

print('Initial spot filtering with quality above ' + str(model.getInitialSpotFilterValue()))

trackmate.execInitialSpotFiltering()

print(str(model.getSpots()))

#--------------------------
# Execute spot filtering
#--------------------------

filter1 = fiji.plugin.trackmate.FeatureFilter('QUALITY', 20, True)

print('Spot filtering with ' + str(filter1))

model.addSpotFilter(filter1)
trackmate.execSpotFiltering()

print(str(model.getFilteredSpots()))

#-----------------
# Execute tracking
#-----------------

print('Tracking with parameters:')
print(str(settings.tracker))
print(str(settings.trackerSettings))

trackmate.execTracking()

print('Found ' + str(model.getNTracks()) + ' tracks.')

#----------------
# Display results
#----------------

displayer =  fiji.plugin.trackmate.visualization.hyperstack.HyperStackDisplayer()
displayer.setModel(model)
displayer.render()
displayer.refresh()


JeanYvesTinevez 17:35, 20 June 2012 (CEST)