-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvisualizer.py
231 lines (190 loc) · 7.6 KB
/
visualizer.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
from tqdm import tqdm
import os
import time
from random import randint
import nibabel as nib
import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.animation as anim
import matplotlib.patches as mpatches
import matplotlib.gridspec as gridspec
import imageio
from skimage.transform import resize
from skimage.util import montage
import numpy
import torch
class Image3dToGIF3d:
"""
Displaying 3D images in 3d axes.
Parameters:
img_dim: shape of cube for resizing.
figsize: figure size for plotting in inches.
"""
def __init__(self,
img_dim: tuple = (55, 55, 55),
figsize: tuple = (15, 10),
binary: bool = False,
normalizing: bool = True,
):
"""Initialization."""
self.img_dim = img_dim
print(img_dim)
self.figsize = figsize
self.binary = binary
self.normalizing = normalizing
def _explode(self, data: np.ndarray):
"""
Takes: array and return an array twice as large in each dimension,
with an extra space between each voxel.
"""
shape_arr = np.array(data.shape)
size = shape_arr[:3] * 2 - 1
exploded = np.zeros(np.concatenate([size, shape_arr[3:]]),
dtype=data.dtype)
exploded[::2, ::2, ::2] = data
return exploded
def _expand_coordinates(self, indices: np.ndarray):
x, y, z = indices
x[1::2, :, :] += 1
y[:, 1::2, :] += 1
z[:, :, 1::2] += 1
return x, y, z
def _normalize(self, arr: np.ndarray):
"""Normilize image value between 0 and 1."""
arr_min = np.min(arr)
return (arr - arr_min) / (np.max(arr) - arr_min)
def _scale_by(self, arr: np.ndarray, factor: int):
"""
Scale 3d Image to factor.
Parameters:
arr: 3d image for scalling.
factor: factor for scalling.
"""
mean = np.mean(arr)
return (arr - mean) * factor + mean
def get_transformed_data(self, data: np.ndarray):
"""Data transformation: normalization, scaling, resizing."""
if self.binary:
resized_data = resize(data, self.img_dim, preserve_range=True)
return np.clip(resized_data.astype(np.uint8), 0, 1).astype(np.float32)
norm_data = np.clip(self._normalize(data)-0.1, 0, 1) ** 0.4
scaled_data = np.clip(self._scale_by(norm_data, 2) - 0.1, 0, 1)
resized_data = resize(scaled_data, self.img_dim, preserve_range=True)
return resized_data
def plot_cube(self,
cube,
title: str = '',
init_angle: int = 0,
make_gif: bool = False,
path_to_save: str = 'filename.gif'
):
"""
Plot 3d data.
Parameters:
cube: 3d data
title: title for figure.
init_angle: angle for image plot (from 0-360).
make_gif: if True create gif from every 5th frames from 3d image plot.
path_to_save: path to save GIF file.
"""
if self.binary:
facecolors = cm.winter(cube)
print("binary")
else:
if self.normalizing:
cube = self._normalize(cube)
facecolors = cm.gist_stern(cube)
print("not binary")
facecolors[:,:,:,-1] = cube
facecolors = self._explode(facecolors)
filled = facecolors[:,:,:,-1] != 0
x, y, z = self._expand_coordinates(np.indices(np.array(filled.shape) + 1))
with plt.style.context("dark_background"):
fig = plt.figure(figsize=self.figsize)
ax = fig.gca(projection='3d')
ax.view_init(30, init_angle)
ax.set_xlim(right = self.img_dim[0] * 2)
ax.set_ylim(top = self.img_dim[1] * 2)
ax.set_zlim(top = self.img_dim[2] * 2)
ax.set_title(title, fontsize=18, y=1.05)
ax.voxels(x, y, z, filled, facecolors=facecolors, shade=False)
if make_gif:
images = []
for angle in tqdm(range(0, 360, 5)):
ax.view_init(30, angle)
fname = str(angle) + '.png'
plt.savefig(fname, dpi=120, format='png', bbox_inches='tight')
images.append(imageio.imread(fname))
#os.remove(fname)
imageio.mimsave(path_to_save, images)
plt.close()
else:
plt.show()
class ShowResult:
def mask_preprocessing(self, mask):
"""
Test.
"""
mask = mask.squeeze().cpu().detach().numpy()
mask = np.moveaxis(mask, (0, 1, 2, 3), (0, 3, 2, 1))
mask_WT = np.rot90(montage(mask[0]))
mask_TC = np.rot90(montage(mask[1]))
mask_ET = np.rot90(montage(mask[2]))
return mask_WT, mask_TC, mask_ET
def image_preprocessing(self, image):
"""
Returns image flair as mask for overlaping gt and predictions.
"""
image = image.squeeze().cpu().detach().numpy()
image = np.moveaxis(image, (0, 1, 2, 3), (0, 3, 2, 1))
flair_img = np.rot90(montage(image[0]))
return flair_img
def plot(self, image, ground_truth, prediction):
image = self.image_preprocessing(image)
gt_mask_WT, gt_mask_TC, gt_mask_ET = self.mask_preprocessing(ground_truth)
pr_mask_WT, pr_mask_TC, pr_mask_ET = self.mask_preprocessing(prediction)
fig, axes = plt.subplots(1, 2, figsize = (35, 30))
[ax.axis("off") for ax in axes]
axes[0].set_title("Ground Truth", fontsize=35, weight='bold')
axes[0].imshow(image, cmap ='bone')
axes[0].imshow(np.ma.masked_where(gt_mask_WT == False, gt_mask_WT),
cmap='cool_r', alpha=0.6)
axes[0].imshow(np.ma.masked_where(gt_mask_TC == False, gt_mask_TC),
cmap='autumn_r', alpha=0.6)
axes[0].imshow(np.ma.masked_where(gt_mask_ET == False, gt_mask_ET),
cmap='autumn', alpha=0.6)
axes[1].set_title("Prediction", fontsize=35, weight='bold')
axes[1].imshow(image, cmap ='bone')
axes[1].imshow(np.ma.masked_where(pr_mask_WT == False, pr_mask_WT),
cmap='cool_r', alpha=0.6)
axes[1].imshow(np.ma.masked_where(pr_mask_TC == False, pr_mask_TC),
cmap='autumn_r', alpha=0.6)
axes[1].imshow(np.ma.masked_where(pr_mask_ET == False, pr_mask_ET),
cmap='autumn', alpha=0.6)
plt.tight_layout()
plt.show()
def merging_two_gif(path1: str, path2: str, name_to_save: str):
"""
Merging GIFs side by side.
Parameters:
path1: path to gif with ground truth.
path2: path to gif with prediction.
name_to_save: name for saving new GIF.
"""
#https://stackoverflow.com/questions/51517685/combine-several-gif-horizontally-python
#Create reader object for the gif
gif1 = imageio.get_reader(path1)
gif2 = imageio.get_reader(path2)
#If they don't have the same number of frame take the shorter
number_of_frames = min(gif1.get_length(), gif2.get_length())
#Create writer object
new_gif = imageio.get_writer(name_to_save)
for frame_number in range(number_of_frames):
img1 = gif1.get_next_data()
img2 = gif2.get_next_data()
#here is the magic
new_image = np.hstack((img1, img2))
new_gif.append_data(new_image)
gif1.close()
gif2.close()
new_gif.close()