22struct io_impl<T, typename std::enable_if<std::is_base_of<Eigen::DenseBase<T>, T>::value>::type> {
26 using row_major = Eigen::Ref<
27 const Eigen::Array<typename std::decay<T>::type::Scalar,
28 std::decay<T>::type::RowsAtCompileTime,
29 std::decay<T>::type::ColsAtCompileTime,
30 std::decay<T>::type::ColsAtCompileTime == 1 ? Eigen::ColMajor
32 std::decay<T>::type::MaxRowsAtCompileTime,
33 std::decay<T>::type::MaxColsAtCompileTime>,
35 Eigen::InnerStride<1>>;
38 Eigen::Map<Eigen::Array<typename std::decay<T>::type::Scalar,
39 std::decay<T>::type::RowsAtCompileTime,
40 std::decay<T>::type::ColsAtCompileTime,
41 std::decay<T>::type::ColsAtCompileTime == 1 ? Eigen::ColMajor
43 std::decay<T>::type::MaxRowsAtCompileTime,
44 std::decay<T>::type::MaxColsAtCompileTime>>;
48 inline static std::vector<size_t> shape(
const T& data) {
49 if (std::decay<T>::type::RowsAtCompileTime == 1) {
50 return {
static_cast<size_t>(data.cols())};
52 if (std::decay<T>::type::ColsAtCompileTime == 1) {
53 return {
static_cast<size_t>(data.rows())};
55 return {
static_cast<size_t>(data.rows()),
static_cast<size_t>(data.cols())};
58 using EigenIndex = Eigen::DenseIndex;
62 inline static std::vector<EigenIndex> shape(
const File& file,
63 const std::string& path,
65 int RowsAtCompileTime) {
66 std::vector<size_t> dims = dataset.getDimensions();
68 if (dims.size() == 1 && RowsAtCompileTime == 1) {
69 return std::vector<EigenIndex>{1u,
static_cast<EigenIndex
>(dims[0])};
71 if (dims.size() == 1) {
72 return std::vector<EigenIndex>{
static_cast<EigenIndex
>(dims[0]), 1u};
74 if (dims.size() == 2) {
75 return std::vector<EigenIndex>{
static_cast<EigenIndex
>(dims[0]),
76 static_cast<EigenIndex
>(dims[1])};
79 throw detail::error(file, path,
"H5Easy::load: Inconsistent rank");
82 inline static DataSet
dump(File& file,
83 const std::string& path,
85 const DumpOptions& options) {
86 using row_major_type =
typename types<T>::row_major;
87 using value_type =
typename std::decay<T>::type::Scalar;
88 row_major_type row_major(data);
89 DataSet dataset = initDataset<value_type>(file, path, shape(data), options);
90 dataset.write_raw(row_major.data());
91 if (options.flush()) {
97 inline static T
load(
const File& file,
const std::string& path) {
98 DataSet dataset = file.getDataSet(path);
99 std::vector<typename T::Index> dims = shape(file, path, dataset, T::RowsAtCompileTime);
100 T data(dims[0], dims[1]);
101 dataset.read_raw(data.data());
102 if (data.IsVectorAtCompileTime || data.IsRowMajor) {
105 using col_major =
typename types<T>::col_major;
106 return col_major(data.data(), dims[0], dims[1]);
110 const std::string& path,
111 const std::string& key,
113 const DumpOptions& options) {
114 using row_major_type =
typename types<T>::row_major;
115 using value_type =
typename std::decay<T>::type::Scalar;
116 row_major_type row_major(data);
117 Attribute attribute = initAttribute<value_type>(file, path, key, shape(data), options);
118 attribute.write_raw(row_major.data());
119 if (options.flush()) {
126 const std::string& path,
127 const std::string& key) {
128 DataSet dataset = file.getDataSet(path);
129 Attribute attribute = dataset.getAttribute(key);
130 DataSpace dataspace = attribute.getSpace();
131 std::vector<typename T::Index> dims = shape(file, path, dataspace, T::RowsAtCompileTime);
132 T data(dims[0], dims[1]);
133 attribute.read_raw(data.data());
134 if (data.IsVectorAtCompileTime || data.IsRowMajor) {
137 using col_major =
typename types<T>::col_major;
138 return col_major(data.data(), dims[0], dims[1]);
Read/dump DataSets or Attribute using a minimalistic syntax. To this end, the functions are templated...
Definition H5Easy.hpp:59
DataSet dump(File &file, const std::string &path, const T &data, DumpMode mode=DumpMode::Create)
Write object (templated) to a (new) DataSet in an open HDF5 file.
Definition H5Easy_public.hpp:99
T loadAttribute(const File &file, const std::string &path, const std::string &key)
Load a Attribute in an open HDF5 file to an object (templated).
Definition H5Easy_public.hpp:166
Attribute dumpAttribute(File &file, const std::string &path, const std::string &key, const T &data, DumpMode mode=DumpMode::Create)
Write object (templated) to a (new) Attribute in an open HDF5 file.
Definition H5Easy_public.hpp:148
T load(const File &file, const std::string &path, const std::vector< size_t > &idx)
Load entry {i, j, ...} from a DataSet in an open HDF5 file to a scalar.
Definition H5Easy_public.hpp:138