forked from napari/napari
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmgui_with_threadpoolexec_.py
70 lines (56 loc) · 1.7 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
67
68
69
70
"""
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
"""
import sys
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
if sys.version_info < (3, 9):
print('This example requires python >= 3.9')
sys.exit(0)
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],
"edge_color": "red",
"edge_width": 2,
"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)