| 1 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 2 |
|
| 3 |
import unittest |
| 4 |
import os |
| 5 |
import glob |
| 6 |
|
| 7 |
import numpy as np |
| 8 |
import h5py |
| 9 |
import pytest |
| 10 |
from h5py import h5z |
| 11 |
|
| 12 |
from bitshuffle import h5, __zstd__ |
| 13 |
|
| 14 |
|
| 15 |
os.environ["HDF5_PLUGIN_PATH"] = "" |
| 16 |
|
| 17 |
|
| 18 |
class TestFilter(unittest.TestCase): |
| 19 |
def test_filter(self): |
| 20 |
shape = (32 * 1024 + 783,) |
| 21 |
chunks = (4 * 1024 + 23,) |
| 22 |
dtype = np.int64 |
| 23 |
data = np.arange(shape[0]) |
| 24 |
fname = "tmp_test_filters.h5" |
| 25 |
f = h5py.File(fname, "w") |
| 26 |
h5.create_dataset( |
| 27 |
f, |
| 28 |
b"range", |
| 29 |
shape, |
| 30 |
dtype, |
| 31 |
chunks, |
| 32 |
filter_pipeline=(32008, 32000), |
| 33 |
filter_flags=(h5z.FLAG_MANDATORY, h5z.FLAG_MANDATORY), |
| 34 |
filter_opts=None, |
| 35 |
) |
| 36 |
f["range"][:] = data |
| 37 |
|
| 38 |
f.close() |
| 39 |
|
| 40 |
f = h5py.File(fname, "r") |
| 41 |
d = f["range"][:] |
| 42 |
self.assertTrue(np.all(d == data)) |
| 43 |
f.close() |
| 44 |
|
| 45 |
def test_with_block_size(self): |
| 46 |
shape = (128 * 1024 + 783,) |
| 47 |
chunks = (4 * 1024 + 23,) |
| 48 |
dtype = np.int64 |
| 49 |
data = np.arange(shape[0]) |
| 50 |
fname = "tmp_test_filters.h5" |
| 51 |
f = h5py.File(fname, "w") |
| 52 |
h5.create_dataset( |
| 53 |
f, |
| 54 |
b"range", |
| 55 |
shape, |
| 56 |
dtype, |
| 57 |
chunks, |
| 58 |
filter_pipeline=(32008, 32000), |
| 59 |
filter_flags=(h5z.FLAG_MANDATORY, h5z.FLAG_MANDATORY), |
| 60 |
filter_opts=((680,), ()), |
| 61 |
) |
| 62 |
f["range"][:] = data |
| 63 |
|
| 64 |
f.close() |
| 65 |
# os.system('h5dump -H -p tmp_test_filters.h5') |
| 66 |
|
| 67 |
f = h5py.File(fname, "r") |
| 68 |
d = f["range"][:] |
| 69 |
self.assertTrue(np.all(d == data)) |
| 70 |
f.close() |
| 71 |
|
| 72 |
def test_with_lz4_compression(self): |
| 73 |
shape = (128 * 1024 + 783,) |
| 74 |
chunks = (4 * 1024 + 23,) |
| 75 |
dtype = np.int64 |
| 76 |
data = np.arange(shape[0]) |
| 77 |
fname = "tmp_test_filters.h5" |
| 78 |
f = h5py.File(fname, "w") |
| 79 |
h5.create_dataset( |
| 80 |
f, |
| 81 |
b"range", |
| 82 |
shape, |
| 83 |
dtype, |
| 84 |
chunks, |
| 85 |
filter_pipeline=(32008,), |
| 86 |
filter_flags=(h5z.FLAG_MANDATORY,), |
| 87 |
filter_opts=((0, h5.H5_COMPRESS_LZ4),), |
| 88 |
) |
| 89 |
f["range"][:] = data |
| 90 |
|
| 91 |
f.close() |
| 92 |
# os.system('h5dump -H -p tmp_test_filters.h5') |
| 93 |
|
| 94 |
f = h5py.File(fname, "r") |
| 95 |
d = f["range"][:] |
| 96 |
self.assertTrue(np.all(d == data)) |
| 97 |
f.close() |
| 98 |
|
| 99 |
@pytest.mark.skipif( |
| 100 |
__zstd__ is False, |
| 101 |
reason="Bitshuffle has not been built with ZSTD support.", |
| 102 |
) |
| 103 |
def test_with_zstd_compression(self): |
| 104 |
shape = (128 * 1024 + 783,) |
| 105 |
chunks = (4 * 1024 + 23,) |
| 106 |
compression_lvl = 10 |
| 107 |
dtype = np.int64 |
| 108 |
data = np.arange(shape[0]) |
| 109 |
fname = "tmp_test_filters.h5" |
| 110 |
f = h5py.File(fname, "w") |
| 111 |
h5.create_dataset( |
| 112 |
f, |
| 113 |
b"range", |
| 114 |
shape, |
| 115 |
dtype, |
| 116 |
chunks, |
| 117 |
filter_pipeline=(32008,), |
| 118 |
filter_flags=(h5z.FLAG_MANDATORY,), |
| 119 |
filter_opts=((0, h5.H5_COMPRESS_ZSTD, compression_lvl),), |
| 120 |
) |
| 121 |
f["range"][:] = data |
| 122 |
|
| 123 |
f.close() |
| 124 |
# os.system('h5dump -H -p tmp_test_filters.h5') |
| 125 |
|
| 126 |
f = h5py.File(fname, "r") |
| 127 |
d = f["range"][:] |
| 128 |
self.assertTrue(np.all(d == data)) |
| 129 |
f.close() |
| 130 |
|
| 131 |
def tearDown(self): |
| 132 |
files = glob.glob("tmp_test_*") |
| 133 |
for f in files: |
| 134 |
os.remove(f) |
| 135 |
|
| 136 |
|
| 137 |
if __name__ == "__main__": |
| 138 |
unittest.main() |