Debian Codegraph @

nixio/test/test_file.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Copyright © 2014, German Neuroinformatics Node (G-Node)
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source 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 time
11 import h5py
12 import unittest
13 import numpy as np
14
15 import nixio as nix
16 import nixio.file as filepy
17 from nixio.exceptions import InvalidFile, DuplicateName
18
19 from .tmp import TempDir
20
21
22 class TestFile(unittest.TestCase):
23
24 def setUp(self):
25 self.tmpdir = TempDir("filetest")
26 self.testfilename = os.path.join(self.tmpdir.path, "filetest.nix")
27 self.file = nix.File.open(self.testfilename, nix.FileMode.Overwrite)
28
29 def tearDown(self):
30 self.file.close()
31 self.tmpdir.cleanup()
32
33 def test_file_format(self):
34 assert self.file.format == "nix"
35 assert self.file.version == filepy.HDF_FF_VERSION
36
37 def test_file_timestamps(self):
38 created_at = self.file.created_at
39 assert created_at > 0
40
41 updated_at = self.file.updated_at
42 assert updated_at > 0
43
44 self.file.force_created_at(1403530068)
45 assert self.file.created_at == 1403530068
46
47 def test_file_blocks(self):
48 assert len(self.file.blocks) == 0
49
50 block = self.file.create_block("test block", "recordingsession")
51 with self.assertRaises(DuplicateName):
52 self.file.create_block("test block", "recordingsession")
53 assert len(self.file.blocks) == 1
54
55 assert block in self.file.blocks
56 assert block.id in self.file.blocks
57 assert "notexist" not in self.file.blocks
58
59 assert block.id == self.file.blocks[0].id
60 assert block.id == self.file.blocks[-1].id
61
62 del self.file.blocks[0]
63
64 assert len(self.file.blocks) == 0
65
66 with self.assertRaises(ValueError):
67 self.file.create_block()
68 self.file.create_block(name="a name")
69
70 b = self.file.create_block(type_="type_a")
71 assert b.id == b.name
72
73 def test_file_sections(self):
74 assert len(self.file.sections) == 0
75
76 section = self.file.create_section("test section", "recordingsession")
77
78 assert len(self.file.sections) == 1
79
80 assert section in self.file.sections
81 assert section.id in self.file.sections
82 assert "notexist" not in self.file.sections
83
84 assert section.id == self.file.sections[0].id
85 assert section.id == self.file.sections[-1].id
86
87 del self.file.sections[0]
88
89 assert len(self.file.sections) == 0
90
91 def test_file_find_sections(self):
92 for i in range(2):
93 self.file.create_section("level1-p0-s" + str(i), "dummy")
94 for i in range(2):
95 self.file.sections[0].create_section("level2-p1-s" + str(i),
96 "dummy")
97 for i in range(2):
98 self.file.sections[1].create_section("level2-p2-s" + str(i),
99 "dummy")
100 for i in range(2):
101 self.file.sections[0].sections[0].create_section(
102 "level3-p1-s" + str(i), "dummy"
103 )
104
105 assert len(self.file.find_sections()) == 8
106 assert len(self.file.find_sections(limit=1)) == 2
107 assert(len(self.file.find_sections(filtr=lambda x: "level2-p1-s" in
108 x.name)) == 2)
109 assert(len(self.file.find_sections(filtr=lambda x: "level2-p1-s" in
110 x.name,
111 limit=1)) == 0)
112
113 def test_order_tracking(self):
114 blknames = []
115 for idx in range(10):
116 name = "block_" + str(idx)
117 self.file.create_block(name, "ordertest")
118 blknames.append(name)
119
120 danames = []
121 datablockname = blknames[0]
122 datablock = self.file.blocks[datablockname]
123 for idx in range(7):
124 name = "data_" + str(idx)
125 da = datablock.create_data_array(name, "thedata",
126 data=np.array([0]))
127 da.definition = "da definition"
128 danames.append(name)
129 self.file.close()
130
131 self.file = nix.File.open(self.testfilename, nix.FileMode.ReadOnly)
132
133 for idx in range(len(self.file.blocks)):
134 self.assertEqual(blknames[idx], self.file.blocks[idx].name)
135
136 datablock = self.file.blocks[datablockname]
137 for idx in range(len(datablock.data_arrays)):
138 self.assertEqual(danames[idx], datablock.data_arrays[idx].name)
139
140 def test_context_open(self):
141 fname = os.path.join(self.tmpdir.path, "contextopen.nix")
142 with nix.File.open(fname, nix.FileMode.Overwrite) as nf:
143 nf.create_block("blocky", "test-block")
144
145 with nix.File.open(fname, nix.FileMode.ReadOnly) as nf:
146 self.assertEqual(nf.blocks[0].name, "blocky")
147
148 def test_copy_on_file(self):
149 tar_filename = os.path.join(self.tmpdir.path, "copytarget.nix")
150 tar_file = nix.File.open(tar_filename, nix.FileMode.Overwrite)
151 blk1 = self.file.create_block("t111t bk", "testcopy")
152 blk2 = tar_file.create_block("blk2", "blk")
153 blk1.create_data_array("da1", 'grp da1', data=[(1, 2, 3)])
154 da2 = blk2.create_data_array("da2", 'grp da2', data=[(4, 5, 6)])
155 blk2.create_multi_tag("mt2", "useless", da2)
156 sec1 = self.file.create_section("test sec", 'test')
157 sec1.create_section("child sec", "child")
158 ori_prop = sec1.create_property("prop origin",
159 values_or_dtype=[1, 2, 3])
160 tar_file.create_block(copy_from=blk1, keep_copy_id=False)
161 copied_sec = tar_file.copy_section(sec1, children=False, keep_id=True)
162 assert tar_file.sections[0].name == sec1.name
163 assert tar_file.blocks[1].name == blk1.name
164 assert tar_file.blocks[1].data_arrays[0].name \
165 == blk1.data_arrays[0].name
166 assert tar_file.blocks[1].data_arrays[0].id != blk1.data_arrays[0].id
167 assert tar_file.sections[0] == sec1
168 assert len(self.file.find_sections()) == 2
169 assert len(tar_file.find_sections()) == 1
170 assert copied_sec.props[0] == ori_prop # Properties are still there
171 assert not copied_sec.sections # children is False
172 tar_file.close()
173 # test copying on the same file
174 self.assertRaises(NameError, self.file.create_block, copy_from=blk1)
175 self.file.create_block(name="111", copy_from=blk1)
176 assert self.file.blocks[0] == self.file.blocks[1] # ID stays the same
177 assert self.file.blocks[0].name != self.file.blocks[1].name
178
179 def test_timestamp_autoupdate(self):
180 # Using Block to test Entity.definition
181 blk = self.file.create_block("block", "timetest")
182 blktime = blk.updated_at
183 time.sleep(1) # wait for time to change
184 blk.definition = "updated"
185 # no update
186 self.assertNotEqual(blk.updated_at, blktime)
187
188 rblk = self.file.blocks["block"] # read through container
189 time.sleep(1) # wait for time to change
190 rblk.definition = "updated again"
191 self.assertNotEqual(rblk.updated_at, blktime)
192
193 # Using Block to test Entity.type
194 blktime = blk.updated_at
195 time.sleep(1) # wait for time to change
196 blk.type = "updated"
197 # no update
198 self.assertNotEqual(blk.updated_at, blktime)
199
200 rblk = self.file.blocks["block"] # read through container
201 time.sleep(1) # wait for time to change
202 rblk.type = "updated again"
203 self.assertNotEqual(rblk.updated_at, blktime)
204
205 def test_timestamp_noautoupdate(self):
206 # Using Block to test Entity.definition
207 blk = self.file.create_block("block", "timetest")
208
209 # disable timestamp autoupdating
210 self.file.auto_update_timestamps = False
211 blktime = blk.updated_at
212 time.sleep(1) # wait for time to change
213 blk.definition = "update"
214 self.assertEqual(blk.updated_at, blktime)
215
216 rblk = self.file.blocks["block"] # read through container
217 rblktime = rblk.updated_at
218 time.sleep(1) # wait for time to change
219 rblk.definition = "time should change"
220 self.assertEqual(rblk.updated_at, rblktime)
221
222 blktime = blk.updated_at
223 time.sleep(1) # wait for time to change
224 blk.type = "update"
225 self.assertEqual(blk.updated_at, blktime)
226
227 rblk = self.file.blocks["block"] # read through container
228 rblktime = rblk.updated_at
229 time.sleep(1) # wait for time to change
230 rblk.type = "time should change"
231 self.assertEqual(rblk.updated_at, rblktime)
232
233
234 class TestFileVer(unittest.TestCase):
235
236 backend = "h5py"
237 filever = filepy.HDF_FF_VERSION
238 fformat = filepy.FILE_FORMAT
239
240 def try_open(self, mode):
241 nix_file = nix.File.open(self.testfilename, mode)
242 nix_file.close()
243
244 def set_header(self, fformat=None, version=None, fileid=None):
245 if fformat is None:
246 fformat = self.fformat
247 if version is None:
248 version = self.filever
249 if fileid is None:
250 fileid = nix.util.create_id()
251 self.h5root.attrs["format"] = fformat
252 self.h5root.attrs["version"] = version
253 self.h5root.attrs["id"] = fileid
254 self.h5root.attrs["created_at"] = 0
255 self.h5root.attrs["updated_at"] = 0
256 if "data" not in self.h5root:
257 self.h5root.create_group("data")
258 self.h5root.create_group("metadata")
259
260 def setUp(self):
261 self.tmpdir = TempDir("vertest")
262 self.testfilename = os.path.join(self.tmpdir.path, "vertest.nix")
263 self.h5file = h5py.File(self.testfilename, mode="w")
264 self.h5root = self.h5file["/"]
265
266 def tearDown(self):
267 self.h5file.close()
268 self.tmpdir.cleanup()
269
270 def test_read_write(self):
271 self.set_header()
272 self.try_open(nix.FileMode.ReadWrite)
273
274 def test_read_only(self):
275 ver_x, ver_y, ver_z = self.filever
276 roversion = (ver_x, ver_y, ver_z+2)
277 self.set_header(version=roversion)
278 self.try_open(nix.FileMode.ReadOnly)
279 with self.assertRaises(RuntimeError):
280 self.try_open(nix.FileMode.ReadWrite)
281
282 def test_no_open(self):
283 ver_x, ver_y, ver_z = self.filever
284 noversion = (ver_x, ver_y+3, ver_z+2)
285 self.set_header(version=noversion)
286 with self.assertRaises(RuntimeError):
287 self.try_open(nix.FileMode.ReadWrite)
288 with self.assertRaises(RuntimeError):
289 self.try_open(nix.FileMode.ReadOnly)
290 noversion = (ver_x, ver_y+1, ver_z)
291 self.set_header(version=noversion)
292 with self.assertRaises(RuntimeError):
293 self.try_open(nix.FileMode.ReadWrite)
294 with self.assertRaises(RuntimeError):
295 self.try_open(nix.FileMode.ReadOnly)
296 noversion = (ver_x+1, ver_y, ver_z)
297 self.set_header(version=noversion)
298 with self.assertRaises(RuntimeError):
299 self.try_open(nix.FileMode.ReadWrite)
300 with self.assertRaises(RuntimeError):
301 self.try_open(nix.FileMode.ReadOnly)
302
303 def test_bad_tuple(self):
304 self.set_header(version=(-1, -1, -1))
305 with self.assertRaises(RuntimeError):
306 self.try_open(nix.FileMode.ReadOnly)
307 self.set_header(version=(1, 2))
308 with self.assertRaises(RuntimeError):
309 self.try_open(nix.FileMode.ReadOnly)
310
311 def test_bad_format(self):
312 self.set_header(fformat="NOT_A_NIX_FILE")
313 with self.assertRaises(InvalidFile):
314 self.try_open(nix.FileMode.ReadOnly)
315
316 def test_bad_id(self):
317 self.set_header(fileid="")
318 with self.assertRaises(RuntimeError):
319 self.try_open(nix.FileMode.ReadOnly)
320
321 # empty file ID OK for versions older than 1.2.0
322 self.set_header(version=(1, 1, 1), fileid="")
323 self.try_open(nix.FileMode.ReadOnly)
324
325 self.set_header(version=(1, 1, 0), fileid="")
326 self.try_open(nix.FileMode.ReadOnly)
327
328 self.set_header(version=(1, 0, 0), fileid="")
329 self.try_open(nix.FileMode.ReadOnly)