Skip to content

Commit 5f2fa28

Browse files
authored
Merge pull request #159 from BrainLesion/157-bug-bet-deface-masks-are-not-saved
157 bug bet deface masks are not saved
2 parents 1d6f546 + d7299bb commit 5f2fa28

File tree

8 files changed

+68
-41
lines changed

8 files changed

+68
-41
lines changed

brainles_preprocessing/modality.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ def __init__(
139139
else:
140140
self.normalized_defaced_output_path = None
141141

142-
self.steps = {k: None for k in PreprocessorSteps}
142+
self.steps: Dict[PreprocessorSteps, Path | None] = {
143+
k: None for k in PreprocessorSteps
144+
}
143145
self.steps[PreprocessorSteps.INPUT] = self.input_path
144146

145147
@property
@@ -265,7 +267,7 @@ def register(
265267
moving_image_path=self.current,
266268
transformed_image_path=registered,
267269
matrix_path=registered_matrix,
268-
log_file_path=registered_log,
270+
log_file_path=str(registered_log),
269271
)
270272
self.current = registered
271273
self.steps[step] = registered
@@ -394,24 +396,24 @@ def transform(
394396
), "Coregistration must be performed before applying atlas registration."
395397

396398
registrator.transform(
397-
fixed_image_path=fixed_image_path,
398-
moving_image_path=self.steps[PreprocessorSteps.INPUT],
399-
transformed_image_path=transformed,
399+
fixed_image_path=str(fixed_image_path),
400+
moving_image_path=str(self.steps[PreprocessorSteps.INPUT]),
401+
transformed_image_path=str(transformed),
400402
matrix_path=[
401403
self.transformation_paths[
402404
PreprocessorSteps.COREGISTERED
403405
], # coregistration matrix
404406
transformation_matrix_path, # atlas registration matrix
405407
],
406-
log_file_path=transformed_log,
408+
log_file_path=str(transformed_log),
407409
)
408410
else:
409411
registrator.transform(
410-
fixed_image_path=fixed_image_path,
411-
moving_image_path=self.current,
412-
transformed_image_path=transformed,
413-
matrix_path=transformation_matrix_path,
414-
log_file_path=transformed_log,
412+
fixed_image_path=str(fixed_image_path),
413+
moving_image_path=str(self.current),
414+
transformed_image_path=str(transformed),
415+
matrix_path=str(transformation_matrix_path),
416+
log_file_path=str(transformed_log),
415417
)
416418

417419
self.current = transformed
@@ -638,6 +640,12 @@ def extract_brain_region(
638640

639641
if self.bet:
640642
self.current = bet
643+
644+
if self.bet_mask_output_path:
645+
self.save_mask(
646+
mask_path=mask_path,
647+
output_path=self.bet_mask_output_path,
648+
)
641649
return mask_path
642650

643651
def deface(
@@ -684,7 +692,7 @@ def deface(
684692
moving_image_path=self.steps[PreprocessorSteps.BET],
685693
transformed_image_path=atlas_bet,
686694
matrix_path=atlas_bet_M,
687-
log_file_path=defaced_dir_path / "atlas_bet.log",
695+
log_file_path=str(defaced_dir_path / "atlas_bet.log"),
688696
)
689697

690698
deface_mask_atlas = defaced_dir_path / "deface_mask_atlas.nii.gz"
@@ -706,6 +714,12 @@ def deface(
706714
mask_image_path=mask_path,
707715
)
708716

717+
if self.defacing_mask_output_path:
718+
self.save_mask(
719+
mask_path=mask_path,
720+
output_path=self.defacing_mask_output_path,
721+
)
722+
709723
return mask_path
710724
else:
711725
logger.warning(

brainles_preprocessing/normalization/normalizer_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from abc import ABC, abstractmethod
2+
from numpy.typing import NDArray
23

34

45
class Normalizer(ABC):
@@ -10,7 +11,7 @@ def __init__(self):
1011
super().__init__()
1112

1213
@abstractmethod
13-
def normalize(self, image):
14+
def normalize(self, image) -> NDArray:
1415
"""
1516
Normalize the input image based on the chosen method.
1617

brainles_preprocessing/normalization/percentile_normalizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from .normalizer_base import Normalizer
3+
from numpy.typing import NDArray
34

45

56
class PercentileNormalizer(Normalizer):
@@ -29,7 +30,7 @@ def __init__(
2930
self.lower_limit = lower_limit
3031
self.upper_limit = upper_limit
3132

32-
def normalize(self, image: np.ndarray):
33+
def normalize(self, image: np.ndarray) -> NDArray:
3334
"""
3435
Normalize the input image using percentile-based mapping.
3536

brainles_preprocessing/normalization/windowing_normalizer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from .normalizer_base import Normalizer
3+
from numpy.typing import NDArray
34

45

56
class WindowingNormalizer(Normalizer):
@@ -19,7 +20,7 @@ def __init__(self, center, width):
1920
self.center = center
2021
self.width = width
2122

22-
def normalize(self, image):
23+
def normalize(self, image) -> NDArray:
2324
"""
2425
Normalize the input image using windowing.
2526

brainles_preprocessing/preprocessor/atlas_centric_preprocessor.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -323,29 +323,34 @@ def run_defacing(
323323
atlas_mask = self.center_modality.deface(
324324
defacer=self.defacer, defaced_dir_path=deface_dir
325325
)
326-
# looping over _all_ modalities since .deface is no applying the computed mask
327-
for moving_modality in self.all_modalities:
328-
logger.info(f"Applying deface mask to {moving_modality.modality_name}...")
329-
moving_modality.apply_deface_mask(
330-
defacer=self.defacer,
331-
mask_path=atlas_mask,
332-
deface_dir=deface_dir,
333-
)
334-
335-
self._save_output(
336-
src=deface_dir,
337-
save_dir=save_dir_defacing,
338-
)
339-
# now we save images that are skull-stripped
340-
logger.info("Saving defaced images...")
341-
for modality in self.all_modalities:
342-
if modality.raw_defaced_output_path:
343-
modality.save_current_image(
344-
modality.raw_defaced_output_path,
345-
normalization=False,
326+
if atlas_mask is not None:
327+
# looping over _all_ modalities since .deface is no applying the computed mask
328+
for moving_modality in self.all_modalities:
329+
logger.info(
330+
f"Applying deface mask to {moving_modality.modality_name}..."
346331
)
347-
if modality.normalized_defaced_output_path:
348-
modality.save_current_image(
349-
modality.normalized_defaced_output_path,
350-
normalization=True,
332+
moving_modality.apply_deface_mask(
333+
defacer=self.defacer,
334+
mask_path=atlas_mask,
335+
deface_dir=deface_dir,
351336
)
337+
338+
self._save_output(
339+
src=deface_dir,
340+
save_dir=save_dir_defacing,
341+
)
342+
# now we save images that are skull-stripped
343+
logger.info("Saving defaced images...")
344+
for modality in self.all_modalities:
345+
if modality.raw_defaced_output_path:
346+
modality.save_current_image(
347+
modality.raw_defaced_output_path,
348+
normalization=False,
349+
)
350+
if modality.normalized_defaced_output_path:
351+
modality.save_current_image(
352+
modality.normalized_defaced_output_path,
353+
normalization=True,
354+
)
355+
else:
356+
logger.warning("Defacing was requested but no defacing mask was created.")

brainles_preprocessing/registration/registrator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def transform(
3636
transformed_image_path: Any,
3737
matrix_path: Any,
3838
log_file_path: str,
39-
interpolator: str,
39+
interpolator: str = "default",
4040
**kwargs
4141
):
4242
"""
@@ -61,7 +61,7 @@ def inverse_transform(
6161
transformed_image_path: Any,
6262
matrix_path: Any,
6363
log_file_path: str,
64-
interpolator: str,
64+
interpolator: str = "default",
6565
):
6666
"""
6767
Abstract method for inverse transforming images.

example/example_atlas_centric_preprocessor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def preprocess(input_dir: Path, output_dir: Path):
4545
raw_defaced_output_path=raw_deface_dir / f"t1c_defaced_raw.nii.gz",
4646
normalizer=percentile_normalizer,
4747
atlas_correction=True,
48+
bet_mask_output_path=output_dir / f"bet_mask.nii.gz",
49+
defacing_mask_output_path=output_dir / f"defacing_mask.nii.gz",
4850
)
4951
moving_modalities = [
5052
Modality(
@@ -92,6 +94,7 @@ def preprocess(input_dir: Path, output_dir: Path):
9294
save_dir_n4_bias_correction=output_dir / "n4_bias_correction",
9395
save_dir_brain_extraction=output_dir / "brain_extraction",
9496
save_dir_defacing=output_dir / "defacing",
97+
save_dir_transformations=output_dir / "transformations",
9598
)
9699

97100

example/example_native_space_preprocessor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def preprocess(input_dir: Path, output_dir: Path):
3838
normalized_skull_output_path=norm_skull_dir / f"t1c_skull_normalized.nii.gz",
3939
raw_defaced_output_path=raw_deface_dir / f"t1c_defaced_raw.nii.gz",
4040
normalizer=percentile_normalizer,
41+
bet_mask_output_path=output_dir / f"bet_mask.nii.gz",
42+
defacing_mask_output_path=output_dir / f"defacing_mask.nii.gz",
4143
)
4244
moving_modalities = [
4345
Modality(

0 commit comments

Comments
 (0)