-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.py
148 lines (96 loc) · 3.34 KB
/
utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
##################################################
# Authors:
# {Christian F. Baumgartner} ([email protected]),
# {Lisa M. Koch} ([email protected])
# https://github.com/baumgach/vagan-code
##################################################
import nibabel as nib
import numpy as np
import os
def ncc(a, v, zero_norm=True):
a = a.flatten()
v = v.flatten()
if zero_norm:
a = (a - np.mean(a)) / (np.std(a) * len(a))
v = (v - np.mean(v)) / np.std(v)
else:
a = (a) / (np.std(a) * len(a))
v = (v) / np.std(v)
return np.correlate(a, v)
def norm_l2(a, v):
a = a.flatten()
v = v.flatten()
a = (a - np.mean(a)) / (np.std(a) * len(a))
v = (v - np.mean(v)) / np.std(v)
return np.mean(np.sqrt(a**2 + v**2))
def all_argmax(arr, axis=None):
return np.argwhere(arr == np.amax(arr, axis=axis))
def makefolder(folder):
'''
Helper function to make a new folder if doesn't exist
:param folder: path to new folder
:return: True if folder created, False if folder already exists
'''
if not os.path.exists(folder):
os.makedirs(folder)
return True
return False
def load_nii(img_path):
'''
Shortcut to load a nifti file
'''
nimg = nib.load(img_path)
return nimg.get_data(), nimg.affine, nimg.header
def save_nii(img_path, data, affine, header):
'''
Shortcut to save a nifty file
'''
nimg = nib.Nifti1Image(data, affine=affine, header=header)
nimg.to_filename(img_path)
def create_and_save_nii(data, img_path):
img = nib.Nifti1Image(data, np.eye(4))
nib.save(img, img_path)
class Bunch:
# Useful shortcut for making struct like contructs
# Example:
# mystruct = Bunch(a=1, b=2)
# print(mystruct.a)
# >>> 1
def __init__(self, **kwds):
self.__dict__.update(kwds)
def convert_to_uint8(image):
image = image - image.min()
image = 255.0*np.divide(image.astype(np.float32), image.max())
return image.astype(np.uint8)
def normalise_image(image):
'''
make image zero mean and unit standard deviation
'''
img_o = np.float32(image.copy())
m = np.mean(img_o)
s = np.std(img_o)
return np.divide((img_o - m), s)
def map_image_to_intensity_range(image, min_o, max_o, percentiles=0):
# If percentile = 0 uses min and max. Percentile >0 makes normalisation more robust to outliers.
if image.dtype in [np.uint8, np.uint16, np.uint32]:
assert min_o >= 0, 'Input image type is uintXX but you selected a negative min_o: %f' % min_o
if image.dtype == np.uint8:
assert max_o <= 255, 'Input image type is uint8 but you selected a max_o > 255: %f' % max_o
min_i = np.percentile(image, 0 + percentiles)
max_i = np.percentile(image, 100 - percentiles)
image = (np.divide((image - min_i), max_i - min_i) * (max_o - min_o) + min_o).copy()
image[image > max_o] = max_o
image[image < min_o] = min_o
return image
def normalise_images(X):
'''
Helper for making the images zero mean and unit standard deviation i.e. `white`
'''
X_white = np.zeros(X.shape, dtype=np.float32)
for ii in range(X.shape[0]):
Xc = X[ii, :, :, :]
mc = Xc.mean()
sc = Xc.std()
Xc_white = np.divide((Xc - mc), sc)
X_white[ii, :, :, :] = Xc_white
return X_white.astype(np.float32)