ritsuko
Helper utilities for ArtifactDB C++ code
Loading...
Searching...
No Matches
load_dataset.hpp
Go to the documentation of this file.
1#ifndef RITSUKO_HDF5_LOAD_DATASET_HPP
2#define RITSUKO_HDF5_LOAD_DATASET_HPP
3
4#include <string>
5#include <vector>
6#include <stdexcept>
7
8#include "H5Cpp.h"
9
10#include "get_name.hpp"
14#include "_strings.hpp"
15
21namespace ritsuko {
22
23namespace hdf5 {
24
30inline std::string load_scalar_string_dataset(const H5::DataSet& handle) {
31 auto dtype = handle.getDataType();
32 if (dtype.isVariableStr()) {
33 char* vptr;
34 handle.read(&vptr, dtype);
35 auto dspace = handle.getSpace(); // don't set as temporary in constructor below, otherwise it gets destroyed and the ID invalidated.
36 [[maybe_unused]] VariableStringCleaner deletor(dtype.getId(), dspace.getId(), &vptr);
37 if (vptr == NULL) {
38 throw std::runtime_error("detected a NULL pointer for a variable length string in '" + get_name(handle) + "'");
39 }
40 std::string output(vptr);
41 return output;
42 } else {
43 size_t fixed_length = dtype.getSize();
44 std::vector<char> buffer(fixed_length);
45 handle.read(buffer.data(), dtype);
46 return std::string(buffer.begin(), buffer.begin() + find_string_length(buffer.data(), fixed_length));
47 }
48}
49
57inline std::vector<std::string> load_1d_string_dataset(const H5::DataSet& handle, hsize_t full_length, hsize_t buffer_size) {
58 Stream1dStringDataset stream(&handle, full_length, buffer_size);
59 std::vector<std::string> output;
60 output.reserve(full_length);
61 for (hsize_t i = 0; i < full_length; ++i, stream.next()) {
62 output.emplace_back(stream.steal());
63 }
64 return output;
65}
66
73inline std::vector<std::string> load_1d_string_dataset(const H5::DataSet& handle, hsize_t buffer_size) {
74 return load_1d_string_dataset(handle, get_1d_length(handle, false), buffer_size);
75}
76
77
84template<typename Type_>
85Type_ load_scalar_numeric_dataset(const H5::DataSet& handle) {
86 Type_ output;
87 handle.read(&output, as_numeric_datatype<Type_>());
88 return output;
89}
90
99template<typename Type_>
100std::vector<Type_> load_1d_numeric_dataset(const H5::DataSet& handle, hsize_t full_length, hsize_t buffer_size) {
101 Stream1dNumericDataset<Type_> stream(&handle, full_length, buffer_size);
102 std::vector<Type_> output;
103 output.reserve(full_length);
104 for (hsize_t i = 0; i < full_length; ++i, stream.next()) {
105 output.push_back(stream.get());
106 }
107 return output;
108}
109
117template<typename Type_>
118std::vector<Type_> load_1d_numeric_dataset(const H5::DataSet& handle, hsize_t buffer_size) {
119 return load_1d_numeric_dataset<Type_>(handle, get_1d_length(handle, false), buffer_size);
120}
121
122}
123
124}
125
126#endif
Stream a numeric 1-dimensional HDF5 dataset into memory.
Stream a numeric 1-dimensional HDF5 dataset into memory.
Choose a HDF5 datatype.
Stream a numeric 1-dimensional HDF5 dataset into memory.
Definition Stream1dNumericDataset.hpp:31
Type_ get()
Definition Stream1dNumericDataset.hpp:62
void next(size_t jump=1)
Definition Stream1dNumericDataset.hpp:88
Stream a 1-dimensional HDF5 string dataset into memory.
Definition Stream1dStringDataset.hpp:31
void next(size_t jump=1)
Definition Stream1dStringDataset.hpp:97
std::string steal()
Definition Stream1dStringDataset.hpp:84
Get the name of a HDF5 object.
std::string load_scalar_string_dataset(const H5::DataSet &handle)
Definition load_dataset.hpp:30
std::string get_name(const Handle_ &handle)
Definition get_name.hpp:24
const H5::PredType & as_numeric_datatype()
Definition as_numeric_datatype.hpp:26
std::vector< std::string > load_1d_string_dataset(const H5::DataSet &handle, hsize_t full_length, hsize_t buffer_size)
Definition load_dataset.hpp:57
hsize_t get_1d_length(const H5::DataSpace &space, bool allow_scalar)
Definition get_1d_length.hpp:25
Type_ load_scalar_numeric_dataset(const H5::DataSet &handle)
Definition load_dataset.hpp:85
std::vector< Type_ > load_1d_numeric_dataset(const H5::DataSet &handle, hsize_t full_length, hsize_t buffer_size)
Definition load_dataset.hpp:100
Assorted helper functions for parsing and validation.
Definition choose_missing_placeholder.hpp:15