You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have adjusted an example to the following to generate a h5 file which contains a 1d array:
use hdf5::{File,H5Type,Result};use ndarray::{arr1, s};#[derive(H5Type,Clone,PartialEq,Debug)]// register with HDF5#[repr(u8)]pubenumColor{R = 1,G = 2,B = 3,}#[derive(H5Type,Clone,PartialEq,Debug)]// register with HDF5#[repr(C)]pubstructPixel{x:i64,y:i64,color:Color,}implPixel{pubfnnew(x:i64,y:i64,color:Color) -> Self{Self{ x, y, color }}}fnwrite_hdf5() -> Result<()>{useColor::*;let file = File::create("pixels.h5")?;// open for writinglet group = file.create_group("dir")?;// create a grouplet builder = group.new_dataset_builder();let ds = builder
.with_data(&arr1(&[Pixel::new(1,2,R),Pixel::new(2,3,B),Pixel::new(3,4,G),Pixel::new(4,5,R),Pixel::new(5,6,B),Pixel::new(6,7,G),]))// finalize and write the dataset.create("pixels")?;Ok(())}fnread_hdf5() -> Result<()>{useColor::*;let file = File::open("pixels.h5")?;// open for readinglet ds = file.dataset("dir/pixels")?;// open the dataset// How can I read all `x` of Pixels?assert_eq!(
ds.read_slice::<Pixel, _, _>(s![2..])?,
arr1(&[Pixel::new(3,4,G),Pixel::new(4,5,R),Pixel::new(5,6,B),Pixel::new(6,7,G),]));Ok(())}fnmain() -> Result<()>{write_hdf5()?;read_hdf5()?;Ok(())}
Question
In python I can do this to read all x values:
importh5pyf=h5py.File("pixels.h5")
f["dir/pixels"]['x'] # read all x values from pixels
What's hdf5-rust way to achieve the same behavior? Sorry that I have no luck to find a proper solution.
The text was updated successfully, but these errors were encountered:
WindSoilder
changed the title
Question: how can I select a field of h5 dataset
Question: how can I select a field from h5 dataset
Jan 15, 2024
This crate does not support reading just a single compund field (PRs welcome) as one can do in h5py. You will have to read the compund type/struct and extract after reading.
If you are in charge of data creation I would reccommend storing each field as a dataset if it makes sense for your application. SoA (struct of arrays) may be beneficial over AoS (array of structs).
Background code
I have adjusted an example to the following to generate a h5 file which contains a 1d array:
Question
In python I can do this to read all
x
values:What's
hdf5-rust
way to achieve the same behavior? Sorry that I have no luck to find a proper solution.The text was updated successfully, but these errors were encountered: