19std::vector<std::string> split_path(
const std::string& path)
21 std::vector<std::string>
out;
26 out.push_back(std::move(cur));
33 if (!cur.empty())
out.push_back(std::move(cur));
40hid_t open_or_create_groups(hid_t parent,
41 const std::vector<std::string>& groups)
45 for (
const auto& g: groups) {
47 if (H5Lexists(cur, g.c_str(), H5P_DEFAULT) > 0) {
48 next = H5Gopen2(cur, g.c_str(), H5P_DEFAULT);
50 next = H5Gcreate2(cur, g.c_str(), H5P_DEFAULT, H5P_DEFAULT,
54 if (owned) H5Gclose(cur);
55 throw std::runtime_error(
"H5Dump: failed to open/create group '" +
58 if (owned) H5Gclose(cur);
67H5Dumper::H5Dumper(
const std::string& path, Mode mode) : file_(-1)
69 if (mode == Mode::Overwrite) {
71 H5Fcreate(path.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
75 if (H5Fis_accessible(path.c_str(), H5P_DEFAULT) > 0) {
76 file_ = H5Fopen(path.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
78 file_ = H5Fcreate(path.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
83 throw std::runtime_error(
"H5Dump: failed to open/create file '" + path +
90 if (file_ >= 0) H5Fclose(file_);
93void H5Dumper::dump(
const std::string& dataset_path,
const double* data,
96 const hsize_t dims[1] = {
static_cast<hsize_t
>(n)};
97 write(dataset_path, H5T_NATIVE_DOUBLE, 1, dims, data);
100void H5Dumper::dump2d(
const std::string& dataset_path,
const double* data,
101 std::size_t rows, std::size_t cols)
103 const hsize_t dims[2] = {
static_cast<hsize_t
>(rows),
104 static_cast<hsize_t
>(cols)};
105 write(dataset_path, H5T_NATIVE_DOUBLE, 2, dims, data);
108void H5Dumper::dumpInt32(
const std::string& dataset_path,
109 const std::int32_t* data, std::size_t n)
111 const hsize_t dims[1] = {
static_cast<hsize_t
>(n)};
112 write(dataset_path, H5T_NATIVE_INT32, 1, dims, data);
115void H5Dumper::rootAttribute(
const std::string& name,
const std::string& value)
117 if (H5Aexists(file_, name.c_str()) > 0) H5Adelete(file_, name.c_str());
118 const hid_t type = H5Tcopy(H5T_C_S1);
119 H5Tset_size(type, H5T_VARIABLE);
120 H5Tset_cset(type, H5T_CSET_UTF8);
121 const hid_t space = H5Screate(H5S_SCALAR);
123 H5Acreate2(file_, name.c_str(), type, space, H5P_DEFAULT, H5P_DEFAULT);
126 const char* text = value.c_str();
127 status = H5Awrite(attr, type, &text);
133 throw std::runtime_error(
"H5Dump: failed to write attribute '" + name +
138void H5Dumper::write(
const std::string& dataset_path, hid_t type,
int rank,
139 const hsize_t* dims,
const void* data)
141 auto parts = split_path(dataset_path);
143 throw std::runtime_error(
"H5Dump: empty dataset path");
145 std::string dset_name = parts.back();
148 hid_t parent = open_or_create_groups(file_, parts);
149 bool parent_owned = !parts.empty();
152 if (H5Lexists(parent, dset_name.c_str(), H5P_DEFAULT) > 0) {
153 H5Ldelete(parent, dset_name.c_str(), H5P_DEFAULT);
156 hid_t space = H5Screate_simple(rank, dims,
nullptr);
157 hid_t dset = H5Dcreate2(parent, dset_name.c_str(), type, space, H5P_DEFAULT,
158 H5P_DEFAULT, H5P_DEFAULT);
161 if (parent_owned) H5Gclose(parent);
162 throw std::runtime_error(
"H5Dump: H5Dcreate2 failed for '" +
166 herr_t
status = H5Dwrite(dset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
169 if (parent_owned) H5Gclose(parent);
172 throw std::runtime_error(
"H5Dump: H5Dwrite failed for '" +
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.