H5Writer#
- class pyvisgen.io.H5Writer(output_path: Path, dataset_type: str, **kwargs)[source]#
Bases:
DataWriterHDF5 file writer for pyvisgen datasets.
This writer saves data arrays to HDF5 files using the h5py library. Each sample is written to a separate
.h5file. The writer automatically crops images to half their height with a small overlap and validates array shapes before writing.- Parameters:
- output_pathstr or Path
Directory path where HDF5 files will be written.
- dataset_typestr
Type of dataset being written (e.g., ‘train’, ‘test’, ‘validation’). This is used in the output filename pattern.
Examples
>>> writer = H5Writer(output_path="./data", dataset_type="train") >>> writer.write(x_data, y_data, index=0)
Or as a context manager:
>>> rng = np.random.default_rng() >>> >>> with H5Writer(output_path="./data", dataset_type="train") as writer: ... x_data = rng.uniform(size=(5, 10, 2, 256, 256)) ... y_data = rng.uniform(size=(5, 10, 2, 256, 256)) ... ... for bundle_id, (x, y) in enumerate(zip(x_data, y_data)): ... writer.write(x, y, index=bundle_id)
Methods Summary
write(x, y, index[, name_x, name_y, overlap])Write FFT pair data to an HDF5 file.
Methods Documentation
- write(x, y, index, name_x='x', name_y='y', overlap: int = 5, **kwargs) None[source]#
Write FFT pair data to an HDF5 file.
Creates a new HDF5 file for each sample with pattern
samp_{dataset_type}_{index}.h5. The input arrays are cropped to half their height (with 5 pixel overlap) and validated before writing.- Parameters:
- xnp.ndarray
First array of the FFT pair with shape (batch, 2, height, width). Expected to have 4 dimensions with axis 1 of size 2.
- ynp.ndarray
Second array of the FFT pair with shape (batch, 2, height, width). Expected to have 4 dimensions with axis 1 of size 2.
- indexint
Bundle index used in the output filename.
- overlapint, optional
Overlap parameter for extracting half-images. Default: 5.
- name_xstr, optional
Key of the dataset for x array in the HDF5 file. Default:
"x".- name_ystr, optional
Key of the dataset for y array in the HDF5 file. Default:
"y".
- Raises:
- ValueError
If x or y arrays don’t have the expected shape (4 dimensions with axis 1 of size 2).
Examples
>>> rng = np.random.default_rng() >>> >>> with H5Writer(output_path="./data", dataset_type="train") as writer: ... x_data = rng.uniform(size=(5, 10, 2, 256, 256)) ... y_data = rng.uniform(size=(5, 10, 2, 256, 256)) ... ... for bundle_id, (x, y) in enumerate(zip(x_data, y_data)): ... writer.write(x, y, index=bundle_id)