Skip to content

Commit d2e5285

Browse files
committed
Document CellposeStarDist example
Clarify instructions for mac vs windows Closes #22
1 parent 807603c commit d2e5285

File tree

1 file changed

+58
-27
lines changed

1 file changed

+58
-27
lines changed

src/main/resources/script_templates/PyImageJ/CellposeStarDistSegmentation.py

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,71 @@
11
#@ ImageJ ij
22

33
'''
4-
Note that this script requires a Python environment that includes StarDist and Cellpose
5-
StarDist currently only supports NumPy 1.x, which necessitates using TensorFlow 2.15 or earlier
4+
==========================
5+
Building a Python Environment
6+
==========================
7+
NOTE: this script requires a Python environment that includes StarDist and Cellpose.
8+
At time of writing, StarDist only supports NumPy 1.x, which necessitates using TensorFlow 2.15.
69
TensorFlow 2.15 itself requires python 3.11 or earlier.
710
8-
We also use cellpose 3.x because cellpose 4.x is heavily biased towards using their `cpsam`,
11+
We use cellpose 3.x because cellpose 4.x is heavily biased towards using their `cpsam`,
912
"segment anything" model. This is a very cool model, but it is also huge and performance
10-
with a CPU is not great. It is also overkill for this example.
13+
with a CPU is not great. It is also more powerful than needed for this example.
1114
1215
Using cellpose 3.x allows us to stick with the light and focused `ctyo` model for segmentation.
1316
14-
You can rebuild your Python environment by using:
15-
Edit > Options > Python…
17+
See the Conda and Pip dependency sections below for specific library versions that were used
18+
to develop this script.
1619
17-
The following configuration was used to develop this script:
20+
Use these versions to create an appropriate Python environment with:
21+
Edit > Options > Python…
1822
19-
--Conda dependencies--
23+
==========================
24+
Platform Dependency Tuning
25+
==========================
26+
NOTE: python can be very sensitive to operating system.
27+
It is possible to install "compatible" versions of libraries that are actually incompatible,
28+
based on the source of the library.
29+
In testing this script, the following guidelines were used:
30+
Windows: numpy should be installed with pip (as indicated below)
31+
MacOS (arm64): numpy should be installed with Conda (NOT with pip)
32+
33+
==========================
34+
Conda-managed dependencies
35+
==========================
2036
python=3.11
2137
22-
--Pip dependencies--
23-
numpy=1.26.4
38+
==========================
39+
Pip-managed dependencies
40+
==========================
41+
numpy==1.26.4
2442
csbdeep==0.8.0
25-
tensorflow==2.15
43+
tensorflow==2.15.1
2644
cellpose==3.1.1.1
2745
stardist==0.9.0
28-
'''
2946
30-
# Although cellpose 3.x "recognizes" the `ctyo` model, all models must be downloaded
31-
# once before use (to USER_HOME/.cellpose/models).
32-
# Unfortunately the built-in logic for downloading models in cellpose is completely
33-
# tied to tqdm, which breaks when running in Fiji on Windows.
34-
# Attempts to disable tqdm with environment variables and "monkey patching" failed
35-
# Thus, the following code is necessary to download the cyto model if not already available.
47+
==========================
48+
Future Directions
49+
==========================
50+
The "segment anything" Cellpose model is very powerful. The next steps for this script would be
51+
to remove StarDist and upgrade to Cellpose 4.x, which would be used to segment both cytoplasm and
52+
nuclear channels.
53+
54+
==========================
55+
Known Issues
56+
==========================
57+
Although cellpose 3.x "recognizes" the `ctyo` model, all models must be downloaded
58+
once before use (to USER_HOME/.cellpose/models).
59+
Unfortunately the built-in logic for downloading models in cellpose is completely
60+
tied to tqdm, which breaks when running in the script editor on Windows.
61+
Attempts to disable tqdm with environment variables and "monkey patching" failed.
62+
Thus, we provide the `cache_cyto_model` method below to download the model if needed.
63+
'''
3664

37-
import os
3865
from pathlib import Path
3966
import urllib.request
4067

41-
def ensure_cyto_model():
68+
def cache_cyto_model():
4269
"""Ensure the Cellpose 'cyto' model files exist in ~/.cellpose/models/."""
4370
model_dir = Path.home() / ".cellpose" / "models"
4471
model_dir.mkdir(parents=True, exist_ok=True)
@@ -58,9 +85,8 @@ def ensure_cyto_model():
5885
print(f"Skipping {filename} - already cached at {target}")
5986

6087
# Download and cache the cyto model
61-
ensure_cyto_model()
88+
cache_cyto_model()
6289

63-
import sys
6490
import imagej.convert as convert
6591
import numpy as np
6692
from cellpose import models
@@ -162,17 +188,22 @@ def get_bounding_box(indices: np.ndarray):
162188
imp.setC(2)
163189
ij.IJ.run(imp, "Enhance Contrast", "saturated=0.35")
164190

165-
# convert a single ImgLib2 roi to a legacy ImageJ ROI with the ConvertService.
191+
# ROI Conversion Options
192+
# At this point, we have ImgLib2 ROIs. To display in the RoiManager, we need ImageJ 1.x ROIs
193+
194+
# Option 1: convert a single ImgLib2 ROI to a legacy ImageJ ROI with the ConvertService
195+
# This ROI could then be manually added to the Roi manager
166196
imglib_polygon_roi = nuc_roi_tree.children().get(0).data()
167197
ij_polygon_roi = ij.convert().convert(imglib_polygon_roi, sj.jimport('ij.gui.PolygonRoi'))
168198
print(type(ij_polygon_roi))
169199

170-
# convert index images to ImageJ ROI in RoiManager
171-
#TODO any way to color the selections? We can use Colors... but it appears to be global and the last one run wins
172-
#ij.IJ.run(imp, "Colors...", "foreground=blue background=black selection=red");
200+
# Option 2: convert index images to ImageJ ROIs in RoiManager
201+
# This convenience method simplifies the batch conversion and addition to the RoiManager
173202
convert.index_img_to_roi_manager(ij, nuc_labels)
174203
convert.index_img_to_roi_manager(ij, cyto_labels[0])
204+
#TODO any way to color the selections? We can use Colors... but it appears to be global and the last one run wins
205+
#ij.IJ.run(imp, "Colors...", "foreground=blue background=black selection=red");
175206

176207
#TODO this pops an unnecessary display at the end but if I don't make it the last line the ROIs don't show
177208
rm.moveRoisToOverlay(imp)
178-
rm.runCommand(imp, "Show All")
209+
rm.runCommand(imp, "Show All")

0 commit comments

Comments
 (0)