forked from VCG/compresso
-
Notifications
You must be signed in to change notification settings - Fork 1
/
automated_test.py
222 lines (182 loc) · 8.68 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
import pytest
import gzip
from io import BytesIO
import numpy as np
import compresso
DTYPES = [
np.uint8, np.uint16, np.uint32, np.uint64,
]
STEPS = [
(4,4,1), (5,5,1), (8,8,1),
(4,4,2), (5,5,2)
]
CONNECTIVITY = (4,6)
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
@pytest.mark.parametrize('connectivity', CONNECTIVITY)
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_empty(dtype, steps, connectivity, random_access_z_index):
labels = np.zeros((0,0,0), dtype=dtype, order="F")
compressed = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
@pytest.mark.parametrize('connectivity', CONNECTIVITY)
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_black(dtype, steps, connectivity, random_access_z_index):
labels = np.zeros((100,100,100), dtype=dtype, order="F")
compressed = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
@pytest.mark.parametrize('connectivity', CONNECTIVITY)
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_uniform_field(dtype, steps, connectivity, random_access_z_index):
labels = np.zeros((100,100,100), dtype=dtype, order="F") + 1
compressed = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert len(compressed) < labels.nbytes
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
labels = np.zeros((100,100,100), dtype=dtype, order="F") + np.iinfo(dtype).max
compressed2 = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed2)
assert len(compressed2) < labels.nbytes
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed2))
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
@pytest.mark.parametrize('connectivity', CONNECTIVITY)
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_arange_field(dtype, steps, connectivity, random_access_z_index):
labels = np.arange(0,1024).reshape((16,16,4)).astype(dtype)
compressed = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
labels = np.arange(1,1025).reshape((16,16,4)).astype(dtype)
compressed = compresso.compress(labels, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
@pytest.mark.parametrize('connectivity', CONNECTIVITY)
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_2d_arange_field(dtype, steps, connectivity, random_access_z_index):
labels = np.arange(0,16*16).reshape((16,16,1)).astype(dtype)
compressed = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
@pytest.mark.parametrize('connectivity', CONNECTIVITY)
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_2_field(dtype, steps, connectivity, random_access_z_index):
labels = np.arange(0,1024).reshape((16,16,4)).astype(dtype)
compressed = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
labels[2,2,1] = np.iinfo(dtype).max
compressed = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
@pytest.mark.parametrize('order', ("C", "F"))
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
@pytest.mark.parametrize('connectivity', CONNECTIVITY)
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_random_field(dtype, order, steps, connectivity, random_access_z_index):
labels = np.random.randint(0, 25, size=(100, 100, 25)).astype(dtype)
if order == "C":
labels = np.ascontiguousarray(labels)
else:
labels = np.asfortranarray(labels)
compressed = compresso.compress(labels, steps=steps, connectivity=connectivity, random_access_z_index=random_access_z_index)
reconstituted = compresso.decompress(compressed)
assert np.all(labels == reconstituted)
assert np.all(np.unique(labels) == compresso.labels(compressed))
# Watershed volumes can blow out the RLE encoding
# due to too many windows.
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_watershed(random_access_z_index):
with gzip.open("./ws.npy.cpso.gz", "rb") as f:
binary = f.read()
labels = compresso.decompress(binary)
del binary
binary = compresso.compress(labels)
head = compresso.header(binary)
assert head["xstep"] == 8
assert head["ystep"] == 8
assert head["zstep"] == 1
try:
binary = compresso.compress(labels, steps=(4,4,1), random_access_z_index=random_access_z_index)
assert False
except compresso.EncodeError:
pass
# This volume blew out a previous logic error in
# the RLE encoder where zero overflow was not
# handled correctly.
@pytest.mark.parametrize('random_access_z_index', (True, False))
def test_rle_overflow(random_access_z_index):
with gzip.open("./rle_defect.npy.gz", "rb") as f:
binary = BytesIO(f.read())
labels = np.load(binary)
binary = compresso.compress(labels, steps=(4,4,1), random_access_z_index=random_access_z_index)
labels = compresso.decompress(binary)
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
@pytest.mark.parametrize('connectivity', CONNECTIVITY)
def test_remap(dtype, steps, connectivity):
labels = np.random.randint(0, 15, size=(64,63,61)).astype(dtype)
remap = { i: i+20 for i in range(15) }
binary = compresso.compress(labels)
assert np.all(compresso.labels(binary) == list(range(15)))
binary2 = compresso.remap(binary, remap)
assert np.all(compresso.labels(binary2) == list(range(20, 35)))
@pytest.mark.parametrize('dtype', DTYPES)
@pytest.mark.parametrize('steps', STEPS)
def test_arange_field_decode_range(dtype, steps):
labels = np.arange(0,64).reshape((4,4,4), order="F").astype(dtype)
compressed = compresso.compress(
labels, steps=steps,
connectivity=4, random_access_z_index=True
)
for z in range(labels.shape[2]):
reconstituted = compresso.decompress(compressed, z=z)
assert np.all(labels[:,:,z] == reconstituted[:,:,0])
for z in range(labels.shape[2]-1):
reconstituted = compresso.decompress(compressed, z=(z,z+2))
assert np.all(labels[:,:,z:z+2] == reconstituted)
try:
compresso.decompress(compressed, z=-1)
assert False
except ValueError:
pass
try:
compresso.decompress(compressed, z=4)
assert False
except ValueError:
pass
try:
compresso.decompress(compressed, z=(3,1))
assert False
except ValueError:
pass
@pytest.mark.parametrize("random_access_z_index", (True,False))
def test_array_works_all_formats(random_access_z_index):
labels = np.arange(0,64).reshape((4,4,4), order="F").astype(np.uint32)
compressed = compresso.compress(
labels, steps=(4,4,1),
connectivity=4, random_access_z_index=random_access_z_index
)
arr = compresso.CompressoArray(compressed)
assert np.all(arr[:,:,3] == labels[:,:,3])
assert arr.random_access_enabled == random_access_z_index