Skip to content

Trackmate_Cellpose_GUI.py

Vanessa Dao edited this page Jun 26, 2024 · 14 revisions

What is Trackmate_Cellpose_GUI.py

This script will take a root directory of images through a GUI input then perform Trackmate-Cellpose detection and tracking at multiple sizes and output Trackmate XML files for each image at each size.

Within the provided directory, the code looks for TIFF files - images must be of 2D+t, the code will check image metadata then re-save file as 2D+t if it is 3D then continue with detection and tracking.

Sub-directories are made within the root directory for each image where the Trackmate XML files for that image are outputted, the image is moved into its respective sub-directory when detection and tracking of all provided predicted object sizes are finished.

This allows for the script to be run again, if interrupted, without executing on images which have already finished detection.

Setting Up

Cellpose conda environment

FIJI's Trackmate-Cellpose plugin is useless without a working Cellpose environment. Please check cellpose for conda installation instructions. For Crick OnDemand users (suggested method), please see this page for instructions.

Trackmate-Cellpose plugin

In FIJI, go to Help > Update... then click on Manage update sites button on the bottom-left of the window. A new window containing update sites will appear, check Trackmate-Cellpose and restart FIJI.

Running Trackmate_Cellpose_GUI.py

Ideally, this will be run within a GPU OnDemand session.

Activate cellpose environment

Activate your conda environment containing Cellpose.

$ conda activate cellpose-env

Open the script

Download the script into a folder of your choosing and open it in FIJI. Before running the script, two things must be modified.

1. Predicted object pixel size

You need to specify at what predicted object pixel sizes you want Cellpose to perform spot detection on for every image. Multiple sizes can be provided, allowing Cellpose to detect objects of small, large and everything in between sizes.

In Line 63, the variable PixelSizes takes a list of floats - this should be modified for object sizes in pixels you wish to detect in your image. For example:

62   # List of sizes for cellpose detection
63   PixelSizes = [15., 60., 75.]

This script will resave your image with pixel size expressed in pixels and not physical units. If your images have pixels expressed in physical units and you wish to keep everything in physical units, including variable PixelSizes for detection, then remove/comment out imp.removeScale().

2. Paths to cellpose / python executable and models

With your cellpose environment activated, find the cellpose executable by entering the following in the terminal:

$ which python

The terminal will print a path which you should use for the parameter CELLPOSE_PYTHON_PATH when configuring the cellpose detector settings.

You will also need to find the path to .cellpose/models to use for the parameter CELLPOSE_MODEL_FILEPATH (it's most likely in your home directory). These parameters must be in a string and the paths must be accessible to you. For example:

# Configure detector
settings.detectorSettings = {
   	'TARGET_CHANNEL' : 0,
  	'OPTIONAL_CHANNEL_2' : 0,
	'CELLPOSE_PYTHON_FILEPATH' : '/nemo/stp/lm/working/daov/.conda/envs/cellpose_attempt/bin/python',
	'CELLPOSE_MODEL' : PretrainedModel.CYTO2,
	'CELLPOSE_MODEL_FILEPATH' : '/nemo/stp/lm/working/daov/.cellpose/models',
	'CELL_DIAMETER' : PixelSize,
	'USE_GPU' : True,
	'SIMPLIFY_CONTOURS' : True,
}

Save the script for reuse with your paths.

Running the code

With specified predicted object size for detection and required cellpose paths provided, the script is ready to be executed. Click Run from the script editor window. A GUI will appear, prompting the user to point to the directory containing all images to be processed.

Expected format of images is 2D+t or 3D (3D files will be converted and resaved automatically) TIFF files with channel to be detected in the first channel i.e channel 0.

Images are processed one at a time at each predicted object size. When an image has finished detection and tracking at every size, it will be moved to its respective sub-directory marking the completion of its processing.

Outputs

Each detection and tracking at a predicted object size creates a single Trackmate XML file called Image_Name_<pixelsize>_pixels.xml. These files have all detected spots and their tracks through time (z-slice). You can filter these spots/tracks using FIJI's Trackmate-Cellpose plugin and these filters can be saved for reproducibility. Labels can then be made and saved from these files, as well as other quantifiable data for more information refer to Trackmate documentation.

Creating Labels

From Trackmate XML files, you can get labels. First open FIJI and navigate to Plugins > Tracking > Load a TrackMate file and select the file you wish to extract labels from first. Two windows will appear:

  • The raw image itself with tracks and labels visible which will dynamically change with adjustable filters
  • Trackmate window which controls the spots and tracks

Human refinement

The Trackmate window will open up on the Display options page where you can uncheck Display tracks so the image window only displays detected spots.

Using the left green arrow, navigate backwards 4 pages. This takes you to where you can add filters to your spots based on spot features such as Circularity, Contrast in ch1, Size, and more to refine the segmentations.

To do this, click the green plus button in the bottom-left. A feature and histogram will appear in the panel, you can change the feature which the filter acts on with the dropdown menu and the threshold with the adjustable slider on the histogram.

Important! You can save the final sets of filters and parameters by clicking the Save button on the bottom-left which will allow you to return to this Trackmate XML file with the same filters preloaded. Also useful for documentation of methods for reproducibility.

Saving labels

Using the right Next button, navigate to the end of the Trackmate window where you are prompted to Select an **action**. From the drop down menu, select Export label image and click Execute button in the bottom-right.

You will be presented with checkboxes, leave this unchecked and ensure the Label ID is set to Track ID, this ensures that your spot labels are consistent through the z-slices which allows for 3D segmentation.

A new FIJI window will appear containing just the labels and you can save the labels, preferably within the same sub-directory containing the raw image.

Suggested filename LblImg_Image_Name_<pixelsize>_pixels.tif

This is suggested if you wish to use this Jupyter notebook to combine labels and filter overlapping objects for each image set which is detailed in this page.

General Debugging

You run the script and get the cellpose module not detected error

Your cellpose environment is not active. Close FIJI, activate the correct environment then open FIJI and try running the script again.

Feel free to add more common mistakes

And its respective potential solution.