Debian Codegraph @

avogadro/io/hdf5dataformat.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_HDF5DATAFORMAT_H
7 #define AVOGADRO_HDF5DATAFORMAT_H
8
9 #include "avogadroioexport.h"
10
11 #include <avogadro/core/matrix.h> // can't forward declare eigen types
12
13 #include <cstddef>
14 #include <string>
15 #include <vector>
16
17 namespace Avogadro {
18 namespace Core {
19 template <typename T>
20 class Array;
21 }
22 namespace Io {
23
24 /**
25 * @class Hdf5DataFormat hdf5dataformat.h <avogadro/io/hdf5dataformat.h>
26 * @brief The Hdf5DataFormat class provides access to data stored in HDF5 files.
27 * @author Allison Vacanti
28 *
29 * This class is intended to supplement an existing format reader/writer by
30 * providing the option to write large data to an HDF5 file store. The purpose
31 * is to keep text format files at a manageable size.
32 *
33 * To use this class, open or create an HDF5 file with the openFile method,
34 * using the appropriate OpenMode for the intended operation. Data can be
35 * written to the file using the writeDataset methods and retrieved using the
36 * readDataset methods. When finished, call closeFile to release the file
37 * resources from the HDF5 library.
38 *
39 * A complete set of datasets available in an open file can be retrieved with
40 * the datasets() method, and the existence of a particular dataset can be
41 * tested with datasetExists(). removeDataset() can be used to unlink an
42 * existing dataset from the file, though this will not free any space on disk.
43 * The space occupied by an unlinked dataset may be reclaimed by new write
44 * operations, but only if they occur before the file is closed.
45 *
46 * A convenient thresholding system is implemented to help the accompanying text
47 * format writer determine which data is "large" enough to be stored in HDF5. A
48 * size threshold (in bytes) may be set with the setThreshold() function (the
49 * default is 1KB). A data object may be passed to the exceedsThreshold method
50 * to see if the size of the data in the container exceeds the currently set
51 * threshold. If so, it should be written into the HDF5 file by writeDataset.
52 * If not, it should be serialized into the text file in a suitable format. The
53 * thresholding operations are optional; the threshold size does not affect the
54 * behavior of the read/write methods and are only for user convenience.
55 */
56 class AVOGADROIO_EXPORT Hdf5DataFormat
57 {
58 public:
59 Hdf5DataFormat();
60
61 /** Destructor. Closes any open file before freeing memory. */
62 ~Hdf5DataFormat();
63
64 /** Open modes for use with openFile(). */
65 enum OpenMode
66 {
67 /** Open an existing file in read-only mode. The file must exist. */
68 ReadOnly = 0,
69 /**
70 * Create a file in read/write mode, removing any existing file with the
71 * same name.
72 */
73 ReadWriteTruncate,
74 /**
75 * Open an file in read/write mode. If the file exist, its contents will be
76 * preserved. If it does not, a new file will be created.
77 */
78 ReadWriteAppend
79 };
80
81 /** @return true if a file is open. */
82 bool isOpen() const;
83
84 /**
85 * @brief openFile Open a file for use by this reader/writer.
86 * @param filename_ Name of the file to open.
87 * @param mode OpenMode for the file. Default is ReadWriteAppend.
88 * @note Only a single file may be opened at a time. Attempting to open
89 * multiple files without calling closeFile() will fail.
90 * @return True if the file is successfully opened/create by the HDF5
91 * subsystem, false otherwise.
92 */
93 bool openFile(const std::string& filename_, OpenMode mode = ReadWriteAppend);
94
95 /**
96 * @return The name of the open file, or an empty string if no file is open.
97 */
98 std::string filename() const;
99
100 /**
101 * @brief closeFile Close the file and reset the reader/writer. Another file
102 * may be opened after calling this function.
103 * @return true if the file is successfully released by the HDF5 subsystem.
104 */
105 bool closeFile();
106
107 /**
108 * @brief setThreshold Set the threshold size in bytes that will be used in
109 * the exceedsThreshold functions. The threshold can be used to determine
110 * which
111 * data is considered "large enough" to be stored in HDF5, rather than an
112 * accompanying format.
113 * @param bytes The size in bytes for the threshold. Default: 1KB.
114 */
115 void setThreshold(size_t bytes);
116
117 /** @return The current threshold size in bytes. Default: 1KB. */
118 size_t threshold() const;
119
120 /**
121 * @brief exceedsThreshold Test if a data set is "large enough" to be stored
122 * in HDF5 format. If this function returns true, the number of bytes tested
123 * is larger than the threshold and the data should be written into the HDF5
124 * file. If false, the data should be written into the accompanying format.
125 * @param bytes The size of the dataset in bytes
126 * @return true if the size exceeds the threshold set by setThreshold.
127 */
128 bool exceedsThreshold(size_t bytes) const;
129
130 /**
131 * @brief exceedsThreshold Test if a data set is "large enough" to be stored
132 * in HDF5 format. If this function returns true, the size of the data in the
133 * object is larger than the threshold and should be written into the HDF5
134 * file. If false, the data should be written into the accompanying format.
135 * @param data Data object to test.
136 * @return true if the size of the serializable data in @a data exceeds the
137 * threshold set by setThreshold.
138 */
139 bool exceedsThreshold(const MatrixX& data) const;
140
141 /**
142 * @brief exceedsThreshold Test if a data set is "large enough" to be stored
143 * in HDF5 format. If this function returns true, the size of the data in the
144 * object is larger than the threshold and should be written into the HDF5
145 * file. If false, the data should be written into the accompanying format.
146 * @param data Data object to test.
147 * @return true if the size of the serializable data in @a data exceeds the
148 * threshold set by setThreshold.
149 */
150 bool exceedsThreshold(const std::vector<double>& data) const;
151
152 /**
153 * @brief exceedsThreshold Test if a data set is "large enough" to be stored
154 * in HDF5 format. If this function returns true, the size of the data in the
155 * object is larger than the threshold and should be written into the HDF5
156 * file. If false, the data should be written into the accompanying format.
157 * @param data Data object to test.
158 * @return true if the size of the serializable data in @a data exceeds the
159 * threshold set by setThreshold.
160 */
161 bool exceedsThreshold(const Core::Array<double>& data) const;
162
163 /**
164 * @brief datasetExists Test if the currently open file contains a dataset at
165 * the HDF5 absolute path @a path.
166 * @param path An absolute path into the HDF5 data.
167 * @return true if the object at @a path both exists and is a dataset, false
168 * otherwise.
169 */
170 bool datasetExists(const std::string& path) const;
171
172 /**
173 * @brief removeDataset Remove a dataset from the currently opened file.
174 * @param path An absolute path into the HDF5 data.
175 * @return true if the dataset exists and has been successfully removed.
176 * \warning Removing datasets can be expensive in terms of filesize, as
177 * deleted space cannot be reclaimed by HDF5 once the file is closed, and the
178 * file will not decrease in size as datasets are removed. For details, see
179 * http://www.hdfgroup.org/HDF5/doc/H5.user/Performance.html#Freespace.
180 */
181 bool removeDataset(const std::string& path) const;
182
183 /**
184 * @brief datasetDimensions Find the dimensions of a dataset.
185 * @param path An absolute path into the HDF5 data.
186 * @return A vector containing the dimensionality of the data, major dimension
187 * first. If an error is encountered, an empty vector is returned.
188 */
189 std::vector<int> datasetDimensions(const std::string& path) const;
190
191 /**
192 * @brief writeDataset Write the data to the currently opened file at the
193 * specified absolute HDF5 path.
194 * @param path An absolute path into the HDF5 data.
195 * @param data The data container to serialize to HDF5.
196 * @return true if the data is successfully written, false otherwise.
197 */
198 bool writeDataset(const std::string& path, const MatrixX& data) const;
199
200 /**
201 * @brief writeDataset Write the data to the currently opened file at the
202 * specified absolute HDF5 path.
203 * @param path An absolute path into the HDF5 data.
204 * @param data The data container to serialize to HDF5.
205 * @param ndims The number of dimensions in the data. Default: 1.
206 * @param dims The dimensionality of the data, major dimension first. Default:
207 * data.size().
208 * @note Since std::vector is a flat container, the dimensionality data is
209 * only used to set up the dataset metadata in the HDF5 container. Omitting
210 * the dimensionality parameters will write a flat array.
211 * @return true if the data is successfully written, false otherwise.
212 */
213 bool writeDataset(const std::string& path, const std::vector<double>& data,
214 int ndims = 1, size_t* dims = nullptr) const;
215
216 /**
217 * @brief writeDataset Write the data to the currently opened file at the
218 * specified absolute HDF5 path.
219 * @param path An absolute path into the HDF5 data.
220 * @param data The data container to serialize to HDF5.
221 * @param ndims The number of dimensions in the data. Default: 1.
222 * @param dims The dimensionality of the data, major dimension first. Default:
223 * data.size().
224 * @note Since this is a flat container, the dimensionality data is
225 * only used to set up the dataset metadata in the HDF5 container. Omitting
226 * the dimensionality parameters will write a flat array.
227 * @return true if the data is successfully written, false otherwise.
228 */
229 bool writeDataset(const std::string& path, const Core::Array<double>& data,
230 int ndims = 1, size_t* dims = nullptr) const;
231
232 /**
233 * @brief readDataset Populate the data container @data with data at from the
234 * specified path in the currently opened HDF5 file.
235 * @param path An absolute path into the HDF5 data.
236 * @param data The data container to into which the HDF5 data shall be
237 * deserialized. @a data will be resized to fit the data.
238 * @return true if the data is successfully read, false otherwise. If the
239 * read fails, the @a data object may be left in an unpredictable state.
240 */
241 bool readDataset(const std::string& path, MatrixX& data) const;
242
243 /**
244 * @brief readDataset Populate the data container @data with data at from the
245 * specified path in the currently opened HDF5 file.
246 * @param path An absolute path into the HDF5 data.
247 * @param data The data container to into which the HDF5 data shall be
248 * deserialized. @a data will be resized to fit the data.
249 * @return A vector containing the dimensionality of the dataset, major
250 * dimension first. If an error occurs, an empty vector is returned and *data
251 * will be set to nullptr.
252 */
253 std::vector<int> readDataset(const std::string& path,
254 std::vector<double>& data) const;
255
256 /**
257 * @brief readDataset Populate the data container @data with data at from the
258 * specified path in the currently opened HDF5 file.
259 * @param path An absolute path into the HDF5 data.
260 * @param data The data container to into which the HDF5 data shall be
261 * deserialized. @a data will be resized to fit the data.
262 * @return A vector containing the dimensionality of the dataset, major
263 * dimension first. If an error occurs, an empty vector is returned and *data
264 * will be set to nullptr.
265 */
266 std::vector<int> readDataset(const std::string& path,
267 Core::Array<double>& data) const;
268
269 /**
270 * @brief datasets Traverse the currently opened file and return a list of all
271 * dataset objects in the file.
272 * @return A list of datasets in the current file.
273 * \warning The list is not cached internal and is recalculated on each call.
274 * This may be expensive on large HDF5 files, so external caching is
275 * recommended if this data is frequently needed.
276 */
277 std::vector<std::string> datasets() const;
278
279 /** Used to abstract details of container resizing. */
280 class ResizeContainer;
281
282 private:
283 /**
284 * @brief writeRawDataset Write the data to the currently opened file at the
285 * specified absolute HDF5 path.
286 * @param path An absolute path into the HDF5 data.
287 * @param data The data container to serialize to HDF5.
288 * @param ndims The number of dimensions in the data.
289 * @param dims The data dimensions, major dimension first.
290 * @note Since a double[] is a flat container, the dimensionality data is
291 * only used to set up the dataset metadata in the HDF5 container. The result
292 * of multiplying all values in @a dims must equal the length of the @a data.
293 * @return true if the data is successfully written, false otherwise.
294 */
295 bool writeRawDataset(const std::string& path, const double data[], int ndims,
296 size_t dims[]) const;
297
298 /**
299 * @brief readRawDataset Populate the data container @data with data from the
300 * specified path in the currently opened HDF5 file.
301 * @param path An absolute path into the HDF5 data.
302 * @param container A subclass of ResizeContainer with the container to read
303 * data into.
304 * @return A vector containing the dimensionality of the dataset, major
305 * dimension first. If an error occurs, an empty vector is returned.
306 */
307 std::vector<int> readRawDataset(const std::string& path,
308 ResizeContainer& container) const;
309
310 class Private;
311 /** Internal storage, used to encapsulate HDF5 data. */
312 Private* const d;
313 };
314
315 } // namespace Io
316 } // namespace Avogadro
317
318 #endif // AVOGADRO_HDF5DATAFORMAT_H