Debian Codegraph @

nixio/test/test_compression.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 nixio as nix
11 import unittest
12 from .tmp import TempDir
13
14
15 def compr_enabled(da):
16 grp = da._h5group.group
17 h5data = grp.require_dataset("data", shape=(1,), dtype=int)
18 return h5data.compression == "gzip"
19
20
21 class TestCompression(unittest.TestCase):
22
23 def setUp(self):
24 self.tmpdir = TempDir("compressiontest")
25 self.testfilename = os.path.join(self.tmpdir.path,
26 "compressiontest.nix")
27
28 def tearDown(self):
29 pass
30
31 def test_compress_dataarray(self):
32
33 exception = ValueError("Unknown compression value in test")
34 for filecompr in nix.Compression:
35 nf = nix.File.open(self.testfilename, nix.FileMode.Overwrite,
36 compression=filecompr)
37 for blockcompr in nix.Compression:
38 block = nf.create_block("block-{}".format(blockcompr),
39 "block", compression=blockcompr)
40 for dacompr in nix.Compression:
41 da = block.create_data_array("da-{}".format(dacompr),
42 "data", data=[0],
43 compression=dacompr)
44
45 if dacompr == nix.Compression.No:
46 comprenabled = False
47 elif dacompr == nix.Compression.DeflateNormal:
48 comprenabled = True
49 elif dacompr == nix.Compression.Auto:
50 # inherited from Block setting
51 if blockcompr == nix.Compression.No:
52 comprenabled = False
53 elif blockcompr == nix.Compression.DeflateNormal:
54 comprenabled = True
55 elif blockcompr == nix.Compression.Auto:
56 # inherited from File setting
57 if filecompr == nix.Compression.No:
58 comprenabled = False
59 elif filecompr == nix.Compression.DeflateNormal:
60 comprenabled = True
61 elif filecompr == nix.Compression.Auto:
62 comprenabled = False
63 else:
64 raise exception
65 else:
66 raise exception
67 else:
68 raise exception
69
70 errmsg = ("Compression: File [{}] Block [{}] Data [{}] "
71 "Expected compression {}".format(
72 filecompr, blockcompr, dacompr, comprenabled
73 ))
74 self.assertEqual(compr_enabled(da), comprenabled, errmsg)
75 nf.close()