33'''
44Note that this script requires a Python environment that includes StarDist and Cellpose
55StarDist currently only supports NumPy 1.x, which necessitates using TensorFlow 2.15 or earlier
6- TensorFlow 2.15 itself requires python 3.11 or earlier
6+ TensorFlow 2.15 itself requires python 3.11 or earlier.
7+
8+ We also use cellpose 3.x because cellpose 4.x is heavily biased towards using their `cpsam`,
9+ "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.
11+
12+ Using cellpose 3.x allows us to stick with the light and focused `ctyo` model for segmentation.
713
814You can rebuild your Python environment by using:
915Edit > Options > Python…
1218
1319--Conda dependencies--
1420python=3.11
15- numpy=1.26.4
1621
1722--Pip dependencies--
23+ numpy=1.26.4
24+ csbdeep==0.8.0
1825tensorflow==2.15
19- cellpose==4.0.6
26+ cellpose==3.1.1.1
2027stardist==0.9.0
21- csbdeep==0.8.0
2228'''
2329
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.
36+
37+ import os
38+ from pathlib import Path
39+ import urllib .request
40+
41+ def ensure_cyto_model ():
42+ """Ensure the Cellpose 'cyto' model files exist in ~/.cellpose/models/."""
43+ model_dir = Path .home () / ".cellpose" / "models"
44+ model_dir .mkdir (parents = True , exist_ok = True )
45+
46+ files = {
47+ "cytotorch_0" : "https://www.cellpose.org/models/cytotorch_0" ,
48+ "size_cytotorch_0.npy" : "https://www.cellpose.org/models/size_cytotorch_0.npy"
49+ }
50+
51+ for filename , url in files .items ():
52+ target = model_dir / filename
53+ if not target .exists ():
54+ print (f"Downloading { filename } ..." )
55+ urllib .request .urlretrieve (url , target )
56+ print (f"{ filename } downloaded to { target } " )
57+ else :
58+ print (f"Skipping { filename } - already cached at { target } " )
59+
60+ # Download and cache the cyto model
61+ ensure_cyto_model ()
62+
2463import sys
2564import imagej .convert as convert
2665import numpy as np
@@ -81,7 +120,7 @@ def get_bounding_box(indices: np.ndarray):
81120nuc_labels , _ = model .predict_instances (normalize (xdata [:, :, 0 ]))
82121
83122# run Cellpose on cytoplasm (grayscale)
84- model = models .CellposeModel (gpu = False , model_type = 'cyto' )
123+ model = models .Cellpose (gpu = False , model_type = 'cyto' )
85124ch = [0 , 0 ]
86125cyto_labels = model .eval (xdata [:, :, 1 ].data , channels = ch , diameter = 72.1 )
87126
@@ -136,4 +175,4 @@ def get_bounding_box(indices: np.ndarray):
136175
137176#TODO this pops an unnecessary display at the end but if I don't make it the last line the ROIs don't show
138177rm .moveRoisToOverlay (imp )
139- rm .runCommand (imp , "Show All" )
178+ rm .runCommand (imp , "Show All" )
0 commit comments