File size: 12,774 Bytes
3953219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import ipywidgets
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
from itertools import chain, islice
from ipywidgets import interactive, widgets

def _create_label(text:str)->ipywidgets.widgets.Label:
    "Create label widget"
    
    label = widgets.Label(
            text,
            layout=widgets.Layout(
            width='100%', 
            display='flex', 
            justify_content="center"
            )
        )
    return label
        
def _create_slider(
    slider_min: int, 
    slider_max: int, 
    value: int, 
    step: int=1, 
    description:str ='',
    continuous_update: bool=True, 
    readout: bool=False, 
    slider_type: str='IntSlider',
    **kwargs)->ipywidgets.widgets:
    "Create slider widget"
    
    slider = getattr(widgets, slider_type)(
        min=slider_min, 
        max=slider_max, 
        step=step, 
        value=value,
        description=description, 
        continuous_update=continuous_update,
        readout = readout,
        layout=widgets.Layout(width='99%', min_width='200px'),
        style={'description_width': 'initial'}, 
        **kwargs
    )   
    return slider
    
def _create_button(description:str)->ipywidgets.widgets.Button:
    "Create button widget"
    button = widgets.Button(
            description=description,
            layout=widgets.Layout(
                width='95%',
                margin='5px 5px'
            )
        )
    return button

def _create_togglebutton(description: str, 
                         value: int, 
                         **kwargs)->ipywidgets.widgets.Button:
    "Create toggle button widget"
    button = widgets.ToggleButton(
            description=description,
            value = value,
            layout=widgets.Layout(
                width='95%',
                margin='5px 5px'
            ), **kwargs
        )
    return button


class BasicViewer():
    """ Base class for viewing TensorDicom3D objects. 
    
    Args: 
        x: main image object to view as rank 3 tensor
        y: either a segmentation mask as as rank 3 tensor or a label as str. 
        prediction: a class predicton as str
        description: description of the whole image
        figsize: size of image, passed as plotting argument
        cmap: colormap for the image
    Returns: 
        Instance of BasicViewer
    """
    
    def __init__(self, x:torch.Tensor, y=None, prediction:str=None, description: str=None, 
                 figsize=(3, 3), cmap:str='bone'):
        assert x.ndim == 3, f"x.ndim needs to be equal to but is {x.ndim}"
        if isinstance(y, torch.Tensor): 
            assert x.shape == y.shape, f"Shapes of x {x.shape} and y {y.shape} do not match"
        self.x=x
        self.y=y
        self.prediction=prediction
        self.description=description
        self.figsize=figsize
        self.cmap=cmap
        self.with_mask = isinstance(y, torch.Tensor)
        self.slice_range = (1, len(x)) # len(x) == im.shape[0]
                            
    def _plot_slice(self, im_slice, with_mask, px_range):
        "Plot slice of image"
        fig, ax = plt.subplots(1, 1, figsize=self.figsize) 
        ax.imshow(self.x[im_slice-1, :, :].clip(*px_range), cmap=self.cmap)
        if isinstance(self.y, (torch.Tensor)) and with_mask:
            ax.imshow(self.y[im_slice-1, :, :], cmap='jet', alpha = 0.25)
        plt.axis('off')
        ax.set_xticks([])
        ax.set_yticks([])
        plt.show()
    
    def _create_image_box(self, figsize):
        "Create widget items, order them in item_box and generate view box"
        items = []
        
        if self.description: plot_description = _create_label(self.description)
        
        if isinstance(self.y, str):
            label = f'{self.y} | {self.prediction}' if self.prediction else self.y
            if self.prediction: 
                font_color = 'green' if self.y == self.prediction else 'red'
                y_label = _create_label(r'\(\color{' + font_color + '} {' + label  + '}\)') 
            else: 
                y_label = _create_label(label)
        else: y_label = _create_label(' ')
            
        slice_slider = _create_slider(
            slider_min = min(self.slice_range), 
            slider_max = max(self.slice_range), 
            value = max(self.slice_range)//2, 
            readout = True)

        toggle_mask_button = _create_togglebutton('Show Mask', True)
        
        range_slider = _create_slider(
            slider_min = self.x.min().numpy(), 
            slider_max = self.x.max().numpy(),  
            value = [self.x.min().numpy(), self.x.max().numpy()], 
            slider_type = 'FloatRangeSlider' if torch.is_floating_point(self.x) else 'IntRandSlider', 
            step = 0.01 if torch.is_floating_point(self.x) else 1, 
            readout=True)
        
        image_output = widgets.interactive_output(
            f = self._plot_slice,
            controls = {'im_slice': slice_slider, 
                'with_mask': toggle_mask_button, 
                'px_range': range_slider})

        image_output.layout.height = f'{self.figsize[0]/1.2}in' # suppress flickering
        image_output.layout.width = f'{self.figsize[1]/1.2}in' # suppress flickering
        
        if self.description: items.append(plot_description)
        items.append(y_label)
        items.append(range_slider)        
        items.append(image_output)
        if isinstance(self.y, torch.Tensor): 
            slice_slider = widgets.HBox([slice_slider, toggle_mask_button])     
        items.append(slice_slider)        
                    
        image_box=widgets.VBox(
            items,
            layout = widgets.Layout(
                border = 'none', 
                margin = '10px 5px 0px 0px', 
                padding =  '5px'))
        
        return image_box
    
    def _generate_views(self):
        image_box = self._create_image_box(self.figsize)
        self.box = widgets.HBox(children=[image_box])

    @property
    def image_box(self):
        return self._create_image_box(self.figsize)
    
    def show(self):
        self._generate_views()
        plt.style.use('default')
        display(self.box)
        
        
class DicomExplorer(BasicViewer):
    """ DICOM viewer for basic image analysis inside iPython notebooks. 
    Can display a single 3D volume together with a segmentation mask, a histogram 
    of voxel/pixel values and some summary statistics. 
    Allows simple windowing by clipping the pixel/voxel values to a region, which 
    can be manually specified.    
    
    """
    
    vbox_layout = widgets.Layout(
        margin = '10px 5px 5px 5px', 
        padding =  '5px',
        display='flex',
        flex_flow='column',
        align_items='center', 
        min_width = '250px')
            
    def _plot_hist(self, px_range):
        x = self.x.numpy().flatten()
        fig, ax = plt.subplots(figsize=self.figsize)
        N, bins, patches = plt.hist(x, 100, color='grey')
        lwr = int(px_range[0] * 100/max(x))
        upr = int(np.ceil(px_range[1] * 100/max(x)))
        
        for i in range(0,lwr):
            patches[i].set_facecolor('grey' if lwr > 0 else 'darkblue')
        for i in range(lwr, upr):
            patches[i].set_facecolor('darkblue')
        for i in range(upr,100):
            patches[i].set_facecolor('grey' if upr < 100 else 'darkblue')
        
        plt.show()
        
    def _image_summary(self, px_range):
        x = self.x.clip(*px_range)

        diffs = x - x.mean()
        var = torch.mean(torch.pow(diffs, 2.0))
        std = torch.pow(var, 0.5)
        zscores = diffs / std
        skews = torch.mean(torch.pow(zscores, 3.0))
        kurt = torch.mean(torch.pow(zscores, 4.0)) - 3.0

        table = f'Statistics:\n' + \
                f'  Mean px value:     {x.mean()} \n' + \
                f'  Std of px values:  {x.std()} \n' + \
                f'  Min px value:      {x.min()} \n' + \
                f'  Max px value:      {x.max()} \n' + \
                f'  Median px value:   {x.median()} \n' + \
                f'  Skewness:          {skews} \n' + \
                f'  Kurtosis:          {kurt} \n\n' + \
                f'Tensor properties \n' + \
                f'  Tensor shape:      {tuple(x.shape)}\n' + \
                f'  Tensor dtype:      {x.dtype}'
        print(table)
        
    def _generate_views(self):   
        
        slice_slider = _create_slider(
            slider_min = min(self.slice_range), 
            slider_max = max(self.slice_range), 
            value = max(self.slice_range)//2, 
            readout = True)

        toggle_mask_button = _create_togglebutton('Show Mask', True)
        
        range_slider = _create_slider(
            slider_min = self.x.min().numpy(), 
            slider_max = self.x.max().numpy(),  
            value = [self.x.min().numpy(), self.x.max().numpy()], 
            continuous_update=False,
            slider_type = 'FloatRangeSlider' if torch.is_floating_point(self.x) else 'IntRandSlider', 
            step = 0.01 if torch.is_floating_point(self.x) else 1)
        
        image_output = widgets.interactive_output(
            f = self._plot_slice,
            controls = {'im_slice': slice_slider, 
                'with_mask': toggle_mask_button, 
                'px_range': range_slider})
                        
        image_output.layout.height = f'{self.figsize[0]/1.2}in' # suppress flickering
        image_output.layout.width = f'{self.figsize[1]/1.2}in' # suppress flickering
        
        if isinstance(self.y, torch.Tensor): 
            slice_slider = widgets.HBox([slice_slider, toggle_mask_button])     
        
        hist_output = widgets.interactive_output(
            f = self._plot_hist,
            controls = {'px_range': range_slider})
        
        hist_output.layout.height = f'{self.figsize[0]/1.2}in' # suppress flickering
        hist_output.layout.width = f'{self.figsize[1]/1.2}in' # suppress flickering

        toggle_mask_button = _create_togglebutton('Show Mask', True)
        
        table_output = widgets.interactive_output(
            f = self._image_summary, 
            controls = {'px_range': range_slider})
        
        table_box = widgets.VBox([table_output], layout=self.vbox_layout)
        
        hist_box = widgets.VBox(
            [hist_output, range_slider],
            layout=self.vbox_layout)
        
        image_box = widgets.VBox(
            [image_output, slice_slider],
            layout=self.vbox_layout)
        
        self.box = widgets.HBox(
            [image_box, hist_box, table_box], 
            layout = widgets.Layout(
                border = 'solid 1px lightgrey', 
                margin = '10px 5px 0px 0px', 
                padding =  '5px', 
                width = f'{self.figsize[1]*2 + 3}in'))
        
        
class ListViewer(object):
    """ Display multipple images with their masks or labels/predictions.
    Arguments: 
        x (tuple, list): Tensor objects to view
        y (tuple, list): Tensor objects (in case of segmentation task) or class labels as string. 
        predictions (str): Class predictions 
        cmap: colormap for display of `x`
        max_n: maximum number of items to display
    """
    
    def __init__(self, x:(list, tuple), y=None, prediction:str=None, description: str=None, 
                 figsize=(4, 4), cmap:str='bone', max_n = 9):
        self.slice_range = (1, len(x))      
        x = x[0:max_n]
        if y: y = y[0:max_n]
        self.x=x
        self.y=y
        self.prediction=prediction
        self.description=description
        self.figsize=figsize
        self.cmap=cmap
        self.max_n=max_n
        
    def _generate_views(self):
        n_images = len(self.x)
        image_grid, image_list = [], []
        
        for i in range(0, n_images):
            image = self.x[i]
            mask = self.y[i] if isinstance(self.y, list) else None
            pred = self.prediction[i] if self.prediction else None
            
            image_list.append(
                BasicViewer(
                    x = image, 
                    y = mask, 
                    prediction = pred, 
                    figsize = self.figsize, 
                    cmap = self.cmap)
                .image_box)
            
            if (i+1) % np.ceil(np.sqrt(n_images)) == 0 or i == n_images - 1: 
                image_grid.append(widgets.HBox(image_list))
                image_list = []
        
        self.box = widgets.VBox(children=image_grid) 
        
    def show(self):
        self._generate_views()
        plt.style.use('default')
        display(self.box)