hops
H5Easy.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 #ifndef H5EASY_HPP
10 #define H5EASY_HPP
11 
12 #include <string>
13 #include <vector>
14 
15 // optionally enable xtensor plug-in and load the library
16 #ifdef XTENSOR_VERSION_MAJOR
17 #ifndef H5_USE_XTENSOR
18 #define H5_USE_XTENSOR
19 #endif
20 #endif
21 
22 #ifdef H5_USE_XTENSOR
23 #include <xtensor/xarray.hpp>
24 #include <xtensor/xtensor.hpp>
25 #endif
26 
27 // optionally enable Eigen plug-in and load the library
28 #ifdef EIGEN_WORLD_VERSION
29 #ifndef H5_USE_EIGEN
30 #define H5_USE_EIGEN
31 #endif
32 #endif
33 
34 #ifdef H5_USE_EIGEN
35 #include <Eigen/Eigen>
36 #endif
37 
38 // optionally enable OpenCV plug-in and load the library
39 #ifdef CV_MAJOR_VERSION
40 #ifndef H5_USE_OPENCV
41 #define H5_USE_OPENCV
42 #endif
43 #endif
44 
45 #ifdef H5_USE_OPENCV
46 #include <opencv2/opencv.hpp>
47 #endif
48 
49 #include "H5File.hpp"
50 
51 namespace H5Easy {
52 
55 using HighFive::Chunking;
56 using HighFive::DataSet;
59 using HighFive::Deflate;
61 using HighFive::File;
63 using HighFive::Shuffle;
64 
67 enum class DumpMode {
68  Create = 0,
69  Overwrite = 1
70 };
71 
74 enum class Flush
75 {
76  False = 0,
77  True = 1
78 };
79 
83 {
84 public:
85 
86  //
87  // \brief Enable compression with the highest compression level (9).
88  // or disable compression (set compression level to 0).
89  explicit Compression(bool enable = true);
90 
91  //
92  // \brief Set compression level.
93  template <class T>
94  Compression(T level);
95 
96  //
97  // \brief Return compression level.
98  inline unsigned get() const;
99 
100 private:
101  unsigned m_compression_level;
102 };
103 
113 {
114 public:
117  DumpOptions() = default;
118 
122  template <class... Args>
123  DumpOptions(Args... args)
124  {
125  set(args...);
126  }
127 
131  inline void set(DumpMode mode);
132 
136  inline void set(Flush mode);
137 
141  inline void set(const Compression& level);
142 
146  template <class T, class... Args>
147  inline void set(T arg, Args... args);
148 
152  template <class T>
153  inline void setChunkSize(const std::vector<T>& shape);
154 
158  inline void setChunkSize(std::initializer_list<size_t> shape);
159 
162  inline bool overwrite() const;
163 
166  inline bool flush() const;
167 
170  inline bool compress() const;
171 
174  inline unsigned getCompressionLevel() const;
175 
178  inline bool isChunked() const;
179 
182  inline std::vector<hsize_t> getChunkSize() const;
183 
184 private:
185  bool m_overwrite = false;
186  bool m_flush = true;
187  unsigned m_compression_level = 0;
188  std::vector<hsize_t> m_chunk_size = {};
189 };
190 
198 inline size_t getSize(const File& file, const std::string& path);
199 
207 inline std::vector<size_t> getShape(const File& file, const std::string& path);
208 
219 template <class T>
220 inline DataSet dump(File& file,
221  const std::string& path,
222  const T& data,
223  DumpMode mode = DumpMode::Create);
224 
235 template <class T>
236 inline DataSet dump(File& file,
237  const std::string& path,
238  const T& data,
239  const DumpOptions& options);
240 
251 template <class T>
252 inline DataSet dump(File& file,
253  const std::string& path,
254  const T& data,
255  const std::vector<size_t>& idx);
256 
267 template <class T>
268 inline DataSet dump(File& file,
269  const std::string& path,
270  const T& data,
271  const std::initializer_list<size_t>& idx);
272 
284 template <class T>
285 inline DataSet dump(File& file,
286  const std::string& path,
287  const T& data,
288  const std::vector<size_t>& idx,
289  const DumpOptions& options);
290 
302 template <class T>
303 inline DataSet dump(File& file,
304  const std::string& path,
305  const T& data,
306  const std::initializer_list<size_t>& idx,
307  const DumpOptions& options);
308 
318 template <class T>
319 inline T load(const File& file, const std::string& path, const std::vector<size_t>& idx);
320 
329 template <class T>
330 inline T load(const File& file, const std::string& path);
331 
343 template <class T>
344 inline Attribute dumpAttribute(File& file,
345  const std::string& path,
346  const std::string& key,
347  const T& data,
348  DumpMode mode = DumpMode::Create);
349 
361 template <class T>
362 inline Attribute dumpAttribute(File& file,
363  const std::string& path,
364  const std::string& key,
365  const T& data,
366  const DumpOptions& options);
367 
377 template <class T>
378 inline T loadAttribute(const File& file, const std::string& path, const std::string& key);
379 
380 } // namespace H5Easy
381 
389 
390 #endif // H5EASY_HPP
H5Easy::DumpMode
DumpMode
Write mode for DataSets.
Definition: H5Easy.hpp:67
H5Easy::load
T load(const File &file, const std::string &path, const std::vector< size_t > &idx)
Load entry "(i,j)" from a rank-two DataSet in an open HDF5 file to a scalar.
Definition: H5Easy_public.hpp:157
H5Easy::DumpOptions::setChunkSize
void setChunkSize(const std::vector< T > &shape)
Set chunk-size. If the input is rank (size) zero, automatic chunking is enabled.
Definition: H5Easy_public.hpp:58
H5Easy::DumpOptions::compress
bool compress() const
Check to compress.
Definition: H5Easy_public.hpp:78
H5File.hpp
H5Easy::Compression
Set compression level for written DataSets.
Definition: H5Easy.hpp:82
H5Easy_Eigen.hpp
H5Easy::Flush
Flush
Enable/disable automatic flushing after write operations.
Definition: H5Easy.hpp:74
H5Easy_misc.hpp
H5Easy::DumpOptions::set
void set(DumpMode mode)
Overwrite setting.
Definition: H5Easy_public.hpp:35
H5Easy_scalar.hpp
H5Easy_vector.hpp
H5Easy::DumpOptions::getChunkSize
std::vector< hsize_t > getChunkSize() const
Get chunk size.
Definition: H5Easy_public.hpp:93
H5Easy::DumpOptions::getCompressionLevel
unsigned getCompressionLevel() const
Get compression level.
Definition: H5Easy_public.hpp:83
HighFive::DataSetCreateProps
PropertyList< PropertyType::DATASET_CREATE > DataSetCreateProps
Definition: H5PropertyList.hpp:79
H5Easy_xtensor.hpp
H5Easy::DumpOptions
Options for dumping data.
Definition: H5Easy.hpp:112
H5Easy::Flush::False
@ False
HighFive::ObjectType::Attribute
@ Attribute
HighFive::ObjectType
ObjectType
Enum of the types of objects (H5O api)
Definition: H5Object.hpp:25
HighFive::Deflate
Definition: H5PropertyList.hpp:117
H5Easy::getSize
size_t getSize(const File &file, const std::string &path)
Get the size of an existing DataSet in an open HDF5 file.
Definition: H5Easy_public.hpp:98
H5Easy::Flush::True
@ True
HighFive::Chunking
Definition: H5PropertyList.hpp:95
H5Easy::getShape
std::vector< size_t > getShape(const File &file, const std::string &path)
Get the shape of an existing DataSet in an readable file.
Definition: H5Easy_public.hpp:102
H5Easy
Definition: H5Easy.hpp:51
H5Easy::DumpOptions::isChunked
bool isChunked() const
Check if chunk-size is manually set (or should be computed automatically).
Definition: H5Easy_public.hpp:88
H5Easy_opencv.hpp
HighFive::File
File class.
Definition: H5File.hpp:24
HighFive::AtomicType
create an HDF5 DataType from a C++ type
Definition: H5_definitions.hpp:36
HighFive::Attribute
Class representing an attribute of a dataset or group.
Definition: H5Attribute.hpp:23
H5Easy::DumpOptions::DumpOptions
DumpOptions()=default
Constructor: accept all defaults.
H5Easy::Compression::get
unsigned get() const
Definition: H5Easy_public.hpp:30
H5Easy::DumpOptions::overwrite
bool overwrite() const
Check to overwrite.
Definition: H5Easy_public.hpp:68
H5Easy::DumpMode::Overwrite
@ Overwrite
HighFive::DataSpace
Class representing the space (dimensions) of a dataset.
Definition: H5DataSpace.hpp:37
H5Easy::dumpAttribute
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:167
H5Easy_public.hpp
HighFive::Shuffle
Definition: H5PropertyList.hpp:128
HighFive::Exception
Basic HighFive Exception class.
Definition: H5Exception.hpp:24
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
H5Easy::dump
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:115
H5Easy::Compression::Compression
Compression(bool enable=true)
Definition: H5Easy_public.hpp:16
H5Easy::DumpOptions::DumpOptions
DumpOptions(Args... args)
Constructor: overwrite (some of the) defaults.
Definition: H5Easy.hpp:123
H5Easy::DumpOptions::flush
bool flush() const
Check to flush.
Definition: H5Easy_public.hpp:73
H5Easy::loadAttribute
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:185
HighFive::DataSet
Class representing a dataset.
Definition: H5DataSet.hpp:27
H5Easy::DumpMode::Create
@ Create