3
3
'''
4
4
Note that this script requires a Python environment that includes StarDist and Cellpose
5
5
StarDist 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.
7
13
8
14
You can rebuild your Python environment by using:
9
15
Edit > Options > Python…
12
18
13
19
--Conda dependencies--
14
20
python=3.11
15
- numpy=1.26.4
16
21
17
22
--Pip dependencies--
23
+ numpy=1.26.4
24
+ csbdeep==0.8.0
18
25
tensorflow==2.15
19
- cellpose==4.0.6
26
+ cellpose==3.1.1.1
20
27
stardist==0.9.0
21
- csbdeep==0.8.0
22
28
'''
23
29
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
+
24
63
import sys
25
64
import imagej .convert as convert
26
65
import numpy as np
@@ -81,7 +120,7 @@ def get_bounding_box(indices: np.ndarray):
81
120
nuc_labels , _ = model .predict_instances (normalize (xdata [:, :, 0 ]))
82
121
83
122
# run Cellpose on cytoplasm (grayscale)
84
- model = models .CellposeModel (gpu = False , model_type = 'cyto' )
123
+ model = models .Cellpose (gpu = False , model_type = 'cyto' )
85
124
ch = [0 , 0 ]
86
125
cyto_labels = model .eval (xdata [:, :, 1 ].data , channels = ch , diameter = 72.1 )
87
126
@@ -136,4 +175,4 @@ def get_bounding_box(indices: np.ndarray):
136
175
137
176
#TODO this pops an unnecessary display at the end but if I don't make it the last line the ROIs don't show
138
177
rm .moveRoisToOverlay (imp )
139
- rm .runCommand (imp , "Show All" )
178
+ rm .runCommand (imp , "Show All" )
0 commit comments