-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrats_loader.py
70 lines (59 loc) · 1.73 KB
/
brats_loader.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
def load_data(path):
t1 = []
t1gd = []
t2 = []
flair = []
labels = []
for root, dirs, files in os.walk(path):
for file in files:
filepath = os.path.join(root, file)
if(file.endswith('t1.nii.gz')):
t1.append(nib.load(filepath).get_fdata())
elif(file.endswith('t1Gd.nii.gz')):
t1gd.append(nib.load(filepath).get_fdata())
elif(file.endswith('t2.nii.gz')):
t2.append(nib.load(filepath).get_fdata())
elif(file.endswith('flair.nii.gz')):
flair.append(nib.load(filepath).get_fdata())
elif(file.endswith('GlistrBoost.nii.gz')):
labels.append(nib.load(filepath).get_fdata())
return t1, t1gd, t2, flair, labels
def mode(array):
counts = dict()
array_flat = array.flatten()
for a in array_flat:
if a == 0:
continue
if a not in counts:
counts[a] = 1
else:
counts[a] += 1
maxCount = 0
maxElement = None
for c in counts:
if(counts[c] >= maxCount):
maxElement = c
maxCount = counts[c]
return maxElement
"""
in-place data processing
It subtracts the mode of the data, and normalize standard deviation to 1
"""
def preprocess_mode(data):
epsilon = 1e-6
data -= mode(data)
data = data / (data.std() + epsilon)
return data
def preprocess_mean(data):
epsilon = 1e-6
data -= data.mean()
data = data / (data.std() + epsilon)
return data
def preprocess_map_to_one(data):
epsilon = 1e-6
data -= data.mean()
data /= max(abs(data.min()), data.max())
return data