aqnwb 0.5.0
Loading...
Searching...
No Matches
DynamicTable.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <any>
5#include <optional>
6#include <string>
7#include <unordered_map>
8
9#include "Utils.hpp"
10#include "io/BaseIO.hpp"
11#include "io/ReadIO.hpp"
16#include "spec/hdmf_common.hpp"
17
18namespace AQNWB::NWB
19{
20class MeaningsTable;
21} // namespace AQNWB::NWB
22
23namespace AQNWB::NWB
24{
32class DynamicTable : public Container
33{
34public:
36 using DataSpecPtr = std::shared_ptr<DataSpec>;
37
38public:
40 using RowData = std::unordered_map<std::string, CellValue>;
41
42 // Register the TimeSeries as a subclass of Container
46
47protected:
53 DynamicTable(const std::string& path, std::shared_ptr<IO::BaseIO> io);
54
55public:
59 ~DynamicTable() override;
60
72 Status initialize(const std::string& description,
73 const std::vector<DataSpecPtr>& dataSpecs = {});
74
87 const std::vector<DataSpecPtr>& dataSpecs) const;
88
99 static std::vector<DataSpecPtr> createDefaultDataSpecs(
100 const SizeType rowChunkSize = 100);
101
109 Status finalize() override;
110
121 Status addColumn(const std::shared_ptr<VectorData>& vectorData,
122 const std::vector<std::string>& values);
123
138 Status addColumn(const DataSpecPtr& dataSpec);
139
150 Status addColumn(const std::shared_ptr<VectorData>& vectorData);
151
159 Status addReferenceColumn(const std::string& name,
160 const std::string& colDescription,
161 const std::vector<std::string>& dataset);
162
170 const std::optional<int>& rowId = std::nullopt);
171
178 Status addRows(const std::vector<AQNWB::Types::RowData>& rows,
179 const std::vector<int>& rowIds = {});
180
186
200 std::vector<AQNWB::Types::RowData> readRows(
201 SizeType start = 0,
203 const std::vector<std::string>& colNames = {},
204 bool includeId = true);
205
216 std::string toString(SizeType start = 0,
218 const std::vector<std::string>& colNames = {},
219 bool includeId = true);
220
226 Status setRowIDs(const std::vector<int>& values);
227
240 virtual void setColNames(const std::vector<std::string>& newColNames);
241
258 std::shared_ptr<MeaningsTable> createMeaningsTable(
259 const std::string& columnName, const SizeType rowChunkSize = 100);
260
267 template<typename T>
269 {
270 using type =
271 typename std::conditional<std::is_base_of<VectorData, T>::value,
272 T,
274 };
275
282 template<typename T>
283 struct IsVectorDataTyped : std::false_type
284 {
285 };
286
287 template<typename DTYPE>
288 struct IsVectorDataTyped<VectorDataTyped<DTYPE>> : std::true_type
289 {
290 };
291
308 template<typename T = std::any>
309 std::shared_ptr<typename ColumnReturnType<T>::type> readColumn(
310 const std::string& colName)
311 {
312 using ReturnType = typename ColumnReturnType<T>::type;
313 std::string columnPath = AQNWB::mergePaths(m_path, colName);
314 auto ioPtr = getIO();
315 if (ioPtr != nullptr) {
316 if (ioPtr->objectExists(columnPath)) {
317 if (ioPtr->getStorageObjectType(columnPath)
318 == StorageObjectType::Dataset)
319 {
320 if constexpr (std::is_base_of<VectorData, T>::value
322 {
323 // Resolve the concrete on-disk registered type before caching it.
324 auto column = RegisteredType::create(columnPath, ioPtr);
325 return std::dynamic_pointer_cast<ReturnType>(column);
326 } else {
327 return ReturnType::create(columnPath, ioPtr);
328 }
329 }
330 }
331 } else {
332 std::cerr << "IO object has been deleted. Can't read column: " << colName
333 << " in DynamicTable: " << m_path << std::endl;
334 }
335 return nullptr;
336 }
337
339 std::string,
340 "colnames",
341 The names of the columns in the table)
342
344 std::string,
345 "description",
346 Description of what is in this dynamic table)
347
351 "id",
352 "unique identifiers for the rows of this dynamic table")
353
354
366 std::shared_ptr<MeaningsTable> readMeaningsTable(
367 const std::string& objectName) const;
368
384 const std::string& objectName) const;
385
386protected:
398 SizeType addColumnName(const std::string& colName);
399
408
410
415 {
416 std::string name;
418 std::shared_ptr<VectorData>
420 };
421
434 std::shared_ptr<VectorData> getConfiguredColumn(
435 const std::string& name) const;
436
445 SizeType registerColumn(const std::shared_ptr<VectorData>& column);
446
454 void clearColumns();
455
462 Status configureDataObjects(const std::vector<DataSpecPtr>& dataSpecs);
463
470 Status configureDataObject(const DataSpec& dataSpec);
471
483
494
509 template<typename RequiredDataSpec>
510 Status checkRequiredColumnSpec(const std::string& requiredName,
511 const std::vector<DataSpecPtr>& dataSpecs,
512 const std::optional<IO::BaseDataType>&
513 expectedDataType = std::nullopt) const
514 {
515 std::vector<IO::BaseDataType> allowedDataTypes;
516 if (expectedDataType.has_value()) {
517 allowedDataTypes.push_back(expectedDataType.value());
518 }
520 requiredName, dataSpecs, allowedDataTypes);
521 }
522
539 template<typename RequiredDataSpec>
541 const std::string& requiredName,
542 const std::vector<DataSpecPtr>& dataSpecs,
543 const std::vector<IO::BaseDataType>& allowedDataTypes) const
544 {
545 bool found = false;
546 for (const auto& spec : dataSpecs) {
547 if (!spec || spec->name != requiredName) {
548 continue;
549 }
550
551 found = true;
552 if (!std::dynamic_pointer_cast<RequiredDataSpec>(spec)) {
553 std::cerr << "DynamicTable::checkRequiredColumnSpec: required column '"
554 << requiredName
555 << "' does not use the required concrete DataSpec type."
556 << std::endl;
557 return Status::Failure;
558 }
559 if (spec->getShape().size() != 1) {
560 std::cerr << "DynamicTable::checkRequiredColumnSpec: required column '"
561 << requiredName << "' must be one-dimensional." << std::endl;
562 return Status::Failure;
563 }
564 if (!allowedDataTypes.empty()
565 && std::none_of(allowedDataTypes.begin(),
566 allowedDataTypes.end(),
567 [&spec](const IO::BaseDataType& dataType)
568 { return spec->getType() == dataType; }))
569 {
570 std::cerr << "DynamicTable::checkRequiredColumnSpec: required column '"
571 << requiredName << "' has an invalid data type." << std::endl;
572 return Status::Failure;
573 }
574 }
575
576 if (!found) {
577 std::cerr << "DynamicTable::checkRequiredColumnSpec: required column '"
578 << requiredName << "' not found." << std::endl;
579 return Status::Failure;
580 }
581 return Status::Success;
582 }
583
597 const ConfiguredColumn& configuredColumn,
599 SizeType rowCount);
600
607 std::vector<int> generateRowIDs(SizeType rowCount);
608
612 std::vector<std::string> m_colNames;
613
617 std::shared_ptr<ElementIdentifiers> m_rowElementIdentifiers;
618
622 std::vector<ConfiguredColumn> m_configuredColumns;
623
627 std::unordered_map<std::string, SizeType> m_configuredColumnIndices;
628};
629} // namespace AQNWB::NWB
AQNWB::Types::Status Status
Definition BaseIO.hpp:21
AQNWB::Types::SizeType SizeType
Definition Channel.hpp:8
#define REGISTER_SUBCLASS(T, BASE, NAMESPACE)
Macro to register a subclass with the RegisteredType class registry.
Definition RegisteredType.hpp:547
#define DEFINE_ATTRIBUTE_FIELD(name, default_type, fieldPath, description)
Defines a lazy-loaded attribute field accessor function.
Definition RegisteredType.hpp:580
#define DEFINE_REGISTERED_FIELD(name, registeredType, fieldPath, description)
Defines a lazy-loaded accessor function for reading fields that are RegisteredTypes.
Definition RegisteredType.hpp:672
Represents a base data type.
Definition BaseIO.hpp:47
AQNWB::Types::VectorDataVariant BaseDataVectorVariant
Definition BaseIO.hpp:104
Container(const std::string &path, std::shared_ptr< IO::BaseIO > io)
Constructor.
Definition Container.cpp:10
Status initialize()
Initialize the container.
Definition Container.cpp:20
Status flushColNames()
Flush the column names to the file.
Definition DynamicTable.cpp:714
std::vector< std::string > m_colNames
Names of the columns in the table.
Definition DynamicTable.hpp:612
std::vector< ConfiguredColumn > m_configuredColumns
Ordered list of configured columns managed by the table.
Definition DynamicTable.hpp:622
Status addRows(const std::vector< AQNWB::Types::RowData > &rows, const std::vector< int > &rowIds={})
Adds multiple rows to the table.
Definition DynamicTable.cpp:393
SizeType addColumnName(const std::string &colName)
Add a column name to m_colNames while preventing duplicates.
Definition DynamicTable.cpp:113
std::unique_ptr< AQNWB::IO::ReadDataWrapper< AQNWB::NWB::AttributeField, VTYPE > > readDescription() const
Status configureDataObjects(const std::vector< DataSpecPtr > &dataSpecs)
Configure multiple data objects.
Definition DynamicTable.cpp:849
SizeType registerColumn(const std::shared_ptr< VectorData > &column)
Register a column in the table's column registry.
Definition DynamicTable.cpp:898
std::shared_ptr< MeaningsTable > createMeaningsTableInstance(const std::string &objectName) const
Returns the instance of the class representing the named MeaningsTable field for write.
Definition DynamicTable.cpp:817
Status writeColumnBuffer(const ConfiguredColumn &configuredColumn, const IO::BaseDataType::BaseDataVectorVariant &buffer, SizeType rowCount)
Write a buffer of data to a configured column.
virtual Status validateDataSpecs(const std::vector< DataSpecPtr > &dataSpecs) const
Validates the provided data specifications for the table.
Definition DynamicTable.cpp:73
std::shared_ptr< MeaningsTable > readMeaningsTable(const std::string &objectName) const
Returns the instance of the class representing the named MeaningsTable field.
Definition DynamicTable.cpp:800
std::shared_ptr< typename ColumnReturnType< T >::type > readColumn(const std::string &colName)
Read an arbitrary column of the DynamicTable.
Definition DynamicTable.hpp:309
Status checkRequiredColumnSpec(const std::string &requiredName, const std::vector< DataSpecPtr > &dataSpecs, const std::vector< IO::BaseDataType > &allowedDataTypes) const
Check that a required column spec exists and has one of the allowed data types.
Definition DynamicTable.hpp:540
virtual void setColNames(const std::vector< std::string > &newColNames)
Sets the column names of the DynamicTable.
Definition DynamicTable.cpp:89
DynamicTable(const std::string &path, std::shared_ptr< IO::BaseIO > io)
Constructor.
Definition DynamicTable.cpp:19
Data::DataSpecBase DataSpec
Definition DynamicTable.hpp:35
~DynamicTable() override
Destructor.
Definition DynamicTable.cpp:41
std::shared_ptr< MeaningsTable > createMeaningsTable(const std::string &columnName, const SizeType rowChunkSize=100)
Create a MeaningsTable for a specific VectorData column in this DynamicTable.
Definition DynamicTable.cpp:737
Status addRow(const AQNWB::Types::RowData &row, const std::optional< int > &rowId=std::nullopt)
Adds a single row to the table.
Definition DynamicTable.cpp:202
std::shared_ptr< DataSpec > DataSpecPtr
Definition DynamicTable.hpp:36
std::unordered_map< std::string, SizeType > m_configuredColumnIndices
Lookup map from column name to its index in m_configuredColumns.
Definition DynamicTable.hpp:627
SizeType getNumberOfRows() const
Get the number of rows in the table.
Definition DynamicTable.cpp:212
Status checkRequiredColumnSpec(const std::string &requiredName, const std::vector< DataSpecPtr > &dataSpecs, const std::optional< IO::BaseDataType > &expectedDataType=std::nullopt) const
Validate the concrete specification, shape, and optionally data type of a required column.
Definition DynamicTable.hpp:510
std::unique_ptr< AQNWB::IO::ReadDataWrapper< AQNWB::NWB::AttributeField, VTYPE > > readColNames() const
Status loadConfiguredColumnsFromFile()
Load all configured columns and row element identifiers from the file.
Definition DynamicTable.cpp:945
Status setRowIDs(const std::vector< int > &values)
Sets the values of the element identifiers on the table.
Definition DynamicTable.cpp:172
static std::vector< DataSpecPtr > createDefaultDataSpecs(const SizeType rowChunkSize=100)
Create the default data specs for a DynamicTable.
Definition DynamicTable.cpp:80
Status addReferenceColumn(const std::string &name, const std::string &colDescription, const std::vector< std::string > &dataset)
Adds a column of references to the table.
Definition DynamicTable.cpp:682
Status addColumn(const std::shared_ptr< VectorData > &vectorData, const std::vector< std::string > &values)
Adds a column of vector string data to the table.
Definition DynamicTable.cpp:137
Status ensureConfiguredColumnsLoaded()
Ensure that all configured columns are loaded from the file.
Definition DynamicTable.cpp:937
std::vector< AQNWB::Types::RowData > readRows(SizeType start=0, SizeType count=Types::SizeTypeNotSet, const std::vector< std::string > &colNames={}, bool includeId=true)
Read rows from the table.
Definition DynamicTable.cpp:225
std::string toString(SizeType start=0, SizeType count=Types::SizeTypeNotSet, const std::vector< std::string > &colNames={}, bool includeId=true)
Convert the table to a string representation.
Definition DynamicTable.cpp:319
std::vector< int > generateRowIDs(SizeType rowCount)
Generate row IDs for the table.
Definition DynamicTable.cpp:977
AQNWB::NWB::CellValue CellValue
Definition DynamicTable.hpp:39
std::shared_ptr< ElementIdentifiers > m_rowElementIdentifiers
The row ids data object for write.
Definition DynamicTable.hpp:617
std::shared_ptr< VectorData > getConfiguredColumn(const std::string &name) const
Look up an already-configured column by name.
Definition DynamicTable.cpp:831
std::unordered_map< std::string, CellValue > RowData
Definition DynamicTable.hpp:40
Status finalize() override
Finalizes writing the DynamicTable.
Definition DynamicTable.cpp:731
std::shared_ptr< RTYPE > readIdColumn() const
Status configureDataObject(const DataSpec &dataSpec)
Configure a single data object.
Definition DynamicTable.cpp:866
void clearColumns()
Clear all registered columns and column names.
Definition DynamicTable.cpp:841
A list of unique identifiers for values within a dataset, e.g. rows of a DynamicTable.
Definition ElementIdentifiers.hpp:13
A table to store information about the meanings of values in a referenced VectorData object....
Definition MeaningsTable.hpp:38
std::string m_path
The path of the registered type.
Definition RegisteredType.hpp:471
static std::shared_ptr< RegisteredType > create(const std::string &fullClassName, const std::string &path, std::shared_ptr< IO::BaseIO > io, bool fallbackToBase=false)
Create or retrieve the canonical instance of a registered subclass.
Definition RegisteredType.cpp:87
std::shared_ptr< AQNWB::IO::BaseIO > getIO() const
Get a shared pointer to the IO object.
Definition RegisteredType.hpp:77
A typed n-dimensional dataset representing a column of a DynamicTable.
Definition VectorData.hpp:143
Namespace for all classes related to the NWB data standard.
AQNWB::Types::CellValue CellValue
Definition Data.hpp:14
const std::string namespaceName
Definition hdmf_common.hpp:21
constexpr SizeType SizeTypeNotSet
Value to use to indicate that a SizeType index is not set.
Definition Types.hpp:109
std::unordered_map< std::string, CellValue > RowData
Represents a row of data in a DynamicTable.
Definition Types.hpp:371
static std::string mergePaths(const std::string &path1, const std::string &path2)
Merge two paths into a single path, handling extra trailing and starting "/".
Definition Utils.hpp:275
Non-templated base class for runtime Data configuration.
Definition Data.hpp:33
Type trait to determine the return type of readColumn.
Definition DynamicTable.hpp:269
typename std::conditional< std::is_base_of< VectorData, T >::value, T, VectorDataTyped< T > >::type type
Definition DynamicTable.hpp:270
Internal state structure tracking a configured table column.
Definition DynamicTable.hpp:415
IO::BaseDataType dataType
The underlying data type.
Definition DynamicTable.hpp:417
std::shared_ptr< VectorData > column
Shared pointer to the column dataset.
Definition DynamicTable.hpp:419
std::string name
The column name.
Definition DynamicTable.hpp:416
Type trait identifying non-registered typed VectorData facades.
Definition DynamicTable.hpp:284