forked from garrison/eigen3-hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheigen3-hdf5.hpp
320 lines (278 loc) · 8.91 KB
/
eigen3-hdf5.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#ifndef _EIGEN3_HDF5_HPP
#define _EIGEN3_HDF5_HPP
#include <cassert>
#include <complex>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <vector>
//#include <hdf5.h>
#include <H5Cpp.h>
#include <Eigen/Dense>
namespace EigenHDF5
{
template <typename T>
struct DatatypeSpecialization;
// floating-point types
template <>
struct DatatypeSpecialization<float>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_FLOAT;
}
};
template <>
struct DatatypeSpecialization<double>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_DOUBLE;
}
};
template <>
struct DatatypeSpecialization<long double>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_LDOUBLE;
}
};
// integer types
template <>
struct DatatypeSpecialization<short>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_SHORT;
}
};
template <>
struct DatatypeSpecialization<unsigned short>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_USHORT;
}
};
template <>
struct DatatypeSpecialization<int>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_INT;
}
};
template <>
struct DatatypeSpecialization<unsigned int>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_UINT;
}
};
template <>
struct DatatypeSpecialization<long>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_LONG;
}
};
template <>
struct DatatypeSpecialization<unsigned long>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_ULONG;
}
};
template <>
struct DatatypeSpecialization<long long>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_LLONG;
}
};
template <>
struct DatatypeSpecialization<unsigned long long>
{
static inline const H5::DataType * get (void)
{
return &H5::PredType::NATIVE_ULLONG;
}
};
// complex types
//
// inspired by http://www.mail-archive.com/[email protected]/msg00759.html
template <typename T>
class ComplexH5Type : public H5::CompType
{
public:
ComplexH5Type (void)
: CompType(sizeof(std::complex<T>))
{
const H5::DataType * const datatype = DatatypeSpecialization<T>::get();
assert(datatype->getSize() == sizeof(T));
// If we call the members "r" and "i", h5py interprets the
// structure correctly as complex numbers.
this->insertMember(std::string("r"), 0, *datatype);
this->insertMember(std::string("i"), sizeof(T), *datatype);
this->pack();
}
static const ComplexH5Type<T> * get_singleton (void)
{
// NOTE: constructing this could be a race condition
static ComplexH5Type<T> singleton;
return &singleton;
}
};
template <typename T>
struct DatatypeSpecialization<std::complex<T> >
{
static inline const H5::DataType * get (void)
{
return ComplexH5Type<T>::get_singleton();
}
};
// string types, to be used mainly for attributes
template <>
struct DatatypeSpecialization<const char *>
{
static inline const H5::DataType * get (void)
{
static const H5::StrType strtype(0, H5T_VARIABLE);
return &strtype;
}
};
template <>
struct DatatypeSpecialization<char *>
{
static inline const H5::DataType * get (void)
{
static const H5::StrType strtype(0, H5T_VARIABLE);
return &strtype;
}
};
// XXX: for some unknown reason the following two functions segfault if
// H5T_VARIABLE is used. The passed strings should still be null-terminated,
// so this is a bit worrisome.
template <std::size_t N>
struct DatatypeSpecialization<const char [N]>
{
static inline const H5::DataType * get (void)
{
static const H5::StrType strtype(0, N);
return &strtype;
}
};
template <std::size_t N>
struct DatatypeSpecialization<char [N]>
{
static inline const H5::DataType * get (void)
{
static const H5::StrType strtype(0, N);
return &strtype;
}
};
namespace internal
{
template <typename Derived>
H5::DataSpace create_dataspace (const Eigen::EigenBase<Derived> &mat)
{
const std::size_t dimensions_size = 2;
const hsize_t dimensions[dimensions_size] = {
static_cast<hsize_t>(mat.rows()),
static_cast<hsize_t>(mat.cols())
};
return H5::DataSpace(dimensions_size, dimensions);
}
}
template <typename T>
void save_scalar_attribute (const H5::H5Location &h5obj, const std::string &name, const T &value)
{
const H5::DataType * const datatype = DatatypeSpecialization<T>::get();
H5::DataSpace dataspace(H5S_SCALAR);
H5::Attribute att = h5obj.createAttribute(name, *datatype, dataspace);
att.write(*datatype, &value);
}
template <>
inline void save_scalar_attribute (const H5::H5Location &h5obj, const std::string &name, const std::string &value)
{
save_scalar_attribute(h5obj, name, value.c_str());
}
// see http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html
template <typename Derived>
void save (H5::CommonFG &h5group, const std::string &name, const Eigen::EigenBase<Derived> &mat, const H5::DSetCreatPropList &plist=H5::DSetCreatPropList::DEFAULT)
{
typedef typename Derived::Scalar Scalar;
const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> row_major_mat(mat);
const H5::DataSpace dataspace = internal::create_dataspace(mat);
const H5::DataType * const datatype = DatatypeSpecialization<Scalar>::get();
H5::DataSet dataset = h5group.createDataSet(name, *datatype, dataspace, plist);
dataset.write(row_major_mat.data(), *datatype);
}
template <typename Derived>
void save_attribute (const H5::H5Location &h5obj, const std::string &name, const Eigen::EigenBase<Derived> &mat)
{
typedef typename Derived::Scalar Scalar;
const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> row_major_mat(mat);
const H5::DataSpace dataspace = internal::create_dataspace(mat);
const H5::DataType * const datatype = DatatypeSpecialization<Scalar>::get();
H5::Attribute dataset = h5obj.createAttribute(name, *datatype, dataspace);
dataset.write(*datatype, row_major_mat.data());
}
namespace internal
{
// H5::Attribute and H5::DataSet both have similar API's, and although they
// share a common base class, the relevant methods are not virtual. Worst
// of all, they take their arguments in different orders!
template <typename Scalar>
inline void read_data (const H5::DataSet &dataset, Scalar *data, const H5::DataType &datatype)
{
dataset.read(data, datatype);
}
template <typename Scalar>
inline void read_data (const H5::Attribute &dataset, Scalar *data, const H5::DataType &datatype)
{
dataset.read(datatype, data);
}
template <typename Derived, typename DataSet>
void _load (const DataSet &dataset, const Eigen::DenseBase<Derived> &mat)
{
typedef typename Derived::Scalar Scalar;
const H5::DataSpace dataspace = dataset.getSpace();
const std::size_t ndims = dataspace.getSimpleExtentNdims();
assert(ndims > 0);
const std::size_t dimensions_size = 2;
hsize_t dimensions[dimensions_size];
dimensions[1] = 1; // in case it's 1D
if (ndims > dimensions_size) {
throw std::runtime_error("HDF5 array has too many dimensions.");
}
dataspace.getSimpleExtentDims(dimensions);
const hsize_t rows = dimensions[0], cols = dimensions[1];
std::vector<Scalar> data(rows * cols);
const H5::DataType * const datatype = DatatypeSpecialization<Scalar>::get();
internal::read_data(dataset, data.data(), *datatype);
// see http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html
Eigen::DenseBase<Derived> &mat_ = const_cast<Eigen::DenseBase<Derived> &>(mat);
mat_.derived().resize(rows, cols);
mat_ = Eigen::Map<Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >(data.data(), rows, cols);
}
}
template <typename Derived>
void load (const H5::CommonFG &h5group, const std::string &name, const Eigen::DenseBase<Derived> &mat)
{
const H5::DataSet dataset = h5group.openDataSet(name);
internal::_load(dataset, mat);
}
template <typename Derived>
void load_attribute (const H5::H5Location &h5obj, const std::string &name, const Eigen::DenseBase<Derived> &mat)
{
const H5::Attribute dataset = h5obj.openAttribute(name);
internal::_load(dataset, mat);
}
} // namespace EigenHDF5
#endif