99import pybdv
1010import tifffile
1111
12+ from cluster_tools .utils .volume_utils import write_format_metadata
13+ from elf .io import open_file
14+ from skimage .transform import rescale
15+
1216
1317def _read_resolution_and_unit_flamingo (mdata_path ):
1418 resolution = None
@@ -106,6 +110,51 @@ def derive_scale_factors(shape):
106110 return scale_factors
107111
108112
113+ def _to_bdv (
114+ data , out_path , scale_factors , n_threads , resolution , unit , channel_id , channel_name , tile_id , tile_transformation
115+ ):
116+ pybdv .make_bdv (
117+ data , out_path ,
118+ downscale_factors = scale_factors , downscale_mode = "mean" ,
119+ n_threads = n_threads ,
120+ resolution = resolution , unit = unit ,
121+ attributes = {
122+ "channel" : {"id" : channel_id , "name" : channel_name }, "tile" : {"id" : tile_id , "name" : str (tile_id )},
123+ "angle" : {"id" : 0 , "name" : "0" }, "illumination" : {"id" : 0 , "name" : "0" }
124+ },
125+ affine = tile_transformation ,
126+ )
127+
128+
129+ def _to_ome_zarr (
130+ data , out_path , scale_factors , n_threads , resolution , unit , channel_id , channel_name , tile_id , tile_transformation
131+ ):
132+ # Write the base dataset.
133+ base_key = f"c{ channel_id } -t{ tile_id } "
134+ chunks = (128 , 128 , 128 )
135+ with open_file (out_path , "a" ) as f :
136+ ds = f .create_dataset (f"{ base_key } /s0" , shape = data .shape , compression = 'gzip' ,
137+ chunks = chunks , dtype = data .dtype )
138+ ds .n_threads = n_threads
139+ ds [:] = data
140+
141+ # TODO parallelized implementation.
142+ # Do downscaling.
143+ for level , scale_factor in enumerate (scale_factors , 1 ):
144+ inv_scale = [1.0 / sc for sc in scale_factor ]
145+ data = rescale (data , inv_scale , preserve_range = True ).astype (data .dtype )
146+ ds = f .create_dataset (f"{ base_key } /s{ level } " , shape = data .shape , compression = 'gzip' ,
147+ chunks = chunks , dtype = data .dtype )
148+ ds .n_threads = n_threads
149+ ds [:] = data
150+
151+ # Write the ome zarr metadata.
152+ metadata_dict = {"unit" : unit , "resolution" : resolution }
153+ write_format_metadata (
154+ "ome.zarr" , out_path , metadata_dict , scale_factors = scale_factors , prefix = base_key
155+ )
156+
157+
109158def convert_lightsheet_to_bdv (
110159 root : str ,
111160 channel_folders : Dict [str , str ],
@@ -169,6 +218,11 @@ def convert_lightsheet_to_bdv(
169218 ext = os .path .splitext (out_path )[1 ]
170219 if ext == "" :
171220 out_path = str (Path (out_path ).with_suffix (".n5" ))
221+ conversion_function = _to_bdv
222+ elif ext == ".zarr" :
223+ conversion_function = _to_ome_zarr
224+ else :
225+ conversion_function = _to_bdv
172226
173227 # Iterate over the channels
174228 for channel_id , (channel_name , channel_folder ) in enumerate (channel_folders .items ()):
@@ -197,7 +251,7 @@ def convert_lightsheet_to_bdv(
197251 assert len (metadata_paths ) == len (file_paths )
198252 resolution , unit , tile_transformations = read_metadata_flamingo (metadata_paths , center_tiles )
199253
200- if channel_name is None or channel_name .strip () == "" : # channel name is empty, assign channel id as name
254+ if channel_name is None or channel_name .strip () == "" : # channel name is empty, assign channel id as name
201255 channel_name = str (channel_id )
202256
203257 for tile_id , (file_path , tile_transformation ) in enumerate (zip (file_paths , tile_transformations )):
@@ -213,16 +267,9 @@ def convert_lightsheet_to_bdv(
213267 if scale_factors is None :
214268 scale_factors = derive_scale_factors (data .shape )
215269
216- pybdv .make_bdv (
217- data , out_path ,
218- downscale_factors = scale_factors , downscale_mode = "mean" ,
219- n_threads = n_threads ,
220- resolution = resolution , unit = unit ,
221- attributes = {
222- "channel" : {"id" : channel_id , "name" : channel_name }, "tile" : {"id" : tile_id , "name" : str (tile_id )},
223- "angle" : {"id" : 0 , "name" : "0" }, "illumination" : {"id" : 0 , "name" : "0" }
224- },
225- affine = tile_transformation ,
270+ conversion_function (
271+ data , out_path , scale_factors , n_threads , resolution , unit ,
272+ channel_id , channel_name , tile_id , tile_transformation
226273 )
227274
228275
0 commit comments