chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
validate.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_VALIDATE_HPP
2#define CHIHAYA_VALIDATE_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
6#include "ritsuko/hdf5/hdf5.hpp"
7
8#include "subset.hpp"
9#include "combine.hpp"
10#include "transpose.hpp"
11
12#include "dense_array.hpp"
13#include "sparse_matrix.hpp"
14#include "external_hdf5.hpp"
15#include "custom_array.hpp"
16#include "constant_array.hpp"
17
18#include "dimnames.hpp"
19#include "subset_assignment.hpp"
20
21#include "unary_arithmetic.hpp"
22#include "unary_comparison.hpp"
23#include "unary_logic.hpp"
24#include "unary_math.hpp"
26
27#include "binary_arithmetic.hpp"
28#include "binary_comparison.hpp"
29#include "binary_logic.hpp"
30
31#include "matrix_product.hpp"
32
33#include "utils_public.hpp"
34
35#include <string>
36#include <stdexcept>
37
43namespace chihaya {
44
48namespace internal {
49
50inline auto default_operation_registry() {
51 std::unordered_map<std::string, std::function<ArrayDetails(const H5::Group&, const ritsuko::Version&, Options&)> > registry;
52 registry["subset"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return subset::validate(h, v, o); };
53 registry["combine"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return combine::validate(h, v, o); };
54 registry["transpose"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return transpose::validate(h, v, o); };
55 registry["dimnames"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return dimnames::validate(h, v, o); };
56 registry["subset assignment"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return subset_assignment::validate(h, v, o); };
57 registry["unary arithmetic"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return unary_arithmetic::validate(h, v, o); };
58 registry["unary comparison"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return unary_comparison::validate(h, v, o); };
59 registry["unary logic"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return unary_logic::validate(h, v, o); };
60 registry["unary math"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return unary_math::validate(h, v, o); };
61 registry["unary special check"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return unary_special_check::validate(h, v, o); };
62 registry["binary arithmetic"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return binary_arithmetic::validate(h, v, o); };
63 registry["binary comparison"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return binary_comparison::validate(h, v, o); };
64 registry["binary logic"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return binary_logic::validate(h, v, o); };
65 registry["matrix product"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return matrix_product::validate(h, v, o); };
66 return registry;
67}
68
69inline auto default_array_registry() {
70 std::unordered_map<std::string, std::function<ArrayDetails(const H5::Group&, const ritsuko::Version&, Options&)> > registry;
71 registry["dense array"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return dense_array::validate(h, v, o); };
72 registry["sparse matrix"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return sparse_matrix::validate(h, v, o); };
73 registry["constant array"] = [](const H5::Group& h, const ritsuko::Version& v, Options& o) -> ArrayDetails { return constant_array::validate(h, v, o); };
74 return registry;
75}
76
77}
92inline ArrayDetails validate(const H5::Group& handle, const ritsuko::Version& version, Options& options) {
93 auto dtype = ritsuko::hdf5::open_and_load_scalar_string_attribute(handle, "delayed_type");
94 ArrayDetails output;
95
96 if (dtype == "array") {
97 auto atype = ritsuko::hdf5::open_and_load_scalar_string_attribute(handle, "delayed_array");
98
99 const auto& custom = options.array_validate_registry;
100 auto cit = custom.find(atype);
101 if (cit != custom.end()) {
102 try {
103 output = (cit->second)(handle, version, options);
104 } catch (std::exception& e) {
105 throw std::runtime_error("failed to validate delayed array of type '" + atype + "'; " + std::string(e.what()));
106 }
107
108 } else {
109 static const auto global = internal::default_array_registry();
110 auto git = global.find(atype);
111 if (git != global.end()) {
112 try {
113 output = (git->second)(handle, version, options);
114 } catch (std::exception& e) {
115 throw std::runtime_error("failed to validate delayed array of type '" + atype + "'; " + std::string(e.what()));
116 }
117 } else if (atype.rfind("custom ", 0) != std::string::npos) {
118 try {
119 output = custom_array::validate(handle, version, options);
120 } catch (std::exception& e) {
121 throw std::runtime_error("failed to validate delayed array of type '" + atype + "'; " + std::string(e.what()));
122 }
123 } else if (atype.rfind("external hdf5 ", 0) != std::string::npos && version.lt(1, 1, 0)) {
124 try {
125 output = external_hdf5::validate(handle, version, options);
126 } catch (std::exception& e) {
127 throw std::runtime_error("failed to validate delayed array of type '" + atype + "'; " + std::string(e.what()));
128 }
129 } else {
130 throw std::runtime_error("unknown array type '" + atype + "'");
131 }
132 }
133
134 } else if (dtype == "operation") {
135 auto otype = ritsuko::hdf5::open_and_load_scalar_string_attribute(handle, "delayed_operation");
136
137 const auto& custom = options.operation_validate_registry;
138 auto cit = custom.find(otype);
139 if (cit != custom.end()) {
140 try {
141 output = (cit->second)(handle, version, options);
142 } catch (std::exception& e) {
143 throw std::runtime_error("failed to validate delayed operation of type '" + otype + "'; " + std::string(e.what()));
144 }
145
146 } else {
147 static const auto global = internal::default_operation_registry();
148 auto git = global.find(otype);
149 if (git != global.end()) {
150 try {
151 output = (git->second)(handle, version, options);
152 } catch (std::exception& e) {
153 throw std::runtime_error("failed to validate delayed operation of type '" + otype + "'; " + std::string(e.what()));
154 }
155 } else {
156 throw std::runtime_error("unknown operation type '" + otype + "'");
157 }
158 }
159
160 } else {
161 throw std::runtime_error("unknown delayed type '" + dtype + "'");
162 }
163
164 return output;
165}
166
176inline ritsuko::Version extract_version(const H5::Group& handle) {
177 ritsuko::Version version;
178
179 if (handle.attrExists("delayed_version")) {
180 auto ahandle = handle.openAttribute("delayed_version");
181 if (!ritsuko::hdf5::is_utf8_string(ahandle)) {
182 throw std::runtime_error("expected 'delayed_version' to use a datatype that can be represented by a UTF-8 encoded string");
183 }
184
185 auto vstring = ritsuko::hdf5::load_scalar_string_attribute(ahandle);
186 if (vstring == "1.0.0") {
187 version.major = 1;
188 } else {
189 version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(), /* skip_patch = */ true);
190 }
191 } else {
192 version.minor = 99;
193 }
194
195 return version;
196}
197
205inline ArrayDetails validate(const H5::Group& handle, Options& options) {
206 return validate(handle, extract_version(handle), options);
207}
208
219inline ArrayDetails validate(const std::string& path, const std::string& name, Options& options) {
220 H5::H5File handle(path, H5F_ACC_RDONLY);
221 auto ghandle = handle.openGroup(name);
222 return validate(ghandle, options);
223}
224
234inline ArrayDetails validate(const std::string& path, const std::string& name) {
235 H5::H5File handle(path, H5F_ACC_RDONLY);
236 Options options;
237 auto ghandle = handle.openGroup(name);
238 return validate(ghandle, options);
239}
240
241}
242
243#endif
Validation for delayed binary arithmetic operations.
Validation for delayed binary comparisons.
Validation for delayed binary logical operations.
Validation for delayed combining operations.
Constant array, stored inside the file.
Validation for custom third-party arrays.
Dense array, stored inside the file.
Validation for delayed dimnames assignment.
Validation for external HDF5 arrays.
Validation for delayed matrix products.
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: custom_array.hpp:31
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: external_hdf5.hpp:31
Namespace for all chihaya functions.
Definition: binary_arithmetic.hpp:22
ritsuko::Version extract_version(const H5::Group &handle)
Definition: validate.hpp:176
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: validate.hpp:92
Validation for compressed sparse column matrices.
Details about an array.
Definition: utils_public.hpp:36
Validation options.
Definition: utils_public.hpp:66
std::unordered_map< std::string, std::function< ArrayDetails(const H5::Group &, const ritsuko::Version &, Options &)> > array_validate_registry
Definition: utils_public.hpp:77
std::unordered_map< std::string, std::function< ArrayDetails(const H5::Group &, const ritsuko::Version &, Options &)> > operation_validate_registry
Definition: utils_public.hpp:83
Validation for delayed subsets.
Validation for delayed subset assignment.
Validation for delayed transposition.
Validation for delayed unary arithmetic operations.
Validation for delayed unary comparisons.
Validation for delayed unary logic operations.
Validation for delayed unary math operations.
Validation for delayed unary special checks.
Various public utilities.