Skip to content

Commit

Permalink
Merge pull request #33 from NeurodataWithoutBorders/refactor-data-write
Browse files Browse the repository at this point in the history
update methods for writing n-dimensional data
  • Loading branch information
stephprince authored Jul 29, 2024
2 parents 74cf4c7 + c71ec22 commit 8354798
Show file tree
Hide file tree
Showing 26 changed files with 1,114 additions and 573 deletions.
6 changes: 4 additions & 2 deletions src/BaseIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ BaseRecordingData::BaseRecordingData() {}

BaseRecordingData::~BaseRecordingData() {}

Status BaseRecordingData::writeDataBlock(const SizeType& xDataSize,
// Overload that uses the member variable position (works for simple data
// extension)
Status BaseRecordingData::writeDataBlock(const std::vector<SizeType>& dataShape,
const BaseDataType& type,
const void* data)
{
return writeDataBlock(xDataSize, size[1], type, data);
return writeDataBlock(dataShape, position, type, data);
}
53 changes: 20 additions & 33 deletions src/BaseIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ class BaseIO
* @param path The location in the file of the new dataset.
* @return A pointer to the created dataset.
*/
virtual BaseRecordingData* createDataSet(const BaseDataType& type,
const SizeArray& size,
const SizeArray& chunking,
const std::string& path) = 0;
virtual BaseRecordingData* createArrayDataSet(const BaseDataType& type,
const SizeArray& size,
const SizeArray& chunking,
const std::string& path) = 0;

/**
* @brief Returns a pointer to a dataset at a given path.
Expand Down Expand Up @@ -357,64 +357,51 @@ class BaseRecordingData
virtual ~BaseRecordingData();

/**
* @brief Writes a 1D block of data (samples).
* @param xDataSize The size of the data block in the x dimension (samples).
* @brief Writes a block of data using the stored position information.
* This is not intended to be overwritten by derived classes, but is a
* convenience function for writing data using the last recorded position.
* @param dataShape The size of the data block.
* @param type The data type of the elements in the data block.
* @param data A pointer to the data block.
* @return The status of the write operation.
*/
Status writeDataBlock(const SizeType& xDataSize,
Status writeDataBlock(const std::vector<SizeType>& dataShape,
const BaseDataType& type,
const void* data);

/**
* @brief Writes a 2D block of data (samples x channels).
* @param xDataSize The size of the data block in the x dimension (samples).
* @param yDataSize The size of the data block in the y dimension (channels).
* @brief Writes a block of data (any number of dimensions).
* @param dataShape The size of the data block.
* @param positionOffset The position of the data block to write to.
* @param type The data type of the elements in the data block.
* @param data A pointer to the data block.
* @return The status of the write operation.
*/
virtual Status writeDataBlock(const SizeType& xDataSize,
const SizeType& yDataSize,
virtual Status writeDataBlock(const std::vector<SizeType>& dataShape,
const std::vector<SizeType>& positionOffset,
const BaseDataType& type,
const void* data) = 0;

/**
* @brief Writes a row of data in a 2D block.
* @param xDataSize The size of the data row in the x dimension (samples).
* @param yPosition The position of the data row in the y dimension
* (channels).
* @param type The data type of the elements in the data block.
* @param data A pointer to the data block.
* @return The status of the write operation.
*/
virtual Status writeDataRow(const SizeType& xDataSize,
const SizeType& yPos,
const BaseDataType& type,
const void* data) = 0;

protected:
/**
* @brief The current position in the x dimension.
*/
SizeType xPos;

/**
* @brief The size of the data block in each dimension.
* @brief The size of the dataset in each dimension.
*/
SizeType size[3];
std::vector<SizeType> size;

/**
* @brief The number of dimensions in the data block.
* @brief The current position in the dataset.
*/
SizeType dimension; /**< The number of dimensions in the data block. */
std::vector<SizeType> position;

/**
* @brief The position in the x dimension of samples written for each row
* (channel).
* @brief The number of dimensions in the data block.
*/
std::vector<uint32_t> rowXPos;
SizeType nDimensions;
};

} // namespace AQNWB
4 changes: 3 additions & 1 deletion src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Channel::Channel(const std::string name,
const float conversion,
const float samplingRate,
const float bitVolts,
const std::array<float, 3> position)
const std::array<float, 3> position,
const std::string comments)
: name(name)
, groupName(groupName)
, localIndex(localIndex)
Expand All @@ -20,6 +21,7 @@ Channel::Channel(const std::string name,
, conversion(conversion)
, samplingRate(samplingRate)
, bitVolts(bitVolts)
, comments(comments)
{
}

Expand Down
9 changes: 8 additions & 1 deletion src/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Channel
const float bitVolts = 0.000002f, // least significant bit needed to
// convert 16-bit int to volts
// currently a placeholder
const std::array<float, 3> position = {0.f, 0.f, 0.f});
const std::array<float, 3> position = {0.f, 0.f, 0.f},
const std::string comments = "no comments");

/**
* @brief Destructor
Expand All @@ -45,6 +46,7 @@ class Channel
* @return The samplingRate value.
*/
float getSamplingRate() const;

/**
* @brief Getter for bitVolts
* @return The bitVolts value.
Expand Down Expand Up @@ -76,6 +78,11 @@ class Channel
*/
std::array<float, 3> position;

/**
* @brief Comments about the channel.
*/
std::string comments;

private:
/**
* @brief Conversion factor.
Expand Down
4 changes: 2 additions & 2 deletions src/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Types
using SizeArray = std::vector<size_t>;

/**
* @brief Alias for a group of channels.
* @brief Alias for a vector of channels.
*/
using ChannelGroup = std::vector<Channel>;
using ChannelVector = std::vector<Channel>;
};
} // namespace AQNWB
25 changes: 25 additions & 0 deletions src/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,29 @@ inline std::unique_ptr<BaseIO> createIO(const std::string& type,
throw std::invalid_argument("Invalid IO type");
}
}

inline std::unique_ptr<int16_t[]> transformToInt16(SizeType numSamples,
float conversion_factor,
const float* data)
{
std::unique_ptr<float[]> scaledData = std::make_unique<float[]>(numSamples);
std::unique_ptr<int16_t[]> intData = std::make_unique<int16_t[]>(numSamples);

// copy data and multiply by scaling factor
double multFactor = 1 / (32767.0f * conversion_factor);
std::transform(data,
data + numSamples,
scaledData.get(),
[multFactor](float value) { return value * multFactor; });

// convert float to int16
std::transform(
scaledData.get(),
scaledData.get() + numSamples,
intData.get(),
[](float value)
{ return static_cast<int16_t>(std::clamp(value, -32768.0f, 32767.0f)); });

return intData;
}
} // namespace AQNWB
Loading

0 comments on commit 8354798

Please sign in to comment.