Debian Codegraph @

src/SPERR/test_scripts/dwt_unit_test.cpp

raw
1 #include <cstdlib>
2 #include "CDF97.h"
3 #include "Conditioner.h"
4 #include "gtest/gtest.h"
5
6 namespace {
7
8 TEST(dwt1d, big_image_even)
9 {
10 const char* input = "../test_data/128x128.float";
11 size_t dim_x = 128;
12 const size_t total_vals = dim_x;
13
14 // Let's read in binaries as 4-byte floats.
15 auto in_buf = sperr::read_n_bytes(input, sizeof(float) * total_vals);
16 if (in_buf.size() != sizeof(float) * total_vals)
17 std::cerr << "Input read error!" << std::endl;
18 const float* fptr = reinterpret_cast<const float*>(in_buf.data());
19
20 // Make a copy and then use a conditioner
21 auto in_copy = sperr::vecd_type(total_vals);
22 std::copy(fptr, fptr + total_vals, in_copy.begin());
23 auto condi = sperr::Conditioner();
24 auto meta = condi.condition(in_copy, {dim_x, 1, 1});
25
26 // Use a sperr::CDF97 to perform DWT and IDWT.
27 sperr::CDF97 cdf;
28 cdf.take_data(std::move(in_copy), {dim_x, 1, 1});
29 cdf.dwt1d();
30 cdf.idwt1d();
31
32 // Claim that with single precision, the result is identical to the input
33 auto result = cdf.release_data();
34 EXPECT_EQ(result.size(), total_vals);
35
36 // Apply the conditioner
37 auto rtn = condi.inverse_condition(result, {dim_x, 1, 1}, meta);
38
39 for (size_t i = 0; i < total_vals; i++) {
40 EXPECT_EQ(fptr[i], float(result[i]));
41 }
42 }
43
44 TEST(dwt1d, big_image_odd)
45 {
46 const char* input = "../test_data/999x999.float";
47 size_t dim_x = 999;
48 const size_t total_vals = dim_x;
49
50 // Let's read in binaries as 4-byte floats.
51 auto in_buf = sperr::read_n_bytes(input, sizeof(float) * total_vals);
52 if (in_buf.size() != sizeof(float) * total_vals)
53 std::cerr << "Input read error!" << std::endl;
54 const float* fptr = reinterpret_cast<const float*>(in_buf.data());
55
56 // Make a copy and use a conditioner
57 auto in_copy = sperr::vecd_type(total_vals);
58 std::copy(fptr, fptr + total_vals, in_copy.begin());
59 auto condi = sperr::Conditioner();
60 auto meta = condi.condition(in_copy, {dim_x, 1, 1});
61
62 // Use a sperr::CDF97 to perform DWT and IDWT.
63 sperr::CDF97 cdf;
64 cdf.take_data(std::move(in_copy), {dim_x, 1, 1});
65 cdf.dwt1d();
66 cdf.idwt1d();
67
68 // Claim that with single precision, the result is identical to the input
69 auto result = cdf.release_data();
70 EXPECT_EQ(result.size(), total_vals);
71
72 // Apply the conditioner again
73 auto rtn = condi.inverse_condition(result, {dim_x, 1, 1}, meta);
74
75 for (size_t i = 0; i < total_vals; i++) {
76 EXPECT_EQ(fptr[i], float(result[i]));
77 }
78 }
79
80 TEST(dwt2d, small_image_even)
81 {
82 const char* input = "../test_data/16x16.float";
83 size_t dim_x = 16, dim_y = 16;
84 const size_t total_vals = dim_x * dim_y;
85
86 // Let's read in binaries as 4-byte floats.
87 auto in_buf = sperr::read_whole_file<float>(input);
88 if (in_buf.size() != total_vals)
89 std::cerr << "Input read error!" << std::endl;
90
91 // Make a copy and use a conditioner
92 auto in_copy = sperr::vecd_type(total_vals);
93 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
94 auto condi = sperr::Conditioner();
95 auto meta = condi.condition(in_copy, {dim_x, dim_y, 1});
96
97 // Use a sperr::CDF97 to perform DWT and IDWT.
98 sperr::CDF97 cdf;
99 cdf.take_data(std::move(in_copy), {dim_x, dim_y, 1});
100 cdf.dwt2d();
101 cdf.idwt2d();
102
103 // Claim that with single precision, the result is identical to the input
104 auto result = cdf.release_data();
105 EXPECT_EQ(result.size(), total_vals);
106
107 // Apply the conditioner again
108 auto rtn = condi.inverse_condition(result, {dim_x, dim_y, 1}, meta);
109
110 for (size_t i = 0; i < total_vals; i++) {
111 EXPECT_EQ(in_buf[i], float(result[i]));
112 }
113 }
114
115 TEST(dwt2d, small_image_odd)
116 {
117 const char* input = "../test_data/15x15.float";
118 size_t dim_x = 15, dim_y = 15;
119 const size_t total_vals = dim_x * dim_y;
120
121 // Let's read in binaries as 4-byte floats.
122 auto in_buf = sperr::read_whole_file<float>(input);
123 if (in_buf.size() != total_vals)
124 std::cerr << "Input read error!" << std::endl;
125
126 // Make a copy and use a conditioner
127 auto in_copy = sperr::vecd_type(total_vals);
128 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
129 auto condi = sperr::Conditioner();
130 auto meta = condi.condition(in_copy, {dim_x, dim_y, 1});
131
132 // Use a sperr::CDF97 to perform DWT and IDWT.
133 sperr::CDF97 cdf;
134 cdf.take_data(std::move(in_copy), {dim_x, dim_y, 1});
135 cdf.dwt2d();
136 cdf.idwt2d();
137
138 // Claim that with single precision, the result is identical to the input
139 auto result = cdf.release_data();
140 EXPECT_EQ(result.size(), total_vals);
141
142 // Apply the conditioner
143 auto rtn = condi.inverse_condition(result, {dim_x, dim_y, 1}, meta);
144
145 for (size_t i = 0; i < total_vals; i++) {
146 EXPECT_EQ(in_buf[i], float(result[i]));
147 }
148 }
149
150 TEST(dwt2d, big_image_even)
151 {
152 const char* input = "../test_data/128x128.float";
153 size_t dim_x = 128, dim_y = 128;
154 const size_t total_vals = dim_x * dim_y;
155
156 // Let's read in binaries as 4-byte floats.
157 auto in_buf = sperr::read_whole_file<float>(input);
158 if (in_buf.size() != total_vals)
159 std::cerr << "Input read error!" << std::endl;
160
161 // Make a copy and use a conditioner
162 auto in_copy = sperr::vecd_type(total_vals);
163 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
164 auto condi = sperr::Conditioner();
165 auto meta = condi.condition(in_copy, {dim_x, dim_y, 1});
166
167 // Use a sperr::CDF97 to perform DWT and IDWT.
168 sperr::CDF97 cdf;
169 cdf.take_data(std::move(in_copy), {dim_x, dim_y, 1});
170 cdf.dwt2d();
171 cdf.idwt2d();
172
173 // Claim that with single precision, the result is identical to the input
174 auto result = cdf.release_data();
175 EXPECT_EQ(result.size(), total_vals);
176
177 // Apply the conditioner
178 auto rtn = condi.inverse_condition(result, {dim_x, dim_y, 1}, meta);
179
180 for (size_t i = 0; i < total_vals; i++) {
181 EXPECT_EQ(in_buf[i], float(result[i]));
182 }
183 }
184
185 TEST(dwt2d, big_image_odd)
186 {
187 const char* input = "../test_data/127x127.float";
188 size_t dim_x = 127, dim_y = 127;
189 const size_t total_vals = dim_x * dim_y;
190
191 // Let's read in binaries as 4-byte floats.
192 auto in_buf = sperr::read_whole_file<float>(input);
193 if (in_buf.size() != total_vals)
194 std::cerr << "Input read error!" << std::endl;
195
196 // Make a copy and use a conditioner
197 auto in_copy = sperr::vecd_type(total_vals);
198 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
199 auto condi = sperr::Conditioner();
200 auto meta = condi.condition(in_copy, {dim_x, dim_y, 1});
201
202 // Use a sperr::CDF97 to perform DWT and IDWT.
203 sperr::CDF97 cdf;
204 cdf.take_data(std::move(in_copy), {dim_x, dim_y, 1});
205 cdf.dwt2d();
206 cdf.idwt2d();
207
208 // Claim that with single precision, the result is identical to the input
209 auto result = cdf.release_data();
210 EXPECT_EQ(result.size(), total_vals);
211
212 // Apply the conditioner
213 auto rtn = condi.inverse_condition(result, {dim_x, dim_y, 1}, meta);
214
215 for (size_t i = 0; i < total_vals; i++) {
216 EXPECT_EQ(in_buf[i], float(result[i]));
217 }
218 }
219
220 TEST(dwt2d, image_multi_res)
221 {
222 const char* input = "../test_data/127x127.float";
223 size_t dim_x = 127, dim_y = 127;
224 const size_t total_vals = dim_x * dim_y;
225
226 // Let's read in binaries as 4-byte floats.
227 auto in_buf = sperr::read_whole_file<float>(input);
228 if (in_buf.size() != total_vals)
229 std::cerr << "Input read error!" << std::endl;
230
231 // Make a copy and use a conditioner
232 auto in_copy = sperr::vecd_type(total_vals);
233 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
234 auto condi = sperr::Conditioner();
235 auto meta = condi.condition(in_copy, {dim_x, dim_y, 1});
236
237 // Use a sperr::CDF97 to perform DWT and multi-res IDWT.
238 sperr::CDF97 cdf;
239 cdf.take_data(std::move(in_copy), {dim_x, dim_y, 1});
240 cdf.dwt2d();
241 auto hierarchy = cdf.idwt2d_multi_res();
242
243 // Claim that with single precision, the result is identical to the input
244 auto result = cdf.release_data();
245 EXPECT_EQ(result.size(), total_vals);
246
247 // Apply the conditioner
248 auto rtn = condi.inverse_condition(result, {dim_x, dim_y, 1}, meta);
249 for (size_t i = 0; i < total_vals; i++)
250 ASSERT_EQ(in_buf[i], float(result[i])) << " i = " << i;
251
252 // Examine the coarsened levels.
253 auto resolutions = sperr::coarsened_resolutions({dim_x, dim_y, 1});
254 EXPECT_EQ(hierarchy.size(), resolutions.size());
255 for (size_t i = 0; i < hierarchy.size(); i++) {
256 const auto& slice = hierarchy[i];
257 auto dims = resolutions[i];
258 EXPECT_EQ(slice.size(), dims[0] * dims[1] * dims[2]) << "i = " << i;
259 }
260 }
261
262 TEST(dwt3d, small_even_cube)
263 {
264 const char* input = "../test_data/wmag16.float";
265 size_t dim_x = 16, dim_y = 16, dim_z = 16;
266 const size_t total_vals = dim_x * dim_y * dim_z;
267
268 // Let's read in binaries as 4-byte floats.
269 auto in_buf = sperr::read_whole_file<float>(input);
270 if (in_buf.size() != total_vals)
271 std::cerr << "Input read error!" << std::endl;
272
273 // Make a copy and use a conditioner
274 auto in_copy = sperr::vecd_type(total_vals);
275 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
276 auto condi = sperr::Conditioner();
277 auto meta = condi.condition(in_copy, {dim_x, dim_y, dim_z});
278
279 // Use a sperr::CDF97 to perform DWT and IDWT.
280 sperr::CDF97 cdf;
281 cdf.take_data(std::move(in_copy), {dim_x, dim_y, dim_z});
282 cdf.dwt3d();
283 cdf.idwt3d();
284
285 // Claim that with single precision, the result is identical to the input
286 auto result = cdf.release_data();
287 EXPECT_EQ(result.size(), total_vals);
288
289 // Apply the conditioner
290 auto rtn = condi.inverse_condition(result, {dim_x, dim_y, dim_z}, meta);
291 for (size_t i = 0; i < total_vals; i++) {
292 ASSERT_EQ(in_buf[i], float(result[i])) << "i = " << i;
293 }
294 }
295
296 TEST(dwt3d, big_odd_cube)
297 {
298 const char* input = "../test_data/wmag91.float";
299 size_t dim_x = 91, dim_y = 91, dim_z = 91;
300 const size_t total_vals = dim_x * dim_y * dim_z;
301
302 // Let's read in binaries as 4-byte floats.
303 auto in_buf = sperr::read_whole_file<float>(input);
304 if (in_buf.size() != total_vals)
305 std::cerr << "Input read error!" << std::endl;
306
307 // Make a copy and use a conditioner
308 auto in_copy = sperr::vecd_type(total_vals);
309 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
310 auto condi = sperr::Conditioner();
311 auto meta = condi.condition(in_copy, {dim_x, dim_y, dim_z});
312
313 // Use a sperr::CDF97 to perform DWT and IDWT.
314 sperr::CDF97 cdf;
315 cdf.take_data(std::move(in_copy), {dim_x, dim_y, dim_z});
316 cdf.dwt3d();
317 cdf.idwt3d();
318
319 // Claim that with single precision, the result is identical to the input
320 auto result = cdf.release_data();
321 EXPECT_EQ(result.size(), total_vals);
322
323 // Apply the conditioner
324 auto rtn = condi.inverse_condition(result, {dim_x, dim_y, dim_z}, meta);
325 for (size_t i = 0; i < total_vals; i++) {
326 ASSERT_EQ(in_buf[i], float(result[i])) << "i = " << i;
327 }
328 }
329
330 TEST(dwt3d, big_even_cube)
331 {
332 const char* input = "../test_data/wmag128.float";
333 size_t dim_x = 128, dim_y = 128, dim_z = 128;
334 const size_t total_vals = dim_x * dim_y * dim_z;
335
336 // Let's read in binaries as 4-byte floats.
337 auto in_buf = sperr::read_whole_file<float>(input);
338 if (in_buf.size() != total_vals)
339 std::cerr << "Input read error!" << std::endl;
340
341 // Make a copy and use a conditioner
342 auto in_copy = sperr::vecd_type(total_vals);
343 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
344 auto condi = sperr::Conditioner();
345 auto meta = condi.condition(in_copy, {dim_x, dim_y, dim_z});
346
347 // Use a sperr::CDF97 to perform DWT and IDWT.
348 sperr::CDF97 cdf;
349 cdf.take_data(std::move(in_copy), {dim_x, dim_y, dim_z});
350 cdf.dwt3d();
351 cdf.idwt3d();
352
353 // Claim that with single precision, the result is identical to the input
354 auto result = cdf.release_data();
355 EXPECT_EQ(result.size(), total_vals);
356
357 // Apply the conditioner
358 auto rtn = condi.inverse_condition(result, {dim_x, dim_y, dim_z}, meta);
359 for (size_t i = 0; i < total_vals; i++) {
360 ASSERT_EQ(in_buf[i], float(result[i])) << "i = " << i;
361 }
362 }
363
364 TEST(dwt3d, multi_res)
365 {
366 const char* input = "../test_data/wmag91.float";
367 auto dims = sperr::dims_type{91, 91, 91};
368 const size_t total_vals = dims[0] * dims[1] * dims[2];
369
370 // Let's read in binaries as 4-byte floats.
371 auto in_buf = sperr::read_whole_file<float>(input);
372 if (in_buf.size() != total_vals)
373 std::cerr << "Input read error!" << std::endl;
374
375 // Make a copy and use a conditioner
376 auto in_copy = sperr::vecd_type(total_vals);
377 std::copy(in_buf.begin(), in_buf.end(), in_copy.begin());
378 auto condi = sperr::Conditioner();
379 auto meta = condi.condition(in_copy, dims);
380
381 // Use a sperr::CDF97 to perform DWT and multi-res IDWT.
382 sperr::CDF97 cdf;
383 cdf.take_data(std::move(in_copy), dims);
384 cdf.dwt3d();
385 auto hierarchy = std::vector<sperr::vecd_type>();
386 cdf.idwt3d_multi_res(hierarchy);
387
388 // Claim that with single precision, the result is identical to the input
389 auto result = cdf.release_data();
390 EXPECT_EQ(result.size(), total_vals);
391
392 // Apply the conditioner
393 auto rtn = condi.inverse_condition(result, dims, meta);
394 for (size_t i = 0; i < total_vals; i++)
395 ASSERT_EQ(in_buf[i], float(result[i])) << " i = " << i;
396
397 // Examine the coarsened levels.
398 auto resolutions = sperr::coarsened_resolutions(dims);
399 EXPECT_EQ(hierarchy.size(), resolutions.size());
400 for (size_t i = 0; i < hierarchy.size(); i++) {
401 const auto& vol = hierarchy[i];
402 auto res = resolutions[i];
403 EXPECT_EQ(vol.size(), res[0] * res[1] * res[2]) << "i = " << i;
404 }
405 }
406
407 } // namespace