Debian Codegraph @

tests/make_regression_tdata.py

raw
1 """
2 Script to create data used for regression testing.
3
4 """
5
6 import numpy as np
7 from numpy import random
8 import h5py
9
10 import bitshuffle
11 from bitshuffle import h5
12 from h5py import h5z
13
14 BLOCK_SIZE = 64 # Smallish such that datasets have many blocks but are small.
15 COMP_LVL = 10 # ZSTD compression level
16 FILTER_PIPELINE = [h5.H5FILTER]
17 FILTER_OPTS = [
18 [(BLOCK_SIZE, h5.H5_COMPRESS_LZ4)],
19 [(BLOCK_SIZE, h5.H5_COMPRESS_ZSTD, COMP_LVL)],
20 ]
21
22 OUT_FILE = "tests/data/regression_%s.h5" % bitshuffle.__version__
23
24 DTYPES = ["a1", "a2", "a3", "a4", "a6", "a8", "a10"]
25
26 f = h5py.File(OUT_FILE, "w")
27 g_orig = f.create_group("original")
28 g_comp_lz4 = f.create_group("compressed")
29 g_comp_zstd = f.create_group("compressed_zstd")
30
31 for dtype in DTYPES:
32 for rep in ["a", "b", "c"]:
33 dset_name = "%s_%s" % (dtype, rep)
34 dtype = np.dtype(dtype)
35 n_elem = 3 * BLOCK_SIZE + random.randint(0, BLOCK_SIZE)
36 shape = (n_elem,)
37 chunks = shape
38 data = random.randint(0, 255, n_elem * dtype.itemsize)
39 data = data.astype(np.uint8).view(dtype)
40
41 g_orig.create_dataset(dset_name, data=data)
42
43 # Create LZ4 compressed data
44 h5.create_dataset(
45 g_comp_lz4,
46 bytes(dset_name, "utf-8"),
47 shape,
48 dtype,
49 chunks=chunks,
50 filter_pipeline=FILTER_PIPELINE,
51 filter_flags=(h5z.FLAG_MANDATORY,),
52 filter_opts=FILTER_OPTS[0],
53 )
54 g_comp_lz4[dset_name][:] = data
55
56 # Create ZSTD compressed data
57 h5.create_dataset(
58 g_comp_zstd,
59 bytes(dset_name, "utf-8"),
60 shape,
61 dtype,
62 chunks=chunks,
63 filter_pipeline=FILTER_PIPELINE,
64 filter_flags=(h5z.FLAG_MANDATORY,),
65 filter_opts=FILTER_OPTS[1],
66 )
67 g_comp_zstd[dset_name][:] = data
68
69 f.close()