jturner314 / ndarray-npy

.npy and .npz file format support for ndarray
https://docs.rs/ndarray-npy
Apache License 2.0
56 stars 18 forks source link

Err(WrongDescriptor) with complex arrays #70

Closed mdhoney closed 1 year ago

mdhoney commented 1 year ago

I'm trying to read in a .npy file previously written with Python:

>>> rB1D_holo_pad = np.load("rB1D_holo_pad.npy")
>>> rB1D_holo_pad.shape
(1024,)
>>> rB1D_holo_pad.dtype
dtype('complex64')

Unfortunately, using read_npy gives me a "WrongDescriptor" error that I don't understand: Err(WrongDescriptor(String("<c8")))

Here is my code:

use ndarray::prelude::*;
use num::complex::Complex;
use ndarray_npy::{read_npy, ReadNpyError};

fn main() {
    let rB1D_holo_pad: Result<Array1<Complex<f64>>, ReadNpyError> = read_npy("rB1D_holo_pad.npy");
    println!("{:?}", rB1D_holo_pad);
}

ndarray-npy should be able to read Complex<f64> ndarrays, right?

jturner314 commented 1 year ago

numpy.complex64 corresponds to Complex<f32>, and numpy.complex128 corresponds to Complex<f64>. If you change your code to let rB1D_holo_pad: Result<Array1<Complex<f32>>, ReadNpyError> = ..., it should work.

The error message is saying that the type descriptor in the header of the .npy file is the string "<c8", which doesn't match the Complex<f64> element type. The string type descriptors are described on NumPy's data type objects documentation page.

mdhoney commented 1 year ago

numpy.complex64 corresponds to Complex<f32>, and numpy.complex128 corresponds to Complex<f64>. If you change your code to let rB1D_holo_pad: Result<Array1<Complex<f32>>, ReadNpyError> = ..., it should work.

It did! Thanks for helping me catch this beginner's mistake @jturner314 :smile:

jturner314 commented 1 year ago

Okay, great! I'm closing this issue as completed.