From c2cc450b165363cce8c12b728499b37bbd474b74 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 14:21:12 +0100 Subject: [PATCH 1/9] handle Nones Signed-off-by: neuronflow --- brainles_preprocessing/modality.py | 35 +++++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/brainles_preprocessing/modality.py b/brainles_preprocessing/modality.py index fca3219..2e320ef 100644 --- a/brainles_preprocessing/modality.py +++ b/brainles_preprocessing/modality.py @@ -42,15 +42,23 @@ def __init__( self, modality_name: str, input_path: str, - raw_bet_output_path: str = None, - raw_skull_output_path: str = None, - normalized_bet_output_path: str = None, - normalized_skull_output_path: str = None, + raw_bet_output_path: str | None = None, + raw_skull_output_path: str | None = None, + normalized_bet_output_path: str | None = None, + normalized_skull_output_path: str | None = None, normalizer: Optional[Normalizer] = None, atlas_correction: bool = True, ) -> None: + # basics self.modality_name = modality_name + self.input_path = turbopath(input_path) + self.current = self.input_path + + self.normalizer = normalizer + self.atlas_correction = atlas_correction + + # check that atleast one output is generated if ( raw_bet_output_path is None and normalized_bet_output_path is None @@ -60,26 +68,27 @@ def __init__( raise ValueError( "All output paths are None. At least one output path must be provided." ) - self.raw_bet_output_path = turbopath(raw_bet_output_path) - self.raw_skull_output_path = turbopath(raw_skull_output_path) + + # handle input paths + if raw_bet_output_path is not None: + self.raw_bet_output_path = turbopath(raw_bet_output_path) + + if raw_skull_output_path is not None: + self.raw_skull_output_path = turbopath(raw_skull_output_path) + if normalized_bet_output_path is not None: if normalizer is None: raise ValueError( "A normalizer must be provided if normalized_bet_output_path is not None." ) - self.normalized_bet_output_path = turbopath(normalized_bet_output_path) + self.normalized_bet_output_path = turbopath(normalized_bet_output_path) if normalized_skull_output_path is not None: if normalizer is None: raise ValueError( "A normalizer must be provided if normalized_skull_output_path is not None." ) - self.normalized_skull_output_path = turbopath(normalized_skull_output_path) - - self.normalizer = normalizer - self.atlas_correction = atlas_correction - - self.current = self.input_path + self.normalized_skull_output_path = turbopath(normalized_skull_output_path) @property def bet(self) -> bool: From 5a4bc893bceb99b0d9649438861ef1639e24b073 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 14:21:40 +0100 Subject: [PATCH 2/9] simpler example Signed-off-by: neuronflow --- .../example_modality_centric_preprocessor.py | 215 ++++++++---------- 1 file changed, 98 insertions(+), 117 deletions(-) diff --git a/example/example_modality_centric_preprocessor.py b/example/example_modality_centric_preprocessor.py index 5fc002c..f89a119 100644 --- a/example/example_modality_centric_preprocessor.py +++ b/example/example_modality_centric_preprocessor.py @@ -13,128 +13,109 @@ def preprocess(inputDir): inputDir = turbopath(inputDir) - try: - print("*** start ***") - - # where are the raw mr files? - btk_raw_dir = turbopath(inputDir) - - # is the exam already processed? - brainles_dir = turbopath(inputDir) + "/" + inputDir.name + "_brainles" - raw_bet_dir = brainles_dir / "raw_bet" - norm_bet_dir = brainles_dir / "normalized_bet" - raw_skull_dir = brainles_dir / "raw_skull" - norm_skull_dir = brainles_dir / "normalized_skull" - - # if not os.path.exists(prep_dir): - # if os.path.exists(prep_dir): - t1_file = btk_raw_dir.files("*t1.nii.gz") - t1c_file = btk_raw_dir.files("*t1c.nii.gz") - t2_file = btk_raw_dir.files("*t2.nii.gz") - flair_file = btk_raw_dir.files("*fla.nii.gz") - - if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1: - # print(t1_file) - # print(t1c_file) - # print(t2_file) - # print(flair_file) - - t1File = t1_file[0] - t1cFile = t1c_file[0] - t2File = t2_file[0] - flaFile = flair_file[0] - - # normalizer - percentile_normalizer = PercentileNormalizer( - lower_percentile=0.1, - upper_percentile=99.9, - lower_limit=0, - upper_limit=1, - ) - - # define modalities - center = Modality( - modality_name="t1c", - input_path=t1cFile, - raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1c_bet_raw.nii.gz", + print("*** start ***") + + # where are the raw mr files? + btk_raw_dir = turbopath(inputDir) + # is the exam already processed? + brainles_dir = turbopath(inputDir) + "/" + inputDir.name + "_brainles" + raw_bet_dir = brainles_dir / "raw_bet" + norm_bet_dir = brainles_dir / "normalized_bet" + raw_skull_dir = brainles_dir / "raw_skull" + norm_skull_dir = brainles_dir / "normalized_skull" + # if not os.path.exists(prep_dir): + # if os.path.exists(prep_dir): + t1_file = btk_raw_dir.files("*t1.nii.gz") + t1c_file = btk_raw_dir.files("*t1c.nii.gz") + t2_file = btk_raw_dir.files("*t2.nii.gz") + flair_file = btk_raw_dir.files("*fla.nii.gz") + if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1: + # print(t1_file) + # print(t1c_file) + # print(t2_file) + # print(flair_file) + t1File = t1_file[0] + t1cFile = t1c_file[0] + t2File = t2_file[0] + flaFile = flair_file[0] + # normalizer + percentile_normalizer = PercentileNormalizer( + lower_percentile=0.1, + upper_percentile=99.9, + lower_limit=0, + upper_limit=1, + ) + # define modalities + center = Modality( + modality_name="t1c", + input_path=t1cFile, + raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1c_bet_raw.nii.gz", + raw_skull_output_path=raw_skull_dir / inputDir.name + + "_t1c_skull_raw.nii.gz", + # normalized_bet_output_path=norm_bet_dir / inputDir.name + # + "_t1c_bet_normalized.nii.gz", + normalized_skull_output_path=norm_skull_dir / inputDir.name + + "_t1c_skull_normalized.nii.gz", + atlas_correction=True, + normalizer=percentile_normalizer, + ) + moving_modalities = [ + Modality( + modality_name="t1", + input_path=t1File, + raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1_bet_raw.nii.gz", raw_skull_output_path=raw_skull_dir / inputDir.name - + "_t1c_skull_raw.nii.gz", + + "_t1_skull_raw.nii.gz", normalized_bet_output_path=norm_bet_dir / inputDir.name - + "_t1c_bet_normalized.nii.gz", + + "_t1_bet_normalized.nii.gz", normalized_skull_output_path=norm_skull_dir / inputDir.name - + "_t1c_skull_normalized.nii.gz", + + "_t1_skull_normalized.nii.gz", atlas_correction=True, normalizer=percentile_normalizer, - ) - - moving_modalities = [ - Modality( - modality_name="t1", - input_path=t1File, - raw_bet_output_path=raw_bet_dir / inputDir.name - + "_t1_bet_raw.nii.gz", - raw_skull_output_path=raw_skull_dir / inputDir.name - + "_t1_skull_raw.nii.gz", - normalized_bet_output_path=norm_bet_dir / inputDir.name - + "_t1_bet_normalized.nii.gz", - normalized_skull_output_path=norm_skull_dir / inputDir.name - + "_t1_skull_normalized.nii.gz", - atlas_correction=True, - normalizer=percentile_normalizer, - ), - Modality( - modality_name="t2", - input_path=t2File, - raw_bet_output_path=raw_bet_dir / inputDir.name - + "_t2_bet_raw.nii.gz", - raw_skull_output_path=raw_skull_dir / inputDir.name - + "_t2_skull_raw.nii.gz", - normalized_bet_output_path=norm_bet_dir / inputDir.name - + "_t2_bet_normalized.nii.gz", - normalized_skull_output_path=norm_skull_dir / inputDir.name - + "_t2_skull_normalized.nii.gz", - atlas_correction=True, - normalizer=percentile_normalizer, - ), - Modality( - modality_name="flair", - input_path=flaFile, - raw_bet_output_path=raw_bet_dir / inputDir.name - + "_fla_bet_raw.nii.gz", - raw_skull_output_path=raw_skull_dir / inputDir.name - + "_fla_skull_raw.nii.gz", - normalized_bet_output_path=norm_bet_dir / inputDir.name - + "_fla_bet_normalized.nii.gz", - normalized_skull_output_path=norm_skull_dir / inputDir.name - + "_fla_skull_normalized.nii.gz", - atlas_correction=True, - normalizer=percentile_normalizer, - ), - ] - - preprocessor = Preprocessor( - center_modality=center, - moving_modalities=moving_modalities, - registrator=NiftyRegRegistrator(), - brain_extractor=HDBetExtractor(), - temp_folder="tempo", - limit_cuda_visible_devices="1", - ) - - preprocessor.run( - save_dir_coregistration=brainles_dir + "/co-registration", - save_dir_atlas_registration=brainles_dir + "/atlas-registration", - save_dir_atlas_correction=brainles_dir + "/atlas-correction", - save_dir_brain_extraction=brainles_dir + "/brain-extraction", - ) - - except Exception as e: - print("error: " + str(e)) - print("conversion error for:", inputDir) - - time = str(datetime.datetime.now().time()) - - print("** finished:", inputDir.name, "at:", time) + ), + Modality( + modality_name="t2", + input_path=t2File, + raw_bet_output_path=raw_bet_dir / inputDir.name + "_t2_bet_raw.nii.gz", + raw_skull_output_path=raw_skull_dir / inputDir.name + + "_t2_skull_raw.nii.gz", + normalized_bet_output_path=norm_bet_dir / inputDir.name + + "_t2_bet_normalized.nii.gz", + normalized_skull_output_path=norm_skull_dir / inputDir.name + + "_t2_skull_normalized.nii.gz", + atlas_correction=True, + normalizer=percentile_normalizer, + ), + Modality( + modality_name="flair", + input_path=flaFile, + raw_bet_output_path=raw_bet_dir / inputDir.name + "_fla_bet_raw.nii.gz", + raw_skull_output_path=raw_skull_dir / inputDir.name + + "_fla_skull_raw.nii.gz", + normalized_bet_output_path=norm_bet_dir / inputDir.name + + "_fla_bet_normalized.nii.gz", + normalized_skull_output_path=norm_skull_dir / inputDir.name + + "_fla_skull_normalized.nii.gz", + atlas_correction=True, + normalizer=percentile_normalizer, + ), + ] + + preprocessor = Preprocessor( + center_modality=center, + moving_modalities=moving_modalities, + registrator=NiftyRegRegistrator(), + brain_extractor=HDBetExtractor(), + temp_folder="tempo", + limit_cuda_visible_devices="1", + ) + + preprocessor.run( + save_dir_coregistration=brainles_dir + "/co-registration", + save_dir_atlas_registration=brainles_dir + "/atlas-registration", + save_dir_atlas_correction=brainles_dir + "/atlas-correction", + save_dir_brain_extraction=brainles_dir + "/brain-extraction", + ) ### *** GOGOGO *** ### From b327ed1a93e9191f32e88ff547a2c823b8d08a52 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 14:28:41 +0100 Subject: [PATCH 3/9] make skullstripping check work for none paths Signed-off-by: neuronflow --- brainles_preprocessing/modality.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/brainles_preprocessing/modality.py b/brainles_preprocessing/modality.py index 2e320ef..ff4a94a 100644 --- a/brainles_preprocessing/modality.py +++ b/brainles_preprocessing/modality.py @@ -92,7 +92,10 @@ def __init__( @property def bet(self) -> bool: - return any([self.raw_bet_output_path, self.normalized_bet_output_path]) + return any( + path is not None + for path in [self.raw_bet_output_path, self.normalized_skull_output_path] + ) def normalize( self, From d6db3ffeb3b3f3c92835548b0c31ee92ce3c9476 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 14:35:00 +0100 Subject: [PATCH 4/9] handle paths that are none Signed-off-by: neuronflow --- brainles_preprocessing/preprocessor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/brainles_preprocessing/preprocessor.py b/brainles_preprocessing/preprocessor.py index e03d588..f578016 100644 --- a/brainles_preprocessing/preprocessor.py +++ b/brainles_preprocessing/preprocessor.py @@ -203,12 +203,12 @@ def run( # now we save images that are not skullstripped for modality in self.all_modalities: - if modality.raw_skull_output_path: + if modality.raw_skull_output_path is not None: modality.save_current_image( modality.raw_skull_output_path, normalization=False, ) - if modality.normalized_skull_output_path: + if modality.normalized_skull_output_path is not None: modality.save_current_image( modality.normalized_skull_output_path, normalization=True, @@ -245,7 +245,7 @@ def run( modality.raw_bet_output_path, normalization=False, ) - if modality.normalized_bet_output_path: + if modality.normalized_bet_output_path is not None: modality.save_current_image( modality.normalized_bet_output_path, normalization=True, @@ -256,7 +256,7 @@ def _save_output( src: str, save_dir: Optional[str], ): - if save_dir: + if save_dir is not None: save_dir = turbopath(save_dir) shutil.copytree( src=src, From f37b9de9169d89c5628bbc4b062c3a99617d9364 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 14:45:46 +0100 Subject: [PATCH 5/9] we try with optional Signed-off-by: neuronflow --- brainles_preprocessing/modality.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/brainles_preprocessing/modality.py b/brainles_preprocessing/modality.py index ff4a94a..7d16dfa 100644 --- a/brainles_preprocessing/modality.py +++ b/brainles_preprocessing/modality.py @@ -42,10 +42,10 @@ def __init__( self, modality_name: str, input_path: str, - raw_bet_output_path: str | None = None, - raw_skull_output_path: str | None = None, - normalized_bet_output_path: str | None = None, - normalized_skull_output_path: str | None = None, + raw_bet_output_path: Optional[str] = None, + raw_skull_output_path: Optional[str] = None, + normalized_bet_output_path: Optional[str] = None, + normalized_skull_output_path: Optional[str] = None, normalizer: Optional[Normalizer] = None, atlas_correction: bool = True, ) -> None: From 0f91a744ba62ff9a5ca02ca190739ed0bff5f3ca Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 15:04:02 +0100 Subject: [PATCH 6/9] handle None cases Signed-off-by: neuronflow --- brainles_preprocessing/modality.py | 8 ++++++++ brainles_preprocessing/preprocessor.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/brainles_preprocessing/modality.py b/brainles_preprocessing/modality.py index 7d16dfa..81f6c07 100644 --- a/brainles_preprocessing/modality.py +++ b/brainles_preprocessing/modality.py @@ -72,9 +72,13 @@ def __init__( # handle input paths if raw_bet_output_path is not None: self.raw_bet_output_path = turbopath(raw_bet_output_path) + else: + self.raw_bet_output_path = raw_bet_output_path if raw_skull_output_path is not None: self.raw_skull_output_path = turbopath(raw_skull_output_path) + else: + self.raw_skull_output_path = raw_skull_output_path if normalized_bet_output_path is not None: if normalizer is None: @@ -82,6 +86,8 @@ def __init__( "A normalizer must be provided if normalized_bet_output_path is not None." ) self.normalized_bet_output_path = turbopath(normalized_bet_output_path) + else: + self.normalized_bet_output_path = normalized_bet_output_path if normalized_skull_output_path is not None: if normalizer is None: @@ -89,6 +95,8 @@ def __init__( "A normalizer must be provided if normalized_skull_output_path is not None." ) self.normalized_skull_output_path = turbopath(normalized_skull_output_path) + else: + self.normalized_skull_output_path = normalized_skull_output_path @property def bet(self) -> bool: diff --git a/brainles_preprocessing/preprocessor.py b/brainles_preprocessing/preprocessor.py index f578016..e1376f6 100644 --- a/brainles_preprocessing/preprocessor.py +++ b/brainles_preprocessing/preprocessor.py @@ -240,7 +240,7 @@ def run( # now we save images that are skullstripped for modality in self.all_modalities: - if modality.raw_bet_output_path: + if modality.raw_bet_output_path is not None: modality.save_current_image( modality.raw_bet_output_path, normalization=False, From 61885c6caf83b61c0334b1b85e50e51f5e705258 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 15:11:30 +0100 Subject: [PATCH 7/9] tempo Signed-off-by: neuronflow --- example/example_modality_centric_preprocessor.py | 4 ++-- {tempo => temporary_directory}/.gitignore | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename {tempo => temporary_directory}/.gitignore (100%) diff --git a/example/example_modality_centric_preprocessor.py b/example/example_modality_centric_preprocessor.py index f89a119..558c44b 100644 --- a/example/example_modality_centric_preprocessor.py +++ b/example/example_modality_centric_preprocessor.py @@ -106,8 +106,8 @@ def preprocess(inputDir): moving_modalities=moving_modalities, registrator=NiftyRegRegistrator(), brain_extractor=HDBetExtractor(), - temp_folder="tempo", - limit_cuda_visible_devices="1", + temp_folder="temporary_directory", + limit_cuda_visible_devices="0", ) preprocessor.run( diff --git a/tempo/.gitignore b/temporary_directory/.gitignore similarity index 100% rename from tempo/.gitignore rename to temporary_directory/.gitignore From f90a55a8b9359cb687fcc728f8c5ee8c5a2fe102 Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 15:12:47 +0100 Subject: [PATCH 8/9] exa Signed-off-by: neuronflow --- example/example_modality_centric_preprocessor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/example_modality_centric_preprocessor.py b/example/example_modality_centric_preprocessor.py index 558c44b..0b0c706 100644 --- a/example/example_modality_centric_preprocessor.py +++ b/example/example_modality_centric_preprocessor.py @@ -52,8 +52,8 @@ def preprocess(inputDir): raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1c_bet_raw.nii.gz", raw_skull_output_path=raw_skull_dir / inputDir.name + "_t1c_skull_raw.nii.gz", - # normalized_bet_output_path=norm_bet_dir / inputDir.name - # + "_t1c_bet_normalized.nii.gz", + normalized_bet_output_path=norm_bet_dir / inputDir.name + + "_t1c_bet_normalized.nii.gz", normalized_skull_output_path=norm_skull_dir / inputDir.name + "_t1c_skull_normalized.nii.gz", atlas_correction=True, From 146172eb68fdae4475c5167fe468e0bb86d5a4eb Mon Sep 17 00:00:00 2001 From: neuronflow Date: Thu, 15 Feb 2024 15:15:08 +0100 Subject: [PATCH 9/9] clean Signed-off-by: neuronflow --- example/example_modality_centric_preprocessor.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/example/example_modality_centric_preprocessor.py b/example/example_modality_centric_preprocessor.py index 0b0c706..384a139 100644 --- a/example/example_modality_centric_preprocessor.py +++ b/example/example_modality_centric_preprocessor.py @@ -1,6 +1,4 @@ # This script is an example of how to use the ModalityCentricPreprocessor class to preprocess a set of MR images. It is only here for quick development and testing purposes. It is not intended to be used in a production environment. -import datetime - from auxiliary.normalization.percentile_normalizer import PercentileNormalizer from auxiliary.turbopath import turbopath from tqdm import tqdm @@ -16,19 +14,17 @@ def preprocess(inputDir): print("*** start ***") # where are the raw mr files? - btk_raw_dir = turbopath(inputDir) - # is the exam already processed? brainles_dir = turbopath(inputDir) + "/" + inputDir.name + "_brainles" + raw_bet_dir = brainles_dir / "raw_bet" norm_bet_dir = brainles_dir / "normalized_bet" raw_skull_dir = brainles_dir / "raw_skull" norm_skull_dir = brainles_dir / "normalized_skull" - # if not os.path.exists(prep_dir): - # if os.path.exists(prep_dir): - t1_file = btk_raw_dir.files("*t1.nii.gz") - t1c_file = btk_raw_dir.files("*t1c.nii.gz") - t2_file = btk_raw_dir.files("*t2.nii.gz") - flair_file = btk_raw_dir.files("*fla.nii.gz") + + t1_file = inputDir.files("*t1.nii.gz") + t1c_file = inputDir.files("*t1c.nii.gz") + t2_file = inputDir.files("*t2.nii.gz") + flair_file = inputDir.files("*fla.nii.gz") if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1: # print(t1_file) # print(t1c_file)