-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.py
160 lines (127 loc) · 6.78 KB
/
widgets.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
import ipywidgets as ipyw
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
class ImageSliceViewer3D:
def __init__(self, im, mas, figsize=(4,4)):
self.image = im
self.mask = mas
self.figsize = figsize
self.v = [np.min(self.image), np.max(self.image)]
self.m = [np.min(self.mask), np.max(self.mask)]
x_size, y_size, z_size = self.image.shape
self.xy_label = f'x-y ({x_size}x{y_size})'
self.yz_label = f'y-z ({y_size}x{z_size})'
self.zx_label = f'z-x ({z_size}x{x_size})'
# Call to select slice plane
ipyw.interact(self.view_selection, view=ipyw.RadioButtons(
options=[self.xy_label, self.yz_label, self.zx_label],
description='Plane:', disabled=False,
style={'description_width': 'initial'}))
def view_selection(self, view):
# Transpose the volume to orient according to the slice plane selection
orient = {self.xy_label:[0,1,2], self.yz_label:[2,0,1], self.zx_label: [1,2,0]}
label = {self.xy_label: "z", self.yz_label: "x", self.zx_label: "y"}
self.vol = np.transpose(self.image, orient[view])
self.mas = np.transpose(self.mask, orient[view])
maxZ = self.vol.shape[2] - 1
# Call to view a slice within the selected slice plane
ipyw.interact(self.plot_slice,
z=ipyw.IntSlider(value=maxZ//2, min=0, max=maxZ, step=1, continuous_update=False,
description=f'{label[view]} (0-{maxZ}):'))
def plot_slice(self, z):
# Plot slice for the given plane and slice
self.fig = plt.figure(figsize=self.figsize)
plt.subplot(121, title="Image")
plt.imshow(self.vol[:,:,z], vmin=self.v[0], vmax=self.v[1], cmap='gray')
plt.subplot(122, title="Mask")
#plt.imshow(self.mas[:,:,z], vmin=self.m[0], vmax=self.m[1], cmap='gray')
plt.imshow(self.mas[:,:,z], vmin=0, vmax=2, cmap='gray')
class ContrastsViewer3D:
def __init__(self, phase, t2, mask, *, show_mask=True, figsize=(8,8), titles=("Phase", "T2", "Mask")):
self.titles = titles
self.phase = phase
self.figsize = figsize
self.mask = mask
self.t2 = t2
self.show_mask = show_mask
self.v = [np.min(self.phase), np.max(self.phase)]
self.m = [np.min(self.mask), np.max(self.mask)]
self.o = [np.min(self.t2), np.max(self.t2)]
x_size, y_size, z_size = self.phase.shape
self.xy_label = f'x-y ({x_size}x{y_size})'
self.yz_label = f'y-z ({y_size}x{z_size})'
self.zx_label = f'z-x ({z_size}x{x_size})'
# Call to select slice plane
ipyw.interact(self.view_selection, view=ipyw.RadioButtons(
options=[self.xy_label, self.yz_label, self.zx_label],
description='Plane:', disabled=False,
style={'description_width': 'initial'}))
def view_selection(self, view):
# Transpose the volume to orient according to the slice plane selection
orient = {self.xy_label:[0,1,2], self.yz_label:[2,0,1], self.zx_label: [1,2,0]}
label = {self.xy_label: "z", self.yz_label: "x", self.zx_label: "y"}
self.phase = np.transpose(self.phase, orient[view])
self.mask = np.transpose(self.mask, orient[view])
self.t2 = np.transpose(self.t2, orient[view])
maxZ = self.phase.shape[2] - 1
# Call to view a slice within the selected slice plane
ipyw.interact(self.plot_slice,
z=ipyw.IntSlider(value=maxZ//2, min=0, max=maxZ, step=1, continuous_update=False,
description=f'{label[view]} (0-{maxZ}):'))
def plot_slice(self, z):
# Plot slice for the given plane and slice
self.fig = plt.figure(figsize=self.figsize)
plt.subplot(131, title=self.titles[0])
plt.imshow(self.phase[:,:,z], vmin=self.v[0], vmax=self.v[1], cmap='gray')
plt.subplot(132, title=self.titles[1])
plt.imshow(self.t2[:,:,z], vmin=self.o[0], vmax=self.o[1], cmap='gray')
if self.show_mask:
plt.subplot(133, title=self.titles[2])
plt.imshow(self.mask[:,:,z], vmin=self.m[0], vmax=self.m[1], cmap='gray')
class OcclusionMapViewer3D:
def __init__(self, im, mask, occl, *, show_mask=False, figsize=(8,8)):
self.image = im
self.figsize = figsize
self.mask = mask
self.occl = occl
self.show_mask = show_mask
self.v = [np.min(self.image), np.max(self.image)]
self.m = [np.min(self.mask), np.max(self.mask)]
self.o = [np.min(self.occl), np.max(self.occl)]
x_size, y_size, z_size = self.image.shape
self.xy_label = f'x-y ({x_size}x{y_size})'
self.yz_label = f'y-z ({y_size}x{z_size})'
self.zx_label = f'z-x ({z_size}x{x_size})'
# Call to select slice plane
ipyw.interact(self.view_selection, view=ipyw.RadioButtons(
options=[self.xy_label, self.yz_label, self.zx_label],
description='Plane:', disabled=False,
style={'description_width': 'initial'}))
def view_selection(self, view):
# Transpose the volume to orient according to the slice plane selection
orient = {self.xy_label:[0,1,2], self.yz_label:[2,0,1], self.zx_label: [1,2,0]}
label = {self.xy_label: "z", self.yz_label: "x", self.zx_label: "y"}
self.image = np.transpose(self.image, orient[view])
self.mask = np.transpose(self.mask, orient[view])
self.occl = np.transpose(self.occl, orient[view])
maxZ = self.image.shape[2] - 1
# Call to view a slice within the selected slice plane
ipyw.interact(self.plot_slice,
z=ipyw.IntSlider(value=maxZ//2, min=0, max=maxZ, step=1, continuous_update=False,
description=f'{label[view]} (0-{maxZ}):'))
def plot_slice(self, z):
# Plot slice for the given plane and slice
self.fig = plt.figure(figsize=self.figsize)
plt.subplot(131, title="Image")
plt.imshow(self.image[:,:,z], vmin=self.v[0], vmax=self.v[1], cmap='gray')
if self.show_mask:
plt.subplot(132, title="Mask")
plt.imshow(self.mask[:,:,z], vmin=self.m[0], vmax=self.m[1], cmap='gray')
ax = plt.subplot(133, title="Map")
im = plt.imshow(self.occl[:,:,z], vmin=self.o[0], vmax=self.o[1], cmap='gray')
else:
plt.subplot(121, title="Image")
plt.imshow(self.image[:,:,z], vmin=self.v[0], vmax=self.v[1], cmap='gray')
ax = plt.subplot(122, title="Map")
im = plt.imshow(self.occl[:,:,z], vmin=self.o[0], vmax=self.o[1], cmap='gray')