This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
visualization.py
362 lines (307 loc) · 11.8 KB
/
visualization.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from pathlib import Path
from PIL import Image, ImageFont, ImageDraw
from skimage.color import gray2rgb
from skimage import exposure
import scipy.ndimage as ndi
import nibabel as nib
import numpy as np
import torch
from torchvision.utils import make_grid
import matplotlib.pyplot as plt
FOREGROUND_INDEX = 1
CHANNELS_DIMENSION_SAMPLE = 0
def plot_segmentation_batch_old(batch, predictions=None):
with torch.no_grad():
images = batch['image']['data']
labels = batch['label']['data']
mid_slice = int(labels.shape[-1] // 2)
images = images[..., mid_slice]
labels = labels[..., mid_slice]
images_grid = make_grid(
images,
normalize=True,
scale_each=True,
padding=0,
)
images_grid = images_grid.numpy().transpose(1, 2, 0)
# If the volume has been resampled (e.g. scaling or rotation), a
# non-linear interpolation (e.g. spline) might have been performed
# so values will be [0, 1]. We just want the ones after 0.5 for
# visualization.
labels = labels > 0.5
labels_grid = make_grid(labels, padding=0).numpy().transpose(1, 2, 0)
# Keep only one channel since grayscale
labels_grid = labels_grid[..., 0]
images_grid[..., 1][labels_grid > 0.5] = 1 # green
if predictions is not None:
predictions = predictions[..., mid_slice]
foreground = predictions[:, 1:2, ...]
foreground = foreground > 0.5 # foreground
predictions_grid = make_grid(
foreground, padding=0).detach().cpu().numpy().transpose(1, 2, 0)
# Keep only one channel since grayscale
predictions_grid = predictions_grid[..., 0]
images_grid[..., 0][predictions_grid > 0] = 1 # red
images_grid[..., 2][predictions_grid > 0] = 1 # blue
fig, axis = plt.subplots()
axis.imshow(images_grid)
if 'name' in batch:
names = [name.split('_t1')[0] for name in batch['name']]
axis.set_title(' '.join(names))
plt.tight_layout()
return fig
def get_regression_grid(batch, predictions) -> np.ndarray:
with torch.no_grad():
resected_grid = get_batch_resection_montage(batch, predictions, 'image')
predictions_grid = get_batch_resection_montage(batch, predictions)
if len(batch['image']) > 1:
function = np.vstack
else:
function = np.hstack
grid = function((resected_grid, predictions_grid))
grid = grid.transpose(2, 0, 1)
return grid
def get_batch_grid(batch, predictions, regression=False, border=True, transpose=True) -> np.ndarray:
with torch.no_grad():
batch_grid = get_batch_overlay_montage(
batch, predictions, border=border)
# TODO: get indices beforehand?
probabilities_grid = get_batch_prediction_montage(predictions, batch)
if len(batch['image']) > 1:
function = np.vstack
else:
function = np.hstack
grid = function((batch_grid, probabilities_grid))
if transpose:
grid = grid.transpose(2, 0, 1)
return grid
def plot_segmentation_batch(batch, predictions, border=True):
grid = get_batch_grid(batch, predictions, border=border)
fig, axis = plt.subplots()
axis.imshow(grid)
if 'name' in batch:
names = [name.split('_t1')[0] for name in batch['name']]
axis.set_title(' '.join(names))
plt.tight_layout()
return fig
def tensor_to_array(tensor):
return tensor.squeeze().detach().cpu().numpy()
def normalize_array(array):
array = array.astype(np.float32)
array -= array.min()
if array.max() != 0:
array /= array.max()
return array
def reorient_slice(array, a, b):
"""
a and b are intersection with other planes
They'll be gray for MRI and white for predictions
"""
array[a, :] = 0.5
array[:, b] = 0.5
return np.fliplr(np.rot90(array))
def rescale_array(array):
p2, p98 = np.percentile(array, (2, 98))
rescaled = exposure.rescale_intensity(array, in_range=(p2, p98))
return rescaled
def get_mid_indices(array):
dimensions = 3
shape = size_r, size_a, size_s = array.shape[-dimensions:]
shape = np.array(shape)
return shape // 2
def get_slices(array, i, j, k):
assert array.ndim == 3
sagittal = reorient_slice(array[i, :, :], j, k)
coronal = reorient_slice(array[:, j, :], i, k)
axial = reorient_slice(array[:, :, k], i, j)
return sagittal, coronal, axial
def get_centroid_slices(label_array):
foreground = label_array[1] if len(label_array) == 2 else label_array[0]
coords = np.array(np.where(foreground)).T # N x 4
centroid = coords.mean(axis=0).round().astype(np.uint16)
return centroid
def get_montage(
array,
rescale=False,
normalize=False,
border=False,
text=None,
indices=None,
):
"""
Array should have 3 dimensions?
"""
assert array.ndim == 3
if rescale:
array = rescale_array(array)
if border:
# If the volume has been resampled (e.g. scaling or rotation), a
# non-linear interpolation (e.g. spline) might have been performed
# so values will be [0, 1]. We just want the ones after 0.5 for
# visualization.
array = (array > 0.5).astype(bool)
eroded = ndi.morphology.binary_erosion(array)
array = array ^ eroded
if normalize:
array = normalize_array(array)
dimensions = 3
if indices is None:
indices = get_mid_indices(array)
sagittal, coronal, axial = get_slices(array, *indices)
_, size_a, _ = array.shape[-dimensions:]
# | metrics | axial |
# | sagittal | coronal |
metrics_size = size_a, size_a
metrics_image = Image.new('F', metrics_size, 0)
if text is not None:
add_text(metrics_image, text)
metrics_array = np.array(metrics_image)
first_row = np.hstack((metrics_array, axial))
second_row = np.hstack((sagittal, coronal))
montage = np.vstack((first_row, second_row))
return montage
def add_text(image, text):
font_path = Path('/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf')
if not font_path.is_file(): # UCL cluster?
font_path = Path('/usr/share/fonts/dejavu/DejaVuSans.ttf')
fontsize = 48
draw = ImageDraw.Draw(image)
image_size_x, image_size_y = image.size
font = ImageFont.truetype(str(font_path), fontsize)
text_size_x, text_size_y = draw.multiline_textsize(text, font=font)
while text_size_x >= image_size_x:
fontsize -= 2
font = ImageFont.truetype(str(font_path), fontsize)
text_size_x, text_size_y = draw.multiline_textsize(text, font=font)
start_x = image_size_x // 2 - text_size_x // 2
start_y = image_size_y // 2 - text_size_y // 2
xy = start_x, start_y
gray_value = 200 / 255
draw.multiline_text(
xy, text, fill=gray_value, font=font, align='center')
def get_sample_overlay_montage(
image_tensor,
label_tensor,
prediction,
border=True,
text=None,
indices=None,
):
"""
Prediction is [0, 1]
"""
image = tensor_to_array(image_tensor)
label = tensor_to_array(label_tensor)
if label.ndim == 4:
num_channels = label.shape[CHANNELS_DIMENSION_SAMPLE]
if num_channels > 1:
label = label[FOREGROUND_INDEX]
prediction = prediction[FOREGROUND_INDEX, ...] # extract foreground
prediction = tensor_to_array(prediction) >= 0.5
montage_image = get_montage(
image, rescale=True, normalize=True, text=text, indices=indices)
montage_label = get_montage(
label, border=border, normalize=True, indices=indices)
montage_prediction = get_montage(
prediction, border=border, normalize=True, indices=indices)
montage = gray2rgb(montage_image)
# If the volume has been resampled (e.g. scaling or rotation), a
# non-linear interpolation (e.g. spline) might have been performed
# so values will be [0, 1]. We just want the ones after 0.5 for
# visualization.
montage[..., 1][montage_label > 0.5] = 1 # green
montage[..., 0][montage_prediction > 0] = 1 # red
montage[..., 2][montage_prediction > 0] = 1 # blue
return montage
def get_sample_resection_montage(
image,
text=None,
indices=None,
):
"""
Prediction is [0, 1]
"""
image = tensor_to_array(image)
montage_image = get_montage(image, rescale=True, normalize=True, text=text, indices=indices)
montage = gray2rgb(montage_image)
return montage
def get_resection_indices(batch, i):
"""
Return voxel index from resection center
This would not be accurate if spatial transforms are applied
ESPECIALLY FLIPPING!
"""
if 'random_resection' not in batch:
return None
resection_params = batch['random_resection']
center_ras = resection_params['resection_center'][i]
ijk_to_ras = batch['affine'][i]
ras_to_ijk = np.linalg.inv(ijk_to_ras)
indices = nib.affines.apply_affine(ras_to_ijk, center_ras)
indices = indices.astype(np.uint16)
return indices
def get_batch_resection_montage(batch, predictions, image_key=None, border=True):
montages = []
add_names = 'name' in batch
if add_names:
for i, prediction in enumerate(predictions):
image = batch[image_key]['data'][i] if image_key is not None else prediction
label = batch['label']['data'][i]
filename = batch['name'][i]
name = filename.split('_t1')[0]
indices = get_centroid_slices(label)
montage = get_sample_resection_montage(image, text=name, indices=indices)
montages.append(montage)
else:
for i, prediction in enumerate(predictions):
image = batch[image_key]['data'][i] if image_key is not None else prediction
label = batch['label']['data'][i]
indices = get_centroid_slices(label)
montage = get_sample_resection_montage(image, indices=indices)
montages.append(montage)
grid = np.hstack(montages)
return grid
def get_batch_overlay_montage(batch, predictions, border=True):
montages = []
add_names = 'name' in batch
if add_names:
for i, prediction in enumerate(predictions):
image = batch['image']['data'][i]
label = batch['label']['data'][i]
try:
filename = batch['name'][i]
name = filename.split('_t1')[0]
except Exception: # getting error when mixing with pseudolabeled
name = '[Unknown]'
indices = get_centroid_slices(label)
montage = get_sample_overlay_montage(
image, label, prediction, border=border, text=name, indices=indices)
montages.append(montage)
else:
for i, prediction in enumerate(predictions):
image = batch['image']['data'][i]
label = batch['label']['data'][i]
indices = get_centroid_slices(label)
montage = get_sample_overlay_montage(
image, label, prediction, border=border, indices=indices)
montages.append(montage)
grid = np.hstack(montages)
return grid
def colorize(prediction):
# pylint: disable=E1101
rgba = plt.cm.RdBu_r(prediction)
rgb = rgba[..., :-1]
return rgb
def get_batch_prediction_montage(predictions, batch):
# Predictions are [0, 1]
montages = []
for i, prediction in enumerate(predictions):
label = batch['label']['data'][i]
indices = get_centroid_slices(label)
prediction = prediction[FOREGROUND_INDEX, ...] # extract foreground
prediction_array = tensor_to_array(prediction)
montage = get_montage(prediction_array, indices=indices)
colorized = colorize(montage)
montages.append(colorized)
grid = np.hstack(montages)
return grid