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
7#include "subset.hpp"
8#include "combine.hpp"
9#include "transpose.hpp"
10
11#include "dense_array.hpp"
12#include "sparse_matrix.hpp"
13#include "external_hdf5.hpp"
14#include "custom_array.hpp"
15#include "constant_array.hpp"
16
17#include "dimnames.hpp"
18#include "subset_assignment.hpp"
19
20#include "unary_arithmetic.hpp"
21#include "unary_comparison.hpp"
22#include "unary_logic.hpp"
23#include "unary_math.hpp"
25
26#include "binary_arithmetic.hpp"
27#include "binary_comparison.hpp"
28#include "binary_logic.hpp"
29
30#include "matrix_product.hpp"
31
32#include "utils_public.hpp"
33
34#include <string>
35#include <stdexcept>
36#include <exception>
37#include <unordered_map>
38#include <functional>
39
45namespace chihaya {
46
50inline auto default_operation_registry() {
51 ValidateRegistry registry;
52 registry["subset"] = validate_subset;
53 registry["combine"] = validate_combine;
54 registry["transpose"] = validate_transpose;
55 registry["dimnames"] = validate_dimnames;
56 registry["subset assignment"] = validate_subset_assignment;
57 registry["unary arithmetic"] = validate_unary_arithmetic;
58 registry["unary comparison"] = validate_unary_comparison;
59 registry["unary logic"] = validate_unary_logic;
60 registry["unary math"] = validate_unary_math;
61 registry["unary special check"] = validate_unary_special_check;
62 registry["binary arithmetic"] = validate_binary_arithmetic;
63 registry["binary comparison"] = validate_binary_comparison;
64 registry["binary logic"] = validate_binary_logic;
65 registry["matrix product"] = validate_matrix_product;
66 return registry;
67}
68
69inline auto default_array_registry() {
70 ValidateRegistry registry;
71 registry["dense array"] = validate_dense_array;
72 registry["sparse matrix"] = validate_sparse_matrix;
73 registry["constant array"] = validate_constant_array;
74 return registry;
75}
96inline ArrayDetails validate(const H5::Group& group, const ritsuko::Version& version, const Options& options) {
97 std::string dtype;
98 try {
99 auto ahandle = group.openAttribute("delayed_type");
100 dtype = read_scalar_string_attribute(ahandle);
101 } catch (...) {
102 std::throw_with_nested(std::runtime_error("failed to validate the 'delayed_type' attribute"));
103 }
104
105 ArrayDetails output;
106 if (dtype == "array") {
107 std::string atype;
108 try {
109 auto ahandle = group.openAttribute("delayed_array");
110 atype = read_scalar_string_attribute(ahandle);
111 } catch (...) {
112 std::throw_with_nested(std::runtime_error("failed to validate the 'delayed_array' attribute"));
113 }
114
115 const auto& custom = options.array_validate_registry;
116 auto cit = custom.find(atype);
117 if (cit != custom.end()) {
118 try {
119 output = (cit->second)(group, version, options);
120 } catch (...) {
121 std::throw_with_nested(std::runtime_error("failed to validate delayed array of type '" + atype + "'"));
122 }
123
124 } else {
125 static const auto global = default_array_registry();
126 auto git = global.find(atype);
127 if (git != global.end()) {
128 try {
129 output = (git->second)(group, version, options);
130 } catch (...) {
131 std::throw_with_nested(std::runtime_error("failed to validate delayed array of type '" + atype + "'"));
132 }
133
134 } else if (atype.rfind("custom ", 0) != std::string::npos) {
135 try {
136 output = validate_custom_array(group, version, options);
137 } catch (...) {
138 std::throw_with_nested(std::runtime_error("failed to validate delayed array of type '" + atype + "'"));
139 }
140
141 } else if (atype.rfind("external hdf5 ", 0) != std::string::npos && version.lt(1, 1, 0)) {
142 try {
143 output = validate_external_hdf5(group, version, options);
144 } catch (...) {
145 std::throw_with_nested(std::runtime_error("failed to validate delayed array of type '" + atype + "'"));
146 }
147
148 } else {
149 throw std::runtime_error("unknown array type '" + atype + "'");
150 }
151 }
152
153 } else if (dtype == "operation") {
154 std::string otype;
155 try {
156 auto ohandle = group.openAttribute("delayed_operation");
157 otype = read_scalar_string_attribute(ohandle);
158 } catch (...) {
159 std::throw_with_nested(std::runtime_error("failed to validate the 'delayed_operation' attribute"));
160 }
161
162 const auto& custom = options.operation_validate_registry;
163 auto cit = custom.find(otype);
164 if (cit != custom.end()) {
165 try {
166 output = (cit->second)(group, version, options);
167 } catch (...) {
168 std::throw_with_nested(std::runtime_error("failed to validate delayed operation of type '" + otype + "'"));
169 }
170
171 } else {
172 static const auto global = default_operation_registry();
173 auto git = global.find(otype);
174 if (git != global.end()) {
175 try {
176 output = (git->second)(group, version, options);
177 } catch (...) {
178 std::throw_with_nested(std::runtime_error("failed to validate delayed operation of type '" + otype + "'"));
179 }
180
181 } else {
182 throw std::runtime_error("unknown operation type '" + otype + "'");
183 }
184 }
185
186 } else {
187 throw std::runtime_error("unknown delayed type '" + dtype + "'");
188 }
189
190 return output;
191}
192
203inline ritsuko::Version extract_version(const H5::Group& group) {
204 ritsuko::Version version;
205
206 if (group.attrExists("delayed_version")) {
207 try {
208 auto vhandle = group.openAttribute("delayed_version");
209 auto vstring = read_scalar_string_attribute(vhandle);
210 if (vstring == "1.0.0") {
211 version.major = 1;
212 } else {
213 version = ritsuko::parse_version_string(vstring.c_str(), vstring.size(), /* skip_patch = */ true);
214 }
215 } catch (...) {
216 std::throw_with_nested(std::runtime_error("failed to validate the 'delayed_version' attribute"));
217 }
218 } else {
219 version.minor = 99;
220 }
221
222 return version;
223}
224
238inline ArrayDetails validate(const H5::Group& group, const Options& options) {
239 return validate(group, extract_version(group), options);
240}
241
256inline ArrayDetails validate(const std::string& path, const std::string& name, const Options& options) {
257 try {
258 H5::H5File handle(path, H5F_ACC_RDONLY);
259 auto ghandle = handle.openGroup(name);
260 return validate(ghandle, options);
261 } catch (...) {
262 std::throw_with_nested(std::runtime_error("failed to validate '" + name + "' in '" + path + "'"));
263 }
264}
265
279inline ArrayDetails validate(const std::string& path, const std::string& name) {
280 return validate(path, name, {});
281}
282
283}
284
285#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.
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:21
ArrayDetails validate_sparse_matrix(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition sparse_matrix.hpp:101
std::unordered_map< std::string, std::function< ArrayDetails(const H5::Group &, const ritsuko::Version &, const Options &)> > ValidateRegistry
Definition utils_public.hpp:74
ArrayDetails validate_constant_array(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition constant_array.hpp:38
ArrayDetails validate_subset(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition subset.hpp:34
ArrayDetails validate_transpose(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition transpose.hpp:78
ArrayDetails validate_binary_logic(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition binary_logic.hpp:35
ArrayDetails validate_matrix_product(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition matrix_product.hpp:73
ArrayDetails validate_unary_arithmetic(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_arithmetic.hpp:35
ritsuko::Version extract_version(const H5::Group &group)
Definition validate.hpp:203
ArrayDetails validate_custom_array(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition custom_array.hpp:88
ArrayDetails validate_binary_comparison(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition binary_comparison.hpp:34
ArrayDetails validate(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition validate.hpp:96
ArrayDetails validate_dimnames(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition dimnames.hpp:32
ArrayDetails validate_external_hdf5(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition external_hdf5.hpp:32
ArrayDetails validate_subset_assignment(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition subset_assignment.hpp:37
ArrayDetails validate_unary_comparison(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_comparison.hpp:35
ArrayDetails validate_dense_array(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition dense_array.hpp:58
ArrayDetails validate_unary_logic(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_logic.hpp:36
ArrayDetails validate_unary_math(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_math.hpp:34
ArrayDetails validate_binary_arithmetic(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition binary_arithmetic.hpp:35
ArrayDetails validate_combine(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition combine.hpp:38
ArrayDetails validate_unary_special_check(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_special_check.hpp:33
Validation for compressed sparse column matrices.
Details about an array.
Definition utils_public.hpp:38
Options for validate().
Definition utils_public.hpp:79
ValidateRegistry array_validate_registry
Definition utils_public.hpp:96
ValidateRegistry operation_validate_registry
Definition utils_public.hpp:102
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.