-
Notifications
You must be signed in to change notification settings - Fork 5
/
preprocess.py
70 lines (53 loc) · 1.94 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
import os
import numpy as np
import nibabel as nib
from tqdm import tqdm
import matplotlib.pyplot as plt
config = {
'root_dir_path': './data/MICCAI_BraTS_2019_Data_Validation',
'modalities': [
{'name': 'T1', 'pattern': 't1'},
{'name': 'T1CE', 'pattern': 't1ce'},
{'name': 'T2', 'pattern': 't2'},
{'name': 'FLAIR', 'pattern': 'flair'},
# {'name': 'SEG', 'pattern': 'seg'},
],
}
def z_score_normalize(array):
array = array.astype(np.float32)
mask = array > 0
mean = np.mean(array[mask])
std = np.std(array[mask])
array -= mean
array /= std
return array
if __name__ == '__main__':
for patient_id in tqdm(sorted(os.listdir(config['root_dir_path']))):
print(patient_id)
patient_dir_path = os.path.join(
config['root_dir_path'], patient_id,
)
for modality in config['modalities']:
file_path = os.path.join(
patient_dir_path,
patient_id + '_' + modality['pattern'] + '.nii.gz'
)
nii_file = nib.load(file_path)
series = nii_file.get_data()
if modality['name'] == 'SEG':
series = series.astype(np.int32)
bincount = np.bincount(series.ravel())
if len(bincount) > 3:
assert bincount[3] == 0
series[series == 4] = 3 # 3: ET (GD-enhancing tumor)
series[series == 2] = 2 # 2: ED (peritumoral edema)
series[series == 1] = 1 # 1: NCR/NET (non-enhancing tumor core)
series[series == 0] = 0 # 0: Background
else:
series = z_score_normalize(series)
series = nib.Nifti1Image(series, nii_file.affine)
save_path = os.path.join(
patient_dir_path,
patient_id + '_' + modality['pattern'] + '_norm.nii.gz'
)
nib.save(series, save_path)