hdf5plugin packages a set of HDF5 compression filters (namely: blosc, bitshuffle, lz4, FCIDECOMP, ZFP, Zstandard) and makes them usable from the Python programming language through h5py.
h5py is a thin, pythonic wrapper around HDF5.
Presenter: Thomas VINCENT
European HDF5 User Group Meeting 2022, May 31, 2022
from h5glance import H5Glance # Browsing HDF5 files
H5Glance("data.h5")
import h5py # Pythonic HDF5 wrapper: https://docs.h5py.org/
h5file = h5py.File("data.h5", mode="r") # Open HDF5 file in read mode
data = h5file["/data"][()] # Access HDF5 dataset "/data"
imshow(data) # Display data
data = h5file["/compressed_data"][()] # Access compressed dataset
--------------------------------------------------------------------------- OSError Traceback (most recent call last) Input In [4], in <cell line: 1>() ----> 1 data = h5file["/compressed_data"][()] File h5py/_objects.pyx:54, in h5py._objects.with_phil.wrapper() File h5py/_objects.pyx:55, in h5py._objects.with_phil.wrapper() File ~/venv/ub20.04/lib/python3.8/site-packages/h5py/_hl/dataset.py:741, in Dataset.__getitem__(self, args, new_dtype) 739 if self._fast_read_ok and (new_dtype is None): 740 try: --> 741 return self._fast_reader.read(args) 742 except TypeError: 743 pass # Fall back to Python read pathway below File h5py/_selector.pyx:370, in h5py._selector.Reader.read() OSError: Can't read data (can't open directory: /usr/local/hdf5/lib/plugin)
hdf5plugin
usage¶To enable reading compressed datasets not supported by libHDF5
and h5py
:
Install hdf5plugin & import it.
%%bash
pip3 install hdf5plugin
Or: conda install -c conda-forge hdf5plugin
import hdf5plugin
data = h5file["/compressed_data"][()] # Access datset
imshow(data) # Display data
h5file.close() # Close the HDF5 file
When writing datasets with h5py
, compression can be specified with: h5py.Group.create_dataset
# Create a dataset with h5py without compression
h5file = h5py.File("new_file_uncompressed.h5", mode="w")
h5file.create_dataset("/data", data=data)
h5file.close()
# Create a compressed dataset
h5file = h5py.File("new_file_blosc_bitshuffle_lz4.h5", mode="w")
h5file.create_dataset(
"/compressed_data",
data=data,
compression=32001, # blosc HDF5 filter identifier
# options: 0, 0, 0, 0, level, shuffle, compression
compression_opts=(0, 0, 0, 0, 5, 2, 1)
)
h5file.close()
hdf5plugin
provides some helpers to ease dealing with compression filter and options:
h5file = h5py.File("new_file_blosc_bitshuffle_lz4.h5", mode="w")
h5file.create_dataset(
"/compressed_data",
data=data,
**hdf5plugin.Blosc(
cname='lz4',
clevel=5,
shuffle=hdf5plugin.Blosc.BITSHUFFLE),
)
h5file.close()
help(hdf5plugin.Blosc)
Help on class Blosc in module hdf5plugin: class Blosc(h5py._hl.filters.FilterRefBase) | Blosc(cname='lz4', clevel=5, shuffle=1) | | ``h5py.Group.create_dataset``'s compression arguments for using blosc filter. | | It can be passed as keyword arguments: | | .. code-block:: python | | f = h5py.File('test.h5', 'w') | f.create_dataset( | 'blosc_byte_shuffle_blosclz', | data=numpy.arange(100), | **hdf5plugin.Blosc(cname='blosclz', clevel=9, shuffle=hdf5plugin.Blosc.SHUFFLE)) | f.close() | | :param str cname: | `blosclz`, `lz4` (default), `lz4hc`, `zlib`, `zstd` | Optional: `snappy`, depending on compilation (requires C++11). | :param int clevel: | Compression level from 0 (no compression) to 9 (maximum compression). | Default: 5. | :param int shuffle: One of: | - Blosc.NOSHUFFLE (0): No shuffle | - Blosc.SHUFFLE (1): byte-wise shuffle (default) | - Blosc.BITSHUFFLE (2): bit-wise shuffle | | Method resolution order: | Blosc | h5py._hl.filters.FilterRefBase | collections.abc.Mapping | collections.abc.Collection | collections.abc.Sized | collections.abc.Iterable | collections.abc.Container | builtins.object | | Methods defined here: | | __init__(self, cname='lz4', clevel=5, shuffle=1) | Initialize self. See help(type(self)) for accurate signature. | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | BITSHUFFLE = 2 | | NOSHUFFLE = 0 | | SHUFFLE = 1 | | __abstractmethods__ = frozenset() | | filter_id = 32001 | | ---------------------------------------------------------------------- | Methods inherited from h5py._hl.filters.FilterRefBase: | | __eq__(self, other) | Return self==value. | | __getitem__(self, item) | | __hash__(self) | Return hash(self). | | __iter__(self) | | __len__(self) | | ---------------------------------------------------------------------- | Data descriptors inherited from h5py._hl.filters.FilterRefBase: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes inherited from h5py._hl.filters.FilterRefBase: | | filter_options = () | | ---------------------------------------------------------------------- | Methods inherited from collections.abc.Mapping: | | __contains__(self, key) | | get(self, key, default=None) | D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. | | items(self) | D.items() -> a set-like object providing a view on D's items | | keys(self) | D.keys() -> a set-like object providing a view on D's keys | | values(self) | D.values() -> an object providing a view on D's values | | ---------------------------------------------------------------------- | Data and other attributes inherited from collections.abc.Mapping: | | __reversed__ = None | | ---------------------------------------------------------------------- | Class methods inherited from collections.abc.Collection: | | __subclasshook__(C) from abc.ABCMeta | Abstract classes can override this to customize issubclass(). | | This is invoked early on by abc.ABCMeta.__subclasscheck__(). | It should return True, False or NotImplemented. If it returns | NotImplemented, the normal algorithm is used. Otherwise, it | overrides the normal algorithm (and the outcome is cached).
H5Glance("new_file_blosc_bitshuffle_lz4.h5")
h5file = h5py.File("new_file_blosc_bitshuffle_lz4.h5", mode="r")
imshow(h5file["/compressed_data"][()])
h5file.close()
!ls -sh new_file*.h5
3.4M new_file_blosc_bitshuffle_lz4.h5 3.7M new_file_uncompressed.h5
h5py
¶Compression filters provided by h5py:
libhdf5
: "gzip" and eventually "szip" (optional)h5py
: "lzf"Pre-compression filter: Byte-Shuffle
h5file = h5py.File("new_file_shuffle_gzip.h5", mode="w")
h5file.create_dataset(
"/compressed_data_shuffle_gzip", data=data, shuffle=True, compression="gzip")
h5file.close()
hdf5plugin
¶Additional compression filters provided by hdf5plugin
: Bitshuffle, Blosc, FciDecomp, LZ4, ZFP, Zstandard.
6 out of the 28 HDF5 registered filter plugins as of May 2022.
h5file = h5py.File("new_file_bitshuffle_lz4.h5", mode="w")
h5file.create_dataset(
"/compressed_data_bitshuffle_lz4",
data=data,
**hdf5plugin.Bitshuffle()
)
h5file.close()
blosclz
, lz4
, lz4hc
, snappy
(optional, requires C++11), zlib
, zstd
Blosc
includes pre-compression filters and algorithms provided by other HDF5 compression filters:
Blosc(..., shuffle=Blosc.SHUFFLE)
Bitshuffle()
=> Blosc("lz4", 5, Blosc.BITSHUFFLE)
LZ4()
=> Blosc("lz4", 9)
Zstd()
=> Blosc("zstd", 2)
(u)int8
or (u)int16
float32
, float64
, (u)int32
, (u)int64
hdf5plugin
built from sourceOMP_NUM_THREADS=1
)Some filters can use multithreading:
BLOSC_NTHREADS
environment variableOMP_NUM_THREADS
environment variableHaving different pre-compression filters and compression algorithms at hand offer different read/write speed versus compression rate (and eventually error rate) trade-offs.
Also to keep in mind availability/compatibility: "gzip"
as included in libHDF5
is the most compatible one (and also "lzf"
as included in h5py
).
hdf5plugin
filters with other applications¶Set the HDF5_PLUGIN_PATH
environment variable to: hdf5plugin.PLUGINS_PATH
%%bash
export HDF5_PLUGIN_PATH=`python3 -c "
import hdf5plugin; print(hdf5plugin.PLUGINS_PATH)"`
echo "HDF5_PLUGIN_PATH=${HDF5_PLUGIN_PATH}"
ls ${HDF5_PLUGIN_PATH}
HDF5_PLUGIN_PATH=/home/esrf/tvincent/venv/ub20.04/lib/python3.8/site-packages/hdf5plugin/plugins libh5blosc.so libh5bshuf.so libh5fcidecomp.so libh5lz4.so libh5zfp.so libh5zstd.so
Note: Only works for reading compressed datasets, not for writing!
hdf5plugin
license¶The source code of hdf5plugin
itself is licensed under the MIT license...
It also embeds the source code of the provided compression filters and libraries which are licensed under different open-source licenses (Apache, BSD-2, BSD-3, MIT, Zlib...) and copyrights.
Some limitations of current HDF5 compression filters:
Direct chunk access offers a way to improve performance/flexibility, at the expense of more code on the user side
hdf5plugin
relies on a "hack" to ease the installation of HDF5 compression for Python environments
Most of the compression filters provided by hdf5plugin
are included in blosc (or blosc-2)
hdf5plugin
provides additional HDF5 compression filters (namely: Bitshuffle
, Blosc
, FciDecomp
, LZ4
, ZFP
, Zstandard
) mainly for use with h5py.
Credits to hdf5plugin contributors: Thomas Vincent, Armando Sole, Mark Kittisopikul, \@Florian-toll, Jerome Kieffer, \@fpwg, \@Anthchirp, \@mobiusklein, \@junyuewang and to all contributors of embedded libraries.
Partially funded by the PaNOSC EU-project.
This project has received funding from the European Union’s Horizon 2020 research and innovation programme under grant agreement No. 823852.