aqnwb 0.5.0
Loading...
Searching...
No Matches
ReadIO.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <array>
5#include <cassert>
6#include <cstdint>
7#include <iostream>
8#include <memory>
9#include <numeric>
10#include <stdexcept>
11#include <string>
12#include <type_traits>
13#include <typeindex>
14#include <variant>
15#include <vector>
16
17#include "BaseIO.hpp"
18#include "Types.hpp"
19
22
27namespace AQNWB::IO
28{
29
34{
35public:
43 std::any data;
49
68 std::type_index typeIndex = typeid(void);
69
72
76 DataBlockGeneric() = default;
77
81 DataBlockGeneric(const std::any& inData,
82 const SizeArray& inShape,
83 const std::type_index& inTypeIndex,
84 const IO::BaseDataType inBaseDataType)
85 : data(inData)
86 , shape(inShape)
87 , typeIndex(inTypeIndex)
88 , baseDataType(inBaseDataType)
89 {
90 }
91
97 inline BaseDataType getBaseDataType() const { return baseDataType; }
98
110 {
111 try {
112 switch (baseDataType.type) {
114 return std::any_cast<std::vector<uint8_t>>(data);
116 return std::any_cast<std::vector<uint16_t>>(data);
118 return std::any_cast<std::vector<uint32_t>>(data);
120 return std::any_cast<std::vector<uint64_t>>(data);
122 return std::any_cast<std::vector<int8_t>>(data);
124 return std::any_cast<std::vector<int16_t>>(data);
126 return std::any_cast<std::vector<int32_t>>(data);
128 return std::any_cast<std::vector<int64_t>>(data);
130 return std::any_cast<std::vector<float>>(data);
132 return std::any_cast<std::vector<double>>(data);
134 return std::any_cast<std::vector<std::string>>(data);
135 default:
136 return std::monostate {};
137 }
138 } catch (const std::bad_any_cast&) {
139 // If the actual type stored in `data` does not match the expected type
140 // based on `baseDataType`, a `std::bad_any_cast` exception will be
141 // thrown. In this case, we catch the exception and return
142 // `std::monostate` to indicate that the conversion failed.
143 return std::monostate {};
144 }
145 }
146};
147
164template<typename DTYPE, std::size_t NDIMS>
166{
167public:
168 using size_type = std::size_t;
169
178 ConstMultiArrayView(const DTYPE* data,
179 const std::array<size_type, NDIMS>& shape,
180 const std::array<size_type, NDIMS>& strides)
181 : m_data(data)
182 , m_shape(shape)
183 , m_strides(strides)
184 {
185 }
186
194 template<size_type N = NDIMS, typename std::enable_if_t<N == 1, int> = 0>
195 const DTYPE& operator[](size_type index) const
196 {
197 assert(index < m_shape[0]);
198 return m_data[index * m_strides[0]];
199 }
200
209 template<size_type N = NDIMS, typename std::enable_if_t<(N > 1), int> = 0>
210 ConstMultiArrayView<DTYPE, NDIMS - 1> operator[](size_type index) const
211 {
212 assert(index < m_shape[0]);
213 std::array<size_type, NDIMS - 1> sub_shape {};
214 std::array<size_type, NDIMS - 1> sub_strides {};
215 // cppcheck-suppress knownConditionTrueFalse ; false positive, NDIMS > 1
216 for (size_type i = 1; i < NDIMS; ++i) {
217 sub_shape[i - 1] = m_shape[i];
218 sub_strides[i - 1] = m_strides[i];
219 }
220 return ConstMultiArrayView<DTYPE, NDIMS - 1>(
221 m_data + index * m_strides[0], sub_shape, sub_strides);
222 }
223
230 template<size_type N = NDIMS, typename std::enable_if_t<N == 1, int> = 0>
231 const DTYPE* begin() const
232 {
233 assert(m_shape[0] == 0 || m_data != nullptr);
234 return m_data;
235 }
236
243 template<size_type N = NDIMS, typename std::enable_if_t<N == 1, int> = 0>
244 const DTYPE* end() const
245 {
246 assert(m_shape[0] == 0 || m_data != nullptr);
247 if (m_shape[0] == 0) {
248 return m_data;
249 }
250 return m_data + (m_shape[0] * m_strides[0]);
251 }
252
259 const std::array<size_type, NDIMS>& shape() const { return m_shape; }
260
261private:
262 const DTYPE* m_data;
263 std::array<size_type, NDIMS> m_shape;
264 std::array<size_type, NDIMS> m_strides;
265};
266
272template<typename DTYPE>
274{
275public:
279 std::vector<DTYPE> data;
289 const std::type_index typeIndex = typeid(DTYPE);
290
294 DataBlock(const std::vector<DTYPE>& inData, const SizeArray& inShape)
295 : data(inData)
296 , shape(inShape)
297 {
298 }
299
312 template<std::size_t NDIMS>
314 {
315 if (shape.size() != NDIMS) {
316 throw std::invalid_argument(
317 "Shape size does not match the number of dimensions.");
318 }
319
320 // Calculate the total number of elements expected
321 SizeType expected_size = std::accumulate(
322 shape.begin(), shape.end(), SizeType {1}, std::multiplies<SizeType> {});
323
324 if (data.size() != expected_size) {
325 throw std::invalid_argument("Data size does not match the shape.");
326 }
327
328 std::array<std::size_t, NDIMS> shape_array {};
329 for (std::size_t i = 0; i < NDIMS; ++i) {
330 shape_array[i] = static_cast<std::size_t>(shape[i]);
331 }
332
333 std::array<std::size_t, NDIMS> strides {};
334 std::size_t stride = 1;
335 // cppcheck-suppress knownConditionTrueFalse ; false positive, NDIMS >= 1
336 for (std::size_t i = NDIMS; i-- > 0;) {
337 strides[i] = stride;
338 stride *= shape_array[i];
339 }
340
341 return ConstMultiArrayView<DTYPE, NDIMS>(data.data(), shape_array, strides);
342 }
343
355 const DataBlockGeneric& genericData)
356 {
357 auto result = DataBlock<DTYPE>(
358 std::any_cast<std::vector<DTYPE>>(genericData.data), genericData.shape);
359 return result;
360 }
361
373 {
375 }
376}; // class DataBlock
377
380template<StorageObjectType T>
381struct isAllowedStorageObjectType : std::false_type
382{
383};
384
387template<>
388struct isAllowedStorageObjectType<StorageObjectType::Dataset> : std::true_type
389{
390};
391
394template<>
395struct isAllowedStorageObjectType<StorageObjectType::Attribute> : std::true_type
396{
397};
398
407template<StorageObjectType OTYPE, typename VTYPE = std::any>
409{
410 // Embedded traits for compile time checking of allowed OTYPE for the class
411 // and methods
412private:
421 template<StorageObjectType U>
423 : std::integral_constant<bool, (U == StorageObjectType::Dataset)>
424 {
425 };
426
434 "StorageObjectType not allowed for ReadDataWrapper");
435
436 // Actual definition of the class
437public:
444 ReadDataWrapper(const std::shared_ptr<IO::BaseIO> io, const std::string& path)
445 : m_io(io)
446 , m_path(path)
447 {
448 }
449
454 static inline StorageObjectType getStorageObjectType() { return OTYPE; }
455
460 template<typename T>
461 static constexpr bool isType()
462 {
463 return std::is_same_v<VTYPE, T>;
464 }
465
470
475
479 virtual ~ReadDataWrapper() {}
480
485 inline const std::string& getPath() const { return m_path; }
486
491 inline std::shared_ptr<IO::BaseIO> getIO() const { return m_io; }
492
497 inline SizeArray getShape() const
498 {
499 return m_io->getStorageObjectShape(m_path);
500 }
501
506 inline SizeType getNumDimensions() const { return this->getShape().size(); }
507
514 {
515 return m_io->getStorageObjectDataType(m_path);
516 }
517
529 inline SizeArray getChunking() const
530 {
531 return m_io->getStorageObjectChunking(m_path);
532 }
533
548 template<StorageObjectType U = OTYPE,
549 typename std::enable_if<isDataset<U>::value, int>::type = 0>
554
559 inline bool exists() const
560 {
561 switch (OTYPE) {
562 case StorageObjectType::Dataset: {
563 return m_io->objectExists(m_path);
564 }
565 case StorageObjectType::Attribute: {
566 return m_io->attributeExists(m_path);
567 }
568 default: {
569 throw std::runtime_error("Unsupported StorageObjectType");
570 }
571 }
572 }
573
582 {
583 switch (OTYPE) {
584 case StorageObjectType::Dataset: {
585 return m_io->readDataset(m_path);
586 }
587 case StorageObjectType::Attribute: {
588 return m_io->readAttribute(m_path);
589 }
590 default: {
591 throw std::runtime_error("Unsupported StorageObjectType");
592 }
593 }
594 }
595
610 template<StorageObjectType U = OTYPE,
611 typename std::enable_if<isDataset<U>::value, int>::type = 0>
613 const SizeArray& count = {},
614 const SizeArray& stride = {},
615 const SizeArray& block = {}) const
616 {
617 // The function is only enabled for datasets so we don't need to check
618 // for attributes here.
619 return m_io->readDataset(m_path, start, count, stride, block);
620 }
621
636 template<typename T = VTYPE>
638 {
640 }
641
665 template<typename T = VTYPE,
666 StorageObjectType U = OTYPE,
667 typename std::enable_if<isDataset<U>::value, int>::type = 0>
668 inline DataBlock<VTYPE> values(const SizeArray& start,
669 const SizeArray& count = {},
670 const SizeArray& stride = {},
671 const SizeArray& block = {}) const
672 {
673 // The function is only enabled for datasets so we don't need to check
674 // for attributes here.
676 this->valuesGeneric(start, count, stride, block));
677 }
678
679protected:
683 const std::shared_ptr<IO::BaseIO> m_io;
687 std::string m_path;
688}; // ReadDataWrapper
689
690} // namespace AQNWB::IO
AQNWB::Types::StorageObjectType StorageObjectType
Definition BaseIO.hpp:20
AQNWB::Types::SizeArray SizeArray
Definition BaseIO.hpp:22
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
Represents a base data type.
Definition BaseIO.hpp:47
AQNWB::Types::VectorDataVariant BaseDataVectorVariant
Definition BaseIO.hpp:104
@ T_I32
Signed 32-bit integer.
Definition BaseIO.hpp:60
@ T_I64
Signed 64-bit integer.
Definition BaseIO.hpp:61
@ T_I8
Signed 8-bit integer.
Definition BaseIO.hpp:58
@ T_U32
Unsigned 32-bit integer.
Definition BaseIO.hpp:56
@ T_U64
Unsigned 64-bit integer.
Definition BaseIO.hpp:57
@ T_U8
Unsigned 8-bit integer.
Definition BaseIO.hpp:54
@ T_F32
32-bit floating point
Definition BaseIO.hpp:62
@ T_STR
String.
Definition BaseIO.hpp:64
@ T_F64
64-bit floating point
Definition BaseIO.hpp:63
@ T_U16
Unsigned 16-bit integer.
Definition BaseIO.hpp:55
@ T_I16
Signed 16-bit integer.
Definition BaseIO.hpp:59
static BaseDataType fromTypeId(const std::type_index &typeIndex)
Get the BaseDataType from a std::type_index.
Definition BaseIO.hpp:194
Non-owning, multi-dimensional, read-only array view for contiguous data.
Definition ReadIO.hpp:166
const std::array< size_type, NDIMS > & shape() const
Get the shape of the multi-dimensional array view.
Definition ReadIO.hpp:259
std::array< size_type, NDIMS > m_strides
Stride per dimension.
Definition ReadIO.hpp:264
ConstMultiArrayView< DTYPE, NDIMS - 1 > operator[](size_type index) const
Access a sub-array view at the given index for multi-dimensional arrays.
Definition ReadIO.hpp:210
ConstMultiArrayView(const DTYPE *data, const std::array< size_type, NDIMS > &shape, const std::array< size_type, NDIMS > &strides)
Construct a multi-dimensional view over a contiguous data buffer.
Definition ReadIO.hpp:178
std::size_t size_type
Definition ReadIO.hpp:168
const DTYPE * end() const
Returns a pointer to one past the last element for 1D arrays.
Definition ReadIO.hpp:244
std::array< size_type, NDIMS > m_shape
Size of each dimension.
Definition ReadIO.hpp:263
const DTYPE & operator[](size_type index) const
Access element at the given index for 1D arrays.
Definition ReadIO.hpp:195
const DTYPE * m_data
Pointer to the data buffer.
Definition ReadIO.hpp:262
const DTYPE * begin() const
Returns a pointer to the beginning of the data for 1D arrays.
Definition ReadIO.hpp:231
Generic structure to hold type-erased data and shape.
Definition ReadIO.hpp:34
SizeArray shape
The 1D vector with the n-dimensional shape of the data. Set to empty in case of scalar data.
Definition ReadIO.hpp:48
IO::BaseDataType baseDataType
The base data type for the data block.
Definition ReadIO.hpp:71
DataBlockGeneric()=default
Default constructor.
BaseDataType getBaseDataType() const
Get the BaseDataType for the data.
Definition ReadIO.hpp:97
std::any data
Definition ReadIO.hpp:43
BaseDataType::BaseDataVectorVariant as_variant() const
Cast the data to an std::variant for convenient access.
Definition ReadIO.hpp:109
DataBlockGeneric(const std::any &inData, const SizeArray &inShape, const std::type_index &inTypeIndex, const IO::BaseDataType inBaseDataType)
Parameterized constructor.
Definition ReadIO.hpp:81
std::type_index typeIndex
Type index of the values stored in the data vector.
Definition ReadIO.hpp:68
Structure to hold data and shape for a typed data vector.
Definition ReadIO.hpp:274
SizeArray shape
The 1D vector with the n-dimensional shape of the data. Set to empty in case of scalar data.
Definition ReadIO.hpp:284
static DataBlock< DTYPE > fromGeneric(const DataBlockGeneric &genericData)
Factory method to create an DataBlock from a DataBlockGeneric.
Definition ReadIO.hpp:354
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:289
std::vector< DTYPE > data
Definition ReadIO.hpp:279
DataBlock(const std::vector< DTYPE > &inData, const SizeArray &inShape)
Definition ReadIO.hpp:294
BaseDataType getBaseDataType() const
Get the BaseDataType for the data.
Definition ReadIO.hpp:372
ConstMultiArrayView< DTYPE, NDIMS > as_multi_array() const
Transform the data to a multi-dimensional array view for convenient access.
Definition ReadIO.hpp:313
IO::LinkArrayDataSetConfig toLinkArrayDataSetConfig() const
Constructs a AQNWB::IO::LinkArrayDataSetConfig from this wrapper.
Definition ReadIO.hpp:550
static constexpr bool isType()
Function to check at compile-time whether the object is of a particular VTYPE, e.g....
Definition ReadIO.hpp:461
SizeArray getChunking() const
Get the chunking configuration of the data object.
Definition ReadIO.hpp:529
SizeType getNumDimensions() const
Get the number of dimensions of the data object.
Definition ReadIO.hpp:506
ReadDataWrapper & operator=(const ReadDataWrapper &)=delete
Deleted copy assignment operator to prevent copying.
IO::BaseDataType getDataType() const
Get the data type of the data object.
Definition ReadIO.hpp:513
std::string m_path
Path to the dataset or attribute to read.
Definition ReadIO.hpp:687
const std::shared_ptr< IO::BaseIO > m_io
Pointer to the I/O object to use for reading.
Definition ReadIO.hpp:683
DataBlock< VTYPE > values() const
Reads an attribute with a specified data type.
Definition ReadIO.hpp:637
DataBlock< VTYPE > values(const SizeArray &start, const SizeArray &count={}, const SizeArray &stride={}, const SizeArray &block={}) const
Reads an dataset with a specified data type.
Definition ReadIO.hpp:668
DataBlockGeneric valuesGeneric() const
Reads a dataset and determines the data type.
Definition ReadIO.hpp:581
virtual ~ReadDataWrapper()
Destructor.
Definition ReadIO.hpp:479
const std::string & getPath() const
Gets the path of the registered type.
Definition ReadIO.hpp:485
static StorageObjectType getStorageObjectType()
Function to return the AQNWB::Types::StorageObjectType OTYPE of the instance.
Definition ReadIO.hpp:454
std::shared_ptr< IO::BaseIO > getIO() const
Get a shared pointer to the IO object.
Definition ReadIO.hpp:491
bool exists() const
Check that the object exists.
Definition ReadIO.hpp:559
ReadDataWrapper(const std::shared_ptr< IO::BaseIO > io, const std::string &path)
Default constructor.
Definition ReadIO.hpp:444
DataBlockGeneric valuesGeneric(const SizeArray &start, const SizeArray &count={}, const SizeArray &stride={}, const SizeArray &block={}) const
Reads a dataset and determines the data type.
Definition ReadIO.hpp:612
SizeArray getShape() const
Get the shape of the data object.
Definition ReadIO.hpp:497
ReadDataWrapper(const ReadDataWrapper &)=delete
Deleted copy constructor to prevent construction-copying.
The namespace for IO components of AqNWB.
StorageObjectType
Types of object used in the NWB schema.
Definition Types.hpp:60
size_t SizeType
Alias for the size type used in the project.
Definition Types.hpp:104
Internal embedded Trait to Check the OTYPE Enum Value at compile time.
Definition ReadIO.hpp:424
Definition ReadIO.hpp:382