rogersce / cnpy

library to read/write .npy and .npz files in C/C++
MIT License
1.34k stars 301 forks source link

problem writing npy formatted output #38

Open ghost opened 6 years ago

ghost commented 6 years ago

I am writing two npy formatted files in my C++ program as follows:

vector < vector < int> > vhist;
vector < double > vrmsd;

. . .

cerr <<"saving " << nfeat << " x " << histsize << " features\n" ; cnpy::npy_save(fout,&vhist[0],{nfeat,histsize},"w"); cnpy::npy_save(frmsd,&vrmsd,{nfeat},"w"); cerr << "done saving features\n";

Where the output is:

saving 40339 x 64000 features done saving features

There are two issues:

1) fout ("dock_feat.npy")contains only a header strings dock_feat.npy NUMPY {'descr': '<?24', 'fortran_order': False, 'shape': (40339, 64000), }

2) The descr field in both fout and frmsd is not accepted by np.load()

ValueError: descr is not a valid dtype descriptor: '<?24'

ghost commented 6 years ago

Update: problem #1 was fixed with a debugger. I now declare the size and contents of my containers:

vector < double > vrmsd(max,99.9); vector<vector > vhist(max, vector(histsize, 0));

Problem #2 remains:

ValueError: descr is not a valid dtype descriptor: '<?24'

clemense commented 5 years ago

Same problem (ValueError: descr is not a valid dtype descriptor: '<?24') here, any hints?

clemense commented 5 years ago

Alright, figured it out. In my case i wanted to save a matrix of type std::vector<std::vector<double> >, but my mistake was to give &matrix[0] instead of &matrix[0][0] to the npy_save function.

clemense commented 5 years ago

I was wong. To save an std::vector<std::vector<double> >, you need to first flatten it.