Skip to content

Commit e168a7a

Browse files
authored
Merge pull request #64 from BrainLesion/enable_none_outputs
Enable_none_outputs
2 parents 9d80f22 + 146172e commit e168a7a

File tree

4 files changed

+135
-138
lines changed

4 files changed

+135
-138
lines changed

brainles_preprocessing/modality.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,23 @@ def __init__(
4242
self,
4343
modality_name: str,
4444
input_path: str,
45-
raw_bet_output_path: str = None,
46-
raw_skull_output_path: str = None,
47-
normalized_bet_output_path: str = None,
48-
normalized_skull_output_path: str = None,
45+
raw_bet_output_path: Optional[str] = None,
46+
raw_skull_output_path: Optional[str] = None,
47+
normalized_bet_output_path: Optional[str] = None,
48+
normalized_skull_output_path: Optional[str] = None,
4949
normalizer: Optional[Normalizer] = None,
5050
atlas_correction: bool = True,
5151
) -> None:
52+
# basics
5253
self.modality_name = modality_name
54+
5355
self.input_path = turbopath(input_path)
56+
self.current = self.input_path
57+
58+
self.normalizer = normalizer
59+
self.atlas_correction = atlas_correction
60+
61+
# check that atleast one output is generated
5462
if (
5563
raw_bet_output_path is None
5664
and normalized_bet_output_path is None
@@ -60,30 +68,42 @@ def __init__(
6068
raise ValueError(
6169
"All output paths are None. At least one output path must be provided."
6270
)
63-
self.raw_bet_output_path = turbopath(raw_bet_output_path)
64-
self.raw_skull_output_path = turbopath(raw_skull_output_path)
71+
72+
# handle input paths
73+
if raw_bet_output_path is not None:
74+
self.raw_bet_output_path = turbopath(raw_bet_output_path)
75+
else:
76+
self.raw_bet_output_path = raw_bet_output_path
77+
78+
if raw_skull_output_path is not None:
79+
self.raw_skull_output_path = turbopath(raw_skull_output_path)
80+
else:
81+
self.raw_skull_output_path = raw_skull_output_path
82+
6583
if normalized_bet_output_path is not None:
6684
if normalizer is None:
6785
raise ValueError(
6886
"A normalizer must be provided if normalized_bet_output_path is not None."
6987
)
70-
self.normalized_bet_output_path = turbopath(normalized_bet_output_path)
88+
self.normalized_bet_output_path = turbopath(normalized_bet_output_path)
89+
else:
90+
self.normalized_bet_output_path = normalized_bet_output_path
7191

7292
if normalized_skull_output_path is not None:
7393
if normalizer is None:
7494
raise ValueError(
7595
"A normalizer must be provided if normalized_skull_output_path is not None."
7696
)
77-
self.normalized_skull_output_path = turbopath(normalized_skull_output_path)
78-
79-
self.normalizer = normalizer
80-
self.atlas_correction = atlas_correction
81-
82-
self.current = self.input_path
97+
self.normalized_skull_output_path = turbopath(normalized_skull_output_path)
98+
else:
99+
self.normalized_skull_output_path = normalized_skull_output_path
83100

84101
@property
85102
def bet(self) -> bool:
86-
return any([self.raw_bet_output_path, self.normalized_bet_output_path])
103+
return any(
104+
path is not None
105+
for path in [self.raw_bet_output_path, self.normalized_skull_output_path]
106+
)
87107

88108
def normalize(
89109
self,

brainles_preprocessing/preprocessor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ def run(
203203

204204
# now we save images that are not skullstripped
205205
for modality in self.all_modalities:
206-
if modality.raw_skull_output_path:
206+
if modality.raw_skull_output_path is not None:
207207
modality.save_current_image(
208208
modality.raw_skull_output_path,
209209
normalization=False,
210210
)
211-
if modality.normalized_skull_output_path:
211+
if modality.normalized_skull_output_path is not None:
212212
modality.save_current_image(
213213
modality.normalized_skull_output_path,
214214
normalization=True,
@@ -240,12 +240,12 @@ def run(
240240

241241
# now we save images that are skullstripped
242242
for modality in self.all_modalities:
243-
if modality.raw_bet_output_path:
243+
if modality.raw_bet_output_path is not None:
244244
modality.save_current_image(
245245
modality.raw_bet_output_path,
246246
normalization=False,
247247
)
248-
if modality.normalized_bet_output_path:
248+
if modality.normalized_bet_output_path is not None:
249249
modality.save_current_image(
250250
modality.normalized_bet_output_path,
251251
normalization=True,
@@ -256,7 +256,7 @@ def _save_output(
256256
src: str,
257257
save_dir: Optional[str],
258258
):
259-
if save_dir:
259+
if save_dir is not None:
260260
save_dir = turbopath(save_dir)
261261
shutil.copytree(
262262
src=src,

example/example_modality_centric_preprocessor.py

Lines changed: 96 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
# 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.
2-
import datetime
3-
42
from auxiliary.normalization.percentile_normalizer import PercentileNormalizer
53
from auxiliary.turbopath import turbopath
64
from tqdm import tqdm
@@ -13,128 +11,107 @@
1311

1412
def preprocess(inputDir):
1513
inputDir = turbopath(inputDir)
16-
try:
17-
print("*** start ***")
18-
19-
# where are the raw mr files?
20-
btk_raw_dir = turbopath(inputDir)
21-
22-
# is the exam already processed?
23-
brainles_dir = turbopath(inputDir) + "/" + inputDir.name + "_brainles"
24-
raw_bet_dir = brainles_dir / "raw_bet"
25-
norm_bet_dir = brainles_dir / "normalized_bet"
26-
raw_skull_dir = brainles_dir / "raw_skull"
27-
norm_skull_dir = brainles_dir / "normalized_skull"
28-
29-
# if not os.path.exists(prep_dir):
30-
# if os.path.exists(prep_dir):
31-
t1_file = btk_raw_dir.files("*t1.nii.gz")
32-
t1c_file = btk_raw_dir.files("*t1c.nii.gz")
33-
t2_file = btk_raw_dir.files("*t2.nii.gz")
34-
flair_file = btk_raw_dir.files("*fla.nii.gz")
35-
36-
if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1:
37-
# print(t1_file)
38-
# print(t1c_file)
39-
# print(t2_file)
40-
# print(flair_file)
41-
42-
t1File = t1_file[0]
43-
t1cFile = t1c_file[0]
44-
t2File = t2_file[0]
45-
flaFile = flair_file[0]
46-
47-
# normalizer
48-
percentile_normalizer = PercentileNormalizer(
49-
lower_percentile=0.1,
50-
upper_percentile=99.9,
51-
lower_limit=0,
52-
upper_limit=1,
53-
)
54-
55-
# define modalities
56-
center = Modality(
57-
modality_name="t1c",
58-
input_path=t1cFile,
59-
raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1c_bet_raw.nii.gz",
14+
print("*** start ***")
15+
16+
# where are the raw mr files?
17+
brainles_dir = turbopath(inputDir) + "/" + inputDir.name + "_brainles"
18+
19+
raw_bet_dir = brainles_dir / "raw_bet"
20+
norm_bet_dir = brainles_dir / "normalized_bet"
21+
raw_skull_dir = brainles_dir / "raw_skull"
22+
norm_skull_dir = brainles_dir / "normalized_skull"
23+
24+
t1_file = inputDir.files("*t1.nii.gz")
25+
t1c_file = inputDir.files("*t1c.nii.gz")
26+
t2_file = inputDir.files("*t2.nii.gz")
27+
flair_file = inputDir.files("*fla.nii.gz")
28+
if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1:
29+
# print(t1_file)
30+
# print(t1c_file)
31+
# print(t2_file)
32+
# print(flair_file)
33+
t1File = t1_file[0]
34+
t1cFile = t1c_file[0]
35+
t2File = t2_file[0]
36+
flaFile = flair_file[0]
37+
# normalizer
38+
percentile_normalizer = PercentileNormalizer(
39+
lower_percentile=0.1,
40+
upper_percentile=99.9,
41+
lower_limit=0,
42+
upper_limit=1,
43+
)
44+
# define modalities
45+
center = Modality(
46+
modality_name="t1c",
47+
input_path=t1cFile,
48+
raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1c_bet_raw.nii.gz",
49+
raw_skull_output_path=raw_skull_dir / inputDir.name
50+
+ "_t1c_skull_raw.nii.gz",
51+
normalized_bet_output_path=norm_bet_dir / inputDir.name
52+
+ "_t1c_bet_normalized.nii.gz",
53+
normalized_skull_output_path=norm_skull_dir / inputDir.name
54+
+ "_t1c_skull_normalized.nii.gz",
55+
atlas_correction=True,
56+
normalizer=percentile_normalizer,
57+
)
58+
moving_modalities = [
59+
Modality(
60+
modality_name="t1",
61+
input_path=t1File,
62+
raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1_bet_raw.nii.gz",
6063
raw_skull_output_path=raw_skull_dir / inputDir.name
61-
+ "_t1c_skull_raw.nii.gz",
64+
+ "_t1_skull_raw.nii.gz",
6265
normalized_bet_output_path=norm_bet_dir / inputDir.name
63-
+ "_t1c_bet_normalized.nii.gz",
66+
+ "_t1_bet_normalized.nii.gz",
6467
normalized_skull_output_path=norm_skull_dir / inputDir.name
65-
+ "_t1c_skull_normalized.nii.gz",
68+
+ "_t1_skull_normalized.nii.gz",
6669
atlas_correction=True,
6770
normalizer=percentile_normalizer,
68-
)
69-
70-
moving_modalities = [
71-
Modality(
72-
modality_name="t1",
73-
input_path=t1File,
74-
raw_bet_output_path=raw_bet_dir / inputDir.name
75-
+ "_t1_bet_raw.nii.gz",
76-
raw_skull_output_path=raw_skull_dir / inputDir.name
77-
+ "_t1_skull_raw.nii.gz",
78-
normalized_bet_output_path=norm_bet_dir / inputDir.name
79-
+ "_t1_bet_normalized.nii.gz",
80-
normalized_skull_output_path=norm_skull_dir / inputDir.name
81-
+ "_t1_skull_normalized.nii.gz",
82-
atlas_correction=True,
83-
normalizer=percentile_normalizer,
84-
),
85-
Modality(
86-
modality_name="t2",
87-
input_path=t2File,
88-
raw_bet_output_path=raw_bet_dir / inputDir.name
89-
+ "_t2_bet_raw.nii.gz",
90-
raw_skull_output_path=raw_skull_dir / inputDir.name
91-
+ "_t2_skull_raw.nii.gz",
92-
normalized_bet_output_path=norm_bet_dir / inputDir.name
93-
+ "_t2_bet_normalized.nii.gz",
94-
normalized_skull_output_path=norm_skull_dir / inputDir.name
95-
+ "_t2_skull_normalized.nii.gz",
96-
atlas_correction=True,
97-
normalizer=percentile_normalizer,
98-
),
99-
Modality(
100-
modality_name="flair",
101-
input_path=flaFile,
102-
raw_bet_output_path=raw_bet_dir / inputDir.name
103-
+ "_fla_bet_raw.nii.gz",
104-
raw_skull_output_path=raw_skull_dir / inputDir.name
105-
+ "_fla_skull_raw.nii.gz",
106-
normalized_bet_output_path=norm_bet_dir / inputDir.name
107-
+ "_fla_bet_normalized.nii.gz",
108-
normalized_skull_output_path=norm_skull_dir / inputDir.name
109-
+ "_fla_skull_normalized.nii.gz",
110-
atlas_correction=True,
111-
normalizer=percentile_normalizer,
112-
),
113-
]
114-
115-
preprocessor = Preprocessor(
116-
center_modality=center,
117-
moving_modalities=moving_modalities,
118-
registrator=NiftyRegRegistrator(),
119-
brain_extractor=HDBetExtractor(),
120-
temp_folder="tempo",
121-
limit_cuda_visible_devices="1",
122-
)
123-
124-
preprocessor.run(
125-
save_dir_coregistration=brainles_dir + "/co-registration",
126-
save_dir_atlas_registration=brainles_dir + "/atlas-registration",
127-
save_dir_atlas_correction=brainles_dir + "/atlas-correction",
128-
save_dir_brain_extraction=brainles_dir + "/brain-extraction",
129-
)
130-
131-
except Exception as e:
132-
print("error: " + str(e))
133-
print("conversion error for:", inputDir)
134-
135-
time = str(datetime.datetime.now().time())
136-
137-
print("** finished:", inputDir.name, "at:", time)
71+
),
72+
Modality(
73+
modality_name="t2",
74+
input_path=t2File,
75+
raw_bet_output_path=raw_bet_dir / inputDir.name + "_t2_bet_raw.nii.gz",
76+
raw_skull_output_path=raw_skull_dir / inputDir.name
77+
+ "_t2_skull_raw.nii.gz",
78+
normalized_bet_output_path=norm_bet_dir / inputDir.name
79+
+ "_t2_bet_normalized.nii.gz",
80+
normalized_skull_output_path=norm_skull_dir / inputDir.name
81+
+ "_t2_skull_normalized.nii.gz",
82+
atlas_correction=True,
83+
normalizer=percentile_normalizer,
84+
),
85+
Modality(
86+
modality_name="flair",
87+
input_path=flaFile,
88+
raw_bet_output_path=raw_bet_dir / inputDir.name + "_fla_bet_raw.nii.gz",
89+
raw_skull_output_path=raw_skull_dir / inputDir.name
90+
+ "_fla_skull_raw.nii.gz",
91+
normalized_bet_output_path=norm_bet_dir / inputDir.name
92+
+ "_fla_bet_normalized.nii.gz",
93+
normalized_skull_output_path=norm_skull_dir / inputDir.name
94+
+ "_fla_skull_normalized.nii.gz",
95+
atlas_correction=True,
96+
normalizer=percentile_normalizer,
97+
),
98+
]
99+
100+
preprocessor = Preprocessor(
101+
center_modality=center,
102+
moving_modalities=moving_modalities,
103+
registrator=NiftyRegRegistrator(),
104+
brain_extractor=HDBetExtractor(),
105+
temp_folder="temporary_directory",
106+
limit_cuda_visible_devices="0",
107+
)
108+
109+
preprocessor.run(
110+
save_dir_coregistration=brainles_dir + "/co-registration",
111+
save_dir_atlas_registration=brainles_dir + "/atlas-registration",
112+
save_dir_atlas_correction=brainles_dir + "/atlas-correction",
113+
save_dir_brain_extraction=brainles_dir + "/brain-extraction",
114+
)
138115

139116

140117
### *** GOGOGO *** ###
File renamed without changes.

0 commit comments

Comments
 (0)