The bitshuffle.h5 module provides convenience functions for creating datasets with Bitshuffle compression. For h5py version 2.5.0 and later, you can use the high-level create_dataset interface.
Bitshuffle is identified by the HDF5 filter number 32008. To achieve actual compression (rather than just bit-rearrangement), you must pair Bitshuffle with a compression algorithm like LZ4 or ZSTD via compression_opts.
Compression Options Format:
- LZ4:
(block_size, bitshuffle.h5.H5_COMPRESS_LZ4) - ZSTD:
(block_size, bitshuffle.h5.H5_COMPRESS_ZSTD, COMP_LVL)
Note: Setting block_size = 0 lets Bitshuffle choose its own value.
import h5py
import numpy
import bitshuffle.h5
filename = "example.h5"
f = h5py.File(filename, "w")
# block_size = 0 lets Bitshuffle choose its value
block_size = 0
dataset = f.create_dataset(
"data",
(100, 100, 100),
compression=bitshuffle.h5.H5FILTER,
compression_opts=(block_size, bitshuffle.h5.H5_COMPRESS_LZ4),
dtype='float32',
)
# create some random data
array = numpy.random.rand(100, 100, 100).astype('float32')
dataset[:] = array
f.close()