Skip to content

Commit 6cccc23

Browse files
author
Luke Shaw
committed
Update example for hdf5 custom compression
1 parent ab05fdb commit 6cccc23

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

examples/blosc2_hdf5_compression.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
clevel = 5 # compression level, e.g., 0-9, where 0 is no compression and 9 is maximum compression
2121
fname_in = "kevlar.h5" # input file with the kevlar dataset
2222
fname_out = "kevlar-blosc2.h5"
23+
nframes = 1000
2324
if not os.path.exists(fname_in):
2425
raise FileNotFoundError(
2526
f"Input file {fname_in} does not exist\n"
@@ -30,9 +31,11 @@
3031
# Example 1
3132
# hdf5plugin supports limited blosc2 compression with certain codecs
3233
cname = "zstd"
33-
with h5py.File(fname_in, "r") as fr:
34-
dset = fr["/entry/data/data"][:]
35-
with h5py.File(fname_out, "w") as fw:
34+
35+
if not os.path.exists("STD" + fname_out):
36+
with h5py.File(fname_in, "r") as fr:
37+
dset = fr["/entry/data/data"][:nframes]
38+
with h5py.File("STD" + fname_out, "w") as fw:
3639
g = fw.create_group("/data")
3740
b2comp = hdf5plugin.Blosc2(cname=cname, clevel=clevel, filters=hdf5plugin.Blosc2.BITSHUFFLE)
3841
dset_out = g.create_dataset(
@@ -42,13 +45,13 @@
4245
chunks=(1,) + dset.shape[1:], # chunk size of 1 frame
4346
**b2comp,
4447
)
45-
print("Successfully compressed file with hdf5plugin")
48+
print("Successfully compressed file with hdf5plugin")
4649

4750
# Example 2
4851
# For other codecs (e.g grok) or for more custom compression such as with user-defined block shapes, one
4952
# has to use a more involved route
5053
blocks = (50, 80, 80)
51-
chunks = (200, 240, 240)
54+
chunks = (100, 240, 240)
5255
cparams = {
5356
"codec": blosc2.Codec.LZ4,
5457
"filters": [blosc2.Filter.BITSHUFFLE],
@@ -58,18 +61,17 @@
5861

5962
if os.path.exists("dset.b2nd"): # don't reload dset to blosc2 if already done so once
6063
b2im = blosc2.open(urlpath="dset.b2nd", mode="r")
61-
s, d = b2im.shape, b2im.dtype
6264
else:
6365
with h5py.File(fname_in, "r") as fr: # load file and process to blosc2 array
64-
dset = fr["/entry/data/data"][:]
66+
dset = fr["/entry/data/data"][:nframes]
6567
b2im = blosc2.asarray(
6668
dset, chunks=chunks, blocks=blocks, cparams=cparams, urlpath="dset.b2nd", mode="w"
6769
)
68-
d = dset.dtype
6970
del dset
7071

72+
s, d = b2im.shape, b2im.dtype
7173
# Write to .h5 file #
72-
with h5py.File(fname_out, "w") as fw:
74+
with h5py.File("Custom" + fname_out, "w") as fw:
7375
g = fw.create_group("/data")
7476
b2comp = hdf5plugin.Blosc2() # just for identification, no compression algorithm specified
7577
dset_out = g.create_dataset(

0 commit comments

Comments
 (0)