Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HDF5IO tests for add_read PR #118

Open
wants to merge 16 commits into
base: add_read
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/io/BaseIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ enum class FileMode

/**
* @brief Opens the file with both read and write access.
*
* Note: This is similar to r+ mode, so the file will not be created if it
* does not exist.
*/
ReadWrite,

/**
* @brief Opens the file in read only mode.
*
* Note: This is similar to r+ mode, so the file will not be created if it
* does not exist.
*/
ReadOnly
};
Expand Down
244 changes: 164 additions & 80 deletions src/io/hdf5/HDF5IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ Status HDF5IO::open(FileMode mode)
{
int accFlags = 0;

if (m_opened)
if (m_opened) {
return Status::Failure;
}

if (!std::filesystem::exists(getFileName())) {
if (mode == FileMode::ReadWrite || mode == FileMode::ReadOnly) {
return Status::Failure;
}
}

FileAccPropList fapl = FileAccPropList::DEFAULT;
H5Pset_libver_bounds(fapl.getId(), H5F_LIBVER_LATEST, H5F_LIBVER_LATEST);
Expand Down Expand Up @@ -194,27 +201,71 @@ std::vector<std::string> HDF5IO::readStringDataHelper(
const H5::DataSpace& memspace,
const H5::DataSpace& dataspace) const
{
// Vector to store the read string data
std::vector<std::string> data(numElements);
if (const H5::DataSet* dataset =
dynamic_cast<const H5::DataSet*>(&dataSource))
{
H5::StrType strType(H5::PredType::C_S1);
dataset->read(data.data(), strType, memspace, dataspace);
} else if (const H5::Attribute* attribute =

try {
// Check if the data source is a DataSet
if (const H5::DataSet* dataset =
dynamic_cast<const H5::DataSet*>(&dataSource))
{
// Get the string type of the dataset
H5::StrType strType = dataset->getStrType();

if (strType.isVariableStr()) {
// Handle variable-length strings
std::vector<char*> buffer(numElements);
dataset->read(buffer.data(), strType, memspace, dataspace);

// Convert char* to std::string and free allocated memory
for (size_t i = 0; i < numElements; ++i) {
data[i] = std::string(buffer[i]);
free(buffer[i]); // Free the memory allocated by HDF5
}
} else {
// Handle fixed-length strings
dataset->read(data.data(), strType, memspace, dataspace);
}
}
// Check if the data source is an Attribute
else if (const H5::Attribute* attribute =
dynamic_cast<const H5::Attribute*>(&dataSource))
{
H5::StrType strType(H5::PredType::C_S1);
attribute->read(strType, data.data());
} else {
throw std::runtime_error("Unsupported data source type");
{
// Get the string type of the attribute
H5::StrType strType = attribute->getStrType();

if (strType.isVariableStr()) {
// Handle variable-length strings
std::vector<char*> buffer(numElements);
attribute->read(strType, buffer.data());

// Convert char* to std::string and free allocated memory
for (size_t i = 0; i < numElements; ++i) {
data[i] = std::string(buffer[i]);
free(buffer[i]); // Free the memory allocated by HDF5
}
} else {
// Handle fixed-length strings
attribute->read(strType, data.data());
}
} else {
// Throw an error if the data source type is unsupported
throw std::runtime_error("Unsupported data source type");
}
} catch (const H5::Exception& e) {
// Catch and rethrow HDF5 exceptions with a detailed message
throw std::runtime_error("Failed to read string data: "
+ std::string(e.getDetailMsg()));
}

return data;
}

template<typename HDF5TYPE>
std::vector<std::string> HDF5IO::readStringDataHelper(
const HDF5TYPE& dataSource, size_t numElements) const
{
// Call the main readStringDataHelper function with default DataSpaces
return this->readStringDataHelper(
dataSource, numElements, H5::DataSpace(), H5::DataSpace());
}
Expand All @@ -226,62 +277,48 @@ AQNWB::IO::DataBlockGeneric HDF5IO::readAttribute(
AQNWB::IO::DataBlockGeneric result;
// Read the attribute
auto attributePtr = this->getAttribute(dataPath);
// Make sure dataPath points to an attribute
if (attributePtr == nullptr) {
throw std::invalid_argument("attributePtr is null");
throw std::invalid_argument(
"HDF5IO::readAttribute, attribute does not exist.");
}

H5::Attribute& attribute = *attributePtr;
H5::DataType dataType = attribute.getDataType();

// Determine the shape of the attribute
H5::DataSpace dataspace = attribute.getSpace();
int rank = dataspace.getSimpleExtentNdims();
if (rank == 0) {
// Scalar attribute
result.shape.clear();
} else {
result.shape.clear();
if (rank > 0) {
std::vector<hsize_t> tempShape(rank);
dataspace.getSimpleExtentDims(tempShape.data(), nullptr);
result.shape.clear();
result.shape.reserve(rank);
for (const auto& v : tempShape) {
result.shape.push_back(v);
}
result.shape.assign(tempShape.begin(), tempShape.end());
}

// Determine the size of the attribute from the shape
size_t numElements = 1; // Scalar (default)
if (result.shape.size() > 0) { // N-dimensional array
for (const auto v : result.shape) {
numElements *= v;
}
size_t numElements = 1;
for (const auto v : result.shape) {
numElements *= v;
}

// Read the attribute into a vector of the appropriate type
if (dataType.getClass() == H5T_STRING) {
if (dataType.isVariableStr()) {
// Handle variable-length strings
std::vector<std::string> stringData;
try {
// Allocate a buffer to hold the variable-length string data
char* buffer;
attribute.read(dataType, &buffer);

// Convert the buffer to std::string
stringData.emplace_back(buffer);

// Free the memory allocated by HDF5
H5Dvlen_reclaim(
dataType.getId(), dataspace.getId(), H5P_DEFAULT, &buffer);
} catch (const H5::Exception& e) {
throw std::runtime_error(
"Failed to read variable-length string attribute: "
+ std::string(e.getDetailMsg()));
std::vector<char*> buffer(numElements);
attribute.read(dataType, buffer.data());

for (size_t i = 0; i < numElements; ++i) {
stringData.emplace_back(buffer[i]);
free(buffer[i]); // Free the memory allocated by HDF5
}
result.data = stringData;
result.typeIndex = typeid(std::string);
} else {
result.data = readStringDataHelper(attribute, numElements);
// Handle fixed-length strings
result.data = readStringDataHelper(
attribute, numElements, H5::DataSpace(), dataspace);
result.typeIndex = typeid(std::string);
}
} else if (dataType == H5::PredType::NATIVE_DOUBLE) {
Expand All @@ -290,6 +327,12 @@ AQNWB::IO::DataBlockGeneric HDF5IO::readAttribute(
} else if (dataType == H5::PredType::NATIVE_FLOAT) {
result.data = readDataHelper<float>(attribute, numElements);
result.typeIndex = typeid(float);
} else if (dataType == H5::PredType::NATIVE_INT32) {
result.data = readDataHelper<int32_t>(attribute, numElements);
result.typeIndex = typeid(int32_t);
} else if (dataType == H5::PredType::NATIVE_UINT32) {
result.data = readDataHelper<uint32_t>(attribute, numElements);
result.typeIndex = typeid(uint32_t);
} else if (dataType == H5::PredType::NATIVE_INT) {
result.data = readDataHelper<int>(attribute, numElements);
result.typeIndex = typeid(int);
Expand All @@ -308,6 +351,47 @@ AQNWB::IO::DataBlockGeneric HDF5IO::readAttribute(
} else if (dataType == H5::PredType::NATIVE_ULLONG) {
result.data = readDataHelper<unsigned long long>(attribute, numElements);
result.typeIndex = typeid(unsigned long long);
} else if (dataType.getClass() == H5T_ARRAY) {
// Handle array attributes
H5::ArrayType arrayType(dataType.getId());
H5::DataType baseType = arrayType.getSuper();
int arrayRank = arrayType.getArrayNDims();
std::vector<hsize_t> arrayDims(arrayRank);
arrayType.getArrayDims(arrayDims.data());

// Update the shape to reflect the array dimensions
result.shape.assign(arrayDims.begin(), arrayDims.end());

size_t arrayNumElements = 1;
for (const auto dim : arrayDims) {
arrayNumElements *= dim;
}

if (baseType == H5::PredType::NATIVE_INT32) {
result.data =
readDataHelper<int32_t>(attribute, numElements * arrayNumElements);
result.typeIndex = typeid(int32_t);
} else if (baseType == H5::PredType::NATIVE_UINT32) {
result.data =
readDataHelper<uint32_t>(attribute, numElements * arrayNumElements);
result.typeIndex = typeid(uint32_t);
} else if (baseType == H5::PredType::NATIVE_FLOAT) {
result.data =
readDataHelper<float>(attribute, numElements * arrayNumElements);
result.typeIndex = typeid(float);
} else if (baseType == H5::PredType::NATIVE_DOUBLE) {
result.data =
readDataHelper<double>(attribute, numElements * arrayNumElements);
result.typeIndex = typeid(double);
} else {
throw std::runtime_error("Unsupported array base data type");
}
} else if (dataType.getClass() == H5T_REFERENCE) {
// Handle object references
std::vector<hobj_ref_t> refData(numElements);
attribute.read(dataType, refData.data());
result.data = refData;
result.typeIndex = typeid(hobj_ref_t);
} else {
throw std::runtime_error("Unsupported data type");
}
Expand Down Expand Up @@ -595,21 +679,13 @@ Status HDF5IO::createReferenceAttribute(const std::string& referencePath,
attr = loc->createAttribute(name, H5::PredType::STD_REF_OBJ, attr_space);
}

hobj_ref_t* rdata = new hobj_ref_t[sizeof(hobj_ref_t)];

m_file->reference(rdata, referencePath.c_str());
hobj_ref_t rdata;
m_file->reference(&rdata, referencePath.c_str());

attr.write(H5::PredType::STD_REF_OBJ, rdata);
delete[] rdata;

} catch (GroupIException error) {
error.printErrorStack();
} catch (AttributeIException error) {
error.printErrorStack();
} catch (FileIException error) {
error.printErrorStack();
} catch (DataSetIException error) {
attr.write(H5::PredType::STD_REF_OBJ, &rdata);
} catch (const H5::Exception& error) {
error.printErrorStack();
return Status::Failure;
}

return Status::Success;
Expand Down Expand Up @@ -876,16 +952,24 @@ std::unique_ptr<AQNWB::IO::BaseRecordingData> HDF5IO::createArrayDataSet(

H5O_type_t HDF5IO::getH5ObjectType(const std::string& path) const
{
#if H5_VERSION_GE(1, 12, 0)
// get whether path is a dataset or group
H5O_info_t objInfo; // Structure to hold information about the object
H5Oget_info_by_name(
herr_t status;

#if H5_VERSION_GE(1, 12, 0)
status = H5Oget_info_by_name(
m_file->getId(), path.c_str(), &objInfo, H5O_INFO_BASIC, H5P_DEFAULT);
#else
// get whether path is a dataset or group
H5O_info_t objInfo; // Structure to hold information about the object
H5Oget_info_by_name(m_file->getId(), path.c_str(), &objInfo, H5P_DEFAULT);
status =
H5Oget_info_by_name(m_file->getId(), path.c_str(), &objInfo, H5P_DEFAULT);
#endif

// Check if the object exists
if (status < 0) {
// Return a special value indicating the object does not exist
return H5O_TYPE_UNKNOWN;
}

// Get the object type
H5O_type_t objectType = objInfo.type;

return objectType;
Expand All @@ -897,49 +981,49 @@ H5::DataType HDF5IO::getNativeType(IO::BaseDataType type)

switch (type.type) {
case IO::BaseDataType::Type::T_I8:
baseType = PredType::NATIVE_INT8;
baseType = H5::PredType::NATIVE_INT8;
break;
case IO::BaseDataType::Type::T_I16:
baseType = PredType::NATIVE_INT16;
baseType = H5::PredType::NATIVE_INT16;
break;
case IO::BaseDataType::Type::T_I32:
baseType = PredType::NATIVE_INT32;
baseType = H5::PredType::NATIVE_INT32;
break;
case IO::BaseDataType::Type::T_I64:
baseType = PredType::NATIVE_INT64;
baseType = H5::PredType::NATIVE_INT64;
break;
case IO::BaseDataType::Type::T_U8:
baseType = PredType::NATIVE_UINT8;
baseType = H5::PredType::NATIVE_UINT8;
break;
case IO::BaseDataType::Type::T_U16:
baseType = PredType::NATIVE_UINT16;
baseType = H5::PredType::NATIVE_UINT16;
break;
case IO::BaseDataType::Type::T_U32:
baseType = PredType::NATIVE_UINT32;
baseType = H5::PredType::NATIVE_UINT32;
break;
case IO::BaseDataType::Type::T_U64:
baseType = PredType::NATIVE_UINT64;
baseType = H5::PredType::NATIVE_UINT64;
break;
case IO::BaseDataType::Type::T_F32:
baseType = PredType::NATIVE_FLOAT;
baseType = H5::PredType::NATIVE_FLOAT;
break;
case IO::BaseDataType::Type::T_F64:
baseType = PredType::NATIVE_DOUBLE;
baseType = H5::PredType::NATIVE_DOUBLE;
break;
case IO::BaseDataType::Type::T_STR:
return StrType(PredType::C_S1, type.typeSize);
break;
return H5::StrType(H5::PredType::C_S1, type.typeSize);
case IO::BaseDataType::Type::V_STR:
return StrType(PredType::C_S1, H5T_VARIABLE);
break;
return H5::StrType(H5::PredType::C_S1, H5T_VARIABLE);
default:
baseType = PredType::NATIVE_INT32;
baseType = H5::PredType::NATIVE_INT32;
}

if (type.typeSize > 1) {
hsize_t size = type.typeSize;
return ArrayType(baseType, 1, &size);
} else
return H5::ArrayType(baseType, 1, &size);
} else {
return baseType;
}
}

H5::DataType HDF5IO::getH5Type(IO::BaseDataType type)
Expand Down
Loading
Loading