@@ -214,12 +214,73 @@ def junctions_to_label(img):
214214
215215def run_cellpose (img , scaleXY , diameter = 7 , verbose = True ):
216216 from cellpose import models
217- model = models .CellposeModel (gpu = True , model_type = 'cyto3' )
217+ model = models .CellposeModel ( gpu = True )
218218 diamet = diameter / scaleXY ## increase it ?
219- mask , flow , style = model .eval (img , invert = False , diameter = diamet , channels = [ 0 , 0 ], do_3D = False , cellprob_threshold = 0.05 )
219+ mask , flow , style = model .eval (img , invert = False , diameter = diamet , do_3D = False , cellprob_threshold = 0.05 )
220220 ## convert cellpose result to label image (cellpose result are not touching border, make it as junctions)
221221 return fromcellpose_tojunctions (mask )
222222
223+ def run_cellpose_3 ( img , scaleXY , diameter = 7 , verbose = True , progress_bar = None ):
224+ """ Use CellPose3 instead of main Cellpose(SAM), using Appose environment """
225+ feature = "cp3-" + ut .get_cuda_feature ()
226+ try :
227+ pixi_file = resources .files ("fish_feats.resources" ).joinpath ("pixi_cp3.toml" )
228+ ut .show_info ("Build/Load tensorflow environment" )
229+ env = appose .pixi ( pixi_file ).log_debug ()
230+ env = env .subscribe_output ( lambda line : print ("OUT:" , line , end = "" ) )
231+ env = env .subscribe_error ( lambda line : print ("DBG:" , line , end = "" ) )
232+ env = env .environment ( feature ).build ()
233+ ut .show_info (f"Environment built at: { env .base ()} " )
234+ python = env .python ().init ("import numpy as np; from cellpose import models" \
235+ "" )
236+ if progress_bar is None :
237+ progress_bar = ut .start_progress ( None , total = 1 , descr = "Cellpose3 segmentation" )
238+ toclose = True
239+ else :
240+ progress_bar .set_description ( "Cellpose3 segmentation" )
241+ toclose = False
242+
243+ def log_listener (event ):
244+ """ Transfer appose task message to the main logger """
245+ if event .current and event .maximum :
246+ print ( f"Segmenting slice { event .current } /{ event .maximum } " )
247+ else :
248+ if event .message :
249+ print ( f"[task] { event .message } " )
250+
251+ try :
252+ cellpose3_script = resources .files ("fish_feats.resources" ).joinpath ("run_cellpose3.py" )
253+ cellpose3_script = cellpose3_script .read_text ()
254+ with share_as_ndarray (img ) as image :
255+ task = python .task ( cellpose3_script )
256+ task .listen ( log_listener )
257+ task .inputs ["img" ] = image
258+ task .inputs ["cell_diameter" ] = diameter
259+ task .inputs ["scale_xy" ] = scaleXY
260+ task .wait_for ()
261+ result = image .ndarray ()
262+ mask = np .uint16 ( result )
263+ return fromcellpose_tojunctions (mask )
264+ except Exception as e :
265+ raise RuntimeError ("Running cellpose3 in separated environement failed" ) from e
266+ finally :
267+ python .close ()
268+ if toclose :
269+ ut .close_progress ( None , progress_bar = progress_bar )
270+ except Exception as e :
271+ raise RuntimeError ("Cellpose3 in separated environement failed" ) from e
272+
273+ def getNuclei_stardist2DAsso3D (nucimg , scaleXY , proba = 0.55 , overlap = 0.1 , assoMode = "Munkres" , assolim = 3 , threshold_overlap = 0.25 , verbose = True , progress_bar = None ):
274+ """ Segment nuclei with Stardist2D and reconstruct in 3D - return the nuclei list """
275+
276+ ## segment 2D
277+ appose = not ut .has_dependency ( "stardist" )
278+ if appose :
279+ labnuc = stardist2D_appose (nucimg , prob = proba , over = overlap , progress_bar = progress_bar )
280+ else :
281+ labnuc = stardist2D_local (nucimg , prob = proba , over = overlap , progress_bar = progress_bar )
282+
283+
223284def fromcellpose_tojunctions (mask ):
224285 bined = binary_closing ( find_boundaries (mask ), footprint = np .ones ((10 ,10 )) )
225286 return junctions_to_label ( skeletonize ( bined ) )
@@ -233,9 +294,13 @@ def segmentJunctions(img, mode, scaleXY, imagedir, imagename, diameter=7, chunki
233294 if os .path .exists (tmpdir_path ):
234295 os .chmod (tmpdir_path , stat .S_IRUSR | stat .S_IRGRP | stat .S_IROTH | stat .S_IWUSR | stat .S_IWGRP | stat .S_IWOTH | stat .S_IXUSR )
235296 shutil .rmtree (tmpdir_path , ignore_errors = True )
236- if (isinstance (mode , str ) and mode .lower () == "cellpose" ):
237- show_info ("Starting segmentation with CellPose..." )
238- seged = run_cellpose (img , scaleXY , diameter = diameter , verbose = verbose )
297+ if (isinstance (mode , str ) and mode [0 :8 ].lower () == "cellpose" ):
298+ if mode .lower () == "cellposesam" :
299+ show_info ("Starting segmentation with CellPose-SAM..." )
300+ seged = run_cellpose (img , scaleXY , diameter = diameter , verbose = verbose )
301+ elif mode .lower () == "cellpose3" :
302+ show_info ("Starting segmentation with CellPose3, cyto3 model..." )
303+ seged = run_cellpose_3 (img , scaleXY , diameter = diameter , verbose = verbose )
239304 if (isinstance (mode , str ) and mode .lower () == "epyseg-dask" ):
240305 from fish_feats .DaskedEpyseg import run_dasked_epyseg
241306 show_info ("Starting segmentation with dasked Epyseg..." )
0 commit comments