-
Notifications
You must be signed in to change notification settings - Fork 68
/
preprocess.py
75 lines (70 loc) · 3.33 KB
/
preprocess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import glob
import os
import warnings
import shutil
import argparse
import SimpleITK as sitk
import numpy as np
from tqdm import tqdm
from nipype.interfaces.ants import N4BiasFieldCorrection
def N4BiasFieldCorrect(filename, output_filename):
normalized = N4BiasFieldCorrection()
normalized.inputs.input_image = filename
normalized.inputs.output_image = output_filename
normalized.run()
return None
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--data', help='training data path', default="/data/dataset/BRATS2018/training/")
parser.add_argument('--out', help="output path", default="./N4_Normalized")
parser.add_argument('--mode', help="output path", default="training")
args = parser.parse_args()
if args.mode == 'test':
BRATS_data = glob.glob(args.data + "/*")
patient_ids = [x.split("/")[-1] for x in BRATS_data]
print("Processing Testing data ...")
for idx, file_name in tqdm(enumerate(BRATS_data), total=len(BRATS_data)):
mod = glob.glob(file_name+"/*.nii*")
output_dir = "{}/test/{}/".format(args.out, patient_ids[idx])
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for mod_file in mod:
if 'flair' not in mod_file and 'seg' not in mod_file:
output_path = "{}/{}".format(output_dir, mod_file.split("/")[-1])
N4BiasFieldCorrect(mod_file, output_path)
else:
output_path = "{}/{}".format(output_dir, mod_file.split("/")[-1])
shutil.copy(mod_file, output_path)
else:
HGG_data = glob.glob(args.data + "HGG/*")
LGG_data = glob.glob(args.data + "LGG/*")
hgg_patient_ids = [x.split("/")[-1] for x in HGG_data]
lgg_patient_ids = [x.split("/")[-1] for x in LGG_data]
print("Processing HGG ...")
for idx, file_name in tqdm(enumerate(HGG_data), total=len(HGG_data)):
mod = glob.glob(file_name+"/*.nii*")
output_dir = "{}/HGG/{}/".format(args.out, hgg_patient_ids[idx])
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for mod_file in mod:
if 'flair' not in mod_file and 'seg' not in mod_file:
output_path = "{}/{}".format(output_dir, mod_file.split("/")[-1])
N4BiasFieldCorrect(mod_file, output_path)
else:
output_path = "{}/{}".format(output_dir, mod_file.split("/")[-1])
shutil.copy(mod_file, output_path)
print("Processing LGG ...")
for idx, file_name in tqdm(enumerate(LGG_data), total=len(LGG_data)):
mod = glob.glob(file_name+"/*.nii*")
output_dir = "{}/LGG/{}/".format(args.out, lgg_patient_ids[idx])
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for mod_file in mod:
if 'flair' not in mod_file and 'seg' not in mod_file:
output_path = "{}/{}".format(output_dir, mod_file.split("/")[-1])
N4BiasFieldCorrect(mod_file, output_path)
else:
output_path = "{}/{}".format(output_dir, mod_file.split("/")[-1])
shutil.copy(mod_file, output_path)
if __name__ == "__main__":
main()