forked from napari/napari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgui_with_threadpoolexec_.py
66 lines (53 loc) · 1.64 KB
/
mgui_with_threadpoolexec_.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
"""
magicgui with threadpoolexec
============================
An example of calling a threaded function from a magicgui ``dock_widget``.
using ``ThreadPoolExecutor``
Note: this example requires python >= 3.9
.. tags:: gui
"""
from concurrent.futures import Future, ThreadPoolExecutor
from magicgui import magic_factory
from skimage import data
from skimage.feature import blob_log
import napari
from napari.types import ImageData, LayerDataTuple
pool = ThreadPoolExecutor()
@magic_factory(
min_sigma={'min': 0.5, 'max': 15, 'step': 0.5},
max_sigma={'min': 1, 'max': 200, 'step': 0.5},
num_sigma={'min': 1, 'max': 20},
threshold={'min': 0, 'max': 1000, 'step': 0.1},
)
def make_widget(
image: ImageData,
min_sigma: float = 5,
max_sigma: float = 30,
num_sigma: int = 10,
threshold: float = 0.3,
) -> Future[LayerDataTuple]:
# long running function
def _make_blob():
# skimage.feature may take a while depending on the parameters
blobs = blob_log(
image,
min_sigma=min_sigma,
max_sigma=max_sigma,
num_sigma=num_sigma,
threshold=threshold,
)
data = blobs[:, : image.ndim]
kwargs = {
'size': blobs[:, -1],
'border_color': 'red',
'border_width': 2,
'border_width_is_relative': False,
'face_color': 'transparent',
}
return (data, kwargs, 'points')
return pool.submit(_make_blob)
viewer = napari.Viewer()
viewer.window.add_dock_widget(make_widget(), area='right')
viewer.add_image(data.hubble_deep_field().mean(-1))
napari.run()
pool.shutdown(wait=True)