Skip to content
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
38 changes: 26 additions & 12 deletions brainles_preprocessing/modality.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def __init__(
else:
self.normalized_defaced_output_path = None

self.steps = {k: None for k in PreprocessorSteps}
self.steps: Dict[PreprocessorSteps, Path | None] = {
k: None for k in PreprocessorSteps
}
self.steps[PreprocessorSteps.INPUT] = self.input_path

@property
Expand Down Expand Up @@ -265,7 +267,7 @@ def register(
moving_image_path=self.current,
transformed_image_path=registered,
matrix_path=registered_matrix,
log_file_path=registered_log,
log_file_path=str(registered_log),
)
self.current = registered
self.steps[step] = registered
Expand Down Expand Up @@ -394,24 +396,24 @@ def transform(
), "Coregistration must be performed before applying atlas registration."

registrator.transform(
fixed_image_path=fixed_image_path,
moving_image_path=self.steps[PreprocessorSteps.INPUT],
transformed_image_path=transformed,
fixed_image_path=str(fixed_image_path),
moving_image_path=str(self.steps[PreprocessorSteps.INPUT]),
transformed_image_path=str(transformed),
matrix_path=[
self.transformation_paths[
PreprocessorSteps.COREGISTERED
], # coregistration matrix
transformation_matrix_path, # atlas registration matrix
],
log_file_path=transformed_log,
log_file_path=str(transformed_log),
)
else:
registrator.transform(
fixed_image_path=fixed_image_path,
moving_image_path=self.current,
transformed_image_path=transformed,
matrix_path=transformation_matrix_path,
log_file_path=transformed_log,
fixed_image_path=str(fixed_image_path),
moving_image_path=str(self.current),
transformed_image_path=str(transformed),
matrix_path=str(transformation_matrix_path),
log_file_path=str(transformed_log),
)

self.current = transformed
Expand Down Expand Up @@ -638,6 +640,12 @@ def extract_brain_region(

if self.bet:
self.current = bet

if self.bet_mask_output_path:
self.save_mask(
mask_path=mask_path,
output_path=self.bet_mask_output_path,
)
return mask_path

def deface(
Expand Down Expand Up @@ -684,7 +692,7 @@ def deface(
moving_image_path=self.steps[PreprocessorSteps.BET],
transformed_image_path=atlas_bet,
matrix_path=atlas_bet_M,
log_file_path=defaced_dir_path / "atlas_bet.log",
log_file_path=str(defaced_dir_path / "atlas_bet.log"),
)

deface_mask_atlas = defaced_dir_path / "deface_mask_atlas.nii.gz"
Expand All @@ -706,6 +714,12 @@ def deface(
mask_image_path=mask_path,
)

if self.defacing_mask_output_path:
self.save_mask(
mask_path=mask_path,
output_path=self.defacing_mask_output_path,
)

return mask_path
else:
logger.warning(
Expand Down
3 changes: 2 additions & 1 deletion brainles_preprocessing/normalization/normalizer_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
from numpy.typing import NDArray


class Normalizer(ABC):
Expand All @@ -10,7 +11,7 @@ def __init__(self):
super().__init__()

@abstractmethod
def normalize(self, image):
def normalize(self, image) -> NDArray:
"""
Normalize the input image based on the chosen method.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from .normalizer_base import Normalizer
from numpy.typing import NDArray


class PercentileNormalizer(Normalizer):
Expand Down Expand Up @@ -29,7 +30,7 @@ def __init__(
self.lower_limit = lower_limit
self.upper_limit = upper_limit

def normalize(self, image: np.ndarray):
def normalize(self, image: np.ndarray) -> NDArray:
"""
Normalize the input image using percentile-based mapping.

Expand Down
3 changes: 2 additions & 1 deletion brainles_preprocessing/normalization/windowing_normalizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from .normalizer_base import Normalizer
from numpy.typing import NDArray


class WindowingNormalizer(Normalizer):
Expand All @@ -19,7 +20,7 @@ def __init__(self, center, width):
self.center = center
self.width = width

def normalize(self, image):
def normalize(self, image) -> NDArray:
"""
Normalize the input image using windowing.

Expand Down
53 changes: 29 additions & 24 deletions brainles_preprocessing/preprocessor/atlas_centric_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,29 +323,34 @@ def run_defacing(
atlas_mask = self.center_modality.deface(
defacer=self.defacer, defaced_dir_path=deface_dir
)
# looping over _all_ modalities since .deface is no applying the computed mask
for moving_modality in self.all_modalities:
logger.info(f"Applying deface mask to {moving_modality.modality_name}...")
moving_modality.apply_deface_mask(
defacer=self.defacer,
mask_path=atlas_mask,
deface_dir=deface_dir,
)

self._save_output(
src=deface_dir,
save_dir=save_dir_defacing,
)
# now we save images that are skull-stripped
logger.info("Saving defaced images...")
for modality in self.all_modalities:
if modality.raw_defaced_output_path:
modality.save_current_image(
modality.raw_defaced_output_path,
normalization=False,
if atlas_mask is not None:
# looping over _all_ modalities since .deface is no applying the computed mask
for moving_modality in self.all_modalities:
logger.info(
f"Applying deface mask to {moving_modality.modality_name}..."
)
if modality.normalized_defaced_output_path:
modality.save_current_image(
modality.normalized_defaced_output_path,
normalization=True,
moving_modality.apply_deface_mask(
defacer=self.defacer,
mask_path=atlas_mask,
deface_dir=deface_dir,
)

self._save_output(
src=deface_dir,
save_dir=save_dir_defacing,
)
# now we save images that are skull-stripped
logger.info("Saving defaced images...")
for modality in self.all_modalities:
if modality.raw_defaced_output_path:
modality.save_current_image(
modality.raw_defaced_output_path,
normalization=False,
)
if modality.normalized_defaced_output_path:
modality.save_current_image(
modality.normalized_defaced_output_path,
normalization=True,
)
else:
logger.warning("Defacing was requested but no defacing mask was created.")
4 changes: 2 additions & 2 deletions brainles_preprocessing/registration/registrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def transform(
transformed_image_path: Any,
matrix_path: Any,
log_file_path: str,
interpolator: str,
interpolator: str = "default",
**kwargs
):
"""
Expand All @@ -61,7 +61,7 @@ def inverse_transform(
transformed_image_path: Any,
matrix_path: Any,
log_file_path: str,
interpolator: str,
interpolator: str = "default",
):
"""
Abstract method for inverse transforming images.
Expand Down
3 changes: 3 additions & 0 deletions example/example_atlas_centric_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def preprocess(input_dir: Path, output_dir: Path):
raw_defaced_output_path=raw_deface_dir / f"t1c_defaced_raw.nii.gz",
normalizer=percentile_normalizer,
atlas_correction=True,
bet_mask_output_path=output_dir / f"bet_mask.nii.gz",
defacing_mask_output_path=output_dir / f"defacing_mask.nii.gz",
)
moving_modalities = [
Modality(
Expand Down Expand Up @@ -92,6 +94,7 @@ def preprocess(input_dir: Path, output_dir: Path):
save_dir_n4_bias_correction=output_dir / "n4_bias_correction",
save_dir_brain_extraction=output_dir / "brain_extraction",
save_dir_defacing=output_dir / "defacing",
save_dir_transformations=output_dir / "transformations",
)


Expand Down
2 changes: 2 additions & 0 deletions example/example_native_space_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def preprocess(input_dir: Path, output_dir: Path):
normalized_skull_output_path=norm_skull_dir / f"t1c_skull_normalized.nii.gz",
raw_defaced_output_path=raw_deface_dir / f"t1c_defaced_raw.nii.gz",
normalizer=percentile_normalizer,
bet_mask_output_path=output_dir / f"bet_mask.nii.gz",
defacing_mask_output_path=output_dir / f"defacing_mask.nii.gz",
)
moving_modalities = [
Modality(
Expand Down