@@ -207,9 +207,42 @@ def _write_missing_views(out_path):
207207 tree .write (xml_path )
208208
209209
210+ def _parse_shape (metadata_file ):
211+ depth , height , width = None , None , None
212+
213+ with open (metadata_file , "r" ) as f :
214+ for line in f .readlines ():
215+ line = line .strip ().rstrip ("\n " )
216+ if line .startswith ("AOI width" ):
217+ width = int (line .split (" " )[- 1 ])
218+ if line .startswith ("AOI height" ):
219+ height = int (line .split (" " )[- 1 ])
220+ if line .startswith ("Number of planes saved" ):
221+ depth = int (line .split (" " )[- 1 ])
222+
223+ assert depth is not None
224+ assert height is not None
225+ assert width is not None
226+ return (depth , height , width )
227+
228+
229+ def _load_data (file_path , metadata_file ):
230+ if Path (file_path ).suffix == ".raw" :
231+ shape = _parse_shape (metadata_file )
232+ data = np .memmap (file_path , mode = "r" , dtype = "uint16" , shape = shape )
233+ else :
234+ try :
235+ data = tifffile .memmap (file_path , mode = "r" )
236+ except ValueError :
237+ print (f"Could not memmap the data from { file_path } . Fall back to load it into memory." )
238+ data = tifffile .imread (file_path )
239+ return data
240+
241+
210242def convert_lightsheet_to_bdv (
211243 root : str ,
212244 out_path : str ,
245+ file_ext : str = ".tif" ,
213246 attribute_parser : callable = flamingo_filename_parser ,
214247 attribute_names : Optional [Dict [str , Dict [int , str ]]] = None ,
215248 metadata_file_name_pattern : Optional [str ] = None ,
@@ -233,6 +266,8 @@ def convert_lightsheet_to_bdv(
233266 root: Folder that contains the image data stored as tifs.
234267 This function will take into account all tif files in folders beneath this root directory.
235268 out_path: Output path where the converted data is saved.
269+ file_ext: The name of the file extension. By default assumes tif files (.tif).
270+ Change to '.raw' to read files stored in raw format instead.
236271 attribute_parser: TODO
237272 metadata_file_name_pattern: The pattern for the names of files that contain the metadata.
238273 For flamingo metadata the following pattern should work: '*_Settings.txt'.
@@ -264,7 +299,11 @@ def convert_lightsheet_to_bdv(
264299 elif ext == ".zarr" :
265300 convert_to_ome_zarr = True
266301
267- files = sorted (glob (os .path .join (root , "**/*.tif" ), recursive = True ))
302+ files = sorted (glob (os .path .join (root , f"**/*{ file_ext } " ), recursive = True ))
303+ # Raise an error if we could not find any files.
304+ if len (files ) == 0 :
305+ raise ValueError (f"Could not find any files in { root } with extension { file_ext } ." )
306+
268307 if metadata_file_name_pattern is None :
269308 metadata_files = [None ] * len (files )
270309 offset = None
@@ -275,7 +314,7 @@ def convert_lightsheet_to_bdv(
275314 recursive = True
276315 )
277316 )
278- assert len (metadata_files ) == len (files )
317+ assert len (metadata_files ) == len (files ), f" { len ( metadata_files ) } , { len ( files ) } "
279318
280319 if center_tiles :
281320 start_positions = []
@@ -316,12 +355,7 @@ def convert_lightsheet_to_bdv(
316355 )
317356
318357 print (f"Converting tp={ timepoint } , channel={ attributes ['channel' ]} , tile={ attributes ['tile' ]} " )
319- try :
320- data = tifffile .memmap (file_path , mode = "r" )
321- except ValueError :
322- print (f"Could not memmap the data from { file_path } . Fall back to load it into memory." )
323- data = tifffile .imread (file_path )
324-
358+ data = _load_data (file_path , metadata_file )
325359 if scale_factors is None :
326360 scale_factors = derive_scale_factors (data .shape )
327361
@@ -337,6 +371,7 @@ def convert_lightsheet_to_bdv(
337371 affine = tile_transformation ,
338372 timepoint = timepoint ,
339373 setup_id = setup_id ,
374+ chunks = (128 , 128 , 128 ),
340375 )
341376
342377 # We don't need to add additional xml metadata if we convert to ome-zarr.
0 commit comments