MUQ  0.4.3
H5Object.h
Go to the documentation of this file.
1 #ifndef H5OBJECT_H
2 #define H5OBJECT_H
3 
7 
8 #include <Eigen/Core>
9 #include <string>
10 #include <map>
11 #include <iostream>
12 #include <memory>
13 #include <type_traits>
14 
15 namespace muq
16 {
17 
18 namespace Utilities
19 {
20 
21 
22 class H5Object;
23 
25 H5Object AddChildren(std::shared_ptr<HDF5File> file,
26  std::string const& groupName);
27 
28 
30 
51 H5Object OpenFile(std::string const& filename);
52 
53 class H5Object
54 {
55  friend H5Object muq::Utilities::AddChildren(std::shared_ptr<HDF5File> file, std::string const& path);
56 
57 public:
58 
59  H5Object(){};
60 
61  H5Object(std::shared_ptr<HDF5File> file_,
62  std::string const& path_,
63  bool isDataset_) : file(file_),
64  attrs(file_, path_),
65  path(path_),
66  isDataset(isDataset_){};
67 
68  typedef std::function<void(boost::any const&, H5Object& )> AnyWriterType;
69  typedef std::unordered_map<std::type_index, AnyWriterType> AnyWriterMapType;
70 
71  static std::shared_ptr<AnyWriterMapType> GetAnyWriterMap();
72 
73  H5Object& operator=(boost::any const& val);
74 
75 
76  // Use this templated function for arithmetic types
78  H5Object& operator=(ScalarType val)
79  {
80  assert(path.length()>0);
81  if(isDataset)
82  {
83  Eigen::Matrix<ScalarType,Eigen::Dynamic,Eigen::Dynamic> temp(1,1);
84  temp(0,0) = val;
85  file->WriteMatrix(path, temp);
86  }
87  else
88  {
89  assert(false);
90  }
91 
92  return *this;
93  };
94 
95  // Use this templated function for non-arithmetic types
96  template<typename Derived>
97  H5Object& operator=(Eigen::DenseBase<Derived> const& val)
98  {
99  return (*this)=val.eval();
100  };
101 
102  template<typename ScalarType, int fixedRows, int fixedCols>
103  H5Object& operator=(Eigen::Matrix<ScalarType, fixedRows, fixedCols> const& val)
104  {
105  assert(path.length()>0);
106  if(isDataset){
107  file->WriteMatrix(path, val);
108  }else{
109  throw std::runtime_error("Error in H5Object::operator=. Unable to save matrix to path \"" + path + "\" because it is a group.");
110  }
111  return *this;
112  };
113 
114  template<typename scalarType, int rows, int cols>
115  operator Eigen::Matrix<scalarType,rows,cols>()
116  {
117  return eval<scalarType,rows,cols>();
118  }
119 
123  H5Object& operator=(H5Object const& otherObj);
124 
125  template<typename scalarType=double, int rows=Eigen::Dynamic, int cols=Eigen::Dynamic>
126  Eigen::Matrix<scalarType,rows,cols> eval()
127  {
128  if(isDataset)
129  {
130  return file->ReadMatrix<scalarType,rows,cols>(path).template cast<scalarType>();
131  }
132  else
133  {
134  assert(false);
135  }
136  }
138  // Create groups and datsets
140  H5Object& CreatePlaceholder(std::string const& grpName);
141  H5Object& CreateGroup(std::string const& grpName);
142 
144  template<typename ScalarType>
145  H5Object& CreateDataset(std::string const& setName,
146  unsigned int rows,
147  unsigned int cols=0)
148  {
149  H5Object& temp = CreatePlaceholder(setName);
150  temp.file->CreateDataset<ScalarType>(temp.path, rows, cols);
151  return temp;
152  }
153 
155  // Accessors
157  H5Object& operator[](std::string const& targetPath);
158 
159  //const H5Object& operator[](std::string const& path) const;
160 
161  BlockDataset block(unsigned startRow, unsigned startCol, unsigned numRows, unsigned numCols) const;
162 
163  BlockDataset topLeftCorner(unsigned numRows, unsigned numCols) const;
164  BlockDataset bottomLeftCorner(unsigned numRows, unsigned numCols) const;
165 
166  BlockDataset topRightCorner(unsigned numRows, unsigned numCols) const;
167  BlockDataset bottomRightCorner(unsigned numRows, unsigned numCols) const;
168 
169  BlockDataset topRows(unsigned numRows) const;
170  BlockDataset bottomRows(unsigned numRows) const;
171 
172  BlockDataset leftCols(unsigned numCols) const;
173  BlockDataset rightCols(unsigned numCols) const;
174 
175  BlockDataset col(unsigned col) const;
176 
177  BlockDataset row(unsigned row) const;
178 
179  BlockDataset segment(unsigned startInd, unsigned numInds) const;
180  BlockDataset head(unsigned numInds) const;
181  BlockDataset tail(unsigned numInds) const;
182 
183  unsigned rows() const;
184 
185  unsigned cols() const;
186 
187  unsigned size() const;
188 
189  double operator()(int i) const;
190 
191  double operator()(int i, int j) const;
192 
194  // Utility Functions
196 
197  void Flush();
198 
199  void Print(std::string prefix = "") const;
200 
201  std::string Path() const{return path;};
202 
203  std::shared_ptr<HDF5File> file;
204 
206 
207 private:
208 
213  void ExactCopy(H5Object const& otherObj);
214 
215 
216  // Copy the other objects content into the current dataset
217  void DeepCopy(H5Object const& otherObj);
218 
219  std::string path;
220 
221  std::map<std::string, H5Object> children;
222 
223  bool isDataset;
224 
225 };
226 
227 
228 #ifndef REGISTER_HDF5OBJECT_ANYTYPE
229 #define REGISTER_HDF5OBJECT_ANYTYPE(REGNAME, NAME) static auto regHDF ##REGNAME \
230  = muq::Utilities::H5Object::GetAnyWriterMap()->insert(std::make_pair(std::type_index(typeid(NAME)), muq::Utilities::AnyWriter<NAME>() ));
231 
232 #endif
233 
234 
235 
236 } // namespace Utilities
237 } // namespace muq
238 
239 
240 #endif
H5Object & operator=(ScalarType val)
Definition: H5Object.h:78
BlockDataset rightCols(unsigned numCols) const
Definition: H5Object.cpp:170
unsigned size() const
Definition: H5Object.cpp:246
H5Object & operator=(Eigen::Matrix< ScalarType, fixedRows, fixedCols > const &val)
Definition: H5Object.h:103
H5Object(std::shared_ptr< HDF5File > file_, std::string const &path_, bool isDataset_)
Definition: H5Object.h:61
H5Object & CreateGroup(std::string const &grpName)
Definition: H5Object.cpp:56
static std::shared_ptr< AnyWriterMapType > GetAnyWriterMap()
BlockDataset bottomLeftCorner(unsigned numRows, unsigned numCols) const
Definition: H5Object.cpp:140
BlockDataset topRows(unsigned numRows) const
Definition: H5Object.cpp:155
std::function< void(boost::any const &, H5Object &)> AnyWriterType
Definition: H5Object.h:66
BlockDataset col(unsigned col) const
Definition: H5Object.cpp:175
H5Object & CreatePlaceholder(std::string const &grpName)
Definition: H5Object.cpp:29
AttributeList attrs
Definition: H5Object.h:205
H5Object & CreateDataset(std::string const &setName, unsigned int rows, unsigned int cols=0)
Definition: H5Object.h:145
std::map< std::string, H5Object > children
Definition: H5Object.h:221
void Print(std::string prefix="") const
Definition: H5Object.cpp:281
BlockDataset segment(unsigned startInd, unsigned numInds) const
Definition: H5Object.cpp:191
BlockDataset row(unsigned row) const
Definition: H5Object.cpp:183
unsigned cols() const
Definition: H5Object.cpp:234
std::unordered_map< std::type_index, AnyWriterType > AnyWriterMapType
Definition: H5Object.h:69
unsigned rows() const
Definition: H5Object.cpp:225
void ExactCopy(H5Object const &otherObj)
Creates an exact copy.
Definition: H5Object.cpp:19
BlockDataset topLeftCorner(unsigned numRows, unsigned numCols) const
Definition: H5Object.cpp:135
H5Object & operator=(Eigen::DenseBase< Derived > const &val)
Definition: H5Object.h:97
BlockDataset topRightCorner(unsigned numRows, unsigned numCols) const
Definition: H5Object.cpp:145
Eigen::Matrix< scalarType, rows, cols > eval()
Definition: H5Object.h:126
H5Object & operator[](std::string const &targetPath)
Definition: H5Object.cpp:84
BlockDataset head(unsigned numInds) const
Definition: H5Object.cpp:196
BlockDataset block(unsigned startRow, unsigned startCol, unsigned numRows, unsigned numCols) const
Definition: H5Object.cpp:119
BlockDataset tail(unsigned numInds) const
Definition: H5Object.cpp:219
std::shared_ptr< HDF5File > file
Definition: H5Object.h:201
void DeepCopy(H5Object const &otherObj)
Definition: H5Object.cpp:6
BlockDataset bottomRightCorner(unsigned numRows, unsigned numCols) const
Definition: H5Object.cpp:150
double operator()(int i) const
Definition: H5Object.cpp:255
std::string Path() const
Definition: H5Object.h:201
H5Object & operator=(boost::any const &val)
BlockDataset bottomRows(unsigned numRows) const
Definition: H5Object.cpp:160
BlockDataset leftCols(unsigned numCols) const
Definition: H5Object.cpp:165
H5Object OpenFile(std::string const &filename)
Open an HDF5 file and return the root object.
Definition: H5Object.cpp:321
H5Object AddChildren(std::shared_ptr< HDF5File > file, std::string const &groupName)
Recursively add children to create an HDF5 file hierarchy.
Definition: H5Object.cpp:290
int int FloatType value
Definition: json.h:15223