From 84b09a5cd9f9cacc46406a99ec903420479a76ea Mon Sep 17 00:00:00 2001 From: alessandrofelder Date: Thu, 21 Nov 2024 10:27:21 +0000 Subject: [PATCH] simple crab downsampling --- examples/crab/downsample_source_images.py | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/crab/downsample_source_images.py diff --git a/examples/crab/downsample_source_images.py b/examples/crab/downsample_source_images.py new file mode 100644 index 0000000..18230b9 --- /dev/null +++ b/examples/crab/downsample_source_images.py @@ -0,0 +1,64 @@ +import argparse +from pathlib import Path + +from loguru import logger + +from brainglobe_template_builder.preproc.transform_utils import downsample + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Downsample source images") + parser.add_argument( + "--source_data", + type=str, + 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 " + "be written to the rawdata subfolder.", + 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) + + in_plane_resolution = 0.43 + out_of_plane_resolution = 5 + in_plane_factor = round(target_isotropic_resolution / in_plane_resolution) + axial_factor = round(target_isotropic_resolution / out_of_plane_resolution) + + template_raw_data = template_building_root / "rawdata" + template_raw_data.mkdir(exist_ok=True, parents=True) + + for sample_folder in source_data.iterdir(): + if ( + not str(sample_folder)[-6:-1] == "Crab_" + ): # find folders like Crab_3 + logger.info(f"{str(sample_folder)[-6:-1]} is not a crab folder") + continue + logger.info(f"Downsampling {sample_folder.name}...") + sample_id = str(sample_folder).split("_")[-1].lower() + stain = "Nuclear" + sample_filename = ( + f"sub-{sample_id}_res-{target_isotropic_resolution}" + f"um_stain-{stain}.tif" + ) + downsample( + sample_folder / "Nuclear/", + template_raw_data / sample_filename, + in_plane_factor, + axial_factor, + ) + logger.info(f"{sample_filename} downsampled.")