Skip to content

Commit 57695fb

Browse files
Support loading raw files
1 parent bd8f29f commit 57695fb

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

flamingo_tools/data_conversion.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,29 @@ def _write_missing_views(out_path):
207207
tree.write(xml_path)
208208

209209

210-
def _load_data(file_path):
211-
# TODO how do we read the raw data.
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):
212230
if Path(file_path).suffix == ".raw":
213-
pass
231+
shape = _parse_shape(metadata_file)
232+
data = np.memmap(file_path, mode="r", dtype="uint16", shape=shape)
214233
else:
215234
try:
216235
data = tifffile.memmap(file_path, mode="r")
@@ -223,7 +242,7 @@ def _load_data(file_path):
223242
def convert_lightsheet_to_bdv(
224243
root: str,
225244
out_path: str,
226-
ext: str = ".tif",
245+
file_ext: str = ".tif",
227246
attribute_parser: callable = flamingo_filename_parser,
228247
attribute_names: Optional[Dict[str, Dict[int, str]]] = None,
229248
metadata_file_name_pattern: Optional[str] = None,
@@ -247,7 +266,7 @@ def convert_lightsheet_to_bdv(
247266
root: Folder that contains the image data stored as tifs.
248267
This function will take into account all tif files in folders beneath this root directory.
249268
out_path: Output path where the converted data is saved.
250-
ext: The name of the file extension. By default assumes tif files (.tif).
269+
file_ext: The name of the file extension. By default assumes tif files (.tif).
251270
Change to '.raw' to read files stored in raw format instead.
252271
attribute_parser: TODO
253272
metadata_file_name_pattern: The pattern for the names of files that contain the metadata.
@@ -280,10 +299,10 @@ def convert_lightsheet_to_bdv(
280299
elif ext == ".zarr":
281300
convert_to_ome_zarr = True
282301

283-
files = sorted(glob(os.path.join(root, f"**/*{ext}"), recursive=True))
302+
files = sorted(glob(os.path.join(root, f"**/*{file_ext}"), recursive=True))
284303
# Raise an error if we could not find any files.
285304
if len(files) == 0:
286-
raise ValueError(f"Could not find any files in {root} with extension {ext}.")
305+
raise ValueError(f"Could not find any files in {root} with extension {file_ext}.")
287306

288307
if metadata_file_name_pattern is None:
289308
metadata_files = [None] * len(files)
@@ -295,7 +314,7 @@ def convert_lightsheet_to_bdv(
295314
recursive=True
296315
)
297316
)
298-
assert len(metadata_files) == len(files)
317+
assert len(metadata_files) == len(files), f"{len(metadata_files)}, {len(files)}"
299318

300319
if center_tiles:
301320
start_positions = []
@@ -336,7 +355,7 @@ def convert_lightsheet_to_bdv(
336355
)
337356

338357
print(f"Converting tp={timepoint}, channel={attributes['channel']}, tile={attributes['tile']}")
339-
data = _load_data(file_path)
358+
data = _load_data(file_path, metadata_file)
340359
if scale_factors is None:
341360
scale_factors = derive_scale_factors(data.shape)
342361

@@ -352,6 +371,7 @@ def convert_lightsheet_to_bdv(
352371
affine=tile_transformation,
353372
timepoint=timepoint,
354373
setup_id=setup_id,
374+
chunks=(128, 128, 128),
355375
)
356376

357377
# We don't need to add additional xml metadata if we convert to ome-zarr.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from flamingo_tools.data_conversion import convert_lightsheet_to_bdv
2+
3+
4+
def main():
5+
root = "/mnt/lustre-grete/usr/u12086/moser/lightsheet/M_LR_000172_R"
6+
output = "/mnt/lustre-grete/usr/u12086/moser/lightsheet/M_LR_000172_R/converted.n5"
7+
convert_lightsheet_to_bdv(
8+
root, out_path=output, file_ext=".raw",
9+
metadata_file_name_pattern="*_Settings.txt"
10+
)
11+
12+
13+
main()

0 commit comments

Comments
 (0)