-
Notifications
You must be signed in to change notification settings - Fork 0
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.
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.
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.
Ideally, this will be run within a GPU OnDemand session.
Activate your conda environment containing Cellpose.
$ conda activate cellpose-env
Download the script into a folder of your choosing and open it in FIJI. Before running the script, two things must be modified.
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()
.
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.
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.
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.
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
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.
![](https://private-user-images.githubusercontent.com/138872234/342977240-f616beab-35df-448a-8cf4-4f130e82cb49.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0MzcwNjAsIm5iZiI6MTczOTQzNjc2MCwicGF0aCI6Ii8xMzg4NzIyMzQvMzQyOTc3MjQwLWY2MTZiZWFiLTM1ZGYtNDQ4YS04Y2Y0LTRmMTMwZTgyY2I0OS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxM1QwODUyNDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lYzFlYjFjODliYTI2NjNjN2Q0OTI2NjdkYTdjMWNmMzc3YjAzZmIwYmJmY2FlNTQ3OWIzMWNmMmE5NDUyNGIyJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.z1TAC_JGRdrgEmxW6piR4r2onQF9vgunVux3I5oW_5w)
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.
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.
![](https://private-user-images.githubusercontent.com/138872234/343099179-da1e8035-8cb4-4e91-80b6-87aa27067035.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0MzcwNjAsIm5iZiI6MTczOTQzNjc2MCwicGF0aCI6Ii8xMzg4NzIyMzQvMzQzMDk5MTc5LWRhMWU4MDM1LThjYjQtNGU5MS04MGI2LTg3YWEyNzA2NzAzNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxM1QwODUyNDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1iMDMwOTgwY2JiNmVhMzAxYTU5ZTcxMjdiZmM3YmUyNWRiMDI0M2FkYjNjNjYyODhlYWNmY2E4ZGFjYmM2ODc1JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.fbzF85dNg-ENh37B5Qx36K5GbHnP9vmon0mr7XqmBd4)
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.
![](https://private-user-images.githubusercontent.com/138872234/343100380-b73fc5c6-79bb-4e45-a207-ed1c274214bf.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0MzcwNjAsIm5iZiI6MTczOTQzNjc2MCwicGF0aCI6Ii8xMzg4NzIyMzQvMzQzMTAwMzgwLWI3M2ZjNWM2LTc5YmItNGU0NS1hMjA3LWVkMWMyNzQyMTRiZi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxM1QwODUyNDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05YTQ4YWI0MjA5OGJlNzRjZTk3M2NhYzQ0MjY2YTRiMjgzMGE2ZjM2NDNjZWQzNGE1YmRjZmQ3ZGIxMzg2YmQ1JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.YmoDQdVQan-7z4N_hzb3nHZuByaGOXx-toWkWHzLX4o)
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.
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.