aqnwb 0.3.0
Loading...
Searching...
No Matches
HDF5IO.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <memory>
5#include <string>
6
7#include <H5Opublic.h>
8
9#include "Types.hpp"
10#include "io/BaseIO.hpp"
11#include "io/ReadIO.hpp"
12
13namespace H5
14{
15class DataSet;
16class Attribute;
17class H5File;
18class DataType;
19class Exception;
20class CommonFG;
21class PredType;
22class DataSpace;
23} // namespace H5
24
29namespace AQNWB::IO::HDF5
30{
31
32class HDF5RecordingData; // forward declaration
33
38class HDF5IO : public BaseIO
39{
40public:
45 explicit HDF5IO(const std::string& fileName);
46
50 ~HDF5IO() override;
51
56 Status open() override;
57
63 Status open(FileMode mode) override;
64
65#ifdef H5_HAVE_ROS3_VFD
85 Status openS3(const std::string& aws_region = "",
86 const std::string& secret_id = "",
87 const std::string& secret_key = "");
88#endif // H5_HAVE_ROS3_VFD
89
90#ifdef AQNWB_HAVE_REMFILE_VFD
107#endif // AQNWB_HAVE_REMFILE_VFD
108
113 Status close() override;
114
119 Status flush() override;
120
129 StorageObjectType getStorageObjectType(std::string path) const override;
130
149 std::string findObject(const std::string& name,
150 const std::string& starting_path = "/") const override;
151
165 AQNWB::IO::DataBlockGeneric readDataset(const std::string& dataPath,
166 const SizeArray& start = {},
167 const SizeArray& count = {},
168 const SizeArray& stride = {},
169 const SizeArray& block = {}) override;
170
185 const std::string& dataPath) const override;
186
193 std::string readReferenceAttribute(
194 const std::string& dataPath) const override;
195
206 const void* data,
207 const std::string& path,
208 const std::string& name,
209 const SizeType& size = 1) override;
210
219 Status createAttribute(const std::string& data,
220 const std::string& path,
221 const std::string& name,
222 const bool overwrite = false) override;
223
234 Status createAttribute(const std::vector<std::string>& data,
235 const std::string& path,
236 const std::string& name,
237 const bool overwrite = false) override;
238
246 Status createReferenceAttribute(const std::string& referencePath,
247 const std::string& path,
248 const std::string& name) override;
249
255 Status createGroup(const std::string& path) override;
256
266 Status createLink(const std::string& path,
267 const std::string& reference) override;
268
275 Status createStringDataSet(const std::string& path,
276 const std::string& value) override;
277
284 Status createStringDataSet(const std::string& path,
285 const std::vector<std::string>& values) override;
286
295 const std::string& path,
296 const std::vector<std::string>& references) override;
297
302 Status startRecording() override;
303
317 Status startRecording(bool disableSWMRMode);
318
323 Status stopRecording() override;
324
331 bool canModifyObjects() override;
332
341 std::unique_ptr<IO::BaseRecordingData> createArrayDataSet(
342 const IO::BaseArrayDataSetConfig& config,
343 const std::string& path) override;
344
350 std::shared_ptr<IO::BaseRecordingData> getDataSet(
351 const std::string& path) override;
352
358 SizeArray getStorageObjectShape(const std::string& path) const override;
359
367 SizeArray getStorageObjectChunking(const std::string& path) const override;
368
376 BaseDataType getStorageObjectDataType(const std::string& path) const override;
377
384 bool objectExists(const std::string& path) const override;
385
394 bool attributeExists(const std::string& path) const override;
395
414 virtual std::vector<std::pair<std::string, StorageObjectType>>
415 getStorageObjects(const std::string& path,
416 const StorageObjectType& objectType =
417 StorageObjectType::Undefined) const override;
418
425 H5O_type_t getH5ObjectType(const std::string& path) const;
426
437 static H5::DataType getNativeType(IO::BaseDataType type);
438
447 static IO::BaseDataType getBaseDataType(const H5::DataType& nativeType);
448
458 static H5::DataType getH5Type(IO::BaseDataType type);
459
460protected:
466 Status createGroupIfDoesNotExist(const std::string& path) override;
467
468private:
486 template<typename T, typename HDF5TYPE>
487 std::vector<T> readDataHelper(const HDF5TYPE& dataSource,
488 size_t numElements,
489 const H5::DataSpace& memspace,
490 const H5::DataSpace& dataspace) const;
491
509 template<typename T, typename HDF5TYPE>
510 std::vector<T> readDataHelper(const HDF5TYPE& dataSource,
511 size_t numElements) const;
512
525 template<typename HDF5TYPE>
526 std::vector<std::string> readStringDataHelper(
527 const HDF5TYPE& dataSource,
528 size_t numElements,
529 const H5::DataSpace& memspace,
530 const H5::DataSpace& dataspace) const;
531
547 template<typename HDF5TYPE>
548 std::vector<std::string> readStringDataHelper(const HDF5TYPE& dataSource,
549 size_t numElements) const;
550
551 std::unique_ptr<H5::Attribute> getAttribute(const std::string& path) const;
552
564
568 std::unique_ptr<H5::H5File> m_file;
569
575};
576
577} // namespace AQNWB::IO::HDF5
AQNWB::Types::StorageObjectType StorageObjectType
Definition BaseIO.hpp:20
AQNWB::Types::Status Status
Definition BaseIO.hpp:21
AQNWB::Types::SizeArray SizeArray
Definition BaseIO.hpp:22
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
Base class for array dataset configuration.
Definition BaseIO.hpp:205
Represents a base data type.
Definition BaseIO.hpp:47
BaseIO(const std::string &filename)
Constructor for the BaseIO class.
Definition BaseIO.cpp:151
Generic structure to hold type-erased data and shape.
Definition ReadIO.hpp:34
std::shared_ptr< IO::BaseRecordingData > getDataSet(const std::string &path) override
Returns a pointer to a dataset at a given path.
Definition HDF5IO.cpp:1537
Status openRemote()
Opens a remote file over HTTP(S) in read-only mode using the remfile virtual file driver.
Definition HDF5IO.cpp:103
Status stopRecording() override
Stops the recording process.
Definition HDF5IO.cpp:1318
SizeArray getStorageObjectChunking(const std::string &path) const override
Gets the chunking configuration of a dataset.
Definition HDF5IO.cpp:1459
std::string findObject(const std::string &name, const std::string &starting_path="/") const override
Search for the first object (Group or Dataset) whose last path component matches name,...
Definition HDF5IO.cpp:213
bool m_disableSWMRMode
Tracks whether SWMR mode is disabled for the current recording. Set by startRecording(bool) at the st...
Definition HDF5IO.hpp:574
Status createGroup(const std::string &path) override
Creates a new group in the file.
Definition HDF5IO.cpp:1155
static IO::BaseDataType getBaseDataType(const H5::DataType &nativeType)
Returns the BaseDataType for a given HDF5 native data type.
Definition HDF5IO.cpp:1732
Status createLink(const std::string &path, const std::string &reference) override
Creates a soft link to another location in the file.
Definition HDF5IO.cpp:1187
std::string readReferenceAttribute(const std::string &dataPath) const override
Reads a reference attribute and returns the path to the referenced object.
Definition HDF5IO.cpp:545
Status createReferenceDataSet(const std::string &path, const std::vector< std::string > &references) override
Creates a dataset that holds an array of references to groups within the file.
Definition HDF5IO.cpp:1207
bool attributeExists(const std::string &path) const override
Checks whether an Attribute exists at the location in the file.
Definition HDF5IO.cpp:1367
virtual std::vector< std::pair< std::string, StorageObjectType > > getStorageObjects(const std::string &path, const StorageObjectType &objectType=StorageObjectType::Undefined) const override
Gets the list of storage objects (groups, datasets, attributes) inside a group.
Definition HDF5IO.cpp:1374
BaseDataType getStorageObjectDataType(const std::string &path) const override
Gets the BaseDataType of a dataset or attribute.
Definition HDF5IO.cpp:1492
StorageObjectType getStorageObjectType(std::string path) const override
Get the storage type (Group, Dataset, Attribute) of the object at path.
Definition HDF5IO.cpp:379
AQNWB::IO::DataBlockGeneric readAttribute(const std::string &dataPath) const override
Reads a attribute and determines the data type.
Definition HDF5IO.cpp:608
Status createAttribute(const IO::BaseDataType &type, const void *data, const std::string &path, const std::string &name, const SizeType &size=1) override
Creates an attribute at a given location in the file.
Definition HDF5IO.cpp:924
Status startRecording() override
Start SWMR write to start recording process.
Definition HDF5IO.cpp:1297
std::unique_ptr< H5::Attribute > getAttribute(const std::string &path) const
Definition HDF5IO.cpp:346
std::vector< std::string > readStringDataHelper(const HDF5TYPE &dataSource, size_t numElements, const H5::DataSpace &memspace, const H5::DataSpace &dataspace) const
Reads a variable-length string from an HDF5 dataset or attribute.
Definition HDF5IO.cpp:439
SizeArray getStorageObjectShape(const std::string &path) const override
Returns the size of the dataset or attribute for each dimension.
Definition HDF5IO.cpp:1431
std::unique_ptr< IO::BaseRecordingData > createArrayDataSet(const IO::BaseArrayDataSetConfig &config, const std::string &path) override
Creates an extendable dataset with the given configuration and path.
Definition HDF5IO.cpp:1560
Status createReferenceAttribute(const std::string &referencePath, const std::string &path, const std::string &name) override
Sets an object reference attribute for a given location in the file.
Definition HDF5IO.cpp:1115
Status close() override
Closes the file.
Definition HDF5IO.cpp:173
bool canModifyObjects() override
Checks whether the file is in a mode where objects can be added or deleted. Note, this does not apply...
Definition HDF5IO.cpp:1333
HDF5IO(const std::string &fileName)
Constructor for the HDF5IO class that takes a file name as input.
Definition HDF5IO.cpp:33
H5O_type_t getH5ObjectType(const std::string &path) const
Returns the HDF5 type of object at a given path.
Definition HDF5IO.cpp:1649
AQNWB::IO::DataBlockGeneric readDataset(const std::string &dataPath, const SizeArray &start={}, const SizeArray &count={}, const SizeArray &stride={}, const SizeArray &block={}) override
Reads a dataset or attribute and determines the data type.
Definition HDF5IO.cpp:740
Status createStringDataSet(const std::string &path, const std::string &value) override
Creates a non-modifiable dataset with a string value.
Definition HDF5IO.cpp:1254
static H5::DataType getH5Type(IO::BaseDataType type)
Returns the HDF5 data type for a given base data type.
Definition HDF5IO.cpp:1775
bool objectExists(const std::string &path) const override
Checks whether a Dataset, Group, or Link already exists at the location in the file.
Definition HDF5IO.cpp:1357
std::vector< T > readDataHelper(const HDF5TYPE &dataSource, size_t numElements, const H5::DataSpace &memspace, const H5::DataSpace &dataspace) const
Reads data from an HDF5 dataset or attribute into a vector of the appropriate type.
Definition HDF5IO.cpp:407
Status openS3(const std::string &aws_region="", const std::string &secret_id="", const std::string &secret_key="")
Opens an existing file in read-only mode using the ROS3 driver.
Definition HDF5IO.cpp:57
Status closeFileImpl()
Non-virtual helper that performs the actual HDF5 file close.
Definition HDF5IO.cpp:179
static H5::DataType getNativeType(IO::BaseDataType type)
Returns the HDF5 native data type for a given base data type.
Definition HDF5IO.cpp:1674
Status open() override
Opens an existing file or creates a new file for writing.
Definition HDF5IO.cpp:126
Status flush() override
Flush data to disk.
Definition HDF5IO.cpp:196
Status createGroupIfDoesNotExist(const std::string &path) override
Creates a new group if it does not exist.
Definition HDF5IO.cpp:1173
~HDF5IO() override
Destructor.
Definition HDF5IO.cpp:39
std::unique_ptr< H5::H5File > m_file
Unique pointer to the HDF5 file for reading.
Definition HDF5IO.hpp:568
Namespace for all components of the HDF5 I/O backend.
FileMode
The access mode for the file.
Definition BaseIO.hpp:173
Definition HDF5IO.hpp:14