From c38292d6003898c14f54a0f2f0555d9cf096628a Mon Sep 17 00:00:00 2001 From: alessandrofelder Date: Thu, 15 Aug 2024 17:40:40 +0100 Subject: [PATCH 1/3] implement downsampling script for tadpoles. --- examples/tadpole/downsample_source_images.py | 74 ++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 examples/tadpole/downsample_source_images.py diff --git a/examples/tadpole/downsample_source_images.py b/examples/tadpole/downsample_source_images.py new file mode 100644 index 0000000..777a2cd --- /dev/null +++ b/examples/tadpole/downsample_source_images.py @@ -0,0 +1,74 @@ +import argparse +from pathlib import Path + +from brainglobe_utils.IO.image import read_z_stack, save_any +from loguru import logger + +from brainglobe_template_builder.preproc.transform_utils import ( + downsample_anisotropic_image_stack, +) + + +def downsample_tadpole(folder: Path) -> None: + logger.info(f"Downsampling {folder.name}...") + stack = read_z_stack(str(folder)) + + in_plane_resolution = 1 + out_of_plane_resolution = 3 + in_plane_factor = int(target_isotropic_resolution / in_plane_resolution) + axial_factor = int(target_isotropic_resolution / out_of_plane_resolution) + downsampled = downsample_anisotropic_image_stack( + stack, in_plane_factor=in_plane_factor, axial_factor=axial_factor + ) + + sample_id = str(folder).split("_")[1].lower() + channel = "blue" if str(folder).split("_")[2] == "488" else "orange" + sample_filename = ( + f"sub-{sample_id}_res-{target_isotropic_resolution}" + f"um_channel-{channel}.tif" + ) + save_any(downsampled, template_raw_data / sample_filename) + + logger.info(f"{sample_filename} downsampled.") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Downsample source images") + parser.add_argument( + "--source_data", + type=str, + help="Path to the source data folder", + required=True, + ) + parser.add_argument( + "--template_building_root", + type=str, + help="Path to the template-building root folder", + required=True, + ) + parser.add_argument( + "--target_isotropic_resolution", + type=int, + help="Target isotropic resolution", + required=True, + ) + + args = parser.parse_args() + + source_data = Path(args.source_data) + template_building_root = Path(args.template_building_root) + target_isotropic_resolution = int(args.target_isotropic_resolution) + + for subfolder_name in [ + "rawdata", + "logs", + "derivatives", + "scripts", + "templates", + ]: + subfolder = template_building_root / subfolder_name + subfolder.mkdir(exist_ok=True, parents=True) + + template_raw_data = template_building_root / "rawdata" + for folder in source_data.iterdir(): + downsample_tadpole(folder) From 62e221e2b764085b25232a884486291f260e6501 Mon Sep 17 00:00:00 2001 From: Alessandro Felder Date: Fri, 30 Aug 2024 13:47:01 +0100 Subject: [PATCH 2/3] expand help strings --- examples/tadpole/downsample_source_images.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/tadpole/downsample_source_images.py b/examples/tadpole/downsample_source_images.py index 777a2cd..e88423f 100644 --- a/examples/tadpole/downsample_source_images.py +++ b/examples/tadpole/downsample_source_images.py @@ -37,13 +37,15 @@ def downsample_tadpole(folder: Path) -> None: parser.add_argument( "--source_data", type=str, - help="Path to the source data folder", + help="Path to the source data folder. The source data should " \ + "contain a subfolder per subject, and tiffs within it.", required=True, ) parser.add_argument( "--template_building_root", type=str, - help="Path to the template-building root folder", + help="Path to the template-building root folder. Results will " \ + "be written to the rawdata subfolder.", required=True, ) parser.add_argument( From 6cce8c9a9aec9d9941031ed668932016727b3785 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 30 Aug 2024 12:47:13 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/tadpole/downsample_source_images.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tadpole/downsample_source_images.py b/examples/tadpole/downsample_source_images.py index e88423f..e5e4662 100644 --- a/examples/tadpole/downsample_source_images.py +++ b/examples/tadpole/downsample_source_images.py @@ -37,14 +37,14 @@ def downsample_tadpole(folder: Path) -> None: parser.add_argument( "--source_data", type=str, - help="Path to the source data folder. The source data should " \ + help="Path to the source data folder. The source data should " "contain a subfolder per subject, and tiffs within it.", required=True, ) parser.add_argument( "--template_building_root", type=str, - help="Path to the template-building root folder. Results will " \ + help="Path to the template-building root folder. Results will " "be written to the rawdata subfolder.", required=True, )