-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautomated_test.py
396 lines (297 loc) · 11.5 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import pytest
import numpy as np
import xs3d
@pytest.mark.parametrize("anisotropy", [
[1,1,1],
[2,2,2],
[1000,1000,1000],
[0.0001,0.0001,0.0001],
[1, 1, 0.001],
[1, 0.001, 1],
[0.001, 1, 1],
])
def test_single_voxel(anisotropy):
voxel = np.ones([1,1,1], dtype=bool, order="F")
area, contact = xs3d.cross_sectional_area(
voxel, [0,0,0], [0,0,1], anisotropy,
return_contact=True
)
assert np.isclose(area, anisotropy[0] * anisotropy[1])
assert contact > 0
area, contact = xs3d.cross_sectional_area(
voxel, [0,0,0], [0,1,0], anisotropy,
return_contact=True
)
assert np.isclose(area, anisotropy[0] * anisotropy[2])
assert contact > 0
area, contact = xs3d.cross_sectional_area(
voxel, [0,0,0], [1,0,0], anisotropy,
return_contact=True
)
assert np.isclose(area, anisotropy[1] * anisotropy[2])
assert contact > 0
area, contact = xs3d.cross_sectional_area(
voxel, [0,0,0], [1,1,0], anisotropy,
return_contact=True
)
assert np.isclose(area,
(
np.sqrt(anisotropy[0] * anisotropy[0] + anisotropy[1] * anisotropy[1])
* anisotropy[2]
)
)
assert contact > 0
area, contact = xs3d.cross_sectional_area(
voxel, [0,0,0], [0,1,1], anisotropy,
return_contact=True
)
assert np.isclose(area, (
np.sqrt(anisotropy[1] * anisotropy[1] + anisotropy[2] * anisotropy[2])
* anisotropy[0]
))
assert contact > 0
area, contact = xs3d.cross_sectional_area(
voxel, [0,0,0], [1,0,1], anisotropy,
return_contact=True
)
assert np.isclose(area, (
np.sqrt(anisotropy[0] * anisotropy[0] + anisotropy[2] * anisotropy[2])
* anisotropy[1]
))
assert contact > 0
area, contact = xs3d.cross_sectional_area(
voxel, [0,0,0], [1,1,1], anisotropy,
return_contact=True
)
tri = lambda s: np.sqrt(3) / 8 * (s ** 2)
if 0.001 in anisotropy:
# collapses to a 2D shape
hexagon = 0.75 # 1/4 + 1/4 + 1/8 + 1/8
else:
hexagon = 2 * sum([ tri(a) for a in anisotropy ])
assert np.isclose(area, hexagon)
assert contact > 0
# outside the voxel
area, contact = xs3d.cross_sectional_area(
voxel, [1,0,0], [1,0,0], anisotropy,
return_contact=True
)
assert area == 0
assert contact == False
area = xs3d.cross_sectional_area(voxel, [-1,0,0], [1,0,0], anisotropy)
assert area == 0
# arbitrary angles
for incr in range(11):
area = xs3d.cross_sectional_area(voxel, [0,0,0], [incr * 0.1,0,1])
assert area >= 1
assert area <= np.sqrt(2)
def test_ccl():
img = np.zeros([10,10,10], dtype=bool, order="F")
img[:3,:3,:3] = True
img[6:,6:,:3] = True
area, contact = xs3d.cross_sectional_area(img, [1,1,1], [0,0,1], return_contact=True)
assert area == 9
assert contact > 0
area = xs3d.cross_sectional_area(img, [7,7,1], [0,0,1])
assert area == 16
area = xs3d.cross_sectional_area(img, [7,7,5], [0,0,1])
assert area == 0
img[:4,:4,:4] = True
img[1,1,1] = False
area = xs3d.cross_sectional_area(img, [0,1,1], [0,0,1])
assert area == 15
img[2:4,2:4,6:8] = True
area, contact = xs3d.cross_sectional_area(img, [2,2,6], [0,0,1], return_contact=True)
assert area == 4
assert contact == False
def test_8_connectivity():
img = np.zeros([4,4,3], dtype=bool, order="F")
img[0,0] = True
img[1,1] = True
img[2,2] = True
img[3,3] = True
img[3,3,1] = True
area = xs3d.cross_sectional_area(img, [0,0,0], [0,0,1])
assert area == 4
img = np.zeros([4,4,3], dtype=bool, order="F")
img[3,0] = True
img[2,1] = True
img[1,2] = True
img[3,3] = True
img[3,3,1] = True
area = xs3d.cross_sectional_area(img, [3,0,0], [0,0,1])
assert area == 3
img = np.zeros([4,4,3], dtype=bool, order="F")
img[3,0] = True
img[1,1] = True
img[0,2] = True
img[3,3] = True
img[3,3,1] = True
area = xs3d.cross_sectional_area(img, [3,0,0], [0,0,1])
assert area == 1
def test_sphere():
d = 100
r = d/2
img = np.zeros([125,125,125], dtype=bool, order="F")
offset = 63
def dist(x,y,z):
nonlocal r
x = x - offset
y = y - offset
z = z - offset
return np.sqrt(x*x + y*y + z*z)
for z in range(img.shape[2]):
for y in range(img.shape[1]):
for x in range(img.shape[0]):
if dist(x,y,z) <= r:
img[x,y,z] = True
def angle(theta):
return [ np.cos(theta), np.sin(theta), 0 ]
pos = (offset, offset, offset)
smoothness = ((r-1)**2) / (r**2)
prev_area = xs3d.cross_sectional_area(img, pos, [1,0,0])
for theta in range(0,50):
normal = angle(theta / 50 * 2 * np.pi)
area, contact = xs3d.cross_sectional_area(img, pos, normal, return_contact=True)
assert area > np.pi * (r-0.5) * (r-0.5)
assert area <= np.pi * (r+0.5) * (r+0.5)
ratio = abs(area - prev_area) / area
assert ratio < smoothness
assert contact == False
prev_area = area
def angle2(theta):
return [ 0, np.cos(theta), np.sin(theta) ]
pos = (offset, offset, offset)
prev_area = xs3d.cross_sectional_area(img, pos, [1,0,0])
for theta in range(0,50):
normal = angle2(theta / 50 * 2 * np.pi)
area = xs3d.cross_sectional_area(img, pos, normal)
assert area > np.pi * (r-0.5) * (r-0.5)
assert area <= np.pi * (r+0.5) * (r+0.5)
ratio = abs(area - prev_area) / area
assert ratio < smoothness
prev_area = area
def test_off_angle():
binimg = np.ones([2,2,2], dtype=bool)
pos = [1,1,1]
normal = [ 0.92847669, -0.37139068, 0]
approximate_area = 4 * np.sqrt(1 + (normal[1]/normal[0]) ** 2)
area = xs3d.cross_sectional_area(binimg, pos, normal)
assert abs(area - approximate_area) < 0.001
def test_5x5():
binimg = np.ones([5,5,1], dtype=bool)
area = xs3d.cross_sectional_area(binimg, [0,0,0], [0,0,1])
assert area == 25
def test_symmetric_normals():
labels = np.ones((5,5,5), dtype=bool, order="F")
approximate_area = 5 * 5
areafn = lambda n: xs3d.cross_sectional_area(labels, [2,2,2], n)
assert areafn([1,0,0]) == approximate_area
assert areafn([-1,0,0]) == approximate_area
assert areafn([0,1,0]) == approximate_area
assert areafn([0,-1,0]) == approximate_area
assert areafn([0,0,1]) == approximate_area
assert areafn([0,0,-1]) == approximate_area
approximate_area = 5 * 5 * np.sqrt(2)
assert np.isclose(areafn([1,1,0]), approximate_area)
assert np.isclose(areafn([1,0,1]), approximate_area)
assert np.isclose(areafn([0,1,1]), approximate_area)
assert np.isclose(areafn([-1,-1,0]), approximate_area)
assert np.isclose(areafn([-1,0,-1]), approximate_area)
assert np.isclose(areafn([0,-1,-1]), approximate_area)
assert np.isclose(areafn([-1,1,0]), approximate_area)
assert np.isclose(areafn([1,-1,0]), approximate_area)
assert np.isclose(areafn([0, 1,-1]), approximate_area)
assert np.isclose(areafn([0,-1, 1]), approximate_area)
def test_empty():
labels = np.zeros([0,0,0], dtype=bool)
area = xs3d.cross_sectional_area(labels, [0,0,0], [1,1,1])
assert area == 0
@pytest.mark.parametrize("off", [50, 25])
@pytest.mark.parametrize("normal", [[1,0,0], [0,1,0], [0,0,1], [1,1,1], [-1,-1,1], [.3,-.2,.7]])
def test_moving_window(off, normal):
labels = np.zeros([100,100,100], dtype=bool, order="F")
labels[:off, :off, :off] = True
initial_area = xs3d.cross_sectional_area(labels, [off//2, off//2, off//2], normal)
for i in range(30):
labels[:] = False
labels[i:i+off, i:i+off, i:i+off] = True
area = xs3d.cross_sectional_area(labels, [i+off//2, i+off//2, i+off//2], normal)
assert np.isclose(area, initial_area)
def test_cross_section():
labels = np.ones((5,5,5), dtype=bool, order="F")
pos = (2, 2, 2)
def angle(theta):
return [ 0, np.cos(theta), np.sin(theta) ]
for theta in range(0,25):
normal = angle(theta / 25 * 2 * np.pi)
area = xs3d.cross_sectional_area(labels, pos, normal)
image = xs3d.cross_section(labels, pos, normal)
assert image.dtype == np.float32
assert np.isclose(image.sum(), area)
def test_slice():
labels = np.arange(9, dtype=np.uint8).reshape([3,3,1], order="F")
slc = xs3d.slice(labels, [0,0,0], [0,0,1], standardize_basis=True)
assert np.all(slc == labels[:,:,0])
with pytest.raises(ValueError):
area = xs3d.slice(labels, [0,0,0], [0,0,0])
labels = np.ones([3,3,3,3], dtype=bool, order="F")
with pytest.raises(ValueError):
area = xs3d.slice(labels, [0,0,0], [0,0,1])
def test_cross_sectional_area_inputs():
labels = np.arange(9, dtype=np.uint8).reshape([3,3,1], order="F")
with pytest.raises(ValueError):
area = xs3d.cross_sectional_area(labels, [0,0,0], [0,0,1])
labels = np.ones([3,3,1], dtype=bool, order="F")
with pytest.raises(ValueError):
area = xs3d.cross_sectional_area(labels, [0,0,0], [0,0,0])
labels = np.ones([3,3,1], dtype=bool, order="F")
with pytest.raises(ValueError):
area = xs3d.cross_sectional_area(labels, [0,0,0], [0,0,1], [-1,1,1])
with pytest.raises(ValueError):
area = xs3d.cross_sectional_area(labels, [0,0,0], [0,0,1], [1,-1,1])
with pytest.raises(ValueError):
area = xs3d.cross_sectional_area(labels, [0,0,0], [0,0,1], [1,1,-1])
with pytest.raises(ValueError):
area = xs3d.cross_sectional_area(labels, [0,0,0], [0,0,1], [-1,-1,-1])
labels = np.ones([3,3,3,3], dtype=bool, order="F")
with pytest.raises(ValueError):
area = xs3d.cross_sectional_area(labels, [0,0,0], [0,0,1], [1,1,1])
def test_cross_section_inputs():
labels = np.arange(9, dtype=np.uint8).reshape([3,3,1], order="F")
with pytest.raises(ValueError):
area = xs3d.cross_section(labels, [0,0,0], [0,0,1])
labels = np.ones([3,3,1], dtype=bool, order="F")
with pytest.raises(ValueError):
area = xs3d.cross_section(labels, [0,0,0], [0,0,0])
labels = np.ones([3,3,1], dtype=bool, order="F")
with pytest.raises(ValueError):
area = xs3d.cross_section(labels, [0,0,0], [0,0,1], [-1,1,1])
with pytest.raises(ValueError):
area = xs3d.cross_section(labels, [0,0,0], [0,0,1], [1,-1,1])
with pytest.raises(ValueError):
area = xs3d.cross_section(labels, [0,0,0], [0,0,1], [1,1,-1])
with pytest.raises(ValueError):
area = xs3d.cross_section(labels, [0,0,0], [0,0,1], [-1,-1,-1])
labels = np.ones([3,3,3,3], dtype=bool, order="F")
with pytest.raises(ValueError):
area = xs3d.cross_section(labels, [0,0,0], [0,0,1], [1,1,1])
def test_2d():
labels = np.ones([3,3], dtype=bool)
area = xs3d.cross_sectional_area(labels, [1,1], [0,1])
assert area == 3
area = xs3d.cross_sectional_area(labels, [1,1], [0,1], [3,3])
assert area == 9
area = xs3d.cross_sectional_area(labels, [1,1], [1,0], [1,1])
assert area == 3
area = xs3d.cross_sectional_area(labels, [1,1], [1,0], [5,5])
assert area == 15
area = xs3d.cross_sectional_area(labels, [0,0], [-1,1], [1,1])
assert np.isclose(area, 3 * np.sqrt(2))
area = xs3d.cross_sectional_area(labels, [-1,0], [-1,1], [1,1])
assert area == 0
area = xs3d.cross_sectional_area(labels, [1,-1], [-1,1], [1,1])
assert area == 0
labels[1,1] = 0
area = xs3d.cross_sectional_area(labels, [1,1], [0,1], [1,1])
assert area == 0