potocpav / npy-rs

NumPy file format (de-)serialization in Rust
30 stars 7 forks source link

Save Vec<f32> #3

Closed mariogeiger closed 7 years ago

mariogeiger commented 7 years ago

I dont manage to save a Vec<f32> with to_file because NpyData is not implemented for f32.

Is there a way to save a Vec<f32> such that when I load it from python it is a simple array of type np.float32 ?

potocpav commented 7 years ago

Serializing a simple float is not implemented (not hard to do), just structs. But you can convert your Vec<f32> into a vector of structs that derives the NpyData trait. For example (may not compile, I'm on mobile):

#[derive(NpyData)]
struct Data {
    field: f32,
}

It is possible to unsafely transmute the Vec<f32> into Vec<Data> or just copy it over. Vec<Data> can be serialized. It will create a numpy structured array, which can be converted to np.float32 easily.

potocpav commented 7 years ago

Closing. If the suggested approach doesn't work, feel free to re-open the issue.