From c1e02a298b8ca11035302b6165354f56823099a0 Mon Sep 17 00:00:00 2001 From: Arthur Masson Date: Fri, 12 Mar 2021 10:50:01 +0100 Subject: [PATCH] fix process ordering --- .../animaMSLongitudinalPreprocessing.py | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/ms_lesion_segmentation/animaMSLongitudinalPreprocessing.py b/ms_lesion_segmentation/animaMSLongitudinalPreprocessing.py index 992d196..245b505 100644 --- a/ms_lesion_segmentation/animaMSLongitudinalPreprocessing.py +++ b/ms_lesion_segmentation/animaMSLongitudinalPreprocessing.py @@ -9,9 +9,9 @@ # The preprocessing consists in three or four steps: # - brain extraction +# - mask flair images with the union of the masks of both time points # - bias correction # - (optional) normalization on the given template -# - mask flair images with the union of the masks of both time points # Argument parsing parser = argparse.ArgumentParser( @@ -83,9 +83,9 @@ def call(command): # Preprocess all patients: # - brain extraction +# - mask flair images with the union of the masks of both time points # - bias correction # - normalize (optional) -# - crop for patientName in os.listdir(patients): patient = os.path.join(patients, patientName) @@ -101,8 +101,11 @@ def call(command): masks = [] - # For both time points: extract brain and remove bias - for flairName in ['flair_time01_on_middle_space.nii.gz', 'flair_time02_on_middle_space.nii.gz']: + flairs = ['flair_time01_on_middle_space.nii.gz', 'flair_time02_on_middle_space.nii.gz'] + groundTruths = ['ground_truth_expert1.nii.gz', 'ground_truth_expert2.nii.gz', 'ground_truth_expert3.nii.gz', 'ground_truth_expert4.nii.gz', 'ground_truth.nii.gz'] + + # For both time points: extract brain + for flairName in flairs: flair = os.path.join(patient, flairName) brain = os.path.join(patientOutput, flairName) @@ -111,18 +114,8 @@ def call(command): # Extract brain call(["python", animaBrainExtraction, "-i", flair, "--mask", mask, "--brain", brain]) - # Remove bias - call([animaN4BiasCorrection, "-i", brain, "-o", brain, "-B", "0.3"]) - - if templateFlair: - if os.path.exists(templateFlair): - # Normalize intensities with the given template - call([animaNyulStandardization, "-m", brain, "-r", templateFlair, "-o", brain]) - else: - print('Template file ' + templateFlair + ' not found, skipping normalization.') - masks.append(mask) - + maskUnion = os.path.join(patientOutput, 'brain_mask.nii.gz') # Compute the union of the masks of both time points @@ -132,15 +125,26 @@ def call(command): # Remove intermediate masks for mask in masks: os.remove(mask) - - # Copy the ground truths to the output directory - for imageName in ['ground_truth_expert1.nii.gz', 'ground_truth_expert2.nii.gz', 'ground_truth_expert3.nii.gz', 'ground_truth_expert4.nii.gz', 'ground_truth.nii.gz']: - shutil.copyfile(os.path.join(patient, imageName), os.path.join(patientOutput, imageName)) - # Mask original FLAIR images with the union mask - for flairName in ['flair_time01_on_middle_space.nii.gz', 'flair_time02_on_middle_space.nii.gz']: + # For both time points: mask, remove bias and normalize if necessary + for flairName in flairs: flair = os.path.join(patient, flairName) brain = os.path.join(patientOutput, flairName) - call([animaMaskImage, "-i", flair, "-m", maskUnion, "-o", brain]) \ No newline at end of file + # Mask original FLAIR images with the union mask + call([animaMaskImage, "-i", flair, "-m", maskUnion, "-o", brain]) + + # Remove bias + call([animaN4BiasCorrection, "-i", brain, "-o", brain, "-B", "0.3"]) + + if templateFlair: + if os.path.exists(templateFlair): + # Normalize intensities with the given template + call([animaNyulStandardization, "-m", brain, "-r", templateFlair, "-o", brain]) + else: + print('Template file ' + templateFlair + ' not found, skipping normalization.') + + # Copy the ground truths to the output directory + for imageName in groundTruths: + shutil.copyfile(os.path.join(patient, imageName), os.path.join(patientOutput, imageName))