[78ef36]: / scripts / slideflow-studio.py

Download this file

370 lines (300 with data), 14.2 kB

  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
363
364
365
366
367
368
369
#!/usr/bin/env python
import pkgutil
import threading
import click
import numpy as np
import glfw
import OpenGL.GL as gl
import OpenGL.GL.ARB.texture_float
from os.path import dirname, realpath, join
from PIL import Image, ImageFont
from contextlib import contextmanager
from functools import lru_cache
from os.path import join, dirname
__version__ = "3.0.2"
# -----------------------------------------------------------------------------
@click.command()
@click.argument('slide', metavar='PATH', required=False)
@click.option('--model', '-m', help='Classifier network for categorical predictions.', metavar='PATH')
@click.option('--project', '-p', help='Slideflow project.', metavar='PATH')
@click.option('--low_memory', '-l', is_flag=True, help='Low memory mode.', metavar=bool)
@click.option('--stylegan', '-g', is_flag=True, help='Enable StyleGAN support (requires PyTorch).', metavar=bool)
@click.option('--picam', '-pc', is_flag=True, help='Enable Picamera2 view (experimental).', metavar=bool)
@click.option('--camera', '-c', is_flag=True, help='Enable Camera (OpenCV) view (experimental).', metavar=bool)
@click.option('--cellpose', is_flag=True, help='Enable Cellpose segmentation (experimental).', metavar=bool)
def main(
slide,
model,
project,
low_memory,
stylegan,
picam,
camera,
cellpose
):
"""
Whole-slide image viewer with deep learning model visualization tools.
Optional PATH argument can be used specify which slide to initially load.
"""
# Start the splash screen
import_with_splash()
from slideflow.studio import Studio
if low_memory is None:
low_memory = False
# Load widgets
widgets = Studio.get_default_widgets()
if stylegan:
from slideflow.studio.widgets.stylegan import StyleGANWidget
widgets += [StyleGANWidget]
if picam:
from slideflow.studio.widgets.picam import PicamWidget
widgets += [PicamWidget]
if camera:
from slideflow.studio.widgets.cvcam import CameraWidget
widgets += [CameraWidget]
if cellpose:
from slideflow.studio.widgets.cellseg import CellSegWidget
widgets += [CellSegWidget]
viz = Studio(low_memory=low_memory, widgets=widgets)
viz.project_widget.search_dirs += [dirname(realpath(__file__))]
# Load model.
if model is not None:
viz.load_model(model)
if project is not None:
viz.load_project(project)
# Load slide(s).
if slide:
viz.load_slide(slide)
# Run.
viz.run()
#----------------------------------------------------------------------------
def import_with_splash():
_imported = False
def _import_sildeflow():
nonlocal _imported
import slideflow.studio
_imported = True
# Start the import thread
_thread = threading.Thread(target=_import_sildeflow)
_thread.start()
# Send Tk to the background (used for future file dialogs)
from tkinter import Tk
Tk().withdraw()
# Load image
sf_root = pkgutil.get_loader('slideflow').get_filename()
splash_path = join(dirname(sf_root), 'studio', 'gui', 'splash.png')
icon_path = join(dirname(sf_root), 'studio', 'gui', 'icons', 'logo.png')
img = np.array(Image.open(splash_path))
icon = np.array(Image.open(icon_path))
# Start GLFW window
if not glfw.init():
return
glfw.window_hint(glfw.DECORATED, False)
height = img.shape[0]
width = img.shape[1]
# Center on screen
_, _, mw, mh = glfw.get_monitor_workarea(glfw.get_primary_monitor())
if not mw:
vmode = glfw.get_video_mode(glfw.get_primary_monitor())
mw, mh = vmode.size
wscale, hscale = glfw.get_monitor_content_scale(glfw.get_primary_monitor())
else:
wscale, hscale = 1, 1
window = glfw.create_window(int(width/wscale), int(height/hscale), "Slideflow Studio", None, None)
glfw.set_window_pos(window, (mw - int(width/wscale)) // 2, (mh - int(height/hscale)) // 2)
_tex_bg = None
_tex_icon = None
_version_text = None
_first_frame = True
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
while not glfw.window_should_close(window):
glfw.poll_events()
# Initialize GL state.
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.glTranslate(-1, 1, 0)
gl.glScale(2 / max(width, 1), -2 / max(height, 1), 1)
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glLoadIdentity()
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_ONE, gl.GL_ONE_MINUS_SRC_ALPHA) # Pre-multiplied alpha.
# Clear.
gl.glClearColor(0, 0, 0, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
if _tex_bg is None:
_tex_bg = Texture(image=img, bilinear=False)
if _tex_icon is None:
_tex_icon = Texture(image=icon, bilinear=True)
if _version_text is None:
_version_text = text_texture(__version__, size=22)
_tex_bg.draw(pos=0, zoom=1, align=0.5, rint=True, anchor='topleft')
_tex_icon.draw(pos=(width//2, int(height * 0.3)), zoom=0.25, align=0.5, rint=True, anchor='center')
_version_text.draw(pos=(width//2, int(height * 0.7)), zoom=1, align=0.5, rint=True, anchor='center')
if not _first_frame:
glfw.show_window(window)
_first_frame = False
glfw.swap_buffers(window)
if _imported:
glfw.destroy_window(window)
glfw.default_window_hints()
break
glfw.terminate()
# -----------------------------------------------------------------------------
class Texture:
"""Class to assist with creation and render of an OpenGL texture."""
_texture_formats = {
('uint8', 1): dict(type=gl.GL_UNSIGNED_BYTE, format=gl.GL_LUMINANCE, internalformat=gl.GL_LUMINANCE8),
('uint8', 2): dict(type=gl.GL_UNSIGNED_BYTE, format=gl.GL_LUMINANCE_ALPHA, internalformat=gl.GL_LUMINANCE8_ALPHA8),
('uint8', 3): dict(type=gl.GL_UNSIGNED_BYTE, format=gl.GL_RGB, internalformat=gl.GL_RGB8),
('uint8', 4): dict(type=gl.GL_UNSIGNED_BYTE, format=gl.GL_RGBA, internalformat=gl.GL_RGBA8),
('float32', 1): dict(type=gl.GL_FLOAT, format=gl.GL_LUMINANCE, internalformat=gl.ARB.texture_float.GL_LUMINANCE32F_ARB),
('float32', 2): dict(type=gl.GL_FLOAT, format=gl.GL_LUMINANCE_ALPHA, internalformat=gl.ARB.texture_float.GL_LUMINANCE_ALPHA32F_ARB),
('float32', 3): dict(type=gl.GL_FLOAT, format=gl.GL_RGB, internalformat=gl.GL_RGB32F),
('float32', 4): dict(type=gl.GL_FLOAT, format=gl.GL_RGBA, internalformat=gl.GL_RGBA32F),
}
def __init__(
self,
*,
image=None,
width=None,
height=None,
channels=None,
dtype=None,
bilinear=True,
mipmap=True
) -> None:
self.gl_id = None
self.bilinear = bilinear
self.mipmap = mipmap
# Determine size and dtype.
if image is not None:
image = np.asarray(image)
if image.ndim == 2:
image = image[:, :, np.newaxis]
if image.dtype.name == 'float64':
image = image.astype('float32')
self.height, self.width, self.channels = image.shape
self.dtype = image.dtype
else:
assert width is not None and height is not None
self.width = width
self.height = height
self.channels = channels if channels is not None else 3
self.dtype = np.dtype(dtype) if dtype is not None else np.uint8
# Validate size and dtype.
assert isinstance(self.width, int) and self.width >= 0
assert isinstance(self.height, int) and self.height >= 0
assert isinstance(self.channels, int) and self.channels >= 1
# Create texture object.
self.gl_id = gl.glGenTextures(1)
with self.bind():
gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE)
gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR if self.bilinear else gl.GL_NEAREST)
gl.glTexParameterf(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR if self.mipmap else gl.GL_NEAREST)
with self.bind():
fmt = self._texture_formats[(np.dtype(self.dtype).name, int(self.channels))]
gl.glPushClientAttrib(gl.GL_CLIENT_PIXEL_STORE_BIT)
gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, fmt['internalformat'], self.width, self.height, 0, fmt['format'], fmt['type'], image)
if self.mipmap:
gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
gl.glPopClientAttrib()
@contextmanager
def bind(self):
prev_id = gl.glGetInteger(gl.GL_TEXTURE_BINDING_2D)
gl.glBindTexture(gl.GL_TEXTURE_2D, self.gl_id)
yield
gl.glBindTexture(gl.GL_TEXTURE_2D, prev_id)
def draw(self, *, pos=0, zoom=1, align=0, rint=False, color=1, alpha=1, rounding=0, anchor='center'):
zoom = np.broadcast_to(np.asarray(zoom, dtype='float32'), [2])
size = zoom * [self.width, self.height]
with self.bind():
gl.glPushAttrib(gl.GL_ENABLE_BIT)
gl.glEnable(gl.GL_TEXTURE_2D)
self._draw_rect(pos=pos, size=size, align=align, rint=rint, color=color, alpha=alpha, rounding=rounding, anchor=anchor)
gl.glPopAttrib()
def delete(self):
if self.gl_id is not None:
gl.glDeleteTextures([self.gl_id])
self.gl_id = None
def __del__(self):
try:
self.delete()
except:
pass
def _draw_rect(self, *, pos=0, pos2=None, size=None, align=0, rint=False, color=1, alpha=1, rounding=0, mode=gl.GL_TRIANGLE_FAN, anchor='center'):
assert pos2 is None or size is None
assert anchor in ('center', 'topleft')
pos = np.broadcast_to(np.asarray(pos, dtype='float32'), [2])
pos2 = np.broadcast_to(np.asarray(pos2, dtype='float32'), [2]) if pos2 is not None else None
size = np.broadcast_to(np.asarray(size, dtype='float32'), [2]) if size is not None else None
size = size if size is not None else pos2 - pos if pos2 is not None else np.array([1, 1], dtype='float32')
pos = pos - size * align
if rint:
pos = np.rint(pos)
rounding = np.broadcast_to(np.asarray(rounding, dtype='float32'), [2])
rounding = np.minimum(np.abs(rounding) / np.maximum(np.abs(size), 1e-8), 0.5)
if np.min(rounding) == 0:
rounding *= 0
vertices = self._setup_center_rect(float(rounding[0]), float(rounding[1]))
self._draw_shape(vertices, pos=pos, size=size, color=color, alpha=alpha, mode=mode, anchor=anchor)
def _draw_shape(self, vertices, *, mode=gl.GL_TRIANGLE_FAN, pos=0, size=1, color=1, alpha=1, anchor='center'):
assert vertices.ndim == 2 and vertices.shape[1] == 2
assert anchor in ('center', 'topleft')
pos = np.broadcast_to(np.asarray(pos, dtype='float32'), [2])
size = np.broadcast_to(np.asarray(size, dtype='float32'), [2])
color = np.broadcast_to(np.asarray(color, dtype='float32'), [3])
alpha = np.clip(np.broadcast_to(np.asarray(alpha, dtype='float32'), []), 0, 1)
gl.glPushClientAttrib(gl.GL_CLIENT_VERTEX_ARRAY_BIT)
gl.glPushAttrib(gl.GL_CURRENT_BIT | gl.GL_TRANSFORM_BIT)
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glPushMatrix()
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY)
gl.glVertexPointer(2, gl.GL_FLOAT, 0, vertices)
gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, vertices)
if anchor == 'center':
gl.glTranslate(pos[0], pos[1], 0)
else:
gl.glTranslate(pos[0]+size[0]/2, pos[1]+size[1]/2, 0)
gl.glScale(size[0], size[1], 1)
gl.glColor4f(color[0] * alpha, color[1] * alpha, color[2] * alpha, alpha)
gl.glDrawArrays(mode, 0, vertices.shape[0])
gl.glPopMatrix()
gl.glPopAttrib()
gl.glPopClientAttrib()
@lru_cache(maxsize=10000)
def _setup_center_rect(self, rx, ry):
t = np.linspace(0, np.pi / 2, 1 if max(rx, ry) == 0 else 64)
s = 1 - np.sin(t); c = 1 - np.cos(t)
x = [c * rx, 1 - s * rx, 1 - c * rx, s * rx]
y = [s * ry, c * ry, 1 - s * ry, 1 - c * ry]
v = np.stack([x, y], axis=-1).reshape(-1, 2)
return v.astype('float32')
@lru_cache(maxsize=10000)
def _text_to_array(string, *, font=None, size=32, line_pad: int=None):
if font is None:
sf_root = pkgutil.get_loader('slideflow').get_filename()
font = join(dirname(sf_root), 'studio', 'gui', 'fonts', 'DroidSans.ttf')
pil_font = ImageFont.truetype(font=font, size=size)
lines = [pil_font.getmask(line, 'L') for line in string.split('\n')]
lines = [np.array(line, dtype=np.uint8).reshape([line.size[1], line.size[0]]) for line in lines]
width = max(line.shape[1] for line in lines)
lines = [np.pad(line, ((0, 0), (0, width - line.shape[1])), mode='constant') for line in lines]
line_spacing = line_pad if line_pad is not None else size // 2
lines = [np.pad(line, ((0, line_spacing), (0, 0)), mode='constant') for line in lines[:-1]] + lines[-1:]
mask = np.concatenate(lines, axis=0)
alpha = mask
return np.stack([mask, alpha], axis=-1)
@lru_cache(maxsize=10000)
def text_texture(string, bilinear=False, mipmap=True, **kwargs):
return Texture(image=_text_to_array(string, **kwargs), bilinear=bilinear, mipmap=mipmap)
#----------------------------------------------------------------------------
if __name__ == "__main__":
main()
#----------------------------------------------------------------------------