aqnwb 0.1.0
Loading...
Searching...
No Matches
ReadIO.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 <vector>
10
11#include <boost/multi_array.hpp> // TODO move this and function def to the cpp file
12
13#include "Types.hpp"
14
17
22namespace AQNWB::IO
23{
24
29{
30public:
38 std::any data;
43 std::vector<SizeType> shape;
44
63 std::type_index typeIndex = typeid(void);
64
67
71 DataBlockGeneric() = default;
72
76 DataBlockGeneric(const std::any& inData,
77 const std::vector<SizeType>& inShape,
78 const std::type_index& inTypeIndex)
79 : data(inData)
80 , shape(inShape)
81 , typeIndex(inTypeIndex)
82 {
83 }
84};
85
91template<typename DTYPE>
93{
94public:
98 std::vector<DTYPE> data;
103 std::vector<SizeType> shape;
108 const std::type_index typeIndex = typeid(DTYPE);
109
113 DataBlock(const std::vector<DTYPE>& inData,
114 const std::vector<SizeType>& inShape)
115 : data(inData)
116 , shape(inShape)
117 {
118 }
119
128 template<std::size_t NDIMS>
129 inline boost::const_multi_array_ref<DTYPE, NDIMS> as_multi_array() const
130 {
131 if (shape.size() != NDIMS) {
132 throw std::invalid_argument(
133 "Shape size does not match the number of dimensions.");
134 }
135
136 // Calculate the total number of elements expected
137 SizeType expected_size = 1;
138 for (SizeType dim : shape) {
139 expected_size *= dim;
140 }
141
142 if (data.size() != expected_size) {
143 throw std::invalid_argument("Data size does not match the shape.");
144 }
145
146 // Convert the shape vector to a boost::array
147 boost::array<std::size_t, NDIMS> boost_shape;
148 std::copy(shape.begin(), shape.end(), boost_shape.begin());
149
150 // Construct and return the boost::const_multi_array_ref
151 return boost::const_multi_array_ref<DTYPE, NDIMS>(data.data(), boost_shape);
152 }
153
165 const DataBlockGeneric& genericData)
166 {
167 auto result = DataBlock<DTYPE>(
168 std::any_cast<std::vector<DTYPE>>(genericData.data), genericData.shape);
169 return result;
170 }
171};
172
175template<StorageObjectType T>
176struct isAllowedStorageObjectType : std::false_type
177{
178};
179
182template<>
183struct isAllowedStorageObjectType<StorageObjectType::Dataset> : std::true_type
184{
185};
186
189template<>
190struct isAllowedStorageObjectType<StorageObjectType::Attribute> : std::true_type
191{
192};
193
202template<StorageObjectType OTYPE, typename VTYPE = std::any>
204{
205 // Embedded traits for compile time checking of allowed OTYPE for the class
206 // and methods
207private:
216 template<StorageObjectType U>
218 : std::integral_constant<bool, (U == StorageObjectType::Dataset)>
219 {
220 };
221
229 "StorageObjectType not allowed for ReadDataWrapper");
230
231 // Actual definition of the class
232public:
239 ReadDataWrapper(const std::shared_ptr<IO::BaseIO> io, const std::string& path)
240 : m_io(io)
241 , m_path(path)
242 {
243 }
244
249 inline StorageObjectType getStorageObjectType() const { return OTYPE; }
250
255 template<typename T>
256 static constexpr bool isType()
257 {
258 return std::is_same_v<VTYPE, T>;
259 }
260
265
270
274 virtual ~ReadDataWrapper() {}
275
280 inline std::string getPath() const { return m_path; }
281
286 inline std::shared_ptr<IO::BaseIO> getIO() const { return m_io; }
287
292 inline bool exists() const
293 {
294 switch (OTYPE) {
295 case StorageObjectType::Dataset: {
296 return m_io->objectExists(m_path);
297 }
298 case StorageObjectType::Attribute: {
299 return m_io->attributeExists(m_path);
300 }
301 default: {
302 throw std::runtime_error("Unsupported StorageObjectType");
303 }
304 }
305 }
306
315 {
316 switch (OTYPE) {
317 case StorageObjectType::Dataset: {
318 return m_io->readDataset(m_path);
319 }
320 case StorageObjectType::Attribute: {
321 return m_io->readAttribute(m_path);
322 }
323 default: {
324 throw std::runtime_error("Unsupported StorageObjectType");
325 }
326 }
327 }
328
343 template<StorageObjectType U = OTYPE,
344 typename std::enable_if<isDataset<U>::value, int>::type = 0>
346 const std::vector<SizeType>& start,
347 const std::vector<SizeType>& count = {},
348 const std::vector<SizeType>& stride = {},
349 const std::vector<SizeType>& block = {}) const
350 {
351 // The function is only enabled for datasets so we don't need to check
352 // for attributes here.
353 return m_io->readDataset(m_path, start, count, stride, block);
354 }
355
370 template<typename T = VTYPE>
372 {
374 }
375
399 template<typename T = VTYPE,
400 StorageObjectType U = OTYPE,
401 typename std::enable_if<isDataset<U>::value, int>::type = 0>
402 inline DataBlock<VTYPE> values(const std::vector<SizeType>& start,
403 const std::vector<SizeType>& count = {},
404 const std::vector<SizeType>& stride = {},
405 const std::vector<SizeType>& block = {}) const
406 {
407 // The function is only enabled for datasets so we don't need to check
408 // for attributes here.
410 this->valuesGeneric(start, count, stride, block));
411 }
412
413protected:
417 const std::shared_ptr<IO::BaseIO> m_io;
421 std::string m_path;
422}; // ReadDataWrapper
423
424} // namespace AQNWB::IO
AQNWB::Types::StorageObjectType StorageObjectType
Definition BaseIO.hpp:19
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
Represents a base data type.
Definition BaseIO.hpp:41
Generic structure to hold type-erased data and shape.
Definition ReadIO.hpp:29
std::vector< SizeType > shape
The 1D vector with the n-dimensional shape of the data. Set to empty in case of scalar data.
Definition ReadIO.hpp:43
IO::BaseDataType baseDataType
The base data type for the data block.
Definition ReadIO.hpp:66
DataBlockGeneric()=default
Default constructor.
std::any data
Definition ReadIO.hpp:38
DataBlockGeneric(const std::any &inData, const std::vector< SizeType > &inShape, const std::type_index &inTypeIndex)
Parameterized constructor.
Definition ReadIO.hpp:76
std::type_index typeIndex
Type index of the values stored in the data vector.
Definition ReadIO.hpp:63
Structure to hold data and shape for a typed data vector.
Definition ReadIO.hpp:93
DataBlock(const std::vector< DTYPE > &inData, const std::vector< SizeType > &inShape)
Definition ReadIO.hpp:113
static DataBlock< DTYPE > fromGeneric(const DataBlockGeneric &genericData)
Factory method to create an DataBlock from a DataBlockGeneric.
Definition ReadIO.hpp:164
boost::const_multi_array_ref< DTYPE, NDIMS > as_multi_array() const
Transform the data to a boost multi-dimensional array for convenient access.
Definition ReadIO.hpp:129
const std::type_index typeIndex
Type index of the values stored in the data vector. Here this is fixed to typeid(DTYPE)
Definition ReadIO.hpp:108
std::vector< DTYPE > data
Definition ReadIO.hpp:98
std::vector< SizeType > shape
The 1D vector with the n-dimensional shape of the data. Set to empty in case of scalar data.
Definition ReadIO.hpp:103
static constexpr bool isType()
Function to check at compile-time whether the object is of a particular VTYPE, e.g....
Definition ReadIO.hpp:256
ReadDataWrapper & operator=(const ReadDataWrapper &)=delete
Deleted copy assignment operator to prevent copying.
std::string m_path
Path to the dataset or attribute to read.
Definition ReadIO.hpp:421
DataBlockGeneric valuesGeneric(const std::vector< SizeType > &start, const std::vector< SizeType > &count={}, const std::vector< SizeType > &stride={}, const std::vector< SizeType > &block={}) const
Reads a dataset and determines the data type.
Definition ReadIO.hpp:345
const std::shared_ptr< IO::BaseIO > m_io
Pointer to the I/O object to use for reading.
Definition ReadIO.hpp:417
DataBlock< VTYPE > values() const
Reads an attribute with a specified data type.
Definition ReadIO.hpp:371
DataBlockGeneric valuesGeneric() const
Reads a dataset and determines the data type.
Definition ReadIO.hpp:314
virtual ~ReadDataWrapper()
Destructor.
Definition ReadIO.hpp:274
std::shared_ptr< IO::BaseIO > getIO() const
Get a shared pointer to the IO object.
Definition ReadIO.hpp:286
bool exists() const
Check that the object exists.
Definition ReadIO.hpp:292
ReadDataWrapper(const std::shared_ptr< IO::BaseIO > io, const std::string &path)
Default constructor.
Definition ReadIO.hpp:239
std::string getPath() const
Gets the path of the registered type.
Definition ReadIO.hpp:280
DataBlock< VTYPE > values(const std::vector< SizeType > &start, const std::vector< SizeType > &count={}, const std::vector< SizeType > &stride={}, const std::vector< SizeType > &block={}) const
Reads an dataset with a specified data type.
Definition ReadIO.hpp:402
ReadDataWrapper(const ReadDataWrapper &)=delete
Deleted copy constructor to prevent construction-copying.
StorageObjectType getStorageObjectType() const
Function to return the AQNWB::Types::StorageObjectType OTYPE of the instance.
Definition ReadIO.hpp:249
StorageObjectType
Types of object used in the NWB schema.
Definition Types.hpp:31
size_t SizeType
Alias for the size type used in the project.
Definition Types.hpp:54
Definition BaseIO.hpp:29
Internal embedded Trait to Check the OTYPE Enum Value at compile time.
Definition ReadIO.hpp:219
Definition ReadIO.hpp:177