Starting trackscheme within script, doesn't synchronize selections with hyperstackdisplayer

I am writing a script to load an existing TrackMate file, find some spots of interest and select them in TrackScheme and HyperStackDisplayer at the same time for the user to correct them manually.

When I open the TrackScheme within the script I can select the spot but when I select a new spot in HyperStackDisplayer, TrackScheme doesn’t focus on the new spot. Subsequent manual selections aren’t synchronized either. This makes it hard to navigate to correct things. TrackScheme also doesn’t clear the previous selection in this case.

When I leave the TrachScheme part out in the script and manually start it in the GUI (after running the script), TrackMate is able to synchronize selections.

I wonder, if I am doing something wrong here:

from fiji.plugin.trackmate.visualization.hyperstack import HyperStackDisplayer
from fiji.plugin.trackmate import Settings
from fiji.plugin.trackmate import SelectionModel
from fiji.plugin.trackmate import LoadTrackMatePlugIn_
from java.io import File
import fiji.plugin.trackmate.visualization.trackscheme.TrackScheme as TrackScheme
import fiji.plugin.trackmate.visualization.PerTrackFeatureColorGenerator as PerTrackFeatureColorGenerator
import sys
from java.util import Collections, ArrayList

loadTM = LoadTrackMatePlugIn_()
loadTM.run('/media/stasciya/WD_Data/Data_for_tracking_ridges_t cells/TMs/190430_flat-ICAM_CCL19_1_segm_cont_merg_clean_gt10spots.xml')

model = loadTM.getModel()
settings = loadTM.getSettings()
guiController = loadTM.getController()
guiModel = guiController.getGuimodel()
gui = guiController.getGUI()

tracks = model.getTrackModel()
trackIDs = tracks.trackIDs(True)
sm = guiController.getSelectionModel()


color = PerTrackFeatureColorGenerator(model, 'TRACK_INDEX')
trackScheme = TrackScheme(model, sm)
trackScheme.setDisplaySettings('TrackColoring', color)
trackScheme.render()
guiModel.addView(trackScheme)

detectorSettings = settings.detectorSettings
sm = guiController.getSelectionModel()
if detectorSettings.containsKey('RADIUS'):
	radius = detectorSettings['RADIUS']
	if radius > 0:
		margin = radius * 4 # 2x Diameter
	else:
		margin = width * 0.05 # or 5%
			
for trackID in trackIDs:
	spots = ArrayList(tracks.trackSpots(trackID))	
	Collections.sort(spots, spots[0].frameComparator)
	
	lastSpot = spots[-1]
	
	x = lastSpot.getDoublePosition(0)
	y = lastSpot.getDoublePosition(1)
	z = lastSpot.getDoublePosition(2)
	t = lastSpot.getFeature(lastSpot.POSITION_T)
	width = settings.width * settings.dx
	height = settings.height * settings.dy
	nslices = settings.nslices * settings.dz

	middle = (x > margin or x < (width - margin)) and (y > margin or y < (height - margin)) and (t < settings.nframes)
	print(middle)
	if middle:	
		print(x)
		print(y)
		print(lastSpot)
		print(lastSpot.getName())
		sm.clearSelection()
		sm.addSpotToSelection(lastSpot)
		break

Hello @SarenT

Could you try this script and tells me whether it works for you?


from fiji.plugin.trackmate import LoadTrackMatePlugIn_
import fiji.plugin.trackmate.visualization.trackscheme.TrackScheme as TrackScheme
import fiji.plugin.trackmate.visualization.PerTrackFeatureColorGenerator as PerTrackFeatureColorGenerator

loadTM = LoadTrackMatePlugIn_()
loadTM.run('c:/Users/Tinevez/Desktop/FakeTracks.xml')

model = loadTM.getModel()
settings = loadTM.getSettings()
guiController = loadTM.getController()
guiModel = guiController.getGuimodel()
gui = guiController.getGUI()
sm = guiController.getSelectionModel()


color = PerTrackFeatureColorGenerator(model, 'TRACK_INDEX')
trackScheme = TrackScheme(model, sm)

for settingKey in guiModel.getDisplaySettings().keySet():
	trackScheme.setDisplaySettings( settingKey, guiModel.getDisplaySettings().get( settingKey ) )

trackScheme.render()
guiModel.addView(trackScheme)


1 Like

This works. I will try to find out and post here, what was different and why my code didn’t work.

1 Like