Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-10 09:13:31

0001 /*
0002  *  Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
0003  *
0004  *  Distributed under the Boost Software License, Version 1.0.
0005  *    (See accompanying file LICENSE_1_0.txt or copy at
0006  *          http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  */
0009 
0010 /// \brief
0011 /// Read/dump DataSets or Attribute using a minimalistic syntax.
0012 /// To this end, the functions are templated, and accept:
0013 /// - Any type accepted by HighFive
0014 /// - Eigen objects
0015 /// - xtensor objects
0016 /// - OpenCV objects
0017 
0018 #pragma once
0019 
0020 #include <string>
0021 #include <vector>
0022 
0023 // optionally enable xtensor plug-in and load the library
0024 #ifdef XTENSOR_VERSION_MAJOR
0025 #ifndef H5_USE_XTENSOR
0026 #define H5_USE_XTENSOR
0027 #endif
0028 #endif
0029 
0030 #ifdef H5_USE_XTENSOR
0031 #include "xtensor.hpp"
0032 #endif
0033 
0034 // optionally enable Eigen plug-in and load the library
0035 #ifdef EIGEN_WORLD_VERSION
0036 #ifndef H5_USE_EIGEN
0037 #define H5_USE_EIGEN
0038 #endif
0039 #endif
0040 
0041 #ifdef H5_USE_EIGEN
0042 #include <Eigen/Eigen>
0043 #include "eigen.hpp"
0044 #endif
0045 
0046 // optionally enable OpenCV plug-in and load the library
0047 #ifdef CV_MAJOR_VERSION
0048 #ifndef H5_USE_OPENCV
0049 #define H5_USE_OPENCV
0050 #endif
0051 #endif
0052 
0053 #ifdef H5_USE_OPENCV
0054 #include <opencv2/opencv.hpp>
0055 #include "experimental/opencv.hpp"
0056 #endif
0057 
0058 #include "H5File.hpp"
0059 
0060 namespace H5Easy {
0061 
0062 using HighFive::AtomicType;
0063 using HighFive::Attribute;
0064 using HighFive::Chunking;
0065 using HighFive::DataSet;
0066 using HighFive::DataSetCreateProps;
0067 using HighFive::DataSpace;
0068 using HighFive::Deflate;
0069 using HighFive::Exception;
0070 using HighFive::File;
0071 using HighFive::ObjectType;
0072 using HighFive::Shuffle;
0073 
0074 ///
0075 /// \brief Write mode for DataSets
0076 enum class DumpMode {
0077     Create = 0,   /*!< Dump only if DataSet does not exist, otherwise throw. */
0078     Overwrite = 1 /*!< Create or overwrite if DataSet of correct shape exists, otherwise throw. */
0079 };
0080 
0081 ///
0082 /// \brief Signal to enable/disable automatic flushing after write operations.
0083 enum class Flush {
0084     False = 0, /*!< No automatic flushing. */
0085     True = 1   /*!< Automatic flushing. */
0086 };
0087 
0088 ///
0089 /// \brief Signal to set compression level for written DataSets.
0090 class Compression {
0091   public:
0092     ///
0093     /// \brief Enable compression with the highest compression level (9).
0094     /// or disable compression (set compression level to 0).
0095     ///
0096     /// \param enable ``true`` to enable with highest compression level
0097     explicit Compression(bool enable = true);
0098 
0099     ///
0100     /// \brief Set compression level.
0101     ///
0102     /// \param level the compression level
0103     template <class T>
0104     Compression(T level);
0105 
0106     ///
0107     /// \brief Return compression level.
0108     inline unsigned get() const;
0109 
0110   private:
0111     unsigned m_compression_level;
0112 };
0113 
0114 ///
0115 /// \brief Define options for dumping data.
0116 ///
0117 /// By default:
0118 /// - DumpMode::Create
0119 /// - Flush::True
0120 /// - Compression: false
0121 /// - ChunkSize: automatic
0122 class DumpOptions {
0123   public:
0124     ///
0125     /// \brief Constructor: accept all default settings.
0126     DumpOptions() = default;
0127 
0128     ///
0129     /// \brief Constructor: overwrite (some of the) defaults.
0130     /// \param args any of DumpMode(), Flush(), Compression() in arbitrary number and order.
0131     template <class... Args>
0132     DumpOptions(Args... args) {
0133         set(args...);
0134     }
0135 
0136     ///
0137     /// \brief Overwrite H5Easy::DumpMode setting.
0138     /// \param mode: DumpMode.
0139     inline void set(DumpMode mode);
0140 
0141     ///
0142     /// \brief Overwrite H5Easy::Flush setting.
0143     /// \param mode Flush.
0144     inline void set(Flush mode);
0145 
0146     ///
0147     /// \brief Overwrite H5Easy::Compression setting.
0148     /// \param level Compression.
0149     inline void set(const Compression& level);
0150 
0151     ///
0152     /// \brief Overwrite any setting(s).
0153     /// \param arg any of DumpMode(), Flush(), Compression in arbitrary number and order.
0154     /// \param args any of DumpMode(), Flush(), Compression in arbitrary number and order.
0155     template <class T, class... Args>
0156     inline void set(T arg, Args... args);
0157 
0158     ///
0159     /// \brief Set chunk-size. If the input is rank (size) zero, automatic chunking is enabled.
0160     /// \param shape Chunk size along each dimension.
0161     template <class T>
0162     inline void setChunkSize(const std::vector<T>& shape);
0163 
0164     ///
0165     /// \brief Set chunk-size. If the input is rank (size) zero, automatic chunking is enabled.
0166     /// \param shape Chunk size along each dimension.
0167     inline void setChunkSize(std::initializer_list<size_t> shape);
0168 
0169     ///
0170     /// \brief Get overwrite-mode.
0171     /// \return bool
0172     inline bool overwrite() const;
0173 
0174     ///
0175     /// \brief Get flush-mode.
0176     /// \return bool
0177     inline bool flush() const;
0178 
0179     ///
0180     /// \brief Get compress-mode.
0181     /// \return bool
0182     inline bool compress() const;
0183 
0184     ///
0185     /// \brief Get compression level.
0186     /// \return [0..9]
0187     inline unsigned getCompressionLevel() const;
0188 
0189     ///
0190     /// \brief Get chunking mode: ``true`` is manually set, ``false`` if chunk-size should be
0191     /// computed automatically.
0192     /// \return bool
0193     inline bool isChunked() const;
0194 
0195     ///
0196     /// \brief Get chunk size. Use DumpOptions::getChunkSize to check if chunk-size should
0197     /// be automatically computed.
0198     inline std::vector<hsize_t> getChunkSize() const;
0199 
0200   private:
0201     bool m_overwrite = false;
0202     bool m_flush = true;
0203     unsigned m_compression_level = 0;
0204     std::vector<hsize_t> m_chunk_size = {};
0205 };
0206 
0207 ///
0208 /// \brief Get the size of an existing DataSet in an open HDF5 file.
0209 ///
0210 /// \param file opened file (has to be readable)
0211 /// \param path path of the DataSet
0212 ///
0213 /// \return Size of the DataSet
0214 inline size_t getSize(const File& file, const std::string& path);
0215 
0216 ///
0217 /// \brief Get the shape of an existing DataSet in an readable file.
0218 ///
0219 /// \param file opened file (has to be readable)
0220 /// \param path Path of the DataSet
0221 ///
0222 /// \return the shape of the DataSet
0223 inline std::vector<size_t> getShape(const File& file, const std::string& path);
0224 
0225 ///
0226 /// \brief Write object (templated) to a (new) DataSet in an open HDF5 file.
0227 ///
0228 /// \param file opened file (has to be writeable)
0229 /// \param path path of the DataSet
0230 /// \param data the data to write (any supported type)
0231 /// \param mode write mode
0232 ///
0233 /// \return The newly created DataSet
0234 ///
0235 template <class T>
0236 inline DataSet dump(File& file,
0237                     const std::string& path,
0238                     const T& data,
0239                     DumpMode mode = DumpMode::Create);
0240 
0241 ///
0242 /// \brief Write object (templated) to a (new) DataSet in an open HDF5 file.
0243 ///
0244 /// \param file opened file (has to be writeable)
0245 /// \param path path of the DataSet
0246 /// \param data the data to write (any supported type)
0247 /// \param options dump options
0248 ///
0249 /// \return The newly created DataSet
0250 ///
0251 template <class T>
0252 inline DataSet dump(File& file, const std::string& path, const T& data, const DumpOptions& options);
0253 
0254 ///
0255 /// \brief Write a scalar to a (new, extendible) DataSet in an open HDF5 file.
0256 ///
0257 /// \param file opened file (has to be writeable)
0258 /// \param path path of the DataSet
0259 /// \param data the data to write (any supported type)
0260 /// \param idx the indices to which to write
0261 ///
0262 /// \return The newly created DataSet
0263 ///
0264 template <class T>
0265 inline DataSet dump(File& file,
0266                     const std::string& path,
0267                     const T& data,
0268                     const std::vector<size_t>& idx);
0269 
0270 ///
0271 /// \brief Write a scalar to a (new, extendable) DataSet in an open HDF5 file.
0272 ///
0273 /// \param file open File (has to be writeable)
0274 /// \param path path of the DataSet
0275 /// \param data the data to write (any supported type)
0276 /// \param idx the indices to which to write
0277 ///
0278 /// \return The newly created DataSet
0279 ///
0280 template <class T>
0281 inline DataSet dump(File& file,
0282                     const std::string& path,
0283                     const T& data,
0284                     const std::initializer_list<size_t>& idx);
0285 
0286 ///
0287 /// \brief Write a scalar to a (new, extendible) DataSet in an open HDF5 file.
0288 ///
0289 /// \param file opened file (has to be writeable)
0290 /// \param path path of the DataSet
0291 /// \param data the data to write (any supported type)
0292 /// \param idx the indices to which to write
0293 /// \param options dump options
0294 ///
0295 /// \return The newly created DataSet
0296 ///
0297 template <class T>
0298 inline DataSet dump(File& file,
0299                     const std::string& path,
0300                     const T& data,
0301                     const std::vector<size_t>& idx,
0302                     const DumpOptions& options);
0303 
0304 ///
0305 /// \brief Write a scalar to a (new, extendible) DataSet in an open HDF5 file.
0306 ///
0307 /// \param file opened file (has to be writeable)
0308 /// \param path path of the DataSet
0309 /// \param data the data to write (any supported type)
0310 /// \param idx the indices to which to write
0311 /// \param options dump options
0312 ///
0313 /// \return The newly created DataSet
0314 ///
0315 template <class T>
0316 inline DataSet dump(File& file,
0317                     const std::string& path,
0318                     const T& data,
0319                     const std::initializer_list<size_t>& idx,
0320                     const DumpOptions& options);
0321 
0322 ///
0323 /// \brief Load entry ``{i, j, ...}`` from a DataSet in an open HDF5 file to a scalar.
0324 ///
0325 /// \param file opened file (has to be writeable)
0326 /// \param idx the indices to load
0327 /// \param path path of the DataSet
0328 ///
0329 /// \return The read data
0330 ///
0331 template <class T>
0332 inline T load(const File& file, const std::string& path, const std::vector<size_t>& idx);
0333 
0334 ///
0335 /// \brief Load a DataSet in an open HDF5 file to an object (templated).
0336 ///
0337 /// \param file opened file (has to be writeable)
0338 /// \param path path of the DataSet
0339 ///
0340 /// \return The read data
0341 ///
0342 template <class T>
0343 inline T load(const File& file, const std::string& path);
0344 
0345 ///
0346 /// \brief Write object (templated) to a (new) Attribute in an open HDF5 file.
0347 ///
0348 /// \param file opened file (has to be writeable)
0349 /// \param path path of the DataSet
0350 /// \param key name of the attribute
0351 /// \param data the data to write (any supported type)
0352 /// \param mode write mode
0353 ///
0354 /// \return The newly created DataSet
0355 ///
0356 template <class T>
0357 inline Attribute dumpAttribute(File& file,
0358                                const std::string& path,
0359                                const std::string& key,
0360                                const T& data,
0361                                DumpMode mode = DumpMode::Create);
0362 
0363 ///
0364 /// \brief Write object (templated) to a (new) Attribute in an open HDF5 file.
0365 ///
0366 /// \param file opened file (has to be writeable)
0367 /// \param path path of the DataSet
0368 /// \param key name of the attribute
0369 /// \param data the data to write (any supported type)
0370 /// \param options dump options
0371 ///
0372 /// \return The newly created DataSet
0373 ///
0374 template <class T>
0375 inline Attribute dumpAttribute(File& file,
0376                                const std::string& path,
0377                                const std::string& key,
0378                                const T& data,
0379                                const DumpOptions& options);
0380 
0381 ///
0382 /// \brief Load a Attribute in an open HDF5 file to an object (templated).
0383 ///
0384 /// \param file opened file (has to be writeable)
0385 /// \param path path of the DataSet
0386 /// \param key name of the attribute
0387 ///
0388 /// \return The read data
0389 ///
0390 template <class T>
0391 inline T loadAttribute(const File& file, const std::string& path, const std::string& key);
0392 
0393 }  // namespace H5Easy
0394 
0395 #include "h5easy_bits/H5Easy_Eigen.hpp"
0396 #include "h5easy_bits/H5Easy_misc.hpp"
0397 #include "h5easy_bits/H5Easy_public.hpp"
0398 #include "h5easy_bits/H5Easy_scalar.hpp"