aqnwb 0.1.0
Loading...
Searching...
No Matches
BaseIO.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <cstdint>
5#include <iostream>
6#include <memory>
7#include <string>
8#include <typeindex>
9#include <unordered_map>
10#include <unordered_set>
11#include <variant>
12#include <vector>
13
14#include <boost/multi_array.hpp> // TODO move this and function def to the cpp file
15
16#include "Types.hpp"
17
18#define DEFAULT_STR_SIZE 256
19#define DEFAULT_ARRAY_SIZE 1
20
25
30namespace AQNWB::IO
31{
32
34
43{
44public:
63
69 BaseDataType(Type t = T_I32, SizeType s = 1);
70
73
74 // handy accessors
75 static const BaseDataType U8;
76 static const BaseDataType U16;
77 static const BaseDataType U32;
78 static const BaseDataType U64;
79 static const BaseDataType I8;
80 static const BaseDataType I16;
81 static const BaseDataType I32;
82 static const BaseDataType I64;
83 static const BaseDataType F32;
84 static const BaseDataType F64;
85 static const BaseDataType DSTR;
86 static BaseDataType STR(
87 SizeType size);
88
89 // Define the equality operator
90 bool operator==(const BaseDataType& other) const
91 {
92 return type == other.type && typeSize == other.typeSize;
93 }
94
95 // Variant data type for representing any 1D vector with BaseDataType values
96 using BaseDataVectorVariant = std::variant<std::monostate,
97 std::vector<uint8_t>,
98 std::vector<uint16_t>,
99 std::vector<uint32_t>,
100 std::vector<uint64_t>,
101 std::vector<int8_t>,
102 std::vector<int16_t>,
103 std::vector<int32_t>,
104 std::vector<int64_t>,
105 std::vector<float>,
106 std::vector<double>,
107 std::vector<std::string>>;
108
117 static BaseDataType fromTypeId(const std::type_index& typeIndex)
118 {
119 if (typeIndex == typeid(uint8_t)) {
120 return BaseDataType(U8);
121 } else if (typeIndex == typeid(uint16_t)) {
122 return BaseDataType(U16);
123 } else if (typeIndex == typeid(uint32_t)) {
124 return BaseDataType(U32);
125 } else if (typeIndex == typeid(uint64_t)) {
126 return BaseDataType(U64);
127 } else if (typeIndex == typeid(int8_t)) {
128 return BaseDataType(I8);
129 } else if (typeIndex == typeid(int16_t)) {
130 return BaseDataType(I16);
131 } else if (typeIndex == typeid(int32_t)) {
132 return BaseDataType(I32);
133 } else if (typeIndex == typeid(int64_t)) {
134 return BaseDataType(I64);
135 } else if (typeIndex == typeid(float)) {
136 return BaseDataType(F32);
137 } else if (typeIndex == typeid(double)) {
138 return BaseDataType(F64);
139 } else {
140 throw std::runtime_error("Unsupported data type");
141 }
142 }
143};
144
145class DataBlockGeneric;
146
151enum class SearchMode
152{
162};
163
167enum class FileMode
168{
173
181
189};
190
200{
201public:
210 const SizeArray& shape,
211 const SizeArray& chunking);
212
216 virtual ~ArrayDataSetConfig() = default;
217
222 inline BaseDataType getType() const { return m_type; }
223
228 inline SizeArray getShape() const { return m_shape; }
229
234 inline SizeArray getChunking() const { return m_chunking; }
235
236protected:
237 // The data type of the dataset
239 // The shape of the dataset
241 // The chunking of the dataset
243};
244
255{
256public:
260 BaseIO(const std::string& filename);
261
265 BaseIO(const BaseIO&) = delete;
266
270 BaseIO& operator=(const BaseIO&) = delete;
271
275 virtual ~BaseIO();
276
281 virtual std::string getFileName() const { return m_filename; }
282
291 virtual StorageObjectType getStorageObjectType(std::string path) const = 0;
292
297 virtual Status open() = 0;
298
304 virtual Status open(FileMode mode) = 0;
305
310 virtual Status close() = 0;
311
316 virtual Status flush() = 0;
317
324 virtual bool objectExists(const std::string& path) const = 0;
325
334 virtual bool attributeExists(const std::string& path) const = 0;
335
353 virtual std::vector<std::pair<std::string, StorageObjectType>>
354 getStorageObjects(const std::string& path,
355 const StorageObjectType& objectType =
356 StorageObjectType::Undefined) const = 0;
357
383 virtual std::unordered_map<std::string, std::string> findTypes(
384 const std::string& starting_path,
385 const std::unordered_set<std::string>& types,
386 SearchMode search_mode,
387 bool exclude_starting_path = false) const;
388
405 const std::string& dataPath,
406 const std::vector<SizeType>& start = {},
407 const std::vector<SizeType>& count = {},
408 const std::vector<SizeType>& stride = {},
409 const std::vector<SizeType>& block = {}) = 0;
410
422 virtual DataBlockGeneric readAttribute(const std::string& dataPath) const = 0;
423
430 virtual std::string readReferenceAttribute(
431 const std::string& dataPath) const = 0;
432
443 const void* data,
444 const std::string& path,
445 const std::string& name,
446 const SizeType& size = 1) = 0;
447
456 virtual Status createAttribute(const std::string& data,
457 const std::string& path,
458 const std::string& name,
459 const bool overwrite = false) = 0;
460
471 virtual Status createAttribute(const std::vector<std::string>& data,
472 const std::string& path,
473 const std::string& name,
474 const bool overwrite = false) = 0;
475
483 virtual Status createReferenceAttribute(const std::string& referencePath,
484 const std::string& path,
485 const std::string& name) = 0;
486
492 virtual Status createGroup(const std::string& path) = 0;
493
501 virtual Status createLink(const std::string& path,
502 const std::string& reference) = 0;
503
510 virtual Status createStringDataSet(const std::string& path,
511 const std::string& value) = 0;
512
520 const std::string& path, const std::vector<std::string>& values) = 0;
521
530 const std::string& path, const std::vector<std::string>& references) = 0;
531
536 virtual Status startRecording() = 0;
537
542 virtual Status stopRecording() = 0;
543
551 virtual bool canModifyObjects() { return true; }
552
560 virtual std::unique_ptr<BaseRecordingData> createArrayDataSet(
561 const ArrayDataSetConfig& config, const std::string& path) = 0;
562
568 virtual std::unique_ptr<BaseRecordingData> getDataSet(
569 const std::string& path) = 0;
570
576 virtual std::vector<SizeType> getStorageObjectShape(
577 const std::string path) = 0;
578
586 Status createCommonNWBAttributes(const std::string& path,
587 const std::string& objectNamespace,
588 const std::string& neurodataType = "");
589
594 inline bool isOpen() const { return m_opened; }
595
600 inline bool isReadyToOpen() const { return m_readyToOpen; }
601
602protected:
606 const std::string m_filename;
607
613 virtual Status createGroupIfDoesNotExist(const std::string& path) = 0;
614
619
624};
625
632{
633public:
638
643
648
652 virtual ~BaseRecordingData();
653
663 Status writeDataBlock(const std::vector<SizeType>& dataShape,
664 const BaseDataType& type,
665 const void* data);
666
675 virtual Status writeDataBlock(const std::vector<SizeType>& dataShape,
676 const std::vector<SizeType>& positionOffset,
677 const BaseDataType& type,
678 const void* data) = 0;
679
690 virtual Status writeDataBlock(const std::vector<SizeType>& dataShape,
691 const std::vector<SizeType>& positionOffset,
692 const BaseDataType& type,
693 const std::vector<std::string>& data) = 0;
694
699 inline SizeType getNumDimensions() const { return m_shape.size(); }
700
705 inline const std::vector<SizeType>& getShape() const { return m_shape; }
706
711 inline const std::vector<SizeType>& getPosition() const { return m_position; }
712
713protected:
717 std::vector<SizeType> m_shape;
718
722 std::vector<SizeType> m_position;
723};
724
725} // namespace AQNWB::IO
AQNWB::Types::StorageObjectType StorageObjectType
Definition BaseIO.hpp:21
AQNWB::Types::Status Status
Definition BaseIO.hpp:22
AQNWB::Types::SizeArray SizeArray
Definition BaseIO.hpp:23
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
The configuration for an array dataset.
Definition BaseIO.hpp:200
virtual ~ArrayDataSetConfig()=default
Virtual destructor to ensure proper cleanup in derived classes.
SizeArray m_chunking
Definition BaseIO.hpp:242
SizeArray m_shape
Definition BaseIO.hpp:240
BaseDataType m_type
Definition BaseIO.hpp:238
SizeArray getShape() const
Returns the shape of the dataset.
Definition BaseIO.hpp:228
ArrayDataSetConfig(const BaseDataType &type, const SizeArray &shape, const SizeArray &chunking)
Constructs an ArrayDataSetConfig object with the specified type, shape, and chunking.
Definition BaseIO.cpp:34
BaseDataType getType() const
Returns the data type of the dataset.
Definition BaseIO.hpp:222
SizeArray getChunking() const
Returns the chunking of the dataset.
Definition BaseIO.hpp:234
Represents a base data type.
Definition BaseIO.hpp:43
static const BaseDataType F32
Accessor for 32-bit floating point.
Definition BaseIO.hpp:83
static const BaseDataType I64
Accessor for signed 64-bit integer.
Definition BaseIO.hpp:82
static BaseDataType STR(SizeType size)
Accessor for string with specified size.
Definition BaseIO.cpp:16
static const BaseDataType U32
Accessor for unsigned 32-bit integer.
Definition BaseIO.hpp:77
static const BaseDataType DSTR
Accessor for dynamic string.
Definition BaseIO.hpp:85
Type
Enumeration of different data types.
Definition BaseIO.hpp:49
@ V_STR
Variable length string.
Definition BaseIO.hpp:61
@ T_I32
Signed 32-bit integer.
Definition BaseIO.hpp:56
@ T_I64
Signed 64-bit integer.
Definition BaseIO.hpp:57
@ T_I8
Signed 8-bit integer.
Definition BaseIO.hpp:54
@ T_U32
Unsigned 32-bit integer.
Definition BaseIO.hpp:52
@ T_U64
Unsigned 64-bit integer.
Definition BaseIO.hpp:53
@ T_U8
Unsigned 8-bit integer.
Definition BaseIO.hpp:50
@ T_F32
32-bit floating point
Definition BaseIO.hpp:58
@ T_STR
String.
Definition BaseIO.hpp:60
@ T_F64
64-bit floating point
Definition BaseIO.hpp:59
@ T_U16
Unsigned 16-bit integer.
Definition BaseIO.hpp:51
@ T_I16
Signed 16-bit integer.
Definition BaseIO.hpp:55
static const BaseDataType U64
Accessor for unsigned 64-bit integer.
Definition BaseIO.hpp:78
static const BaseDataType U16
Accessor for unsigned 16-bit integer.
Definition BaseIO.hpp:76
static const BaseDataType F64
Accessor for 64-bit floating point.
Definition BaseIO.hpp:84
static const BaseDataType U8
Accessor for unsigned 8-bit integer.
Definition BaseIO.hpp:75
static const BaseDataType I32
Accessor for signed 32-bit integer.
Definition BaseIO.hpp:81
static const BaseDataType I16
Accessor for signed 16-bit integer.
Definition BaseIO.hpp:80
BaseDataType(Type t=T_I32, SizeType s=1)
Constructs a BaseDataType object with the specified type and size.
Definition BaseIO.cpp:10
Type type
The data type.
Definition BaseIO.hpp:71
std::variant< std::monostate, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< uint64_t >, std::vector< int8_t >, std::vector< int16_t >, std::vector< int32_t >, std::vector< int64_t >, std::vector< float >, std::vector< double >, std::vector< std::string > > BaseDataVectorVariant
Definition BaseIO.hpp:96
SizeType typeSize
The size of the data type.
Definition BaseIO.hpp:72
static BaseDataType fromTypeId(const std::type_index &typeIndex)
Get the BaseDataType from a std::type_index.
Definition BaseIO.hpp:117
bool operator==(const BaseDataType &other) const
Definition BaseIO.hpp:90
static const BaseDataType I8
Accessor for signed 8-bit integer.
Definition BaseIO.hpp:79
virtual Status startRecording()=0
Starts the recording process.
virtual bool objectExists(const std::string &path) const =0
Checks whether a Dataset, Group, or Link already exists at the location in the file.
virtual StorageObjectType getStorageObjectType(std::string path) const =0
Get the storage type (Group, Dataset, Attribute) of the object at path.
virtual bool attributeExists(const std::string &path) const =0
Checks whether an Attribute exists at the location in the file.
BaseIO & operator=(const BaseIO &)=delete
Assignment operator is deleted to prevent copying.
virtual Status createReferenceAttribute(const std::string &referencePath, const std::string &path, const std::string &name)=0
Sets an object reference attribute for a given location in the file.
virtual Status createStringDataSet(const std::string &path, const std::string &value)=0
Creates a non-modifiable dataset with a string value.
BaseIO(const std::string &filename)
Constructor for the BaseIO class.
Definition BaseIO.cpp:45
bool isOpen() const
Returns true if the file is open.
Definition BaseIO.hpp:594
virtual std::vector< SizeType > getStorageObjectShape(const std::string path)=0
Returns the size of the dataset or attribute for each dimension.
virtual Status createAttribute(const BaseDataType &type, const void *data, const std::string &path, const std::string &name, const SizeType &size=1)=0
Creates an attribute at a given location in the file.
virtual Status createAttribute(const std::string &data, const std::string &path, const std::string &name, const bool overwrite=false)=0
Creates a string attribute at a given location in the file.
virtual 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={})=0
Reads a dataset and determines the data type.
virtual ~BaseIO()
Destructor the BaseIO class.
Definition BaseIO.cpp:52
virtual std::unordered_map< std::string, std::string > findTypes(const std::string &starting_path, const std::unordered_set< std::string > &types, SearchMode search_mode, bool exclude_starting_path=false) const
Finds all datasets and groups of the given types in the HDF5 file.
Definition BaseIO.cpp:65
virtual Status createGroup(const std::string &path)=0
Creates a new group in the file.
Status createCommonNWBAttributes(const std::string &path, const std::string &objectNamespace, const std::string &neurodataType="")
Convenience function for creating NWB related attributes.
Definition BaseIO.cpp:54
virtual Status createLink(const std::string &path, const std::string &reference)=0
Creates a soft link to another location in the file.
const std::string m_filename
The name of the file.
Definition BaseIO.hpp:606
virtual std::unique_ptr< BaseRecordingData > getDataSet(const std::string &path)=0
Returns a pointer to a dataset at a given path.
bool m_readyToOpen
Whether the file is ready to be opened.
Definition BaseIO.hpp:618
virtual Status flush()=0
Flush data to disk.
bool isReadyToOpen() const
Returns true if the file is able to be opened.
Definition BaseIO.hpp:600
virtual std::vector< std::pair< std::string, StorageObjectType > > getStorageObjects(const std::string &path, const StorageObjectType &objectType=StorageObjectType::Undefined) const =0
Gets the list of storage objects (groups, datasets, attributes) inside a group.
virtual Status createAttribute(const std::vector< std::string > &data, const std::string &path, const std::string &name, const bool overwrite=false)=0
Creates an array of variable length strings attribute at a given location in the file.
virtual Status createGroupIfDoesNotExist(const std::string &path)=0
Creates a new group if it does not already exist.
virtual DataBlockGeneric readAttribute(const std::string &dataPath) const =0
Reads a attribute and determines the data type.
virtual std::string getFileName() const
Returns the full path to the file.
Definition BaseIO.hpp:281
virtual std::unique_ptr< BaseRecordingData > createArrayDataSet(const ArrayDataSetConfig &config, const std::string &path)=0
Creates an extendable dataset with the given configuration and path.
virtual Status stopRecording()=0
Stops the recording process.
virtual Status close()=0
Closes the file.
virtual std::string readReferenceAttribute(const std::string &dataPath) const =0
Reads a reference attribute and returns the path to the referenced object.
virtual bool canModifyObjects()
Returns true if the file is in a mode where objects can be added or deleted. Note,...
Definition BaseIO.hpp:551
BaseIO(const BaseIO &)=delete
Copy constructor is deleted to prevent construction-copying.
virtual Status open(FileMode mode)=0
Opens an existing file or creates a new file for writing.
virtual Status open()=0
Opens the file for writing.
virtual Status createReferenceDataSet(const std::string &path, const std::vector< std::string > &references)=0
Creates a dataset that holds an array of references to groups within the file.
virtual Status createStringDataSet(const std::string &path, const std::vector< std::string > &values)=0
Creates a dataset that holds an array of string values.
bool m_opened
Whether the file is currently open.
Definition BaseIO.hpp:623
The base class to represent recording data that can be extended.
Definition BaseIO.hpp:632
virtual Status writeDataBlock(const std::vector< SizeType > &dataShape, const std::vector< SizeType > &positionOffset, const BaseDataType &type, const std::vector< std::string > &data)=0
Writes a block of string data (any number of dimensions).
SizeType getNumDimensions() const
Get the number of dimensions in the dataset.
Definition BaseIO.hpp:699
BaseRecordingData()
Default constructor.
Definition BaseIO.cpp:167
const std::vector< SizeType > & getPosition() const
Get the current position in the dataset.
Definition BaseIO.hpp:711
Status writeDataBlock(const std::vector< SizeType > &dataShape, const BaseDataType &type, const void *data)
Writes a block of data using the stored position information. This is not intended to be overwritten ...
Definition BaseIO.cpp:173
std::vector< SizeType > m_position
The current position in the dataset.
Definition BaseIO.hpp:722
BaseRecordingData(const BaseRecordingData &)=delete
Deleted copy constructor to prevent construction-copying.
virtual Status writeDataBlock(const std::vector< SizeType > &dataShape, const std::vector< SizeType > &positionOffset, const BaseDataType &type, const void *data)=0
Writes a block of data (any number of dimensions).
BaseRecordingData & operator=(const BaseRecordingData &)=delete
Deleted copy assignment operator to prevent copying.
std::vector< SizeType > m_shape
The size of the dataset in each dimension.
Definition BaseIO.hpp:717
virtual ~BaseRecordingData()
Destructor.
Definition BaseIO.cpp:169
const std::vector< SizeType > & getShape() const
Get the size of the dataset.
Definition BaseIO.hpp:705
Generic structure to hold type-erased data and shape.
Definition ReadIO.hpp:30
StorageObjectType
Types of object used in the NWB schema.
Definition Types.hpp:53
Status
Represents the status of an operation.
Definition Types.hpp:22
std::vector< size_t > SizeArray
Alias for an array of size types used in the project.
Definition Types.hpp:81
size_t SizeType
Alias for the size type used in the project.
Definition Types.hpp:76
Definition BaseIO.hpp:31
FileMode
The access mode for the file.
Definition BaseIO.hpp:168
@ ReadOnly
Opens the file in read only mode.
Definition BaseIO.hpp:188
@ ReadWrite
Opens the file with both read and write access.
Definition BaseIO.hpp:180
@ Overwrite
Opens the file and overwrites any existing file.
Definition BaseIO.hpp:172
SearchMode
Enum class for specifying the search mode for findTypes.
Definition BaseIO.hpp:152
@ STOP_ON_TYPE
Stop searching inside an object once a matching type is found.
Definition BaseIO.hpp:156
@ CONTINUE_ON_TYPE
Continue searching inside an object even after a matching type is found.
Definition BaseIO.hpp:161