HighFive 2.9.0
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5Easy_Eigen.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
3 *
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 */
9#pragma once
10
11#include "../H5Easy.hpp"
12#include "H5Easy_misc.hpp"
13#include "H5Easy_scalar.hpp"
14
15#ifdef H5_USE_EIGEN
16
17namespace H5Easy {
18
19namespace detail {
20
21template <typename T>
22struct io_impl<T, typename std::enable_if<std::is_base_of<Eigen::DenseBase<T>, T>::value>::type> {
23 // abbreviate row-major <-> col-major conversions
24 template <typename S>
25 struct types {
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
31 : Eigen::RowMajor,
32 std::decay<T>::type::MaxRowsAtCompileTime,
33 std::decay<T>::type::MaxColsAtCompileTime>,
34 0,
35 Eigen::InnerStride<1>>;
36
37 using col_major =
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
42 : Eigen::RowMajor,
43 std::decay<T>::type::MaxRowsAtCompileTime,
44 std::decay<T>::type::MaxColsAtCompileTime>>;
45 };
46
47 // return the shape of Eigen::DenseBase<T> object as size 1 or 2 "std::vector<size_t>"
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())};
51 }
52 if (std::decay<T>::type::ColsAtCompileTime == 1) {
53 return {static_cast<size_t>(data.rows())};
54 }
55 return {static_cast<size_t>(data.rows()), static_cast<size_t>(data.cols())};
56 }
57
58 using EigenIndex = Eigen::DenseIndex;
59
60 // get the shape of a "DataSet" as size 2 "std::vector<Eigen::Index>"
61 template <class D>
62 inline static std::vector<EigenIndex> shape(const File& file,
63 const std::string& path,
64 const D& dataset,
65 int RowsAtCompileTime) {
66 std::vector<size_t> dims = dataset.getDimensions();
67
68 if (dims.size() == 1 && RowsAtCompileTime == 1) {
69 return std::vector<EigenIndex>{1u, static_cast<EigenIndex>(dims[0])};
70 }
71 if (dims.size() == 1) {
72 return std::vector<EigenIndex>{static_cast<EigenIndex>(dims[0]), 1u};
73 }
74 if (dims.size() == 2) {
75 return std::vector<EigenIndex>{static_cast<EigenIndex>(dims[0]),
76 static_cast<EigenIndex>(dims[1])};
77 }
78
79 throw detail::error(file, path, "H5Easy::load: Inconsistent rank");
80 }
81
82 inline static DataSet dump(File& file,
83 const std::string& path,
84 const T& data,
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()) {
92 file.flush();
93 }
94 return dataset;
95 }
96
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) {
103 return data;
104 }
105 using col_major = typename types<T>::col_major;
106 return col_major(data.data(), dims[0], dims[1]);
107 }
108
109 inline static Attribute dumpAttribute(File& file,
110 const std::string& path,
111 const std::string& key,
112 const T& data,
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()) {
120 file.flush();
121 }
122 return attribute;
123 }
124
125 inline static T loadAttribute(const File& file,
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) {
135 return data;
136 }
137 using col_major = typename types<T>::col_major;
138 return col_major(data.data(), dims[0], dims[1]);
139 }
140};
141
142} // namespace detail
143} // namespace H5Easy
144
145#endif // H5_USE_EIGEN
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