首页 \ 问答 \ 如何将浮点数的2D std向量写入HDF5文件,然后在python中读取它(How to write 2D std vector of floats to HDF5 file and then read it in python)

如何将浮点数的2D std向量写入HDF5文件,然后在python中读取它(How to write 2D std vector of floats to HDF5 file and then read it in python)

我想写一个浮动的2D矢量到HDF5文件。 我使用了以下代码(writeh5.cpp):

#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <H5Cpp.h>

using namespace H5;
using namespace std;

int main(void) {
  int nrow = 5;
  int ncol = 4;

  vector<vector< double > > vec2d;
  vec2d.resize(nrow, vector<double>(ncol, 0.0));

  srand((unsigned)time(0));

  typename vector< vector< double > >::iterator row;
  typename vector< double >::iterator col;
  for (row = vec2d.begin(); row != vec2d.end(); row++) {
    cout << endl;
    for (col = row->begin(); col != row->end(); col++) {

      *col = (rand()/(RAND_MAX+1.0));
      cout << *col << '\t';
    }
  }
  cout << endl;

  H5File file("test.h5", H5F_ACC_TRUNC);

  // dataset dimensions
  hsize_t dimsf[2];
  dimsf[0] = nrow;
  dimsf[1] = ncol;
  DataSpace dataspace(2, dimsf);

  DataType datatype(H5::PredType::NATIVE_DOUBLE);
  DataSet dataset = file.createDataSet("data", datatype, dataspace);

  // dataset.write(vec2d.data(), H5::PredType::NATIVE_DOUBLE);
  dataset.write(&vec2d[0][0], H5::PredType::NATIVE_DOUBLE);

  cout << endl << " vec2d has " << endl;
  for (row = vec2d.begin(); row != vec2d.end(); row++) {
      cout << endl;
      for (col = row->begin(); col != row->end(); col++) {            

        cout << *col << '\t';
      }
  }
  cout << endl;

  dataset.close();
  dataspace.close();
  file.close();

  return 0;
}

我使用g++ writeh5.cpp -I/usr/include/hdf5/ -lhdf5_cpp -lhdf5 -Wall编译它

一段代码产生了以下输出:

0.325553        0.598941        0.364489        0.0125061
0.374205        0.0319419       0.380329        0.815621
0.863754        0.386279        0.0173515       0.15448
0.703936        0.372486        0.728436        0.991631
0.666207        0.568983        0.807475        0.964276

和文件test.h5

然后,当我从python中读取此文件时(使用以下内容)

import h5py
import numpy as np

file = h5py.File("test.h5", 'r')
dataset = np.array(file["data"])

print dataset

file.close()

我有

 [[  3.25553381e-001   5.98941262e-001   3.64488814e-001   1.25061036e-002]
 [  0.00000000e+000   2.42092166e-322   3.74204732e-001   3.19418786e-002]
 [  3.80329057e-001   8.15620518e-001   0.00000000e+000   2.42092166e-322]
 [  8.63753530e-001   3.86278684e-001   1.73514970e-002   1.54479635e-001]
 [  0.00000000e+000   2.42092166e-322   7.03935940e-001   3.72486182e-001]]

第一行是好的,其他行是垃圾。

我尝试使用dataset.write(&vec2d[0]...dataset.write(vec2d[0].data()... ,我遇到了类似的问题。

我要

  1. 写一个HDF5文件,其内容为2D std :: vector of double,
  2. 在python中读取文件并将内容存储在numpy数组中

我做错了什么?


I want to write a 2D vector of floats to a HDF5 file. I used the following code (writeh5.cpp):

#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <H5Cpp.h>

using namespace H5;
using namespace std;

int main(void) {
  int nrow = 5;
  int ncol = 4;

  vector<vector< double > > vec2d;
  vec2d.resize(nrow, vector<double>(ncol, 0.0));

  srand((unsigned)time(0));

  typename vector< vector< double > >::iterator row;
  typename vector< double >::iterator col;
  for (row = vec2d.begin(); row != vec2d.end(); row++) {
    cout << endl;
    for (col = row->begin(); col != row->end(); col++) {

      *col = (rand()/(RAND_MAX+1.0));
      cout << *col << '\t';
    }
  }
  cout << endl;

  H5File file("test.h5", H5F_ACC_TRUNC);

  // dataset dimensions
  hsize_t dimsf[2];
  dimsf[0] = nrow;
  dimsf[1] = ncol;
  DataSpace dataspace(2, dimsf);

  DataType datatype(H5::PredType::NATIVE_DOUBLE);
  DataSet dataset = file.createDataSet("data", datatype, dataspace);

  // dataset.write(vec2d.data(), H5::PredType::NATIVE_DOUBLE);
  dataset.write(&vec2d[0][0], H5::PredType::NATIVE_DOUBLE);

  cout << endl << " vec2d has " << endl;
  for (row = vec2d.begin(); row != vec2d.end(); row++) {
      cout << endl;
      for (col = row->begin(); col != row->end(); col++) {            

        cout << *col << '\t';
      }
  }
  cout << endl;

  dataset.close();
  dataspace.close();
  file.close();

  return 0;
}

I compiled it using g++ writeh5.cpp -I/usr/include/hdf5/ -lhdf5_cpp -lhdf5 -Wall

A run of the code produced the following output:

0.325553        0.598941        0.364489        0.0125061
0.374205        0.0319419       0.380329        0.815621
0.863754        0.386279        0.0173515       0.15448
0.703936        0.372486        0.728436        0.991631
0.666207        0.568983        0.807475        0.964276

And the file test.h5

Then when i read this file from python (using the following)

import h5py
import numpy as np

file = h5py.File("test.h5", 'r')
dataset = np.array(file["data"])

print dataset

file.close()

I got

 [[  3.25553381e-001   5.98941262e-001   3.64488814e-001   1.25061036e-002]
 [  0.00000000e+000   2.42092166e-322   3.74204732e-001   3.19418786e-002]
 [  3.80329057e-001   8.15620518e-001   0.00000000e+000   2.42092166e-322]
 [  8.63753530e-001   3.86278684e-001   1.73514970e-002   1.54479635e-001]
 [  0.00000000e+000   2.42092166e-322   7.03935940e-001   3.72486182e-001]]

the first row is good, the other rows are garbage.

I tried with dataset.write(&vec2d[0]... and dataset.write(vec2d[0].data()..., i got similar problems.

I want to

  1. Write a HDF5 file with the contents of a 2D std::vector of doubles,
  2. Read the file in python and store the contents in a numpy array

What i am doing wrong?


原文:https://stackoverflow.com/questions/32447628
更新时间:2022-03-21 22:03

最满意答案

你有没有尝试将绘图序列放在故事板中? 你可以随意停止/启动它们,或者只是将它们计时到你想要的地方。


have you tried putting your drawing sequence in a storyboard? you can stop/start them pretty much at will, or just time them to what you want.

相关问答

更多
  • 首先,我不会使用DateTime.Now - 您几乎肯定会使用DateTime.UtcNow ,假设您确实希望它是经过时间而不是本地时间的间隔。 当地时间的间隔可能导致一些非常奇怪的行为,例如,如果你把它设置为当地时间01:45到02:15,那时钟在凌晨2点回来...... 但除此之外,您可以将其定义为两个DateTime或DateTimeOffset值之间 - 我不会为此使用TimeSpan 。 或者,你可以使用我的Noda Time项目,它已经定义了一个Interval类型......(而且,IMO,一 ...
  • 将您的初始Y更改为+0.5(或-0.5),您将得到更好的线条。 Change your initial Y to +0.5 (or -0.5) and you'll get nice lines.
  • 回过头来看看jcanvas doc后,我找到了解决方案。 通过使用drawImage()的“load”回调函数,我可以在加载图像后调用drawText()。 更新的代码: 1 {% extends "layout.html" %} 2 {% block body %} 3