Created
June 24, 2021 23:54
-
-
Save ajelenak/997c9ab4879a3a66fb5720e90d36c79e to your computer and use it in GitHub Desktop.
How to compute and display compression ratios of HDF5 datasets in an HDF5 file using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import h5py | |
def comp_ratio(name, obj): | |
if isinstance(obj, h5py.Dataset) and obj.chunks is not None: | |
if obj.id.get_create_plist().get_nfilters(): | |
stor_size = obj.id.get_storage_size() | |
if stor_size != 0: | |
ratio = float(obj.nbytes) / float(stor_size) | |
print(f'Compression ratio for "{obj.name}": {ratio}') | |
if len(sys.argv) > 1: | |
with h5py.File(sys.argv[1], mode='r') as h5f: | |
print(f'Compression ratios for datasets in HDF5 file: {h5f.filename}') | |
h5f.visititems(comp_ratio) | |
else: | |
print('Usage: h5comprat.py <HDF5 file path>') | |
raise SystemExit('No HDF5 file path given') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment