Skip to content

Commit

Permalink
update NWBFile.recordingContainers data type
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Jul 26, 2024
1 parent b971e63 commit 6273c6c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 50 deletions.
42 changes: 12 additions & 30 deletions src/nwb/NWBFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ Status NWBFile::startElectricalSeriesRecording(
// store all recorded data in the acquisition group
std::string rootPath = "/acquisition/";

// setup containers for different data types (e.g. SpikeEventSeries container
// would go here as well)
std::unique_ptr<RecordingContainer> electricalSeriesContainer =
std::make_unique<RecordingContainer>("ElectricalSeries",
recordingArrays.size());

// Setup electrode table
std::string electrodeTablePath = "general/extracellular_ephys/electrodes/";
ElectrodeTable elecTable = ElectrodeTable(electrodeTablePath, io);
Expand Down Expand Up @@ -123,7 +117,7 @@ Status NWBFile::startElectricalSeriesRecording(
SizeArray {0, channelVector.size()},
SizeArray {CHUNK_XSIZE});
electricalSeries->initialize();
electricalSeriesContainer->addData(std::move(electricalSeries));
recordingContainers->addData(std::move(electricalSeries));

// Add electrode information to electrode table (does not write to datasets
// yet)
Expand All @@ -133,9 +127,6 @@ Status NWBFile::startElectricalSeriesRecording(
// write electrode information to datasets
elecTable.finalize();

// add all data containers to the recording container manager
recordingContainers.push_back(std::move(electricalSeriesContainer));

return Status::Success;
}

Expand Down Expand Up @@ -182,33 +173,24 @@ std::unique_ptr<AQNWB::BaseRecordingData> NWBFile::createRecordingData(
io->createArrayDataSet(type, size, chunking, path));
}

TimeSeries* NWBFile::getTimeSeries(const std::string& containerName,
const SizeType& timeseriesInd)
TimeSeries* NWBFile::getTimeSeries(const SizeType& timeseriesInd)
{
for (auto& container : this->recordingContainers) {
if (container->containerName == containerName) {
if (timeseriesInd >= container->data.size()) {
return nullptr;
} else {
return container->data[timeseriesInd].get();
}
}
if (timeseriesInd >= this->recordingContainers->containers.size()) {
return nullptr;
} else {
return this->recordingContainers->containers[timeseriesInd].get();
}
return nullptr;
}

// Recording Container

RecordingContainer::RecordingContainer(const std::string& containerName,
const SizeType& containerSize)
: containerName(containerName)
{
this->data.reserve(containerSize);
};
RecordingContainers::RecordingContainers(const std::string& name)
: name(name)
{}

RecordingContainer::~RecordingContainer() {}
RecordingContainers::~RecordingContainers() {}

void RecordingContainer::addData(std::unique_ptr<TimeSeries> data)
void RecordingContainers::addData(std::unique_ptr<TimeSeries> data)
{
this->data.push_back(std::move(data));
this->containers.push_back(std::move(data));
}
29 changes: 14 additions & 15 deletions src/nwb/NWBFile.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <cstdint>
#include <vector>
#include <memory>

#include "BaseIO.hpp"
#include "Types.hpp"
Expand All @@ -9,7 +11,7 @@
namespace AQNWB::NWB
{

class RecordingContainer; // declare here because gets used in NWBFile class
class RecordingContainers; // declare here because gets used in NWBFile class

/**
* @brief The NWBFile class provides an interface for setting up and managing
Expand Down Expand Up @@ -85,8 +87,7 @@ class NWBFile
* @param containerName The name of the timeseries group.
* @param timeseriesInd The index of the timeseries dataset within the group.
*/
TimeSeries* getTimeSeries(const std::string& containerName,
const SizeType& timeseriesInd);
TimeSeries* getTimeSeries(const SizeType& timeseriesInd);

protected:
/**
Expand Down Expand Up @@ -121,47 +122,45 @@ class NWBFile

const std::string identifierText;
std::shared_ptr<BaseIO> io;
std::vector<std::unique_ptr<RecordingContainer>> recordingContainers;
std::unique_ptr<RecordingContainers> recordingContainers = std::make_unique<RecordingContainers>("RecordingContainers");
};

/**
* @brief The RecordingContainer class provides an interface for managing
* @brief The RecordingContainers class provides an interface for managing
* groups of TimeSeries acquired during a recording.
*/
class RecordingContainer
class RecordingContainers
{
public:
/**
* @brief Constructor for RecordingContainer class.
* @param containerName The name of the group of time series
* @param containerSize The size to preallocate for the group of timeseries.
* @param name The name of the group of time series
*/
RecordingContainer(const std::string& containerName,
const SizeType& containerSize);
RecordingContainers(const std::string& name);

/**
* @brief Deleted copy constructor to prevent construction-copying.
*/
RecordingContainer(const RecordingContainer&) = delete;
RecordingContainers(const RecordingContainers&) = delete;

/**
* @brief Deleted copy assignment operator to prevent copying.
*/
RecordingContainer& operator=(const RecordingContainer&) = delete;
RecordingContainers& operator=(const RecordingContainers&) = delete;

/**
* @brief Destructor for RecordingContainer class.
*/
~RecordingContainer();
~RecordingContainers();

/**
* @brief Adds a TimeSeries object to the container.
* @param data The TimeSeries object to add.
*/
void addData(std::unique_ptr<TimeSeries> data);

std::vector<std::unique_ptr<TimeSeries>> data;
std::string containerName;
std::vector<std::unique_ptr<TimeSeries>> containers;
std::string name;
};

} // namespace AQNWB::NWB
2 changes: 1 addition & 1 deletion src/nwb/NWBRecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Status NWBRecording::writeTimeseriesData(
const void* data,
const void* timestamps)
{
TimeSeries* ts = nwbfile->getTimeSeries(containerName, timeseriesInd);
TimeSeries* ts = nwbfile->getTimeSeries(timeseriesInd);

if (ts == nullptr)
return Status::Failure;
Expand Down
4 changes: 2 additions & 2 deletions src/nwb/base/TimeSeries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ void TimeSeries::initialize()

// setup datasets
this->data = std::unique_ptr<BaseRecordingData>(
io->createDataSet(dataType, dsetSize, chunkSize, getPath() + "/data"));
io->createArrayDataSet(dataType, dsetSize, chunkSize, getPath() + "/data"));
io->createDataAttributes(getPath(), conversion, resolution, unit);

SizeArray tsDsetSize = {
dsetSize[0]}; // timestamps match data along first dimension
this->timestamps = std::unique_ptr<BaseRecordingData>(io->createDataSet(
this->timestamps = std::unique_ptr<BaseRecordingData>(io->createArrayDataSet(
this->timestampsType, tsDsetSize, chunkSize, getPath() + "/timestamps"));
io->createTimestampsAttributes(getPath());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testNWBFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ TEST_CASE("startElectricalSeriesRecording", "[nwb]")
std::vector<SizeType> positionOffset = {0, 0};
std::vector<SizeType> dataShape = {mockData.size(), 0};

NWB::TimeSeries* ts0 = nwbfile.getTimeSeries("ElectricalSeries", 0);
NWB::TimeSeries* ts0 = nwbfile.getTimeSeries(0);
ts0->writeData(
dataShape, positionOffset, mockData.data(), mockTimestamps.data());
NWB::TimeSeries* ts1 = nwbfile.getTimeSeries("ElectricalSeries", 1);
NWB::TimeSeries* ts1 = nwbfile.getTimeSeries(1);
ts1->writeData(
dataShape, positionOffset, mockData.data(), mockTimestamps.data());

Expand Down

0 comments on commit 6273c6c

Please sign in to comment.