-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
319 lines (276 loc) · 8.74 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import time
import os
import re
import sys
import traceback
from functools import reduce
from scipy import ndimage as nd
import numpy as np
from nibabel import load as load_nii
from scipy.ndimage.morphology import binary_dilation as imdilate
from scipy.ndimage.morphology import binary_erosion as imerode
import torch
"""
Utility functions
"""
def color_codes():
"""
Function that returns a custom dictionary with ASCII codes related to
colors.
:return: Custom dictionary with ASCII codes for terminal colors.
"""
codes = {
'nc': '\033[0m',
'b': '\033[1m',
'k': '\033[0m',
'0.25': '\033[30m',
'dgy': '\033[30m',
'r': '\033[31m',
'g': '\033[32m',
'gc': '\033[32m;0m',
'bg': '\033[32;1m',
'y': '\033[33m',
'c': '\033[36m',
'0.75': '\033[37m',
'lgy': '\033[37m',
'clr': '\033[K',
}
return codes
def find_file(name, dirname):
"""
:param name:
:param dirname:
:return:
"""
result = list(filter(
lambda x: not os.path.isdir(x) and re.search(name, x),
os.listdir(dirname)
))
return os.path.join(dirname, result[0]) if result else None
def get_dirs(path):
"""
Function to get the folder name of the patients given a path.
:param path: Folder where the patients should be located.
:return: List of patient names.
"""
# All patients (full path)
patient_paths = sorted(
filter(
lambda d: os.path.isdir(os.path.join(path, d)),
os.listdir(path)
)
)
# Patients used during training
return patient_paths
def print_message(message):
"""
Function to print a message with a custom specification
:param message: Message to be printed.
:return: None.
"""
c = color_codes()
dashes = ''.join(['-'] * (len(message) + 11))
print(dashes)
print(
'%s[%s]%s %s' %
(c['c'], time.strftime("%H:%M:%S", time.localtime()), c['nc'], message)
)
print(dashes)
def time_f(f, stdout=None, stderr=None):
"""
Function to time another function.
:param f: Function to be run. If the function has any parameters, it should
be passed using the lambda keyword.
:param stdout: File where the stdout will be redirected. By default we use
the system's stdout.
:param stderr: File where the stderr will be redirected. By default we use
the system's stderr.
:return: The result of running f.
"""
# Init
stdout_copy = sys.stdout
if stdout is not None:
sys.stdout = stdout
start_t = time.time()
try:
ret = f()
except Exception as e:
ret = None
exc_type, exc_value, exc_traceback = sys.exc_info()
print('{0}: {1}'.format(type(e).__name__, e), file=stderr)
traceback.print_tb(exc_traceback, file=stderr)
finally:
if stdout is not None:
sys.stdout = stdout_copy
print(
time.strftime(
'Time elapsed = %H hours %M minutes %S seconds',
time.gmtime(time.time() - start_t)
)
)
return ret
def time_to_string(time_val):
"""
Function to convert from a time number to a printable string that
represents time in hours minutes and seconds.
:param time_val: Time value in seconds (using functions from the time
package)
:return: String with a human format for time
"""
if time_val < 60:
time_s = '%ds' % time_val
elif time_val < 3600:
time_s = '%dm %ds' % (time_val // 60, time_val % 60)
else:
time_s = '%dh %dm %ds' % (
time_val // 3600,
(time_val % 3600) // 60,
time_val % 60
)
return time_s
def get_int(string):
"""
Function to get the int number contained in a string. If there are more
than one int number (or there is a floating point number), this function
will concatenate all digits and return an int, anyways.
:param string: String that contains an int number
:return: int number
"""
return int(''.join(filter(str.isdigit, string)))
"""
Data related functions
"""
def get_bb(mask, dilate=0):
"""
:param mask:
:param dilate:
:return:
"""
if dilate > 0:
mask = imdilate(mask, iterations=dilate)
idx = np.where(mask)
bb = tuple(
slice(min_i, max_i)
for min_i, max_i in zip(
np.min(idx, axis=-1), np.max(idx, axis=-1)
)
)
return bb
def get_mask(mask_name, dilate=0, dtype=np.uint8):
"""
Function to load a mask image
:param mask_name: Path to the mask image file
:param dilate: Dilation radius
:param dtype: Data type for the final mask
:return:
"""
# Lesion mask
mask_image = (load_nii(mask_name).get_fdata() > 0.5).astype(dtype)
if dilate > 0:
mask_d = imdilate(
mask_image,
iterations=dilate
)
mask_e = imerode(
mask_image,
iterations=dilate
)
mask_image = np.logical_and(mask_d, np.logical_not(mask_e)).astype(dtype)
return mask_image
def get_normalised_image(
image_name, mask=None, dtype=np.float32, masked=False
):
"""
Function to a load an image and normalised it (0 mean / 1 standard
deviation)
:param image_name: Path to the image to be noramlised
:param mask: Mask defining the region of interest
:param dtype: Data type for the final image
:param masked: Whether to mask the image or not
:return:
"""
image = load_nii(image_name).get_fdata()
# If no mask is provided we use the image as a mask (all non-zero values)
if mask is None:
mask_bin = image.astype(np.bool)
else:
mask_bin = mask.astype(np.bool)
if len(image.shape) > len(mask_bin.shape):
image_list = []
for i in range(image.shape[-1]):
image_i = image[..., i]
image_mu = np.mean(image_i[mask_bin])
image_sigma = np.std(image_i[mask_bin])
if masked:
image_i = image_i * mask_bin.astype(image.dtype)
norm_image = ((image_i - image_mu) / image_sigma).astype(dtype)
image_list.append(norm_image)
output = np.stack(image_list, axis=0)
else:
# Parameter estimation using the mask provided
image_mu = np.mean(image[mask_bin])
image_sigma = np.std(image[mask_bin])
if masked:
image = image * mask_bin.astype(image.dtype)
output = ((image - image_mu) / image_sigma).astype(dtype)
return output
def remove_small_regions(img_vol, min_size=3):
"""
Function that removes blobs with a size smaller than a minimum from a mask
volume.
:param img_vol: Mask volume. It should be a numpy array of type bool.
:param min_size: Minimum size for the blobs.
:return: New mask without the small blobs.
"""
blobs, _ = nd.measurements.label(
img_vol,
nd.morphology.generate_binary_structure(3, 3)
)
labels = list(filter(bool, np.unique(blobs)))
areas = [np.count_nonzero(np.equal(blobs, lab)) for lab in labels]
nu_labels = [lab for lab, a in zip(labels, areas) if a >= min_size]
nu_mask = reduce(
lambda x, y: np.logical_or(x, y),
[np.equal(blobs, lab) for lab in nu_labels]
) if nu_labels else np.zeros_like(img_vol)
return nu_mask
def remove_boundary_regions(img_vol, roi, thickness=1):
"""
Function that removes blobs with a size smaller than a minimum from a
mask volume.
:param img_vol: Mask volume. It should be a numpy array of type bool.
:param roi: Region of interest mask. It should be a numpy array of type
bool.
:param thickness: Thickness of the boundary ribbon.
:return: New mask without the small blobs.
"""
small_roi = imerode(roi, iterations=thickness)
boundary = np.logical_and(roi, np.logical_not(small_roi))
blobs, _ = nd.measurements.label(
img_vol,
nd.morphology.generate_binary_structure(3, 3)
)
boundary_labels = list(np.unique(blobs[boundary]))
nu_mask = np.isin(blobs, boundary_labels, invert=True)
return nu_mask
def to_torch_var(
np_array,
device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"),
requires_grad=False,
dtype=torch.float32
):
"""
Function to convert a numpy array into a torch tensor for a given device
:param np_array: Original numpy array
:param device: Device where the tensor will be loaded
:param requires_grad: Whether it requires autograd or not
:param dtype: Datatype for the tensor
:return:
"""
var = torch.tensor(
np_array,
requires_grad=requires_grad,
device=device,
dtype=dtype
)
return var