"""
An example of using MultiBlockSlice to interleave frames in a virtual dataset.
"""
import h5py
files = ["1.h5", "2.h5", "3.h5", "4.h5"]
dataset_name = "data"
dtype = "float"
source_shape = (25000, 256, 512)
target_shape = (100000, 256, 512)
block_size = 1000
v_layout = h5py.VirtualLayout(shape=target_shape, dtype=dtype)
for file_idx, file_path in enumerate(files):
v_source = h5py.VirtualSource(
file_path, name=dataset_name, shape=source_shape, dtype=dtype
)
dataset_frames = v_source.shape[0]
start = file_idx * block_size
stride = len(files) * block_size
count = dataset_frames // block_size
block = block_size
v_layout[h5py.MultiBlockSlice(start, stride, count, block), :, :] = v_source
with h5py.File("interleave_vds.h5", "w", libver="latest") as f:
f.create_virtual_dataset(dataset_name, v_layout, fillvalue=0)