-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_slicer.py
122 lines (89 loc) · 3.71 KB
/
test_slicer.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
from dash_slicer import VolumeSlicer
import numpy as np
from pytest import raises
import dash
import dash_core_components as dcc
def test_slicer_init():
app = dash.Dash()
vol = np.random.uniform(0, 255, (100, 100, 100)).astype(np.uint8)
# Need a valid volume
with raises(TypeError):
VolumeSlicer(app, [3, 4, 5])
with raises(TypeError):
VolumeSlicer(app, vol[0])
# Need a valid axis
with raises(ValueError):
VolumeSlicer(app, vol, axis=4)
# Need a valide thumbnail
with raises(ValueError):
VolumeSlicer(app, vol, thumbnail=20.2)
# This works
s = VolumeSlicer(app, vol)
# Check properties
assert isinstance(s.graph, dcc.Graph)
assert isinstance(s.slider, dcc.Slider)
assert isinstance(s.stores, list)
assert all(isinstance(store, (dcc.Store, dcc.Interval)) for store in s.stores)
for store in [s.clim, s.state, s.extra_traces, s.overlay_data]:
assert isinstance(store, dcc.Store)
def test_slicer_thumbnail():
vol = np.random.uniform(0, 255, (100, 100, 100)).astype(np.uint8)
app = dash.Dash()
_ = VolumeSlicer(app, vol)
# Test for name pattern of server-side callback when thumbnails are used
assert any(["server-data.data" in key for key in app.callback_map])
app = dash.Dash()
_ = VolumeSlicer(app, vol, thumbnail=False)
# No server-side callbacks when no thumbnails are used
assert not any(["server-data.data" in key for key in app.callback_map])
def test_clim():
app = dash.Dash()
vol = np.random.uniform(0, 255, (10, 10, 10)).astype(np.uint8)
mi, ma = vol.min(), vol.max()
s = VolumeSlicer(app, vol)
assert s._initial_clim == (mi, ma)
s = VolumeSlicer(app, vol, clim=None)
assert s._initial_clim == (mi, ma)
s = VolumeSlicer(app, vol, clim=(10, 12))
assert s._initial_clim == (10, 12)
def test_scene_id_and_context_id():
app = dash.Dash()
vol = np.random.uniform(0, 255, (100, 100, 100)).astype(np.uint8)
s1 = VolumeSlicer(app, vol, axis=0)
s2 = VolumeSlicer(app, vol, axis=0)
s3 = VolumeSlicer(app, vol, axis=1)
# The scene id's are equal, so indicators will match up
assert s1.scene_id == s2.scene_id and s1.scene_id == s3.scene_id
# Context id's must be unique
assert s1._context_id != s2._context_id and s1._context_id != s3._context_id
def test_create_overlay_data():
app = dash.Dash()
vol = np.random.uniform(0, 255, (100, 100, 100)).astype(np.uint8)
s = VolumeSlicer(app, vol)
# Bool overlay
overlay = s.create_overlay_data(vol > 10)
assert isinstance(overlay, list) and len(overlay) == s.nslices
assert all(isinstance(x, str) for x in overlay)
# Bool overlay - with color
overlay = s.create_overlay_data(vol > 10, "#ff0000")
assert isinstance(overlay, list) and len(overlay) == s.nslices
assert all(isinstance(x, str) for x in overlay)
# Uint8 overlay - with colormap
overlay = s.create_overlay_data(vol.astype(np.uint8), ["#ff0000", "#00ff00"])
assert isinstance(overlay, list) and len(overlay) == s.nslices
assert all(isinstance(x, str) for x in overlay)
# Reset
overlay = s.create_overlay_data(None)
assert isinstance(overlay, list) and len(overlay) == s.nslices
assert all(x is None for x in overlay)
# Reset by zero mask
overlay = s.create_overlay_data(vol > 300)
assert isinstance(overlay, list) and len(overlay) == s.nslices
assert all(x is None for x in overlay)
# Wrong
with raises(TypeError):
s.create_overlay_data("not a valid mask")
with raises(ValueError):
s.create_overlay_data(vol.astype(np.float32)) # wrong dtype
with raises(ValueError):
s.create_overlay_data(vol[:-1]) # wrong shape