44import numpy as np
55import xarray as xr
66import spatialdata as sd
7- from csbdeep .utils import normalize
7+ #from csbdeep.utils import normalize
8+ from csbdeep .data import Normalizer , normalize_mi_ma
89from stardist .models import StarDist2D
910
1011
@@ -24,8 +25,8 @@ def convert_to_lower_dtype(arr):
2425
2526## VIASH START
2627par = {
27- "input" : "./ resources_test/common/2023_10x_mouse_brain_xenium_rep1/dataset .zarr" ,
28- "output" : "./ temp/stardist/segmentation.zarr" ,
28+ "input" : "resources_test/task_ist_preprocessing/mouse_brain_combined/raw_ist .zarr" ,
29+ "output" : "temp/stardist/segmentation.zarr" ,
2930 "model" : "2D_versatile_fluo"
3031}
3132
@@ -38,10 +39,34 @@ def convert_to_lower_dtype(arr):
3839transformation = sdata ['morphology_mip' ]['scale0' ].image .transform .copy ()
3940
4041# Segment image
42+
4143# Load pretrained model
4244model = StarDist2D .from_pretrained (par ['model' ])
45+
4346# Segment on normalized image
44- labels , _ = model .predict_instances (normalize (image )[0 ,:,:]) # scale = None, **hyperparams)
47+ #labels, _ = model.predict_instances(normalize(image)[0,:,:]) # scale = None, **hyperparams)
48+
49+ # from https://github.com/stardist/stardist/blob/main/examples/other2D/predict_big_data.ipynb
50+ class MyNormalizer (Normalizer ):
51+ def __init__ (self , mi , ma ):
52+ self .mi , self .ma = mi , ma
53+ def before (self , x , axes ):
54+ return normalize_mi_ma (x , self .mi , self .ma , dtype = np .float32 )
55+ def after (* args , ** kwargs ):
56+ assert False
57+ @property
58+ def do_after (self ):
59+ return False
60+
61+ mi , ma = np .percentile (image , [1 ,99.8 ])
62+ normalizer = MyNormalizer (mi , ma )
63+ block_size = min (image .shape [1 ] // 3 , 4096 )
64+ offset = min (block_size // 5.5 , 128 )
65+
66+ labels , _ = model .predict_instances_big (
67+ image [0 ,:,:], axes = 'YX' , block_size = block_size , min_overlap = offset , context = offset , normalizer = normalizer #, n_tiles=(4,4)
68+ )
69+
4570
4671
4772# Create output
0 commit comments