aqnwb 0.1.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:
50 HDF5IO(const std::string& fileName, const bool disableSWMRMode = false);
51
55 ~HDF5IO();
56
61 Status open() override;
62
68 Status open(FileMode mode) override;
69
74 Status close() override;
75
80 Status flush() override;
81
90 StorageObjectType getStorageObjectType(std::string path) const override;
91
106 const std::string& dataPath,
107 const std::vector<SizeType>& start = {},
108 const std::vector<SizeType>& count = {},
109 const std::vector<SizeType>& stride = {},
110 const std::vector<SizeType>& block = {}) override;
111
126 const std::string& dataPath) const override;
127
134 std::string readReferenceAttribute(
135 const std::string& dataPath) const override;
136
147 const void* data,
148 const std::string& path,
149 const std::string& name,
150 const SizeType& size = 1) override;
151
160 Status createAttribute(const std::string& data,
161 const std::string& path,
162 const std::string& name,
163 const bool overwrite = false) override;
164
175 Status createAttribute(const std::vector<std::string>& data,
176 const std::string& path,
177 const std::string& name,
178 const bool overwrite = false) override;
179
187 Status createReferenceAttribute(const std::string& referencePath,
188 const std::string& path,
189 const std::string& name) override;
190
196 Status createGroup(const std::string& path) override;
197
205 Status createLink(const std::string& path,
206 const std::string& reference) override;
207
214 Status createStringDataSet(const std::string& path,
215 const std::string& value) override;
216
223 Status createStringDataSet(const std::string& path,
224 const std::vector<std::string>& values) override;
225
234 const std::string& path,
235 const std::vector<std::string>& references) override;
236
241 Status startRecording() override;
242
247 Status stopRecording() override;
248
255 bool canModifyObjects() override;
256
264 std::unique_ptr<IO::BaseRecordingData> createArrayDataSet(
265 const IO::ArrayDataSetConfig& config, const std::string& path) override;
266
272 std::unique_ptr<IO::BaseRecordingData> getDataSet(
273 const std::string& path) override;
274
280 std::vector<SizeType> getStorageObjectShape(const std::string path) override;
281
288 bool objectExists(const std::string& path) const override;
289
298 bool attributeExists(const std::string& path) const override;
299
318 virtual std::vector<std::pair<std::string, StorageObjectType>>
319 getStorageObjects(const std::string& path,
320 const StorageObjectType& objectType =
321 StorageObjectType::Undefined) const override;
322
329 H5O_type_t getH5ObjectType(const std::string& path) const;
330
341 static H5::DataType getNativeType(IO::BaseDataType type);
342
351 static IO::BaseDataType getBaseDataType(const H5::DataType& nativeType);
352
362 static H5::DataType getH5Type(IO::BaseDataType type);
363
364protected:
370 Status createGroupIfDoesNotExist(const std::string& path) override;
371
372private:
390 template<typename T, typename HDF5TYPE>
391 std::vector<T> readDataHelper(const HDF5TYPE& dataSource,
392 size_t numElements,
393 const H5::DataSpace& memspace,
394 const H5::DataSpace& dataspace) const;
395
413 template<typename T, typename HDF5TYPE>
414 std::vector<T> readDataHelper(const HDF5TYPE& dataSource,
415 size_t numElements) const;
416
429 template<typename HDF5TYPE>
430 std::vector<std::string> readStringDataHelper(
431 const HDF5TYPE& dataSource,
432 size_t numElements,
433 const H5::DataSpace& memspace,
434 const H5::DataSpace& dataspace) const;
435
451 template<typename HDF5TYPE>
452 std::vector<std::string> readStringDataHelper(const HDF5TYPE& dataSource,
453 size_t numElements) const;
454
455 std::unique_ptr<H5::Attribute> getAttribute(const std::string& path) const;
456
460 std::unique_ptr<H5::H5File> m_file;
461
467};
468
469} // namespace AQNWB::IO::HDF5
AQNWB::Types::StorageObjectType StorageObjectType
Definition BaseIO.hpp:21
AQNWB::Types::Status Status
Definition BaseIO.hpp:22
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
The configuration for an array dataset.
Definition BaseIO.hpp:200
Represents a base data type.
Definition BaseIO.hpp:43
BaseIO(const std::string &filename)
Constructor for the BaseIO class.
Definition BaseIO.cpp:45
Generic structure to hold type-erased data and shape.
Definition ReadIO.hpp:30
std::unique_ptr< IO::BaseRecordingData > createArrayDataSet(const IO::ArrayDataSetConfig &config, const std::string &path) override
Creates an extendable dataset with the given configuration and path.
Definition HDF5IO.cpp:1217
Status stopRecording() override
Stops the recording process.
Definition HDF5IO.cpp:1072
bool m_disableSWMRMode
When set true, then do not switch to SWMR mode when starting the recording.
Definition HDF5IO.hpp:466
Status createGroup(const std::string &path) override
Creates a new group in the file.
Definition HDF5IO.cpp:931
static IO::BaseDataType getBaseDataType(const H5::DataType &nativeType)
Returns the BaseDataType for a given HDF5 native data type.
Definition HDF5IO.cpp:1362
Status createLink(const std::string &path, const std::string &reference) override
Creates a soft link to another location in the file.
Definition HDF5IO.cpp:963
std::string readReferenceAttribute(const std::string &dataPath) const override
Reads a reference attribute and returns the path to the referenced object.
Definition HDF5IO.cpp:305
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:978
std::vector< SizeType > getStorageObjectShape(const std::string path) override
Returns the size of the dataset or attribute for each dimension.
Definition HDF5IO.cpp:1176
bool attributeExists(const std::string &path) const override
Checks whether an Attribute exists at the location in the file.
Definition HDF5IO.cpp:1112
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:1119
StorageObjectType getStorageObjectType(std::string path) const override
Get the storage type (Group, Dataset, Attribute) of the object at path.
Definition HDF5IO.cpp:139
AQNWB::IO::DataBlockGeneric readAttribute(const std::string &dataPath) const override
Reads a attribute and determines the data type.
Definition HDF5IO.cpp:368
~HDF5IO()
Destructor for the HDF5IO class.
Definition HDF5IO.cpp:26
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:685
AQNWB::IO::DataBlockGeneric readDataset(const std::string &dataPath, const std::vector< SizeType > &start={}, const std::vector< SizeType > &count={}, const std::vector< SizeType > &stride={}, const std::vector< SizeType > &block={}) override
Reads a dataset or attribute and determines the data type.
Definition HDF5IO.cpp:500
Status startRecording() override
Start SWMR write to start recording process.
Definition HDF5IO.cpp:1060
std::unique_ptr< H5::Attribute > getAttribute(const std::string &path) const
Definition HDF5IO.cpp:103
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:199
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:883
Status close() override
Closes the file.
Definition HDF5IO.cpp:78
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:1083
std::unique_ptr< IO::BaseRecordingData > getDataSet(const std::string &path) override
Returns a pointer to a dataset at a given path.
Definition HDF5IO.cpp:1194
HDF5IO(const std::string &fileName, const bool disableSWMRMode=false)
Constructor for the HDF5IO class that takes a file name as input.
Definition HDF5IO.cpp:20
H5O_type_t getH5ObjectType(const std::string &path) const
Returns the HDF5 type of object at a given path.
Definition HDF5IO.cpp:1286
Status createStringDataSet(const std::string &path, const std::string &value) override
Creates a non-modifiable dataset with a string value.
Definition HDF5IO.cpp:1021
static H5::DataType getH5Type(IO::BaseDataType type)
Returns the HDF5 data type for a given base data type.
Definition HDF5IO.cpp:1405
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:1102
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:167
static H5::DataType getNativeType(IO::BaseDataType type)
Returns the HDF5 native data type for a given base data type.
Definition HDF5IO.cpp:1311
Status open() override
Opens an existing file or creates a new file for writing.
Definition HDF5IO.cpp:31
Status flush() override
Flush data to disk.
Definition HDF5IO.cpp:97
Status createGroupIfDoesNotExist(const std::string &path) override
Creates a new group if it does not exist.
Definition HDF5IO.cpp:949
std::unique_ptr< H5::H5File > m_file
Unique pointer to the HDF5 file for reading.
Definition HDF5IO.hpp:460
Definition HDF5ArrayDataSetConfig.hpp:14
FileMode
The access mode for the file.
Definition BaseIO.hpp:168
Definition HDF5IO.hpp:14