'How do I read an .npy file that contains a string?
I have a .npy file which contains tuples of one string (|S8) and 6 float values. I want to read the .npy in Rust as a vector of tuples.
I tried the npyz crate:
use npyz;
fn main() {
read_depl_npy();
}
fn read_depl_npy() -> Result<(), Box<dyn std::error::Error>> {
let bytes = std::fs::read("test-data/test.npy")?;
let npy = npyz::NpyFile::new(&bytes[..])?;
println!("{:?}", npy.into_vec::<Depl>());
Ok(())
}
#[derive(npyz::Deserialize, Debug)]
struct Depl {
name: String,
a: f64,
b: f64,
c: f64,
d: f64,
e: f64,
f: f64,
}
I get this error:
the trait `Deserialize` is not implemented for `String`
Are there any other solutions or modification to this code that I could use?
I tried to open the .npy in Python and send it to Rust using pyo3 by creating a Python module with Rust, but it's too inefficient. I wonder if Calling Python in Rust code would do any better.
Upadte:
I'm not in control of the data in the .npy file. It is given to me as is. Here are the first 8 items in the file :
0000 = {void} (b'N1 ', 1.15423654e-05, nan, -8.63531175e-06, 0.00234345, -1.93959406e-05, nan)
0001 = {void} (b'N10 ', nan, 0.00014046, 9.3921465e-07, nan, -1.36987648e-05, -0.00021798)
0002 = {void} (b'N100 ', -2.95802408e-06, 5.02222077e-05, 5.02908617e-07, 0.00222162, nan, 0.00015162)
0003 = {void} (b'N1000 ', 1.11732508e-06, 0.00018788, nan, 0.00098555, -6.56358132e-06, -0.00021724)
0004 = {void} (b'N1001 ', -1.07967489e-06, 0.0001863, -3.29593367e-07, 0.00098565, nan, -0.00021703)
0005 = {void} (b'N1002 ', nan, 0.00018486, -4.39249772e-07, 0.00098573, -6.54476282e-06, -0.00021686)
0006 = {void} (b'N1003 ', -1.01067021e-06, 0.00018347, 8.16061298e-07, 0.00098576, -6.56198811e-06, -0.00021675)
0007 = {void} (b'N1004 ', 1.03888923e-18, 0.00016245, -2.65077262e-06, 0.0016541, -1.13024989e-05, -0.00022285)
0008 = {void} (b'N1005 ', 2.02031333e-18, 0.00016073, -1.84684389e-06, 0.00165515, -1.13003433e-05, -0.00022227)
And the data type:
[('name', 'S8'), ('a', '<f8'), ('b', '<f8'), ('c', '<f8'), ('d', '<f8'), ('e', '<f8'), ('f', '<f8')]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
