Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 08:55:36

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