-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_voxcell.py
367 lines (303 loc) · 10.1 KB
/
test_voxcell.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
"""Test the Voxcell extension of the ``dir-content-diff`` package."""
# LICENSE HEADER MANAGED BY add-license-header
# Copyright (c) 2023-2024 Blue Brain Project, EPFL.
#
# This file is part of dir-content-diff.
# See https://github.com/BlueBrain/dir-content-diff for further info.
#
# SPDX-License-Identifier: Apache-2.0
# LICENSE HEADER MANAGED BY add-license-header
# pylint: disable=missing-function-docstring
# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument
# pylint: disable=use-implicit-booleaness-not-comparison
import re
import shutil
import numpy as np
import pytest
import dir_content_diff
import dir_content_diff.comparators.voxcell
from dir_content_diff import _DEFAULT_EXPORT_SUFFIX
from dir_content_diff import compare_trees
try:
import voxcell
except ImportError:
pass
class TestRegistry:
"""Test the internal registry."""
def test_voxcell_register(self, registry_reseter):
assert ".nrrd" not in dir_content_diff.get_comparators()
assert ".mvd3" not in dir_content_diff.get_comparators()
dir_content_diff.comparators.voxcell.register()
assert (
dir_content_diff.get_comparators()[".nrrd"]
== dir_content_diff.comparators.voxcell.NrrdComparator()
)
assert (
dir_content_diff.get_comparators()[".mvd3"]
== dir_content_diff.comparators.voxcell.Mvd3Comparator()
)
def euler_to_matrix(bank, attitude, heading):
"""Copied from voxcell tests."""
sa, ca = np.sin(attitude), np.cos(attitude)
sb, cb = np.sin(bank), np.cos(bank)
sh, ch = np.sin(heading), np.cos(heading)
m = np.vstack(
[
ch * ca,
-ch * sa * cb + sh * sb,
ch * sa * sb + sh * cb,
sa,
ca * cb,
-ca * sb,
-sh * ca,
sh * sa * cb + ch * sb,
-sh * sa * sb + ch * cb,
]
).transpose()
return m.reshape(m.shape[:-1] + (3, 3))
def random_orientations(n):
"""Copied from voxcell tests."""
np.random.seed(0)
return euler_to_matrix(
np.random.random(n) * np.pi * 2,
np.random.random(n) * np.pi * 2,
np.random.random(n) * np.pi * 2,
)
def random_positions(n):
"""Copied from voxcell tests."""
np.random.seed(0)
return np.random.random((n, 3))
@pytest.fixture
def voxcell_registry_reseter(registry_reseter):
dir_content_diff.comparators.voxcell.register()
@pytest.fixture
def cell_collection():
cells = voxcell.cell_collection.CellCollection()
N = 5
cells.positions = random_positions(N)
cells.orientations = random_orientations(N)
cells.properties["a_property"] = ["val_1", "val_2", "val_3", "val_4", "val_5"]
cells.properties["another_property"] = ["val_1", "val_2", "val_3", "val_4", "val_5"]
return cells
@pytest.fixture
def cell_collection_diff(cell_collection):
cell_collection.positions[:, 0] *= 2
cell_collection.properties["a_property"] += "_new"
return cell_collection
@pytest.fixture
def ref_mvd3(empty_ref_tree, cell_collection):
filename = empty_ref_tree / "file.mvd3"
cell_collection.save_mvd3(filename)
return filename
@pytest.fixture
def ref_h5(empty_ref_tree, cell_collection):
filename = empty_ref_tree / "file.h5"
cell_collection.save(filename)
return filename
@pytest.fixture
def res_mvd3_equal(empty_res_tree, ref_mvd3):
filename = empty_res_tree / "file.mvd3"
shutil.copyfile(ref_mvd3, filename)
return filename
@pytest.fixture
def res_h5_equal(empty_res_tree, ref_h5):
filename = empty_res_tree / "file.h5"
shutil.copyfile(ref_h5, filename)
return filename
@pytest.fixture
def res_mvd3_diff(cell_collection_diff, empty_res_tree):
filename = empty_res_tree / "file.mvd3"
cell_collection_diff.save_mvd3(filename)
return filename
@pytest.fixture
def res_h5_diff(cell_collection_diff, empty_res_tree):
filename = empty_res_tree / "file.h5"
cell_collection_diff.save(filename)
return filename
@pytest.fixture
def cell_collection_diff_report():
return (
r"""The files '\S*/file.mvd3' and '\S*/file.mvd3' are different:"""
r"""\n\n"""
r"""Column 'a_property': Series are different\n"""
r"""\n"""
r"""Series values are different \(100.0 %\)\n"""
r"""\[index\]: \[1, 2, 3, 4, 5\]\n"""
r"""\[left\]: \[val_1, val_2, val_3, val_4, val_5\]\n"""
r"""\[right\]: \[val_1_new, val_2_new, val_3_new, val_4_new, val_5_new\]\n"""
r"""(At positional index 0, first diff: val_1 != val_1_new\n)?"""
r"""\n"""
r"""Column 'x': Series are different\n"""
r"""\n"""
r"""Series values are different \(100.0 %\)\n"""
r"""\[index\]: \[1, 2, 3, 4, 5\]\n"""
r"""\[left]: \[0.548813\d+, 0.544883\d+, 0.437587\d+, 0.383441\d+, 0.568044\d+\]\n"""
r"""\[right]: \[1.097627\d+, 1.089766\d+, 0.875174\d+, 0.766883\d+, 1.136089\d+\]"""
r"""(At positional index 0, first diff: 0.548813\d+ != 1.097627\d+\n)?"""
)
@pytest.fixture
def ref_nrrd(empty_ref_tree):
raw_data = np.array([[[11.1], [12.2]], [[21.3], [22.4]]])
vd = voxcell.voxel_data.VoxelData(raw_data, (2, 2))
filename = empty_ref_tree / "file.nrrd"
vd.save_nrrd(str(filename))
return filename
@pytest.fixture
def res_nrrd_equal(empty_res_tree, ref_nrrd):
filename = empty_res_tree / "file.nrrd"
shutil.copyfile(ref_nrrd, filename)
return filename
@pytest.fixture
def res_nrrd_diff(empty_res_tree, ref_nrrd):
# raw_data = np.array([[[11], [12]], [[21], [22]]])
# vd = voxcell.voxel_data.VoxelData(raw_data, (2, 2))
vd = voxcell.voxel_data.VoxelData.load_nrrd(ref_nrrd)
vd.voxel_dimensions += 0.01
vd.raw += 0.01
filename = empty_res_tree / "file.nrrd"
vd.save_nrrd(str(filename))
return filename
@pytest.fixture
def nrrd_diff():
return (
r"""The files '\S*/file.nrrd' and '\S*/file.nrrd' are different:\n"""
r"""Kwargs used for computing differences: {'precision': None}\n"""
r"""\n"""
r"""Voxel dimensions: \n"""
r"""Arrays are not equal\n"""
r"""\n"""
r"""Mismatched elements: 2 / 2 \(100%\)\n"""
r"""Max absolute difference: 0.00999999\n"""
r"""Max relative difference: 0.00497512\n"""
r""" x: array\(\[2., 2.\], dtype=float32\)\n"""
r""" y: array\(\[2.01, 2.01\], dtype=float32\)\n"""
r"""\n"""
r"""Internal raw data: \n"""
r"""Arrays are not equal\n"""
r"""\n"""
r"""Mismatched elements: 4 / 4 \(100%\)\n"""
r"""Max absolute difference: 0.01\n"""
r"""Max relative difference: 0.0009\d*\n"""
r""" x: array\(\[\[\[11.1\],\n"""
r""" \[12.2\]\],\n"""
r"""...\n"""
r""" y: array\(\[\[\[11.11\],\n"""
r""" \[12.21\]\],\n"""
r"""..."""
)
class TestEqualTrees:
"""Tests that should return no difference."""
def test_diff_tree(
self,
empty_ref_tree,
ref_mvd3,
ref_nrrd,
empty_res_tree,
res_mvd3_equal,
res_nrrd_equal,
voxcell_registry_reseter,
):
res = compare_trees(empty_ref_tree, empty_res_tree)
assert res == {}
class TestDiffTrees:
"""Tests that should return differences."""
def test_diff_tree_mvd3(
self,
empty_ref_tree,
ref_mvd3,
empty_res_tree,
res_mvd3_diff,
cell_collection_diff_report,
voxcell_registry_reseter,
):
res = compare_trees(
empty_ref_tree,
empty_res_tree,
export_formatted_files=True,
)
assert len(res) == 1
res_mvd3 = res["file.mvd3"]
match_res = re.match(cell_collection_diff_report, res_mvd3)
assert match_res is not None
# Check the saving capability
assert (
empty_res_tree.parent
/ (empty_res_tree.name + _DEFAULT_EXPORT_SUFFIX)
/ "file.mvd3"
).exists()
def test_diff_tree_h5(
self,
empty_ref_tree,
ref_h5,
empty_res_tree,
res_h5_diff,
cell_collection_diff_report,
voxcell_registry_reseter,
):
res = compare_trees(
empty_ref_tree,
empty_res_tree,
specific_args={
"file.h5": {
"comparator": dir_content_diff.comparators.voxcell.CellCollectionComparator()
}
},
export_formatted_files=True,
)
assert len(res) == 1
res_h5 = res["file.h5"]
match_res = re.match(cell_collection_diff_report.replace("mvd3", "h5"), res_h5)
assert match_res is not None
# Check the saving capability
assert (
empty_res_tree.parent
/ (empty_res_tree.name + _DEFAULT_EXPORT_SUFFIX)
/ "file.h5"
).exists()
def test_diff_tree_nrrd(
self,
empty_ref_tree,
ref_nrrd,
empty_res_tree,
res_nrrd_diff,
nrrd_diff,
voxcell_registry_reseter,
):
res = compare_trees(
empty_ref_tree,
empty_res_tree,
export_formatted_files=True,
)
assert len(res) == 1
res_nrrd = res["file.nrrd"]
match_res = re.match(nrrd_diff, res_nrrd)
assert match_res is not None
# Check the saving capability
assert (
empty_res_tree.parent
/ (empty_res_tree.name + _DEFAULT_EXPORT_SUFFIX)
/ "file.nrrd"
).exists()
def test_diff_tree_nrrd_precision(
self,
empty_ref_tree,
ref_nrrd,
empty_res_tree,
res_nrrd_diff,
nrrd_diff,
voxcell_registry_reseter,
):
specific_args = {
"file.nrrd": {
"precision": 2,
}
}
res = compare_trees(
empty_ref_tree,
empty_res_tree,
specific_args=specific_args,
export_formatted_files=True,
)
assert len(res) == 0
assert re.match(".*/file.nrrd.*", str(res)) is None