Debian Codegraph @

nixio/test/test_validator.py

raw symbols SCIP
1 import nixio as nix
2 import numpy as np
3 import os
4 import unittest
5 from .tmp import TempDir
6
7 VE = nix.validator.ValidationError
8 VW = nix.validator.ValidationWarning
9
10
11 class TestValidate (unittest.TestCase):
12
13 def setUp(self):
14 # Create completely valid objects and break for each test
15 self.tmpdir = TempDir("validatetest")
16 self.testfilename = os.path.join(self.tmpdir.path, "validatetest.nix")
17 self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite)
18 for bidx in range(2):
19 # Block
20 blk = self.file.create_block("block-{}".format(bidx),
21 "validation-test.block")
22 for gidx in range(2):
23 # Groups
24 blk.create_group("group-{}".format(gidx),
25 "validation-test.group")
26
27 # DataArrays + Dimensions
28 da1d = blk.create_data_array("data-1d",
29 "validation-test.data_array",
30 dtype=nix.DataType.Float,
31 data=np.random.random_sample(5))
32 rdim = da1d.append_range_dimension(ticks=[0.1, 0.2, 1.5, 2.4, 3.0])
33 rdim.unit = "ms"
34
35 da2d = blk.create_data_array("data-2d",
36 "validation-test.data_array",
37 dtype=nix.DataType.Float,
38 data=np.random.random_sample((2, 10)))
39 setdim = da2d.append_set_dimension()
40 setdim.labels = ("A", "B")
41 da2d.append_sampled_dimension(0.1)
42
43 da3d = blk.create_data_array("data-3d",
44 "validation-test.data_array",
45 dtype=nix.DataType.Float,
46 data=np.random.random_sample((4, 2, 10)))
47 setdim = da3d.append_set_dimension()
48 setdim.labels = ("1", "2", "3", "4")
49 da3d.append_set_dimension() # set dim without labels
50 smpldim = da3d.append_sampled_dimension(0.05)
51 smpldim.unit = "s"
52
53 # Tags + References + Features
54 tag = blk.create_tag("tag", "validation-test.tag",
55 position=(1, 1, 1))
56 tag.extent = (2, 1, 5)
57 tag.references.append(blk.data_arrays["data-3d"])
58 tag.units = ("", "", "ms")
59
60 pos1d = blk.create_data_array(
61 "pos-1d", "validation-test.multi_tag.positions",
62 data=[0, 1, 10, 20]
63 )
64 pos1d.append_set_dimension()
65
66 tagwf = blk.create_tag("tag-wf", "validation-test.tag",
67 position=(1, 3))
68 tag_feat_data = blk.create_data_array(
69 "feat-1", "validation-test.tag-feature", data=[1]
70 )
71 tag_feat_data.append_set_dimension()
72 tagwf.create_feature(tag_feat_data, nix.LinkType.Untagged)
73
74 # MultiTags + References + Features
75 mtag1d = blk.create_multi_tag(
76 "mtag1d", "validation-test.multi_tag", positions=pos1d
77 )
78 mtag1d.references.append(da1d)
79 mtag1d.units = ("s",)
80
81 pos2d = blk.create_data_array(
82 "pos-2d", "validation-test.multi_tag.positions",
83 data=[(10, 2, 100), (20, 5, 101)]
84 )
85 pos2d.append_set_dimension()
86 pos2d.append_set_dimension()
87
88 ext2d = blk.create_data_array(
89 "ext-3d", "validation-test.multi_tag.extents",
90 data=[(1, 1, 1), (5, 5, 5)]
91 )
92 ext2d.append_set_dimension()
93 ext2d.append_set_dimension()
94 mtag2d = blk.create_multi_tag(
95 "mtag2d", "validation-test.multi_tag", positions=pos2d
96 )
97 mtag2d.extents = ext2d
98 mtag2d.references.append(da3d)
99 mtag2d.units = ("", "", "us")
100
101 poswf = blk.create_data_array(
102 "pos-wf", "validation-test.multi_tag.feature", data=[42]
103 )
104 poswf.append_set_dimension()
105 mtagwf = blk.create_multi_tag(
106 "mtag-wf", "validation-test.multi_tag.feature",
107 positions=poswf
108 )
109 mtag_feat_data = blk.create_data_array(
110 "feat-2", "validation-test.tag-feature", data=[4, 2]
111 )
112 mtag_feat_data.append_set_dimension()
113 mtagwf.create_feature(mtag_feat_data, nix.LinkType.Untagged)
114
115 # Sources
116 # Go 3 levels deep (with N = 3:2:1)
117 typestr = "validation-test.sources"
118 for idx in range(3):
119 isrc = blk.create_source("{}:{}".format(blk.name, idx),
120 typestr)
121 for jdx in range(2):
122 jsrc = isrc.create_source("{}:{}".format(isrc.name, jdx),
123 typestr)
124 jsrc.create_source("{}:0".format(jsrc.name),
125 typestr)
126
127 # Sections
128 # Go 3 levels deep (with N = 4:2:2)
129 # 3 props per Section
130
131 def attach_props(section):
132 section.create_property("intprop", "prop-type", 10).unit = "s"
133 section.create_property("strprop", "prop-type", 0.1).unit = "mA"
134 typestr = "validation-test.sections"
135 for idx in range(4):
136 isec = self.file.create_section("{}:{}".format(blk.name, idx),
137 typestr)
138 attach_props(isec)
139 for jdx in range(2):
140 jsec = isec.create_section("{}:{}".format(isec.name, jdx),
141 typestr)
142 for kdx in range(2):
143 jsec.create_section("{}:{}".format(jsec.name, kdx),
144 typestr)
145
146 def tearDown(self):
147 self.file.close()
148 self.tmpdir.cleanup()
149
150 def test_all_valid(self):
151 res = self.file.validate()
152 assert not res["errors"], self.print_all_results(res)
153 assert not res["warnings"], self.print_all_results(res)
154
155 def test_check_file(self):
156 self.file._root.set_attr("version", tuple())
157 res = self.file.validate()
158 assert VW.NoVersion in res["warnings"][self.file]
159
160 self.file._root.set_attr("format", "")
161 res = self.file.validate()
162 assert VW.NoFormat in res["warnings"][self.file]
163
164 self.file._root.set_attr("version", (1, 2, 0)) # needed for ID check
165 self.file._root.set_attr("id", "")
166 res = self.file.validate()
167 assert VW.NoFileID in res["warnings"][self.file]
168
169 def test_check_block(self):
170 block = self.file.blocks[0]
171 block._h5group.set_attr("name", None)
172 res = self.file.validate()
173 assert res["errors"][block] == [VE.NoName]
174
175 block._h5group.set_attr("type", None)
176 res = self.file.validate()
177 actual = sorted(res["errors"][block])
178 expected = sorted([VE.NoName, VE.NoType])
179 assert actual == expected
180 assert len(res["warnings"]) == 0
181
182 def test_check_group(self):
183 group1 = self.file.blocks[0].groups[0]
184 group2 = self.file.blocks[1].groups[0]
185 group1._h5group.set_attr("name", None)
186 res = self.file.validate()
187 assert res["errors"][group1] == [VE.NoName]
188 assert group2 not in res["errors"]
189
190 group2._h5group.set_attr("name", None)
191 res = self.file.validate()
192 assert res["errors"][group2] == [VE.NoName]
193
194 assert group1 not in res["warnings"]
195 assert group2 not in res["warnings"]
196
197 group2._h5group.set_attr("type", None)
198 res = self.file.validate()
199 actual = sorted(res["errors"][group2])
200 expected = sorted([VE.NoName, VE.NoType])
201 assert actual == expected
202 assert len(res["warnings"]) == 0
203
204 def test_check_data_array_wrong_ticks(self):
205 expmsg = VE.RangeDimTicksMismatch.format(1)
206 da = self.file.blocks[0].data_arrays["data-1d"]
207 rdim = da.dimensions[0]
208 rdim.ticks = [10]
209 res = self.file.validate()
210 assert res["errors"][da] == [expmsg]
211
212 def test_check_data_array_wrong_labels(self):
213 expmsg = VE.SetDimLabelsMismatch.format(1)
214 da = self.file.blocks[0].data_arrays["data-2d"]
215 setdim = da.dimensions[0]
216 setdim.labels = ["-"]
217 res = self.file.validate()
218 assert res["errors"][da] == [expmsg]
219
220 def test_check_data_array_coefficients(self):
221 da1 = self.file.blocks[0].data_arrays["data-1d"]
222 da1.expansion_origin = 0.7
223
224 da2 = self.file.blocks[1].data_arrays["data-1d"]
225 da2.polynom_coefficients = [2, 4]
226
227 res = self.file.validate()
228 warnings = res["warnings"]
229 assert warnings[da1] == [VW.NoPolynomialCoefficients]
230 assert warnings[da2] == [VW.NoExpansionOrigin]
231
232 def test_check_data_array_bad_dims(self):
233 da = self.file.blocks[1].data_arrays["data-3d"]
234 da.append_set_dimension()
235 res = self.file.validate()
236 assert res["errors"][da] == [VE.DimensionMismatch]
237
238 def test_check_data_array_invalid_unit(self):
239 da = self.file.blocks[1].data_arrays["data-1d"]
240 da.unit = "whatevz"
241 res = self.file.validate()
242 assert res["warnings"][da] == [VW.InvalidUnit]
243
244 def test_incorrect_dim_index(self):
245 da = self.file.blocks[1].data_arrays["data-1d"]
246 da.delete_dimensions()
247 # This wont work if we ever change the internals
248 nix.SetDimension.create_new(da, "10")
249 res = self.file.validate()
250 assert VE.IncorrectDimensionIndex.format(1, 10) in res["errors"][da]
251
252 def test_invalid_dim_index(self):
253 da = self.file.blocks[1].data_arrays["data-1d"]
254 da.delete_dimensions()
255 # This wont work if we ever change the internals
256 nix.SetDimension.create_new(da, "-1")
257 res = self.file.validate()
258 assert VE.InvalidDimensionIndex.format(1) in res["errors"][da]
259
260 def test_check_tag_no_pos(self):
261 tag = self.file.blocks[0].tags[0]
262 tag.position = []
263 res = self.file.validate()
264 tagerr = res["errors"][tag]
265 # will also report mismatch in dimensions with reference
266 assert "position is not set" in tagerr
267
268 def test_check_tag_mismatch_dim(self):
269 tag = self.file.blocks[0].tags[0]
270 tag.position = [4, 3, 2, 1]
271 res = self.file.validate()
272 tagerr = res["errors"][tag]
273 assert VE.PositionDimensionMismatch in tagerr
274
275 def test_check_tag_invalid_unit(self):
276 tag = self.file.blocks[0].tags[0]
277 tag.units = ['abc']
278 res = self.file.validate()
279 tagerr = res["errors"][tag]
280 assert VE.InvalidUnit in tagerr
281
282 def test_check_tag_mismatch_units(self):
283 tag = self.file.blocks[0].tags[0]
284 tag.units = ("V", "A")
285 res = self.file.validate()
286 tagerr = res["errors"][tag]
287 assert VE.ReferenceUnitsMismatch in tagerr
288
289 tag.units = ("V", "A", "L")
290 res = self.file.validate()
291 tagerr = res["errors"][tag]
292 assert VE.ReferenceUnitsIncompatible in tagerr
293
294 def test_check_tag_pos_ext_mismatch(self):
295 tag = self.file.blocks[0].tags[0]
296 tag.extent = [100]
297
298 res = self.file.validate()
299 tagerr = res["errors"][tag]
300 assert VE.ExtentDimensionMismatch in tagerr
301
302 def test_check_multi_tag_no_pos(self):
303 blk = self.file.blocks[0]
304 mtag = blk.multi_tags["mtag1d"]
305 mtag.positions = blk.create_data_array("empty", "bork", data=[])
306 res = self.file.validate()
307 mtagerr = res["errors"][mtag]
308 # will also report mismatch in dimensions with reference
309 assert VE.NoPositions in mtagerr
310
311 def test_check_multi_tag_mismatch_dim(self):
312 blk = self.file.blocks[0]
313
314 # 2d tag positions on 2d data, with wrong positions length
315 mtag = blk.multi_tags["mtag2d"]
316 okpos = mtag.positions
317 mtag.positions = blk.create_data_array("wrong dim len", "bork",
318 data=[(1, 1), (2, 2)])
319 res = self.file.validate()
320 mtagerr = res["errors"][mtag]
321 assert VE.PositionsDimensionMismatch in mtagerr
322
323 # 1d tag positions on 2d data
324 mtag.positions = blk.create_data_array("1d v 2d", "bork",
325 data=[1, 2])
326 res = self.file.validate()
327 mtagerr = res["errors"][mtag]
328 assert VE.PositionsDimensionMismatch in mtagerr
329
330 # valid tag positions on 2d data, 1d extents
331 mtag.positions = okpos
332 mtag.extents = blk.create_data_array("1d extents", "bork",
333 data=[1, 2])
334 res = self.file.validate()
335 mtagerr = res["errors"][mtag]
336 assert VE.ExtentsDimensionMismatch in mtagerr
337
338 def test_check_multi_tag_invalid_unit(self):
339 blk = self.file.blocks[0]
340 mtag = blk.multi_tags["mtag2d"]
341 mtag.units = ['abc']
342 res = self.file.validate()
343 tagerr = res["errors"][mtag]
344 assert VE.InvalidUnit in tagerr
345
346 def test_check_multi_tag_mismatch_units(self):
347 mtag = self.file.blocks[0].multi_tags["mtag2d"]
348 mtag.units = ("V", "A")
349 res = self.file.validate()
350 tagerr = res["errors"][mtag]
351 assert VE.ReferenceUnitsMismatch in tagerr
352
353 mtag.units = ("V", "A", "L")
354 res = self.file.validate()
355 tagerr = res["errors"][mtag]
356 assert VE.ReferenceUnitsIncompatible in tagerr
357
358 def test_check_multi_tag_pos_ext_mismatch(self):
359 blk = self.file.blocks[0]
360 mtag = blk.multi_tags["mtag2d"]
361 mtag.extents = blk.create_data_array("wrong dim", "bork",
362 data=[(1, 1), (2, 2)])
363
364 res = self.file.validate()
365 mtagerr = res["errors"][mtag]
366 assert VE.PositionsExtentsMismatch in mtagerr
367
368 def test_check_source(self):
369 # just break one of the deepest Sources
370 def get_deepest_source(sources):
371 for source in sources:
372 if not source.sources:
373 return source
374 return get_deepest_source(source.sources)
375
376 source = get_deepest_source(self.file.blocks[0].sources)
377 source._h5group.set_attr("name", None)
378 source._h5group.set_attr("type", None)
379
380 res = self.file.validate()
381 assert len(res["errors"][source]) == 2
382 assert len(res["warnings"]) == 0
383 assert sorted(res["errors"][source]) == sorted([VE.NoName, VE.NoType])
384
385 def test_check_section(self):
386 # just break one of the deepest Sections
387 def get_deepest_section(sections):
388 for section in sections:
389 if not section.sections:
390 return section
391 return get_deepest_section(section.sections)
392
393 section = get_deepest_section(self.file.sections)
394 section._h5group.set_attr("name", None)
395 section._h5group.set_attr("type", None)
396
397 res = self.file.validate()
398 assert len(res["errors"][section]) == 2
399 assert len(res["warnings"]) == 0
400 assert sorted(res["errors"][section]) == sorted([VE.NoName, VE.NoType])
401
402 def test_check_property(self):
403 section = self.file.sections[0]
404 prop = section.props[0]
405 prop._h5group.set_attr("name", None)
406 res = self.file.validate()
407 errmsg = "property 0: {}".format(VE.NoName)
408 assert res["errors"][section] == [errmsg]
409
410 prop = section.props[1]
411 prop.unit = None
412 res = self.file.validate()
413 warnmsg = "property 1: {}".format(VW.NoUnit)
414 assert res["warnings"][section] == [warnmsg]
415
416 def test_check_range_dim_no_ticks(self):
417 da = self.file.blocks[1].data_arrays["data-1d"]
418 dim = da.dimensions[0]
419 dim.ticks = []
420 res = self.file.validate()
421 assert VE.NoTicks.format(1) in res["errors"][da]
422
423 def test_check_range_dim_invalid_unit(self):
424 da = self.file.blocks[1].data_arrays["data-1d"]
425 dim = da.dimensions[0]
426 dim.unit = "sillyvolts"
427 res = self.file.validate()
428 assert VE.InvalidDimensionUnit.format(1) in res["errors"][da]
429
430 def test_check_range_dim_unsorted_ticks(self):
431 da = self.file.blocks[1].data_arrays["data-1d"]
432 dim = da.dimensions[0]
433 dim._h5group.write_data("ticks", [10, 3, 1])
434 res = self.file.validate()
435 assert VE.UnsortedTicks.format(1) in res["errors"][da]
436
437 def test_check_sampled_dim_no_interval(self):
438 da = self.file.blocks[1].data_arrays["data-2d"]
439 dim = da.dimensions[1]
440 dim.sampling_interval = None
441 res = self.file.validate()
442 assert VE.NoSamplingInterval.format(2) in res["errors"][da]
443
444 def test_check_sampled_dim_bad_interval(self):
445 da = self.file.blocks[1].data_arrays["data-2d"]
446 dim = da.dimensions[1]
447 dim.sampling_interval = -1
448 res = self.file.validate()
449 assert VE.InvalidSamplingInterval.format(2) in res["errors"][da]
450
451 def test_check_sampled_dim_bad_unit(self):
452 da = self.file.blocks[1].data_arrays["data-2d"]
453 dim = da.dimensions[1]
454 dim.unit = "sillyamps"
455 res = self.file.validate()
456 assert VE.InvalidDimensionUnit.format(2) in res["errors"][da]
457
458 def test_check_sampled_dim_no_unit(self):
459 da = self.file.blocks[1].data_arrays["data-2d"]
460 dim = da.dimensions[1]
461 dim.unit = None
462 dim.offset = 10
463 res = self.file.validate()
464 assert VW.OffsetNoUnit.format(2) in res["warnings"][da]
465
466 @staticmethod
467 def print_all_results(res):
468 print("Errors")
469 for obj, msg in res["errors"].items():
470 name = obj.name if hasattr(obj, "name") else str(obj)
471 print(" {}: {}".format(name, msg))
472 print("Warnings")
473 for obj, msg in res["warnings"].items():
474 name = obj.name if hasattr(obj, "name") else str(obj)
475 print(" {}: {}".format(name, msg))