Debian Codegraph @

nixio/test/test_container.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright © 2017, German Neuroinformatics Node (G-Node)
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in section and binary forms, with or without
7 # modification, are permitted under the terms of the BSD License. See
8 # LICENSE file in the root of the Project.
9 import os
10 import random
11 import nixio as nix
12 import unittest
13 from .tmp import TempDir
14
15
16 class TestContainer(unittest.TestCase):
17
18 def setUp(self):
19 self.tmpdir = TempDir("containertest")
20 self.testfilename = os.path.join(self.tmpdir.path, "containertest.nix")
21 self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite)
22 self.block = self.file.create_block("test block", "containertest")
23 self.dataarray = self.block.create_data_array("test array",
24 "containertest",
25 data=[0])
26 self.tag = self.block.create_tag("test tag", "containertest",
27 position=[1.9, 20])
28 self.positions = self.block.create_data_array("test pos",
29 "containertest",
30 data=[1])
31 self.multi_tag = self.block.create_multi_tag("test multitag",
32 "containertest",
33 positions=self.positions)
34 self.group = self.block.create_group("test group",
35 "containertest")
36
37 self.group.data_arrays.append(self.dataarray)
38 self.group.tags.append(self.tag)
39 self.group.multi_tags.append(self.multi_tag)
40
41 def tearDown(self):
42 del self.file.blocks[self.block.id]
43 self.file.close()
44 self.tmpdir.cleanup()
45
46 def test_name_getter(self):
47 self.assertEqual(self.block, self.file.blocks["test block"])
48 self.assertEqual(self.dataarray, self.block.data_arrays["test array"])
49 self.assertEqual(self.tag, self.block.tags["test tag"])
50 self.assertEqual(self.multi_tag,
51 self.block.multi_tags["test multitag"])
52 self.assertEqual(self.group, self.block.groups["test group"])
53 self.assertEqual(self.positions, self.block.data_arrays["test pos"])
54
55 def test_index_getter(self):
56 self.assertEqual(self.block, self.file.blocks[0])
57 self.assertEqual(self.dataarray, self.block.data_arrays[0])
58 self.assertEqual(self.tag, self.block.tags[0])
59 self.assertEqual(self.multi_tag,
60 self.block.multi_tags[0])
61 self.assertEqual(self.group, self.block.groups[0])
62 self.assertEqual(self.positions, self.block.data_arrays[1])
63
64 def test_link_container_name_getter(self):
65 self.assertEqual(self.dataarray, self.group.data_arrays["test array"])
66 self.assertEqual(self.tag, self.group.tags["test tag"])
67 self.assertEqual(self.multi_tag,
68 self.group.multi_tags["test multitag"])
69
70 def test_link_container_index_getter(self):
71 self.assertEqual(self.dataarray, self.group.data_arrays[0])
72 self.assertEqual(self.tag, self.group.tags[0])
73 self.assertEqual(self.multi_tag,
74 self.group.multi_tags[0])
75
76 def test_file_references(self):
77 # add some sources
78 source = self.block.create_source("test source", "containertest")
79 child_source = source.create_source("test source 2", "containertest")
80 # using assertIs since the reference should be the same instance as
81 # the original
82
83 # created objects
84 self.assertIs(self.block.file, self.file)
85 self.assertIs(self.group.file, self.file)
86 self.assertIs(self.dataarray.file, self.file)
87 self.assertIs(self.tag.file, self.file)
88 self.assertIs(self.multi_tag.file, self.file)
89 self.assertIs(self.positions.file, self.file)
90 self.assertIs(source.file, self.file)
91 self.assertIs(child_source.file, self.file)
92
93 # instantiated through container getters
94 blk = self.file.blocks[0]
95 self.assertIs(blk.file, self.file)
96 self.assertIs(blk.groups[0].file, self.file)
97 self.assertIs(blk.data_arrays[0].file, self.file)
98 self.assertIs(blk.tags[0].file, self.file)
99 self.assertIs(blk.multi_tags[0].file, self.file)
100 self.assertIs(blk.sources[0].file, self.file)
101 self.assertIs(blk.sources[0].sources[0].file, self.file)
102
103 # linked through group
104 self.assertIs(self.group.data_arrays[0].file, self.file)
105 self.assertIs(self.group.tags[0].file, self.file)
106 self.assertIs(self.group.multi_tags[0].file, self.file)
107
108 def test_parent_references(self):
109 # add some sources
110 source = self.block.create_source("test source", "containertest")
111 child_source = source.create_source("test source 2", "containertest")
112
113 self.assertEqual(self.block._parent, self.file)
114 self.assertEqual(self.group._parent, self.block)
115 self.assertEqual(self.dataarray._parent, self.block)
116 self.assertEqual(self.tag._parent, self.block)
117 self.assertEqual(self.multi_tag._parent, self.block)
118 self.assertEqual(self.positions._parent, self.block)
119 self.assertEqual(source._parent, self.block)
120 self.assertEqual(child_source._parent, source)
121
122 def test_link_parent_references(self):
123 self.assertEqual(self.group.data_arrays[0]._parent, self.block)
124 self.assertEqual(self.group.tags[0]._parent, self.block)
125 self.assertEqual(self.group.multi_tags[0]._parent, self.block)
126
127 def test_bad_appends(self):
128 # use fresh file for this one
129 filename = os.path.join(self.tmpdir.path, "badappend.nix")
130 nf = nix.File.open(filename, nix.FileMode.Overwrite)
131
132 for blockname in ["a", "b"]:
133 block = nf.create_block(blockname, "block")
134
135 for groupname in ["a", "b"]:
136 group = block.create_group(blockname + groupname, "group")
137 for daname in ["a", "b"]:
138 da = block.create_data_array(
139 blockname + groupname + daname, "data", data=[0]
140 )
141 group.data_arrays.append(da)
142 tag = block.create_tag(
143 blockname + groupname + daname, "tag", position=[0]
144 )
145 group.tags.append(tag)
146
147 # check counts
148 self.assertEqual(len(nf.blocks), 2)
149 for blk in nf.blocks:
150 self.assertEqual(len(blk.groups), 2)
151 self.assertEqual(len(blk.data_arrays), 4)
152 self.assertEqual(len(blk.tags), 4)
153 for grp in blk.groups:
154 self.assertEqual(len(grp.data_arrays), 2)
155 self.assertEqual(len(grp.tags), 2)
156
157 # cross-block append
158 with self.assertRaises(RuntimeError):
159 nf.blocks[0].groups[0].data_arrays.append(
160 nf.blocks[1].data_arrays[1]
161 )
162
163 # another
164 with self.assertRaises(RuntimeError):
165 nf.blocks[1].groups[0].tags.append(
166 nf.blocks[0].tags[1]
167 )
168
169 # append data array to group.tags
170 with self.assertRaises(TypeError):
171 nf.blocks[0].groups[0].tags.append(
172 nf.blocks[0].data_arrays[2]
173 )
174
175 # cross-block *and* incorrect type
176 with self.assertRaises(TypeError):
177 nf.blocks[0].groups[0].tags.append(
178 nf.blocks[1].data_arrays[2]
179 )
180
181 # let's do sources
182 for blk in nf.blocks:
183 for sourcename in ["a", "b"]:
184 src = blk.create_source(blk.name + sourcename, "source")
185 for chsourcename in ["a", "b", "c"]:
186 src.create_source(blk.name + sourcename + chsourcename,
187 "source")
188
189 # check counts
190 for blk in nf.blocks:
191 self.assertEqual(len(blk.sources), 2)
192 for src in blk.sources:
193 self.assertEqual(len(src.sources), 3)
194
195 # cross-block source append
196 with self.assertRaises(RuntimeError):
197 nf.blocks[1].groups[0].sources.append(
198 nf.blocks[0].sources[1]
199 )
200
201 # cross-block source append (deep)
202 with self.assertRaises(RuntimeError):
203 nf.blocks[1].groups[0].sources.append(
204 nf.blocks[0].sources[1].sources[2]
205 )
206
207 # valid deep append
208 nf.blocks[0].groups[0].data_arrays[1].sources.append(
209 nf.blocks[0].sources[1].sources[2]
210 )
211
212 # bad membership test
213 with self.assertRaises(TypeError):
214 _ = nf.blocks[1].data_arrays[1] in nf.blocks[0].tags
215
216 # another
217 with self.assertRaises(TypeError):
218 _ = nf.blocks[1].data_arrays[1] in nf.blocks[0].groups[0]
219
220 # bad membership test on LinkContainer
221 with self.assertRaises(TypeError):
222 _ = nf.blocks[1].data_arrays[1] in nf.blocks[0].groups[0].tags
223
224 def test_delete_links_da(self):
225 # delete DataArray from Block and check Group
226 daname = "new-data-array"
227 da = self.block.create_data_array(daname, "to-be-deleted", data=[0])
228 self.block.groups[0].data_arrays.append(da)
229 self.assertIn(self.block.data_arrays[-1],
230 self.block.groups[-1].data_arrays)
231 self.assertIn(daname, self.block.groups[0].data_arrays)
232
233 self.assertEqual(self.block.groups[-1].data_arrays[-1].name,
234 daname)
235
236 del self.block.data_arrays[daname]
237 self.assertNotIn(daname, self.block.data_arrays)
238 self.assertNotIn(daname, self.block.groups[0].data_arrays)
239
240 def test_delete_links_tag(self):
241 # delete Tag from Block and check Group
242 tagname = "new-tag"
243 tag = self.block.create_tag(tagname, "to-be-deleted", position=[0])
244 self.block.groups[0].tags.append(tag)
245 self.assertIn(self.block.tags[-1],
246 self.block.groups[-1].tags)
247 self.assertIn(tagname, self.block.groups[0].tags)
248
249 self.assertEqual(self.block.groups[-1].tags[-1].name, tagname)
250
251 del self.block.tags[tagname]
252 self.assertNotIn(tagname, self.block.tags)
253 self.assertNotIn(tagname, self.block.groups[0].tags)
254
255 def test_delete_links_multitag(self):
256 # delete MultiTag DataArrays and check positions and extents
257 posname = "new-mt-positions"
258 pos = self.block.create_data_array(posname, "to-be-deleted", data=[0])
259 extname = "new-mt-extents"
260 ext = self.block.create_data_array(extname, "to-be-deleted", data=[0])
261
262 mtagname = "new-multi-tag"
263 mtag = self.block.create_multi_tag(mtagname, "to-be-deleted",
264 positions=pos)
265 mtag.extents = ext
266 self.block.groups[0].multi_tags.append(mtag)
267
268 self.assertEqual(mtag.positions, pos)
269 del self.block.data_arrays[posname]
270 with self.assertRaises(RuntimeError):
271 _ = mtag.positions
272 # ext still here
273 self.assertEqual(mtag.extents, ext)
274 # delete ext and check extents
275 del self.block.data_arrays[extname]
276 self.assertIsNone(mtag.extents)
277
278 # delete MultiTag and check Group
279 self.assertIn(mtagname, self.block.multi_tags)
280 self.assertIn(mtag.id, self.block.multi_tags)
281 self.assertIn(mtag, self.block.multi_tags)
282 del self.block.multi_tags[mtagname]
283 self.assertNotIn(mtagname, self.block.multi_tags)
284
285 def test_delete_links_references(self):
286 posname = "new-mt-positions"
287 pos = self.block.create_data_array(posname, "to-be-deleted", data=[0])
288
289 tagname = "new-tag"
290 tag = self.block.create_tag(tagname, "to-be-deleted",
291 position=[1, 3, 10])
292 self.block.groups[0].tags.append(tag)
293
294 mtagname = "new-multi-tag"
295 mtag = self.block.create_multi_tag(mtagname, "to-be-deleted",
296 positions=pos)
297 self.block.groups[0].multi_tags.append(mtag)
298
299 refnamea = "new-da-ref-a"
300 refa = self.block.create_data_array(refnamea, "to-be-deleted",
301 data=[10])
302 tag.references.append(refa)
303 mtag.references.append(refa)
304
305 refnameb = "new-da-ref-b"
306 refb = self.block.create_data_array(refnameb, "to-be-deleted",
307 data=[11])
308 tag.references.append(refb)
309 mtag.references.append(refb)
310
311 # delete ref DAs and check refs
312 del self.block.data_arrays[refnamea]
313 self.assertNotIn(refnamea, tag.references)
314 self.assertIn(refnameb, tag.references)
315 self.assertNotIn(refnamea, mtag.references)
316 self.assertIn(refnameb, mtag.references)
317
318 del self.block.data_arrays[refnameb]
319 self.assertNotIn(refnameb, tag.references)
320 self.assertNotIn(refnameb, mtag.references)
321
322 def test_delete_links_sources(self):
323 sourcetreedepth = 5
324
325 # make a random source tree
326 def make_sources(par, depth=0):
327 if depth == sourcetreedepth:
328 return
329 for srcidx in range(3):
330 srcname = "{}:src-{}".format(par.name, srcidx)
331 srctype = "Source at depth {}".format(depth)
332 chsrc = par.create_source(srcname, srctype)
333 make_sources(chsrc, depth+1)
334
335 make_sources(self.block)
336
337 def get_rand_src(par, depth=None):
338 if depth is None:
339 depth = random.randint(0, sourcetreedepth-1)
340
341 idx = random.randint(0, len(par.sources)-1)
342 chsrc = par.sources[idx]
343 if depth == 0:
344 return chsrc
345
346 return get_rand_src(chsrc, depth-1)
347
348 # link a couple of random sources to dataarray
349 parenta = get_rand_src(self.block, depth=3)
350 sourcea = parenta.sources[-1]
351 self.dataarray.sources.append(sourcea)
352
353 parentb = get_rand_src(self.block, depth=2)
354 sourceb = parentb.sources[0]
355 self.dataarray.sources.append(sourceb)
356
357 # delete sourcea from parent
358 namelsa = sourcea.name
359 del parenta.sources[namelsa]
360 # check references
361 self.assertNotIn(namelsa, parenta.sources)
362 self.assertNotIn(namelsa, self.dataarray.sources)
363 self.assertIn(sourceb, self.dataarray.sources)
364
365 # delete sourceb from parent
366 namelsb = sourceb.name
367 del parentb.sources[namelsb]
368 # check references
369 self.assertNotIn(namelsb, parentb.sources)
370 self.assertNotIn(sourceb, self.dataarray.sources)
371
372 # link a deep source and delete its parent
373 grandparentc = get_rand_src(self.block, depth=0)
374 parentc = get_rand_src(grandparentc, depth=0)
375 sourcec = get_rand_src(parentc, depth=0)
376 self.tag.sources.append(sourcec)
377 cname = sourcec.name
378 del grandparentc.sources[parentc.name]
379 self.assertNotIn(cname, self.tag.sources)
380
381 def test_delete_links_sections(self):
382 sectiontreedepth = 5
383
384 rootsec = self.file.create_section("GOD", "I AM THE TOP SECTION")
385
386 # make a random section tree
387 def make_sections(par, depth=0):
388 if depth == sectiontreedepth:
389 return
390 for secidx in range(3):
391 secname = "{}:sec-{}".format(par.name, secidx)
392 sectype = "section at depth {}".format(depth)
393 chsec = par.create_section(secname, sectype)
394 make_sections(chsec, depth+1)
395
396 make_sections(rootsec)
397
398 def get_rand_sec(par, depth=None):
399 if depth is None:
400 depth = random.randint(0, sectiontreedepth-1)
401
402 idx = random.randint(0, len(par.sections)-1)
403 chsec = par.sections[idx]
404 if depth == 0:
405 return chsec
406
407 return get_rand_sec(chsec, depth-1)
408
409 # link a couple of random sections to dataarray
410 parenta = get_rand_sec(rootsec, depth=3)
411 sectiona = parenta.sections[-1]
412 self.dataarray.metadata = sectiona
413
414 parentb = get_rand_sec(rootsec, depth=2)
415 sectionb = parentb.sections[0]
416 self.multi_tag.metadata = sectionb
417
418 # delete sectiona from parent
419 namelsa = sectiona.name
420 del parenta.sections[namelsa]
421 # check references
422 self.assertNotIn(namelsa, parenta.sections)
423 self.assertIsNone(self.dataarray.metadata)
424 self.assertEqual(sectionb, self.multi_tag.metadata)
425
426 # delete sectionb from parent
427 namelsb = sectionb.name
428 del parentb.sections[namelsb]
429 # check references
430 self.assertNotIn(namelsb, parentb.sections)
431 self.assertIsNone(self.multi_tag.metadata)
432
433 # link a deep section and delete its parent
434 grandparentc = get_rand_sec(rootsec, depth=0)
435 parentc = get_rand_sec(grandparentc, depth=0)
436 sectionc = get_rand_sec(parentc, depth=0)
437 self.tag.metadata = sectionc
438 del grandparentc.sections[parentc.name]
439 self.assertIsNone(self.tag.metadata)
440
441 def test_delete_links_tag_features(self):
442 posname = "new-mt-positions"
443 pos = self.block.create_data_array(posname, "to-be-deleted", data=[0])
444
445 tagname = "new-tag"
446 tag = self.block.create_tag(tagname, "to-be-deleted",
447 position=[1, 3, 10])
448 self.block.groups[0].tags.append(tag)
449
450 mtagname = "new-multi-tag"
451 mtag = self.block.create_multi_tag(mtagname, "to-be-deleted",
452 positions=pos)
453 self.block.groups[0].multi_tags.append(mtag)
454
455 dataname = "new-da-feat-a"
456 feat_da = self.block.create_data_array(dataname, "to-be-deleted",
457 data=[10])
458
459 tag.create_feature(feat_da, nix.LinkType.Indexed)
460
461 self.assertIn(dataname, self.block.data_arrays)
462 self.assertIn(dataname, tag.features)
463
464 del self.block.data_arrays[dataname]
465
466 self.assertNotIn(dataname, self.block.data_arrays)
467
468 with self.assertRaises(RuntimeError):
469 self.assertNotIn(dataname, tag.features)
470
471 with self.assertRaises(RuntimeError):
472 _ = tag.features[dataname].data
473
474 def test_delete_links_section_link(self):
475 parsec = self.file.create_section("TopDog", "Root Section")
476 chsec = parsec.create_section("1Dog", "Level 1 section")
477 chchsec = chsec.create_section("2Dog", "Level 2 section")
478 chchchsec = chchsec.create_section("3Dog", "Level 3 section")
479
480 chchsec.create_property("2Prop", "Level 2 property")
481 chchchsec.create_property("3Prop", "Level 3 property")
482
483 chsecb = parsec.create_section("1Dog2", "Level 1 section")
484
485 # link chchchsec to chsecb and then delete it
486 chsecb.link = chchchsec
487
488 inhpropnames = [p.name for p in chsecb.inherited_properties()]
489 self.assertEqual(chchchsec, chsecb.link)
490 self.assertIn("3Prop", inhpropnames)
491
492 # delete chchchsec and check link and inherited props
493 del chchsec.sections["3Dog"]
494 inhpropnames = [p.name for p in chsecb.inherited_properties()]
495 self.assertIs(None, chsecb.link)
496 self.assertNotIn("3Prop", inhpropnames)
497
498 # link chchsec to chsecb then delete chsec (chchsec's parent)
499 chsecb.link = chchsec
500
501 inhpropnames = [p.name for p in chsecb.inherited_properties()]
502 self.assertEqual(chchsec, chsecb.link)
503 self.assertIn("2Prop", inhpropnames)
504
505 # delete chsec and check link and inherited props
506 del parsec.sections["1Dog"]
507 inhpropnames = [p.name for p in chsecb.inherited_properties()]
508 self.assertIs(None, chsecb.link)
509 self.assertNotIn("2Prop", inhpropnames)