Medusa  1.1
Coordinate Free Mehless Method implementation
HDF_Eigen.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_IO_HDF_EIGEN_HPP_
2 #define MEDUSA_BITS_IO_HDF_EIGEN_HPP_
3 
9 #include <Eigen/Core>
10 #include "HDF_fwd.hpp"
12 
13 namespace mm {
14 
16 template <typename scalar_t>
17 Eigen::Matrix<scalar_t, Eigen::Dynamic, Eigen::Dynamic> HDF::readEigen(
18  const std::string& dataset_name) const {
19  assert_msg(H5Iis_valid(group), "Group id %d invalid. Did you open a group before reading?",
20  group);
21  hid_t dataset = H5Dopen(group, dataset_name.c_str(), H5P_DEFAULT);
22  assert_msg(dataset >= 0, "Dataset '%s' could not be accessed in group '%s' in file '%s'.",
23  dataset_name, group_name_, filename_);
24 
25  hid_t dataspace = H5Dget_space(dataset);
26  const int ndims = H5Sget_simple_extent_ndims(dataspace);
27  assert_msg(ndims <= 2, "This function is for 1 and 2 dimensional arrays only.");
28  hsize_t dims[2] = {1, 1};
29  H5Sget_simple_extent_dims(dataspace, dims, nullptr); // read dimension into dims
30  hsize_t cols = dims[0], rows = dims[1];
31  Eigen::Matrix<scalar_t, Eigen::Dynamic, Eigen::Dynamic> M(rows, cols);
32 
33  hid_t type = H5Dget_type(dataset);
34  herr_t status = H5Dread(dataset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, M.data());
35  assert_msg(status >= 0, "Failed reading dataset '%s' from group '%s' in file '%s'.",
36  dataset_name, group_name_, filename_);
37 
38  H5Tclose(type);
39  H5Sclose(dataspace);
40  H5Dclose(dataset);
41  return M;
42 }
44 
45 template <typename Derived>
46 void HDF::writeEigen(const std::string& dataset_name, const Eigen::MatrixBase<Derived>& value,
47  bool overwrite) const {
48  typedef typename Eigen::MatrixBase<Derived>::Scalar scalar_t;
49  if (value.IsRowMajor) {
50  writeEigen(dataset_name,
51  Eigen::Matrix<scalar_t, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>(value),
52  overwrite);
53  return;
54  }
55  hsize_t cols = value.cols();
56  hsize_t rows = value.rows();
57  if (std::is_same<double, scalar_t>::value) {
58  writeLinearArray<2>(dataset_name, value.eval().data(), {cols, rows},
59  H5T_NATIVE_DOUBLE, overwrite);
60  } else if (std::is_same<float, scalar_t>::value) {
61  writeLinearArray<2>(dataset_name, value.eval().data(), {cols, rows},
62  H5T_NATIVE_FLOAT, overwrite);
63  } else if (std::is_same<int, scalar_t>::value) {
64  writeLinearArray<2>(dataset_name, value.eval().data(), {cols, rows},
65  H5T_NATIVE_INT, overwrite);
66  } else {
67  assert_msg(false, "Only float, int and double types are supported.");
68  }
69 }
70 
71 } // namespace mm
72 
73 #endif // MEDUSA_BITS_IO_HDF_EIGEN_HPP_
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
scalar_t
Scalar scalar_t
Type of the elements, alias of Scalar.
Definition: MatrixBaseAddons.hpp:16
mm::HDF::group
hid_t group
Currently open group identifier.
Definition: HDF_fwd.hpp:80
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::HDF::writeEigen
void writeEigen(const std::string &dataset_name, const Eigen::MatrixBase< Derived > &value, bool overwrite=false) const
Write given Eigen expression as a 2D array of its type (either int, double or float).
Definition: HDF_Eigen.hpp:46
mm::HDF::group_name_
std::string group_name_
Current group name.
Definition: HDF_fwd.hpp:77
HDF_fwd.hpp
assert.hpp
mm::HDF::filename_
std::string filename_
Current file name.
Definition: HDF_fwd.hpp:76
mm::HDF::readEigen
Eigen::Matrix< scalar_t, -1, -1, 0, -1, -1 > readEigen(const std::string &dataset_name) const
Reads data as an Eigen matrix of type scalar_t.