Skip to content

Commit

Permalink
fix process ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
arthursw committed Mar 12, 2021
1 parent 578314d commit c1e02a2
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions ms_lesion_segmentation/animaMSLongitudinalPreprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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])
# 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))

0 comments on commit c1e02a2

Please sign in to comment.