-
Notifications
You must be signed in to change notification settings - Fork 3
/
automated_test.py
326 lines (245 loc) · 6.97 KB
/
automated_test.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
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
import pytest
import numpy as np
import fastmorph
def test_spherical_dilate():
labels = np.zeros((10,10,10), dtype=bool)
res = fastmorph.spherical_dilate(labels, radius=1000)
assert np.all(res == False)
labels[5,5,5] = True
radius = 5 * np.sqrt(3) + 0.000001
res = fastmorph.spherical_dilate(labels, radius=radius)
assert np.all(res == True)
res = fastmorph.spherical_dilate(labels, radius=1)
assert np.count_nonzero(res) == 7
res = fastmorph.spherical_dilate(labels, radius=np.sqrt(2))
assert np.count_nonzero(res) == 19
res = fastmorph.spherical_dilate(labels, radius=np.sqrt(3))
assert np.count_nonzero(res) == 27
def test_spherical_erode():
labels = np.ones((10,10,10), dtype=bool)
res = fastmorph.spherical_erode(labels, radius=1000)
assert np.all(res == False)
def test_fill_holes():
labels = np.ones((10,10,10), dtype=np.uint8)
labels[:,:,:5] = 2
labels[5,5,2] = 0
labels[5,5,7] = 0
assert np.count_nonzero(labels) == 998
res, ct = fastmorph.fill_holes(labels, return_fill_count=True)
assert np.count_nonzero(res) == 1000
assert list(np.unique(res)) == [1,2]
assert res[5,5,2] == 2
assert res[5,5,7] == 1
assert ct[1] == 1
assert ct[2] == 1
labels = np.ones((10,10,10), dtype=bool)
labels[5,5,2] = 0
res = fastmorph.fill_holes(labels, return_fill_count=False)
assert np.all(res == True)
res = fastmorph.fill_holes(labels, return_fill_count=True)
assert np.all(res[0] == True)
assert res[1] == 1
res = fastmorph.fill_holes(labels, return_removed=True)
assert np.all(res[0] == True)
assert res[1] == set()
res = fastmorph.fill_holes(labels, return_fill_count=True, return_removed=True)
assert np.all(res[0] == True)
assert res[1] == 1
assert res[2] == set()
def test_spherical_open_close_run():
labels = np.zeros((10,10,10), dtype=bool)
res = fastmorph.spherical_open(labels, radius=1)
res = fastmorph.spherical_close(labels, radius=1)
def test_spherical_close():
labels = np.zeros((10,10,10), dtype=bool)
labels[4:7,4:7,4:7] = True
labels[5,5,5] = False
res = fastmorph.spherical_close(labels, radius=1)
assert res[5,5,5] == True
def test_multilabel_dilate_3d():
labels = np.zeros((3,3,3), dtype=bool)
out = fastmorph.dilate(labels)
assert not np.any(out)
labels[1,1,1] = True
out = fastmorph.dilate(labels)
assert np.all(out)
labels = np.zeros((3,3,3), dtype=bool)
labels[0,0,0] = True
out = fastmorph.dilate(labels)
ans = np.zeros((3,3,3), dtype=bool)
ans[:2,:2,:2] = True
assert np.all(out == ans)
labels = np.zeros((3,3,3), dtype=int)
labels[0,1,1] = 1
labels[2,1,1] = 2
out = fastmorph.dilate(labels)
ans = np.ones((3,3,3), dtype=int)
ans[2,:,:] = 2
assert np.all(ans == out)
labels = np.zeros((3,3,3), dtype=int, order="F")
labels[0,1,1] = 1
labels[1,1,1] = 2
labels[2,1,1] = 2
out = fastmorph.dilate(labels)
ans = np.ones((3,3,3), dtype=int, order="F")
ans[1:,:,:] = 2
assert np.all(ans == out)
def test_multilabel_dilate_2d():
labels = np.zeros((3,3), dtype=bool)
out = fastmorph.dilate(labels)
assert not np.any(out)
labels[1,1] = True
out = fastmorph.dilate(labels)
assert np.all(out)
labels = np.zeros((3,3), dtype=bool)
labels[0,0] = True
out = fastmorph.dilate(labels)
ans = np.zeros((3,3), dtype=bool)
ans[:2,:2] = True
assert np.all(out == ans)
labels = np.zeros((3,3), dtype=int)
labels[0,1] = 1
labels[2,1] = 2
out = fastmorph.dilate(labels)
ans = np.ones((3,3), dtype=int)
ans[2,:] = 2
assert np.all(ans == out)
labels = np.zeros((3,3), dtype=int, order="F")
labels[0,1] = 1
labels[1,1] = 2
labels[2,1] = 2
out = fastmorph.dilate(labels)
ans = np.ones((3,3), dtype=int, order="F")
ans[1:,:] = 2
assert np.all(ans == out)
def test_multilabel_erode_3d():
labels = np.ones((3,3,3), dtype=bool)
out = fastmorph.erode(labels)
assert np.sum(out) == 1 and out[1,1,1] == True
out = fastmorph.erode(out)
assert not np.any(out)
out = fastmorph.erode(out)
assert not np.any(out)
labels = np.ones((3,3,3), dtype=int, order="F")
labels[0,:,:] = 1
labels[1,:,:] = 2
labels[2,:,:] = 3
out = fastmorph.erode(labels)
ans = np.zeros((3,3,3), dtype=int, order="F")
assert np.all(ans == out)
labels = np.zeros((5,5,5), dtype=bool)
labels[1:4,1:4,1:4] = True
out = fastmorph.erode(labels)
assert np.sum(out) == 1 and out[2,2,2] == True
labels = np.ones((5,5,5), dtype=bool)
out = fastmorph.erode(labels)
assert np.sum(out) == 27
def test_multilabel_erode_2d():
labels = np.ones((3,3), dtype=bool)
out = fastmorph.erode(labels)
assert np.sum(out) == 1 and out[1,1] == True
out = fastmorph.erode(out)
assert not np.any(out)
out = fastmorph.erode(out)
assert not np.any(out)
labels = np.ones((3,3), dtype=int, order="F")
labels[0,:] = 1
labels[1,:] = 2
labels[2,:] = 3
out = fastmorph.erode(labels)
ans = np.zeros((3,3), dtype=int, order="F")
assert np.all(ans == out)
labels = np.zeros((5,5), dtype=bool)
labels[1:4,1:4] = True
out = fastmorph.erode(labels)
assert np.sum(out) == 1 and out[2,2] == True
labels = np.ones((5,5), dtype=bool)
out = fastmorph.erode(labels)
assert np.sum(out) == 9
@pytest.mark.parametrize('dtype', [
np.uint8,np.uint16,np.uint32,np.uint64,
np.int8,np.int16,np.int32,np.int64,
])
def test_grey_erode(dtype):
labels = np.arange(9, dtype=dtype).reshape((3,3), order="F")
out = fastmorph.erode(labels, mode=fastmorph.Mode.grey)
ans = np.array([
[0, 0, 1],
[0, 0, 1],
[3, 3, 4],
], dtype=dtype).T
assert np.all(out == ans)
out = fastmorph.erode(out, mode=fastmorph.Mode.grey)
assert np.all(out == 0)
labels = np.arange(27, dtype=dtype).reshape((3,3,3), order="F")
out = fastmorph.erode(labels, mode=fastmorph.Mode.grey)
ans = np.array([
[
[0, 0, 1],
[0, 0, 1],
[3, 3, 4],
],
[
[0, 0, 1],
[0, 0, 1],
[3, 3, 4],
],
[
[9, 9, 10],
[9, 9, 10],
[12, 12, 13],
],
], dtype=dtype).T
assert np.all(out == ans)
out = fastmorph.erode(out, mode=fastmorph.Mode.grey)
assert np.all(out == 0)
@pytest.mark.parametrize('dtype', [
np.uint8,np.uint16,np.uint32,np.uint64,
np.int8,np.int16,np.int32,np.int64,
])
def test_grey_dilate(dtype):
L = 5
H = 10
labels = np.zeros((3,3), dtype=dtype)
labels[0,0] = L
labels[2,2] = H
out = fastmorph.dilate(labels, mode=fastmorph.Mode.grey)
ans = np.array([
[L, L, 0],
[L, H, H],
[0, H, H],
], dtype=dtype).T
assert np.all(out == ans)
labels = np.zeros((3,3,3), dtype=dtype)
labels[0,0,0] = L
labels[2,2,2] = H
out = fastmorph.dilate(labels, mode=fastmorph.Mode.grey)
ans = np.array([
[
[L, L, 0],
[L, L, 0],
[0, 0, 0],
],
[
[L, L, 0],
[L, H, H],
[0, H, H],
],
[
[0, 0, 0],
[0, H, H],
[0, H, H],
],
], dtype=dtype).T
assert np.all(out == ans)
out = fastmorph.dilate(out, mode=fastmorph.Mode.grey)
assert np.all(out == H)
def test_grey_dilate_bool():
labels = np.zeros((3,3,3), dtype=bool)
labels[1,1,1] = True
out = fastmorph.dilate(labels, mode=fastmorph.Mode.grey)
assert np.all(out == True)
labels = np.zeros((3,3), dtype=bool)
labels[1,1] = True
out = fastmorph.dilate(labels, mode=fastmorph.Mode.grey)
assert np.all(out == True)