Skip to content

Commit 4999699

Browse files
committed
still script
Signed-off-by: neuronflow <florian.kofler@tum.de>
1 parent 64cf07c commit 4999699

1 file changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# TODO move this to tutorial
2+
# TODO polish this
3+
import datetime
4+
5+
from auxiliary.normalization.percentile_normalizer import PercentileNormalizer
6+
from auxiliary.turbopath import turbopath
7+
from tqdm import tqdm
8+
9+
from brainles_preprocessing.brain_extraction import HDBetExtractor
10+
from brainles_preprocessing.modality import Modality
11+
from brainles_preprocessing.preprocessor import Preprocessor
12+
from brainles_preprocessing.registration import NiftyRegRegistrator
13+
14+
15+
def preprocess(inputDir):
16+
inputDir = turbopath(inputDir)
17+
try:
18+
print("*** start ***")
19+
20+
# where are the raw mr files?
21+
btk_raw_dir = turbopath(inputDir)
22+
23+
# is the exam already processed?
24+
brainles_dir = turbopath(inputDir) + "/" + inputDir.name + "_brainles"
25+
raw_bet_dir = brainles_dir / "raw_bet"
26+
norm_bet_dir = brainles_dir / "normalized_bet"
27+
raw_skull_dir = brainles_dir / "raw_skull"
28+
norm_skull_dir = brainles_dir / "normalized_skull"
29+
30+
# if not os.path.exists(prep_dir):
31+
# if os.path.exists(prep_dir):
32+
t1_file = btk_raw_dir.files("*t1.nii.gz")
33+
t1c_file = btk_raw_dir.files("*t1c.nii.gz")
34+
t2_file = btk_raw_dir.files("*t2.nii.gz")
35+
flair_file = btk_raw_dir.files("*fla.nii.gz")
36+
37+
if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1:
38+
# print(t1_file)
39+
# print(t1c_file)
40+
# print(t2_file)
41+
# print(flair_file)
42+
43+
t1File = t1_file[0]
44+
t1cFile = t1c_file[0]
45+
t2File = t2_file[0]
46+
flaFile = flair_file[0]
47+
48+
# normalizer
49+
percentile_normalizer = PercentileNormalizer(
50+
lower_percentile=0.1,
51+
upper_percentile=99.9,
52+
lower_limit=0,
53+
upper_limit=1,
54+
)
55+
56+
# define modalities
57+
center = Modality(
58+
modality_name="t1c",
59+
input_path=t1cFile,
60+
raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1c_bet_raw.nii.gz",
61+
raw_skull_output_path=raw_skull_dir / inputDir.name
62+
+ "_t1c_skull_raw.nii.gz",
63+
normalized_bet_output_path=norm_bet_dir / inputDir.name
64+
+ "_t1c_bet_normalized.nii.gz",
65+
normalized_skull_output_path=norm_skull_dir / inputDir.name
66+
+ "_t1c_skull_normalized.nii.gz",
67+
atlas_correction=True,
68+
normalizer=percentile_normalizer,
69+
)
70+
71+
moving_modalities = [
72+
Modality(
73+
modality_name="t1",
74+
input_path=t1File,
75+
raw_bet_output_path=raw_bet_dir / inputDir.name
76+
+ "_t1_bet_raw.nii.gz",
77+
raw_skull_output_path=raw_skull_dir / inputDir.name
78+
+ "_t1_skull_raw.nii.gz",
79+
normalized_bet_output_path=norm_bet_dir / inputDir.name
80+
+ "_t1_bet_normalized.nii.gz",
81+
normalized_skull_output_path=norm_skull_dir / inputDir.name
82+
+ "_t1_skull_normalized.nii.gz",
83+
atlas_correction=True,
84+
normalizer=percentile_normalizer,
85+
),
86+
Modality(
87+
modality_name="t2",
88+
input_path=t2File,
89+
raw_bet_output_path=raw_bet_dir / inputDir.name
90+
+ "_t2_bet_raw.nii.gz",
91+
raw_skull_output_path=raw_skull_dir / inputDir.name
92+
+ "_t2_skull_raw.nii.gz",
93+
normalized_bet_output_path=norm_bet_dir / inputDir.name
94+
+ "_t2_bet_normalized.nii.gz",
95+
normalized_skull_output_path=norm_skull_dir / inputDir.name
96+
+ "_t2_skull_normalized.nii.gz",
97+
atlas_correction=True,
98+
normalizer=percentile_normalizer,
99+
),
100+
Modality(
101+
modality_name="flair",
102+
input_path=flaFile,
103+
raw_bet_output_path=raw_bet_dir / inputDir.name
104+
+ "_fla_bet_raw.nii.gz",
105+
raw_skull_output_path=raw_skull_dir / inputDir.name
106+
+ "_fla_skull_raw.nii.gz",
107+
normalized_bet_output_path=norm_bet_dir / inputDir.name
108+
+ "_fla_bet_normalized.nii.gz",
109+
normalized_skull_output_path=norm_skull_dir / inputDir.name
110+
+ "_fla_skull_normalized.nii.gz",
111+
atlas_correction=True,
112+
normalizer=percentile_normalizer,
113+
),
114+
]
115+
116+
preprocessor = Preprocessor(
117+
center_modality=center,
118+
moving_modalities=moving_modalities,
119+
registrator=NiftyRegRegistrator(),
120+
brain_extractor=HDBetExtractor(),
121+
temp_folder="tempo",
122+
limit_cuda_visible_devices="1",
123+
)
124+
125+
preprocessor.run(
126+
save_dir_coregistration=brainles_dir + "/co-registration",
127+
save_dir_atlas_registration=brainles_dir + "/atlas-registration",
128+
save_dir_atlas_correction=brainles_dir + "/atlas-correction",
129+
save_dir_brain_extraction=brainles_dir + "/brain-extraction",
130+
)
131+
132+
except Exception as e:
133+
print("error: " + str(e))
134+
print("conversion error for:", inputDir)
135+
136+
time = str(datetime.datetime.now().time())
137+
138+
print("** finished:", inputDir.name, "at:", time)
139+
140+
141+
### *** GOGOGO *** ###
142+
if __name__ == "__main__":
143+
EXAMPLE_DATA_DIR = turbopath("example/example_data")
144+
145+
exams = EXAMPLE_DATA_DIR.dirs()
146+
147+
for exam in tqdm(exams):
148+
print("processing:", exam)
149+
preprocess(exam)

0 commit comments

Comments
 (0)