Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Jun 5, 2024
1 parent a6d7bc4 commit 64f7a77
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 93 deletions.
6 changes: 3 additions & 3 deletions src/Channel.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <string>
#include <array>
#include <string>

#include "Types.hpp"

Expand All @@ -25,8 +25,8 @@ class Channel
const float conversion = 1e6f, // uV to V
const float samplingRate = 30000.f, // placeholder
const float bitVolts = 0.000002f, // least significant bit needed to
// convert 16-bit int to volts
// currently a placeholder
// convert 16-bit int to volts
// currently a placeholder
const std::array<float, 3> position = {0.f, 0.f, 0.f});

/**
Expand Down
2 changes: 1 addition & 1 deletion src/nwb/NWBFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Status NWBFile::startRecording(std::vector<Types::ChannelGroup> recordingArrays)
io,
"Stores continuously sampled voltage data from an "
"extracellular ephys recording",
channelGroup,
channelGroup,
CHUNK_XSIZE,
elecTable.getPath());
electricalSeries->initialize();
Expand Down
19 changes: 9 additions & 10 deletions src/nwb/NWBRecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ NWBRecording::~NWBRecording()
}

Status NWBRecording::openFile(const std::string& rootFolder,
const std::string& baseName,
int experimentNumber,
std::vector<Types::ChannelGroup> recordingArrays,
const std::string& IOType)
const std::string& baseName,
int experimentNumber,
std::vector<Types::ChannelGroup> recordingArrays,
const std::string& IOType)
{
std::string filename =
rootFolder + baseName + std::to_string(experimentNumber) + ".nwb";
Expand All @@ -54,12 +54,11 @@ void NWBRecording::writeTimeseriesData(SizeType timeseriesInd,
SizeType numSamples)
{
// check if more samples than allocated buffer size
if (numSamples > bufferSize)
{
bufferSize = numSamples;
scaledBuffer = std::make_unique<float[]>(numSamples);
intBuffer = std::make_unique<int16_t[]>(numSamples);
}
if (numSamples > bufferSize) {
bufferSize = numSamples;
scaledBuffer = std::make_unique<float[]>(numSamples);
intBuffer = std::make_unique<int16_t[]>(numSamples);
}

// copy data and multiply by scaling factor
double multFactor = 1 / (32767.0f * channel.getBitVolts());
Expand Down
8 changes: 4 additions & 4 deletions src/nwb/NWBRecording.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class NWBRecording
* @param recordingNumber The recording number.
*/
Status openFile(const std::string& rootFolder,
const std::string& baseName,
int experimentNumber,
std::vector<Types::ChannelGroup> recordingArrays,
const std::string& IOType = "HDF5");
const std::string& baseName,
int experimentNumber,
std::vector<Types::ChannelGroup> recordingArrays,
const std::string& IOType = "HDF5");

/**
* @brief Closes the file and performs necessary cleanup when recording
Expand Down
59 changes: 28 additions & 31 deletions src/nwb/ecephys/ElectricalSeries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,43 @@ void ElectricalSeries::initialize()
}

// make data dataset
data = std::unique_ptr<BaseRecordingData>(io->createDataSet(BaseDataType::I16,
SizeArray {0, channelGroup.size()},
SizeArray {chunkSize},
getPath() + "/data"));
io->createDataAttributes(getPath(),
channelGroup[0].getConversion(),
-1.0f,
"volts");
data = std::unique_ptr<BaseRecordingData>(
io->createDataSet(BaseDataType::I16,
SizeArray {0, channelGroup.size()},
SizeArray {chunkSize},
getPath() + "/data"));
io->createDataAttributes(
getPath(), channelGroup[0].getConversion(), -1.0f, "volts");

// make timestamps dataset
timestamps = std::unique_ptr<BaseRecordingData>(io->createDataSet(BaseDataType::F64,
SizeArray {0},
SizeArray {chunkSize},
getPath() + "/timestamps"));
timestamps = std::unique_ptr<BaseRecordingData>(
io->createDataSet(BaseDataType::F64,
SizeArray {0},
SizeArray {chunkSize},
getPath() + "/timestamps"));
io->createTimestampsAttributes(getPath());

// make channel conversion dataset
channelConversion = std::unique_ptr<BaseRecordingData>(io->createDataSet(BaseDataType::F32,
SizeArray {1},
SizeArray {chunkSize},
getPath() + "/channel_conversion"));
channelConversion = std::unique_ptr<BaseRecordingData>(
io->createDataSet(BaseDataType::F32,
SizeArray {1},
SizeArray {chunkSize},
getPath() + "/channel_conversion"));
io->createCommonNWBAttributes(getPath() + "/channel_conversion",
"hdmf-common",
"",
"Bit volts values for all channels");

// make electrodes dataset
electrodesDataset = std::unique_ptr<BaseRecordingData>(io->createDataSet(BaseDataType::I32,
SizeArray {1},
SizeArray {chunkSize},
getPath() + "/electrodes"));
electrodesDataset->writeDataBlock(channelGroup.size(),
BaseDataType::I32,
&electrodeInds[0]);
io->createCommonNWBAttributes(getPath() + "/electrodes",
"hdmf-common",
"DynamicTableRegion",
"");
io->createReferenceAttribute(electrodesTablePath,
getPath() + "/electrodes",
"table");
electrodesDataset = std::unique_ptr<BaseRecordingData>(
io->createDataSet(BaseDataType::I32,
SizeArray {1},
SizeArray {chunkSize},
getPath() + "/electrodes"));
electrodesDataset->writeDataBlock(
channelGroup.size(), BaseDataType::I32, &electrodeInds[0]);
io->createCommonNWBAttributes(
getPath() + "/electrodes", "hdmf-common", "DynamicTableRegion", "");
io->createReferenceAttribute(
electrodesTablePath, getPath() + "/electrodes", "table");
}

18 changes: 7 additions & 11 deletions src/nwb/file/ElectrodeTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ void ElectrodeTable::initialize()
DynamicTable::initialize();

electrodeDataset->dataset =
std::unique_ptr<BaseRecordingData>(io->createDataSet(BaseDataType::I32,
SizeArray {1},
SizeArray {1},
path + "id"));
groupNamesDataset->dataset =
std::unique_ptr<BaseRecordingData>(io->createDataSet(BaseDataType::STR(250),
std::unique_ptr<BaseRecordingData>(io->createDataSet(
BaseDataType::I32, SizeArray {1}, SizeArray {1}, path + "id"));
groupNamesDataset->dataset = std::unique_ptr<BaseRecordingData>(
io->createDataSet(BaseDataType::STR(250),
SizeArray {0},
SizeArray {1},
path + "group_name"));
locationsDataset->dataset =
std::unique_ptr<BaseRecordingData>(io->createDataSet(BaseDataType::STR(250),
SizeArray {0},
SizeArray {1},
path + "location"));
locationsDataset
->dataset = std::unique_ptr<BaseRecordingData>(io->createDataSet(
BaseDataType::STR(250), SizeArray {0}, SizeArray {1}, path + "location"));
}

void ElectrodeTable::addElectrodes(std::vector<Channel> channels)
Expand Down
63 changes: 36 additions & 27 deletions tests/testNWBRecording.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include <H5Cpp.h>
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_all.hpp>
#include <H5Cpp.h>

#include "BaseIO.hpp"
#include "Channel.hpp"
Expand Down Expand Up @@ -75,39 +75,45 @@ TEST_CASE("writeContinuousData", "[recording]")

// check contents of data
std::string dataPath = "/acquisition/array0/data";
std::unique_ptr<H5::H5File> file = std::make_unique<H5::H5File>(path + "Recording1.nwb", H5F_ACC_RDONLY);
std::unique_ptr<H5::DataSet> dataset = std::make_unique<H5::DataSet>(file->openDataSet(dataPath));
std::unique_ptr<H5::H5File> file =
std::make_unique<H5::H5File>(path + "Recording1.nwb", H5F_ACC_RDONLY);
std::unique_ptr<H5::DataSet> dataset =
std::make_unique<H5::DataSet>(file->openDataSet(dataPath));
SizeType numChannelsToRead = numChannels / 2;

float* buffer = new float[numSamples*numChannelsToRead];
float* buffer = new float[numSamples * numChannelsToRead];

H5::DataSpace fSpace = dataset->getSpace();
hsize_t dims[1] = {numSamples * numChannelsToRead};
H5::DataSpace mSpace(1, dims);
dataset->read(buffer, H5::PredType::NATIVE_FLOAT, mSpace, fSpace);

std::vector<std::vector<float>> dataOut(numChannelsToRead, std::vector<float>(numSamples));
for(SizeType i = 0; i < numChannelsToRead; ++i) {
for(SizeType j = 0; j < numSamples; ++j) {
dataOut[i][j] = buffer[j*numChannelsToRead + i] * (32767.0f * 0.000002f);
}
dataset->read(buffer, H5::PredType::NATIVE_FLOAT, mSpace, fSpace);

std::vector<std::vector<float>> dataOut(numChannelsToRead,
std::vector<float>(numSamples));
for (SizeType i = 0; i < numChannelsToRead; ++i) {
for (SizeType j = 0; j < numSamples; ++j) {
dataOut[i][j] =
buffer[j * numChannelsToRead + i] * (32767.0f * 0.000002f);
}
}
delete[] buffer;
REQUIRE_THAT(dataOut[0], Catch::Matchers::Approx(mockData[0]).margin(1));
REQUIRE_THAT(dataOut[1], Catch::Matchers::Approx(mockData[1]).margin(1));

// check contents of timestamps
std::string timestampsPath = "/acquisition/array0/timestamps";
std::unique_ptr<H5::DataSet> tsDataset = std::make_unique<H5::DataSet>(file->openDataSet(timestampsPath));
std::unique_ptr<H5::DataSet> tsDataset =
std::make_unique<H5::DataSet>(file->openDataSet(timestampsPath));
double* tsBuffer = new double[numSamples];

H5::DataSpace tsfSpace = tsDataset->getSpace();
tsDataset->read(tsBuffer, H5::PredType::NATIVE_DOUBLE, tsfSpace, tsfSpace);
tsDataset->read(tsBuffer, H5::PredType::NATIVE_DOUBLE, tsfSpace, tsfSpace);

std::vector<double> timestampsOut(tsBuffer, tsBuffer + numSamples);
delete[] tsBuffer;
double tolerance = 1e-9;
REQUIRE_THAT(timestampsOut, Catch::Matchers::Approx(mockTimestamps).margin(tolerance));
REQUIRE_THAT(timestampsOut,
Catch::Matchers::Approx(mockTimestamps).margin(tolerance));
}

SECTION("test if more samples than buffer size", "[recording]")
Expand Down Expand Up @@ -146,30 +152,33 @@ TEST_CASE("writeContinuousData", "[recording]")

// write timseries data
nwbRecording.writeTimeseriesData(0,
channel,
dataBuffer.data(),
timestampsBuffer.data(),
dataBuffer.size());
channel,
dataBuffer.data(),
timestampsBuffer.data(),
dataBuffer.size());

nwbRecording.closeFile();

// check contents of data
std::string dataPath = "/acquisition/array0/data";
std::unique_ptr<H5::H5File> file = std::make_unique<H5::H5File>(path + "Recording1.nwb", H5F_ACC_RDONLY);
std::unique_ptr<H5::DataSet> dataset = std::make_unique<H5::DataSet>(file->openDataSet(dataPath));
std::unique_ptr<H5::H5File> file =
std::make_unique<H5::H5File>(path + "Recording1.nwb", H5F_ACC_RDONLY);
std::unique_ptr<H5::DataSet> dataset =
std::make_unique<H5::DataSet>(file->openDataSet(dataPath));

float* buffer = new float[numSamples*numChannels];
float* buffer = new float[numSamples * numChannels];

H5::DataSpace fSpace = dataset->getSpace();
hsize_t dims[1] = {numSamples * numChannels};
H5::DataSpace mSpace(1, dims);
dataset->read(buffer, H5::PredType::NATIVE_FLOAT, mSpace, fSpace);
dataset->read(buffer, H5::PredType::NATIVE_FLOAT, mSpace, fSpace);

std::vector<std::vector<float>> dataOut(numChannels, std::vector<float>(numSamples));
for(SizeType i = 0; i < numChannels; ++i) {
for(SizeType j = 0; j < numSamples; ++j) {
dataOut[i][j] = buffer[j*numChannels + i] * (32767.0f * 0.000002f);
}
std::vector<std::vector<float>> dataOut(numChannels,
std::vector<float>(numSamples));
for (SizeType i = 0; i < numChannels; ++i) {
for (SizeType j = 0; j < numSamples; ++j) {
dataOut[i][j] = buffer[j * numChannels + i] * (32767.0f * 0.000002f);
}
}
delete[] buffer;
REQUIRE_THAT(dataOut[0], Catch::Matchers::Approx(mockData[0]).margin(1));
Expand Down
14 changes: 8 additions & 6 deletions tests/testUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ inline std::string getTestFilePath(std::string filename)
return filepath.u8string();
}

inline std::vector<Types::ChannelGroup> getMockChannelArrays(SizeType numChannels = 2, SizeType numArrays = 2)
inline std::vector<Types::ChannelGroup> getMockChannelArrays(
SizeType numChannels = 2, SizeType numArrays = 2)
{
std::vector<Types::ChannelGroup> arrays(numArrays);
for (SizeType i = 0; i < numArrays; i++)
{
for (SizeType i = 0; i < numArrays; i++) {
std::vector<Channel> chGroup;
for (SizeType j = 0; j < numChannels; j++)
{
Channel ch ("ch" + std::to_string(j), "array" + std::to_string(i), j, i*numArrays + j);
for (SizeType j = 0; j < numChannels; j++) {
Channel ch("ch" + std::to_string(j),
"array" + std::to_string(i),
j,
i * numArrays + j);
chGroup.push_back(ch);
}
arrays[i] = chGroup;
Expand Down

0 comments on commit 64f7a77

Please sign in to comment.