From 75c31a9abe8777cba48f8401cc95e66f7db10364 Mon Sep 17 00:00:00 2001 From: Steph Prince <40640337+stephprince@users.noreply.github.com> Date: Mon, 5 Aug 2024 18:45:14 -0700 Subject: [PATCH] convert stopRecording to pauseRecording --- src/BaseIO.hpp | 2 ++ src/hdf5/HDF5IO.cpp | 8 +++++--- src/hdf5/HDF5IO.hpp | 5 +++-- src/nwb/NWBFile.cpp | 5 ++--- src/nwb/NWBFile.hpp | 7 ++++--- tests/testNWBFile.cpp | 46 +++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 60 insertions(+), 13 deletions(-) diff --git a/src/BaseIO.hpp b/src/BaseIO.hpp index b60f5bcd..bdb7118c 100644 --- a/src/BaseIO.hpp +++ b/src/BaseIO.hpp @@ -248,6 +248,8 @@ class BaseIO * @brief Stops the recording process. * @return The status of the operation. */ + virtual Status pauseRecording() = 0; + /** * @brief Returns true if the file is in a mode where objects can * be added or deleted. Note, this does not apply to the modification diff --git a/src/hdf5/HDF5IO.cpp b/src/hdf5/HDF5IO.cpp index af507ce3..eb7c355f 100644 --- a/src/hdf5/HDF5IO.cpp +++ b/src/hdf5/HDF5IO.cpp @@ -381,16 +381,18 @@ Status HDF5IO::createStringDataSet(const std::string& path, Status HDF5IO::startRecording() { - modifyObjects = false; if (H5Fstart_swmr_write(this->file->getId()) < 0) { return Status::Failure; } return Status::Success; } -Status HDF5IO::stopRecording() +Status HDF5IO::pauseRecording() { - modifyObjects = true; + file->close(); + file.reset(); + opened = false; + open(false); return Status::Success; } diff --git a/src/hdf5/HDF5IO.hpp b/src/hdf5/HDF5IO.hpp index b7437f54..bfb9419a 100644 --- a/src/hdf5/HDF5IO.hpp +++ b/src/hdf5/HDF5IO.hpp @@ -183,9 +183,10 @@ class HDF5IO : public BaseIO Status startRecording() override; /** - * @brief Stops the recording process. - * @return The status of the stop recording operation. + * @brief Pause the recording process. + * @return The status of the pause recording operation. */ + Status pauseRecording() override; /** * @brief Checks whether the file is in a mode where objects diff --git a/src/nwb/NWBFile.cpp b/src/nwb/NWBFile.cpp index 0a960272..165026e4 100644 --- a/src/nwb/NWBFile.cpp +++ b/src/nwb/NWBFile.cpp @@ -42,7 +42,6 @@ Status NWBFile::initialize() Status NWBFile::finalize() { - io->stopRecording(); recordingContainers.reset(); return io->close(); } @@ -145,9 +144,9 @@ Status NWBFile::startRecording() return io->startRecording(); } -void NWBFile::stopRecording() +void NWBFile::pauseRecording() { - io->stopRecording(); + io->pauseRecording(); } void NWBFile::cacheSpecifications(const std::string& specPath, diff --git a/src/nwb/NWBFile.hpp b/src/nwb/NWBFile.hpp index 46b9112b..3475af2a 100644 --- a/src/nwb/NWBFile.hpp +++ b/src/nwb/NWBFile.hpp @@ -57,7 +57,8 @@ class NWBFile * @brief Create ElectricalSeries objects to record data into. * Created objects are stored in recordingContainers. * Note, this function will fail if the file is in a mode where - * new objects cannot be added. + * new objects cannot be added, which can be checked via + * nwbfile.io->canModifyObjects() * @param recordingArrays vector of ChannelVector indicating the electrodes to * record from. A separate ElectricalSeries will be * created for each ChannelVector. @@ -74,9 +75,9 @@ class NWBFile Status startRecording(); /** - * @brief Closes the relevant datasets. + * @brief Pauses the recording but allows the file to remain open. */ - void stopRecording(); + void pauseRecording(); /** * @brief Indicates the NWB schema version. diff --git a/tests/testNWBFile.cpp b/tests/testNWBFile.cpp index 5a26eab9..3ad07f81 100644 --- a/tests/testNWBFile.cpp +++ b/tests/testNWBFile.cpp @@ -33,9 +33,11 @@ TEST_CASE("createElectricalSeries", "[nwb]") std::vector mockArrays = getMockChannelArrays(1, 2); Status resultCreate = nwbfile.createElectricalSeries(mockArrays, BaseDataType::F32); + REQUIRE(resultCreate == Status::Success); // start recording Status resultStart = nwbfile.startRecording(); + REQUIRE(resultStart == Status::Success); // write timeseries data std::vector mockData = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f}; @@ -51,8 +53,6 @@ TEST_CASE("createElectricalSeries", "[nwb]") dataShape, positionOffset, mockData.data(), mockTimestamps.data()); nwbfile.finalize(); - - REQUIRE(resultCreate == Status::Success); } TEST_CASE("setCanModifyObjectsMode", "[nwb]") @@ -78,6 +78,48 @@ TEST_CASE("setCanModifyObjectsMode", "[nwb]") nwbfile.createElectricalSeries(mockArrays, BaseDataType::F32); REQUIRE(resultCreatePostStart == Status::Failure); + // stop recording + nwbfile.finalize(); +} + + +TEST_CASE("pauseRecording", "[nwb]") +{ + std::string filename = getTestFilePath("testPauseRecording.nwb"); + +// initialize nwbfile object and create base structure + NWB::NWBFile nwbfile(generateUuid(), + std::make_unique(filename)); + nwbfile.initialize(); + + // create Electrical Series + std::vector mockArrays = getMockChannelArrays(1, 2); + Status resultCreate = + nwbfile.createElectricalSeries(mockArrays, BaseDataType::F32); + REQUIRE(resultCreate == Status::Success); + + // start recording + Status resultStart = nwbfile.startRecording(); + REQUIRE(resultStart == Status::Success); + + // write timeseries data + std::vector mockData = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f}; + std::vector mockTimestamps = {0.1, 0.2, 0.3, 0.4, 0.5}; + std::vector positionOffset = {0, 0}; + std::vector dataShape = {mockData.size(), 0}; + + NWB::TimeSeries* ts0 = nwbfile.getTimeSeries(0); + ts0->writeData( + dataShape, positionOffset, mockData.data(), mockTimestamps.data()); + NWB::TimeSeries* ts1 = nwbfile.getTimeSeries(1); + ts1->writeData( + dataShape, positionOffset, mockData.data(), mockTimestamps.data()); + + // pause recording and check recording containers still exist + nwbfile.pauseRecording(); + NWB::TimeSeries* ts0AfterPause = nwbfile.getTimeSeries(0); + REQUIRE(ts0AfterPause != nullptr); + // stop recording nwbfile.finalize(); } \ No newline at end of file