Created
February 17, 2022 18:56
-
-
Save mkitti/f44b8a8598470497d46bede964b3abc8 to your computer and use it in GitHub Desktop.
Reading raw ZStandard (Zstd) chunks from a HDF5 file via numcodecs
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 h5py | |
import hdf5plugin | |
import numcodecs | |
import numpy as np | |
# Create data | |
A = np.random.randint(1024, size=(1024, 1024, 128)) | |
print("Data as created:") | |
print(A[1, 1, :10]) | |
print() | |
# Write a HDF5 file | |
f = h5py.File("zstd_chunks.h5", "w") | |
f.create_dataset( | |
"zstd_ds", | |
data=A, | |
chunks=(128, 128, 128), | |
**hdf5plugin.Zstd() | |
) | |
f.close() | |
# Read the HDF5 file | |
f = h5py.File("zstd_chunks.h5", "r") | |
dsetid = f["zstd_ds"].id | |
compressed_bytes = dsetid.read_direct_chunk((0,0,0))[1] | |
print("Data as read by h5py/HDF5 via plugin:") | |
print(f["zstd_ds"][1, 1, :10]) | |
print() | |
# Use numcodecs to decompress | |
raw_bytes = numcodecs.zstd.decompress(compressed_bytes) | |
B = np.frombuffer(raw_bytes, dtype=dsetid.dtype).reshape(128, 128, 128) | |
print("Data decompressed via numcodecs:") | |
print(B[1, 1, :10]) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment