Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable_none_outputs #64

Merged
merged 9 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions brainles_preprocessing/modality.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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:
# 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
Expand All @@ -60,30 +68,42 @@ 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)
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:
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)
else:
self.normalized_bet_output_path = 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)
else:
self.normalized_skull_output_path = normalized_skull_output_path

@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,
Expand Down
10 changes: 5 additions & 5 deletions brainles_preprocessing/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -240,12 +240,12 @@ 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,
)
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,
Expand All @@ -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,
Expand Down
215 changes: 96 additions & 119 deletions example/example_modality_centric_preprocessor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,128 +11,107 @@

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?
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"

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)
# 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="temporary_directory",
limit_cuda_visible_devices="0",
)

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 *** ###
Expand Down
File renamed without changes.
Loading