Debian Codegraph @

src/SPERR/test_scripts/graveyard/speck3d_unit_test.cpp

raw
1 #include "SPECK3D.h"
2 #include "CDF97.h"
3 #include <cstring>
4 #include "gtest/gtest.h"
5
6 namespace
7 {
8
9 extern "C" // C Function calls, and don't include the C header!
10 {
11 int sam_read_n_bytes( const char*, size_t, void* );
12 int sam_get_statsd( const double* arr1, const double* arr2, size_t len,
13 double* rmse, double* lmax, double* psnr,
14 double* arr1min, double* arr1max );
15 }
16
17 // Create a class that executes the entire pipeline, and calculates the error metrics
18 class speck_tester
19 {
20 public:
21 speck_tester( const char* in, size_t x, size_t y, size_t z )
22 {
23 m_input_name = in;
24 m_dim_x = x;
25 m_dim_y = y;
26 m_dim_z = z;
27 }
28
29 void reset( const char* in, size_t x, size_t y, size_t z )
30 {
31 m_input_name = in;
32 m_dim_x = x;
33 m_dim_y = y;
34 m_dim_z = z;
35 }
36
37 double get_psnr() const
38 {
39 return m_psnr;
40 }
41
42 double get_lmax() const
43 {
44 return m_lmax;
45 }
46
47 // Execute the compression/decompression pipeline. Return 0 on success
48
49 #ifdef QZ_TERM
50 int execute( const char* mode, int32_t qz_level )
51 #else
52 int execute( float cratio )
53 #endif
54
55 {
56 const size_t total_vals = m_dim_x * m_dim_y * m_dim_z;
57
58 // Let's read in binaries as 4-byte floats
59 std::unique_ptr<float[]> in_buf( new float[ total_vals ] );
60 if( sam_read_n_bytes( m_input_name.c_str(), sizeof(float) * total_vals, in_buf.get() ) )
61 {
62 std::cerr << "Input read error!" << std::endl;
63 return 1;
64 }
65
66 // Take input to go through DWT.
67 speck::CDF97 cdf;
68 cdf.set_dims( m_dim_x, m_dim_y, m_dim_z );
69 cdf.copy_data( in_buf.get(), total_vals );
70 cdf.dwt3d();
71
72 // Do a speck encoding
73 speck::SPECK3D encoder;
74 encoder.set_dims( m_dim_x, m_dim_y, m_dim_z );
75 encoder.set_image_mean( cdf.get_mean() );
76 encoder.take_coeffs( cdf.release_data(), total_vals );
77
78 #ifdef QZ_TERM
79 if( std::strcmp( mode, "n_iterations" ) == 0 )
80 encoder.set_quantization_iterations( qz_level );
81 else if( std::strcmp( mode, "absolute" ) == 0 )
82 encoder.set_quantization_term_level( qz_level );
83 else {
84 m_psnr = 0.0f;
85 m_lmax = 0.0f;
86 return 1;
87 }
88 #else
89 encoder.set_bit_budget( size_t(32.0f * total_vals / cratio) );
90 #endif
91
92 encoder.encode();
93 if( encoder.write_to_disk( m_output_name ) )
94 {
95 std::cerr << "Write bitstream to disk error!" << std::endl;
96 return 1;
97 }
98
99 // Do a speck decoding
100 speck::SPECK3D decoder;
101 if( decoder.read_from_disk( m_output_name ) )
102 {
103 std::cerr << "Read bitstream from disk error!" << std::endl;
104 return 1;
105 }
106 decoder.set_bit_budget( 0 ); // 0 means decode all available bits
107 decoder.decode();
108
109 speck::CDF97 idwt;
110 size_t dim_x_r, dim_y_r, dim_z_r;
111 decoder.get_dims( dim_x_r, dim_y_r, dim_z_r );
112 idwt.set_dims( dim_x_r, dim_y_r, dim_z_r );
113 idwt.set_mean( decoder.get_image_mean() );
114 idwt.take_data( decoder.release_coeffs_double(), dim_x_r * dim_y_r * dim_z_r );
115 idwt.idwt3d();
116
117 // Compare the result with the original input in double precision
118 std::unique_ptr<double[]> in_bufd( new double[ total_vals ] );
119 for( size_t i = 0; i < total_vals; i++ )
120 in_bufd[i] = in_buf[i];
121 double rmse, lmax, psnr, arr1min, arr1max;
122 sam_get_statsd( in_bufd.get(), idwt.get_read_only_data().get(),
123 total_vals, &rmse, &lmax, &psnr, &arr1min, &arr1max );
124 m_psnr = psnr;
125 m_lmax = lmax;
126
127 return 0;
128 }
129
130
131 private:
132 std::string m_input_name;
133 size_t m_dim_x, m_dim_y, m_dim_z;
134 std::string m_output_name = "output.tmp";
135 double m_psnr, m_lmax;
136 };
137
138
139 #ifdef QZ_TERM
140 TEST( speck3d_qz_term, big )
141 {
142 speck_tester tester( "../test_data/wmag128.float", 128, 128, 128 );
143 tester.execute( "n_iterations", 8 );
144 double psnr = tester.get_psnr();
145 double lmax = tester.get_lmax();
146 EXPECT_GT( psnr, 40.241936 );
147 EXPECT_LT( lmax, 29.461008 );
148
149 tester.execute( "n_iterations", 10 );
150 psnr = tester.get_psnr();
151 lmax = tester.get_lmax();
152 EXPECT_GT( psnr, 48.605482 );
153 EXPECT_LT( lmax, 9.535521 );
154
155 tester.execute( "n_iterations", 13 );
156 psnr = tester.get_psnr();
157 lmax = tester.get_lmax();
158 EXPECT_GT( psnr, 65.496590 );
159 EXPECT_LT( lmax, 1.376005 );
160
161 tester.execute( "n_iterations", 15 );
162 psnr = tester.get_psnr();
163 lmax = tester.get_lmax();
164 EXPECT_GT( psnr, 78.661909 );
165 EXPECT_LT( lmax, 0.257962 );
166 }
167
168 TEST( speck3d_qz_term, narrow_data_range)
169 {
170 speck_tester tester( "../test_data/vorticity.128_128_41", 128, 128, 41 );
171 tester.execute( "n_iterations", 8 );
172 double psnr = tester.get_psnr();
173 double lmax = tester.get_lmax();
174 EXPECT_GT( psnr, 42.292308 );
175 EXPECT_LT( lmax, 0.000032 );
176
177 tester.execute( "n_iterations", 10 );
178 psnr = tester.get_psnr();
179 lmax = tester.get_lmax();
180 EXPECT_GT( psnr, 50.513602 );
181 EXPECT_LT( lmax, 0.000009 );
182 }
183
184 TEST( speck3d_qz_term, n_iteration_and_absolute_qz_level)
185 {
186 speck_tester tester( "../test_data/vorticity.128_128_41", 128, 128, 41 );
187 tester.execute( "n_iterations", 8 );
188 double psnr1 = tester.get_psnr();
189 double lmax1 = tester.get_lmax();
190 tester.execute( "absolute", -16 );
191 double psnr2 = tester.get_psnr();
192 double lmax2 = tester.get_lmax();
193 EXPECT_EQ( psnr1, psnr2 );
194 EXPECT_EQ( lmax1, lmax2 );
195
196 tester.execute( "n_iterations", 10 );
197 psnr1 = tester.get_psnr();
198 lmax1 = tester.get_lmax();
199 tester.execute( "absolute", -18 );
200 psnr2 = tester.get_psnr();
201 lmax2 = tester.get_lmax();
202 EXPECT_EQ( psnr1, psnr2 );
203 EXPECT_EQ( lmax1, lmax2 );
204
205 tester.reset( "../test_data/wmag128.float", 128, 128, 128 );
206
207 tester.execute( "n_iterations", 8 );
208 psnr1 = tester.get_psnr();
209 lmax1 = tester.get_lmax();
210 tester.execute( "absolute", 4 );
211 psnr2 = tester.get_psnr();
212 lmax2 = tester.get_lmax();
213 EXPECT_EQ( psnr1, psnr2 );
214 EXPECT_EQ( lmax1, lmax2 );
215
216 tester.execute( "n_iterations", 15 );
217 psnr1 = tester.get_psnr();
218 lmax1 = tester.get_lmax();
219 tester.execute( "absolute", -3 );
220 psnr2 = tester.get_psnr();
221 lmax2 = tester.get_lmax();
222 EXPECT_EQ( psnr1, psnr2 );
223 EXPECT_EQ( lmax1, lmax2 );
224 }
225 #else
226 TEST( speck3d_bit_rate, small )
227 {
228 speck_tester tester( "../test_data/wmag17.float", 17, 17, 17 );
229
230 tester.execute( 8.0f );
231 double psnr = tester.get_psnr();
232 double lmax = tester.get_lmax();
233 EXPECT_GT( psnr, 52.559024 );
234 EXPECT_LT( lmax, 1.432911 );
235
236 tester.execute( 16.0f );
237 psnr = tester.get_psnr();
238 lmax = tester.get_lmax();
239 EXPECT_GT( psnr, 41.48256 );
240 EXPECT_LT( lmax, 5.468170 );
241
242 tester.execute( 32.0f );
243 psnr = tester.get_psnr();
244 lmax = tester.get_lmax();
245 EXPECT_GT( psnr, 34.713616 );
246 EXPECT_LT( lmax, 12.496039 );
247
248 tester.execute( 64.0f );
249 psnr = tester.get_psnr();
250 lmax = tester.get_lmax();
251 EXPECT_GT( psnr, 30.10208 );
252 EXPECT_LT( lmax, 27.40783 );
253
254 tester.execute( 128.0f );
255 psnr = tester.get_psnr();
256 lmax = tester.get_lmax();
257 EXPECT_GT( psnr, 26.62448 );
258 EXPECT_LT( lmax, 36.11478 );
259 }
260
261
262 TEST( speck3d_bit_rate, big )
263 {
264 speck_tester tester( "../test_data/wmag128.float", 128, 128, 128 );
265
266 tester.execute( 16.0f );
267 double psnr = tester.get_psnr();
268 double lmax = tester.get_lmax();
269 EXPECT_GT( psnr, 54.040208 );
270 EXPECT_LT( lmax, 4.879152 );
271
272 tester.execute( 32.0f );
273 psnr = tester.get_psnr();
274 lmax = tester.get_lmax();
275 EXPECT_GT( psnr, 47.27210 );
276 EXPECT_LT( lmax, 15.96790 );
277
278 tester.execute( 64.0f );
279 psnr = tester.get_psnr();
280 lmax = tester.get_lmax();
281 EXPECT_GT( psnr, 42.68343 );
282 EXPECT_LT( lmax, 25.18307 );
283
284 tester.execute( 128.0f );
285 psnr = tester.get_psnr();
286 lmax = tester.get_lmax();
287 EXPECT_GT( psnr, 39.19998 );
288 EXPECT_LT( lmax, 44.29733 );
289 }
290
291
292 TEST( speck3d_bit_rate, narrow_data_range )
293 {
294 speck_tester tester( "../test_data/vorticity.128_128_41", 128, 128, 41 );
295
296 tester.execute( 8.0f );
297 double psnr = tester.get_psnr();
298 double lmax = tester.get_lmax();
299 EXPECT_GT( psnr, 68.791297 );
300 EXPECT_LT( lmax, 0.000001 );
301
302 tester.execute( 16.0f );
303 psnr = tester.get_psnr();
304 lmax = tester.get_lmax();
305 EXPECT_GT( psnr, 56.628458 );
306 EXPECT_LT( lmax, 0.000005 );
307
308 tester.execute( 32.0f );
309 psnr = tester.get_psnr();
310 lmax = tester.get_lmax();
311 EXPECT_GT( psnr, 49.660499 );
312 EXPECT_LT( lmax, 0.0000104 );
313
314 tester.execute( 64.0f );
315 psnr = tester.get_psnr();
316 lmax = tester.get_lmax();
317 EXPECT_GT( psnr, 45.122822 );
318 EXPECT_LT( lmax, 0.000024 );
319
320 tester.execute( 128.0f );
321 psnr = tester.get_psnr();
322 lmax = tester.get_lmax();
323 EXPECT_GT( psnr, 41.702355 );
324 EXPECT_LT( lmax, 0.0000375 );
325 }
326 #endif
327
328 }