-
Notifications
You must be signed in to change notification settings - Fork 1
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
Enhance H5 I/O #2
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e339b03
Create h5 directory in include and move relevant files
dimitrivlachos 6493c85
Create basic h5 filewriter
dimitrivlachos da24d26
Add unit test for h5 filewriter
dimitrivlachos d8fb7de
Create recursive group creation and traversal for generalisablility
dimitrivlachos 0a4dcc3
Add functions to deduce shape and flatten nested containers for HDF5 …
dimitrivlachos 4a3118a
Add function to write arbitrarily dimensional data to HDF5 files
dimitrivlachos 97361a9
Add comprehensive unit tests for HDF5 data writing functions
dimitrivlachos 963878c
Enhance HDF5 dataset reading function with improved error handling
dimitrivlachos 5e1a186
Update CMake to allow inclusion
dimitrivlachos 3ee6668
Update h5write to handle pre-existing datasets and add test cases
dimitrivlachos 930bcc0
Suppress HDF5 stdout error logging to stdout
jbeilstenedmands 2bd2c02
Refactor HDF5 write tests to use test fixtures for better structure a…
dimitrivlachos a32d6d1
Add empty dataset for testing purposes
dimitrivlachos b6f12a3
Refactor and expand HDF5 read tests
dimitrivlachos 7c675d7
Update header file extensions from .h to .hpp
dimitrivlachos b666dc5
Replace include guards with #pragma once in HDF5 read and write headers
dimitrivlachos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#pragma once | ||
|
||
#include <cassert> | ||
#include <chrono> | ||
#include <cstring> | ||
#include <hdf5.h> | ||
#include <hdf5_hl.h> | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
/** | ||
* @brief Reads a dataset from an HDF5 file into an std::vector. | ||
* | ||
* @tparam T The type of data to read (e.g., int, double). | ||
* @param filename The path to the HDF5 file. | ||
* @param dataset_name The name of the dataset to read. | ||
* @return A std::vector containing the data from the dataset. | ||
* @throws std::runtime_error If the file, dataset, or datatype cannot be opened | ||
* or read. | ||
*/ | ||
template <typename T> | ||
std::vector<T> read_array_from_h5_file(const std::string &filename, | ||
const std::string &dataset_name) { | ||
// Start measuring time | ||
auto start_time = std::chrono::high_resolution_clock::now(); | ||
|
||
// Open the HDF5 file | ||
hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); | ||
if (file < 0) { | ||
throw std::runtime_error("Error: Unable to open file: " + filename); | ||
} | ||
|
||
try { | ||
// Open the dataset | ||
hid_t dataset = H5Dopen(file, dataset_name.c_str(), H5P_DEFAULT); | ||
if (dataset < 0) { | ||
throw std::runtime_error("Error: Unable to open dataset: " + | ||
dataset_name); | ||
} | ||
|
||
try { | ||
// Get the datatype and check size | ||
hid_t datatype = H5Dget_type(dataset); | ||
size_t datatype_size = H5Tget_size(datatype); | ||
if (datatype_size != sizeof(T)) { | ||
throw std::runtime_error( | ||
"Error: Dataset type size does not match expected type size."); | ||
} | ||
|
||
// Get the dataspace and the number of elements | ||
hid_t dataspace = H5Dget_space(dataset); | ||
size_t num_elements = H5Sget_simple_extent_npoints(dataspace); | ||
|
||
// Allocate a vector to hold the data | ||
std::vector<T> data_out(num_elements); | ||
|
||
// Read the data into the vector | ||
herr_t status = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, | ||
data_out.data()); | ||
if (status < 0) { | ||
throw std::runtime_error("Error: Unable to read dataset: " + | ||
dataset_name); | ||
} | ||
|
||
// Close the dataset and return the data | ||
H5Dclose(dataset); | ||
H5Fclose(file); | ||
|
||
// Log timing | ||
auto end_time = std::chrono::high_resolution_clock::now(); | ||
double elapsed_time = | ||
std::chrono::duration<double>(end_time - start_time).count(); | ||
std::cout << "READ TIME for " << dataset_name << " : " << elapsed_time | ||
<< "s" << std::endl; | ||
|
||
return data_out; | ||
|
||
} catch (...) { | ||
H5Dclose(dataset); // Ensure dataset is closed in case of failure | ||
throw; | ||
} | ||
} catch (...) { | ||
H5Fclose(file); // Ensure file is closed in case of failure | ||
throw; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ndevenish I probably shouldn't keep all of this commented. Should I? Or is it something we can come back to later when we're going to need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The decision is we will revisit this later when we need it as the repo is still in an early state