Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error while loading saved .npy in python. #23

Closed
asafben opened this issue Dec 25, 2017 · 3 comments
Closed

Error while loading saved .npy in python. #23

asafben opened this issue Dec 25, 2017 · 3 comments

Comments

@asafben
Copy link

asafben commented Dec 25, 2017

Hi,
Iv'e tried saving the following data type:
std::vector<std::vector > data;

as follows:
cnpy::npy_save(path, &data[0], {nz_, ny_, nx_}, "w");
(the outer vector is of size: nx_ * ny_ * nz_)

and while trying in python:
d = np.load('path/grid.npy')

i'm getting the following error:
Traceback (most recent call last):
File ".../lib/python3.5/site-packages/numpy/lib/format.py", line 510, in _read_array_header
dtype = numpy.dtype(d['descr'])
TypeError: data type "<?24" not understood

How (or should) I consider the size of the inner vector?
Should I load the .npy differently in Python?

Thank you for your help.

@chraibi
Copy link
Contributor

chraibi commented Jan 2, 2018

Please post a complete working example.

@rogersce
Copy link
Owner

rogersce commented Jan 3, 2018

Trying to directly load and save vectors of vectors isn't going to work -- the data isn't laid out contiguously in memory.

Please take a look at issue #20 for loading 2D data from file, into a vector of vectors.

To save a vector of vectors, you first need to copy it to a contiguous block of memory, e.g.

//data is a std::vector<std::vector<double>>. Each sub-vector should have the same size.
size_t N = data.size();
size_t M = data[0].size();

std::vector<double> raw(N*M);

for(int row = 0;row < N; row++) {
   for(int col = 0;col < M; col++) {
      raw[row*M+col] = data[row][col];
   }
}

cnpy::npy_save("arr1.npy",&raw[0],{N,M},"w");

@asafben
Copy link
Author

asafben commented Jan 3, 2018

Excellent! It was very helpful, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants