hdf5-rust icon indicating copy to clipboard operation
hdf5-rust copied to clipboard

Store num_complex datatypes

Open Rikorose opened this issue 3 years ago • 2 comments

Hi,

I am wondering what the best way is to store a Complex<f32> in a hdf5 dataset.

Since the type is defined in the num_complex crate, I cannot easily derive H5Type. Do I need to define a wrapper type? Or, can this be added to hdf5-rust maybe as a Cargo feature?

What other options do I have?

Rikorose avatar Mar 31 '22 20:03 Rikorose

The best way depends a lot on your application. Some prefer having the fields in different datasets, others do this through a packed struct. Since Complex is c-compatible with _Complex it would be a good idea to add this to this crate as an optional dependency. Would you like to make a PR?

mulimoen avatar Apr 06 '22 13:04 mulimoen

Since I want to store a ArrayDyn<Complex>, I don't see an Option to pack a struct efficiently.

I used this workaround which also requires an additional copy to make real/imag contiguous:

fn store_cplx(&self, grp: &Group, data: ArrayViewD<Complex32>) -> Result<()> {
    let complex = data.split_complex();
    grp.new_dataset_builder().with_data(complex.re.as_standard_layout().view()).create("re")?;
    grp.new_dataset_builder().with_data(complex.im.as_standard_layout().view()).create("im")?;
    Ok(())

Would you like to make a PR?

Sure, will try.

Rikorose avatar Apr 06 '22 15:04 Rikorose