CR2RE Pipeline Reference Manual 1.6.10
hdrl_mode-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2012,2013 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27#include "hdrl_random.h"
28
29#include "hdrl_mode.c"
30#include "hdrl_utils.h"
31#include <cpl.h>
32
33#include <math.h>
34#include <time.h>
35#include <stdlib.h>
36#include <gsl/gsl_histogram.h>
37
38#define HDRL_EPS_TEST 1.e-5
39
40/*----------------------------------------------------------------------------*/
44/*----------------------------------------------------------------------------*/
45
46static cpl_error_code test_hdrl_mode(hdrl_random_state * state, int savetodisk)
47{
48
49 double expected = 10000.;
50 cpl_size sx = 1000;
51 cpl_size sy = 1000;
52 cpl_image* ima = cpl_image_new(sx, sy, CPL_TYPE_INT);
53 int* pdata = cpl_image_get_data(ima);
54 cpl_size size = sx * sy;
55
56 for(cpl_size i = 0; i < size; i++) {
57
58 pdata[i] = (int)hdrl_random_poisson(state, expected);
59 }
60 if (savetodisk) {
61 cpl_propertylist* plist = cpl_propertylist_new();
62 cpl_propertylist_save(plist,"ima.fits",CPL_IO_DEFAULT);
63 cpl_image_save(ima, "ima.fits", CPL_TYPE_INT, NULL, CPL_IO_EXTEND);
64 cpl_propertylist_delete(plist);
65 }
66
67 hdrl_image* hima = hdrl_image_create(ima, NULL);
68 cpl_vector* vec = NULL;
69 vec = hdrl_image_to_vector(hdrl_image_get_image(hima), hdrl_image_get_mask(hima));
70 double min = cpl_vector_get_min(vec);
71 double max = cpl_vector_get_max(vec);
72 cpl_msg_debug(cpl_func,"min=%g max=%g", min, max);
73
74 double mode = 0;
75 double mode_err = 0;
76 cpl_size naccepted = 0;
77 /* test invalid input */
78
79 hdrl_mode_clip(NULL, 0, -1, 0, HDRL_MODE_MEDIAN, 0, &mode, &mode_err,
80 &naccepted);
81 cpl_test_error(CPL_ERROR_NULL_INPUT);
82
83 hdrl_mode_clip_image(NULL, 0, -1, 0, HDRL_MODE_MEDIAN, 0, &mode,
84 &mode_err, &naccepted);
85 cpl_test_error(CPL_ERROR_NULL_INPUT);
86
87
88
89 cpl_msg_debug(cpl_func,"===============================================");
90 cpl_msg_debug(cpl_func,"MODE METHOD MEDIAN ");
91 cpl_msg_debug(cpl_func,"-----------------------------------------------");
92 hdrl_mode_clip(vec, 0, -1, 0, HDRL_MODE_MEDIAN, 1, &mode, &mode_err, &naccepted);
93 cpl_test_abs(mode, expected, 1.);
94 cpl_test_abs(mode_err, 0., HDRL_EPS_TEST);
95 cpl_test_error(CPL_ERROR_NONE);
96
97 hdrl_mode_clip(vec, 0, -1, 0, HDRL_MODE_MEDIAN, 0, &mode, &mode_err, &naccepted);
98 cpl_test_abs(mode, expected, 1.);
99 cpl_test_abs(mode_err, 2.00072, HDRL_EPS_TEST);
100 cpl_test_error(CPL_ERROR_NONE);
101
102
103 hdrl_mode_clip_image(ima, 0, -1, 0, HDRL_MODE_MEDIAN, 0, &mode, &mode_err,
104 &naccepted);
105 cpl_test_abs(mode, expected, 1.);
106 cpl_test_abs(mode_err, 2.00072, HDRL_EPS_TEST);
107 cpl_test_error(CPL_ERROR_NONE);
108
109 /* case error_niter < 1 */
110
111 hdrl_mode_clip_image(ima, 0, -1, 0, HDRL_MODE_MEDIAN, -1, &mode, &mode_err,
112 &naccepted);
113 cpl_test_abs(mode, expected, 1.);
114 cpl_test_zero(mode_err);
115 cpl_test_error(CPL_ERROR_NONE);
116
117 cpl_msg_debug(cpl_func,"===============================================");
118 cpl_msg_debug(cpl_func,"MODE METHOD WEIGHT ");
119 cpl_msg_debug(cpl_func,"-----------------------------------------------");
120 hdrl_mode_clip(vec, 0, -1, 0, HDRL_MODE_WEIGHTED, 0, &mode, &mode_err, &naccepted);
121 cpl_test_abs(mode, expected, 4);
122 cpl_test_abs(mode_err, 6.355987781, HDRL_EPS_TEST);
123 cpl_test_error(CPL_ERROR_NONE);
124
125 cpl_test_abs(mode_err, 6.355987781, HDRL_EPS_TEST);
126 cpl_test_error(CPL_ERROR_NONE);
127
128 hdrl_mode_clip_image(ima, 0, -1, 0, HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,
129 &naccepted);
130 cpl_test_abs(mode, expected, 4);
131 cpl_test_abs(mode_err, 6.355987781, HDRL_EPS_TEST);
132 cpl_test_error(CPL_ERROR_NONE);
133
134 /* case error_niter < 1 */
135
136 hdrl_mode_clip_image(ima, 0, -1, 0, HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,
137 &naccepted);
138 cpl_test_abs(mode, expected, 4);
139 cpl_test_zero(mode_err);
140 cpl_test_error(CPL_ERROR_NONE);
141
142 cpl_msg_debug(cpl_func,"===============================================");
143 cpl_msg_debug(cpl_func,"MODE METHOD FIT ");
144 cpl_msg_debug(cpl_func,"-----------------------------------------------");
145 hdrl_mode_clip(vec, 0, -1, 0, HDRL_MODE_FIT, 0, &mode, &mode_err, &naccepted);
146 cpl_test_abs(mode, expected, 1);
147 //cpl_test_abs(mode_err, 1.8927338794908046, HDRL_EPS_TEST);
148 cpl_test_error(CPL_ERROR_NONE);
149
150 //cpl_test_abs(mode_err, 1.8927338794908046, HDRL_EPS_TEST);
151 cpl_test_error(CPL_ERROR_NONE);
152
153 hdrl_mode_clip_image(ima, 0, -1, 0, HDRL_MODE_FIT, 0, &mode, &mode_err,
154 &naccepted);
155 cpl_test_abs(mode, expected, 1);
156 //cpl_test_abs(mode_err, 1.8927338794908046, HDRL_EPS_TEST);
157 cpl_test_error(CPL_ERROR_NONE);
158
159 /* case error_niter < 1 */
160
161 hdrl_mode_clip_image(ima, 0, -1, 0, HDRL_MODE_FIT, -1, &mode, &mode_err,
162 &naccepted);
163 cpl_test_abs(mode, expected, 1);
164 cpl_test_zero(mode_err);
165 cpl_test_error(CPL_ERROR_NONE);
166
167 if (savetodisk) {
168 cpl_table* histo_tab = NULL;
169 int nbins = 100;
170 double bin_size = hdrl_mode_compute_binsize(vec);
171 gsl_histogram * h = hdrl_mode_histogram(vec, min, max, nbins);
172 histo_tab = hdrl_mode_histogram_to_table(h, min, bin_size, nbins);
173 cpl_table_save(histo_tab, NULL, NULL, "histo_tab_sinfo.fits", CPL_IO_DEFAULT);
174
175 cpl_table_delete(histo_tab);
176 gsl_histogram_free(h);
177 }
178
179 /* clean memory */
180
181 cpl_vector_delete(vec);
182 cpl_image_delete(ima);
183 hdrl_image_delete(hima);
184
185 return cpl_error_get_code();
186}
187
188static cpl_error_code test_hdrl_mode_nogoodpixels(void)
189{
190 cpl_msg_debug(cpl_func,"test_hdrl_mode_nogoodpixels");
191 cpl_size nx = 10;
192 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
193 cpl_mask* bpm = cpl_image_get_bpm(ima);
194 cpl_image_set(ima, 1, 1, -1); cpl_mask_set(bpm, 1, 1, CPL_BINARY_1);
195 cpl_image_set(ima, 2, 1, -1); cpl_mask_set(bpm, 2, 1, CPL_BINARY_1);
196 cpl_image_set(ima, 3, 1, -1); cpl_mask_set(bpm, 3, 1, CPL_BINARY_1);
197 cpl_image_set(ima, 4, 1, -1); cpl_mask_set(bpm, 4, 1, CPL_BINARY_1);
198 cpl_image_set(ima, 5, 1, -1); cpl_mask_set(bpm, 5, 1, CPL_BINARY_1);
199 cpl_image_set(ima, 6, 1, -1); cpl_mask_set(bpm, 6, 1, CPL_BINARY_1);
200 cpl_image_set(ima, 7, 1, -1); cpl_mask_set(bpm, 7, 1, CPL_BINARY_1);
201 cpl_image_set(ima, 8, 1, -1); cpl_mask_set(bpm, 8, 1, CPL_BINARY_1);
202 cpl_image_set(ima, 9, 1, -1); cpl_mask_set(bpm, 9, 1, CPL_BINARY_1);
203 cpl_image_set(ima, 10, 1, -1); cpl_mask_set(bpm, 10, 1, CPL_BINARY_1);
204
205 cpl_image* err = cpl_image_duplicate(ima);
206 cpl_image_power(err, 0.5);
207 hdrl_image* hima = hdrl_image_create(ima, err);
208 double mode = 0;
209 double mode_err = 0;
210
211 cpl_size naccepted = 0;
212
213
214 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
215 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
216 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
217
218 cpl_image_delete(err);
219
220 cpl_image_delete(ima);
221 hdrl_image_delete(hima);
222 return cpl_error_get_code();
223}
224
225static cpl_error_code test_hdrl_mode_onevalue(void)
226{
227 cpl_msg_debug(cpl_func,"test_hdrl_mode_onevalue");
228 cpl_size nx = 10;
229 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
230 cpl_image_set(ima, 1, 1, -1);
231 cpl_image_set(ima, 2, 1, -1);
232 cpl_image_set(ima, 3, 1, -1);
233 cpl_image_set(ima, 4, 1, -1);
234 cpl_image_set(ima, 5, 1, -1);
235 cpl_image_set(ima, 6, 1, -1);
236 cpl_image_set(ima, 7, 1, -1);
237 cpl_image_set(ima, 8, 1, -1);
238 cpl_image_set(ima, 9, 1, -1);
239 cpl_image_set(ima, 10, 1, -1);
240
241 cpl_image* err = cpl_image_duplicate(ima);
242 cpl_image_power(err, 0.5);
243 hdrl_image* hima = hdrl_image_create(ima, err);
244 double mode = 0;
245 double mode_err = 0;
246
247 cpl_size naccepted = 0;
248
249
250 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
251 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
252 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
253
254 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
255 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
256 cpl_test_error(CPL_ERROR_NONE);
257
258 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
259 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
260 cpl_test_error(CPL_ERROR_NONE);
261
262 cpl_image_delete(err);
263
264 cpl_image_delete(ima);
265 hdrl_image_delete(hima);
266 return cpl_error_get_code();
267}
268static cpl_error_code test_hdrl_mode_image_one_value(void)
269{
270 cpl_msg_debug(cpl_func,"test_hdrl_mode_image_one_value");
271 cpl_size sx = 5;
272 cpl_size sy = 5;
273 cpl_image* ima = cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
274 cpl_image_add_scalar(ima,5);
275 double mode = 0;
276 double mode_err = 0;
277 cpl_size naccepted = 0;
278 hdrl_mode_clip_image(ima, 4.5, 5.5, 1, HDRL_MODE_FIT, -1, &mode, &mode_err,
279 &naccepted);
280
281 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
282 cpl_test_abs(mode, 0, HDRL_EPS_TEST);
283 cpl_test_abs(mode_err, 0, HDRL_EPS_TEST);
284
285
286 hdrl_mode_clip_image(ima, 4.5, 5.5, 1, HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,
287 &naccepted);
288 cpl_test_error(CPL_ERROR_NONE);
289 cpl_test_abs(mode, 5, HDRL_EPS_TEST);
290 cpl_test_abs(mode_err, 0, HDRL_EPS_TEST);
291
292 hdrl_mode_clip_image(ima, 4.5, 5.5, 1, HDRL_MODE_MEDIAN, -1, &mode, &mode_err,
293 &naccepted);
294 cpl_test_error(CPL_ERROR_NONE);
295 cpl_test_abs(mode, 5, HDRL_EPS_TEST);
296 cpl_test_abs(mode_err, 0, HDRL_EPS_TEST);
297
298
299 hdrl_mode_clip_image(ima, 4, 6, 1, HDRL_MODE_FIT, -1, &mode, &mode_err,
300 &naccepted);
301
302 cpl_test_error(CPL_ERROR_NONE);
303 cpl_test_abs(mode, 5.5, HDRL_EPS_TEST);
304 cpl_test_abs(mode_err, 0.0, HDRL_EPS_TEST);
305
306
307 hdrl_mode_clip_image(ima, 4, 6, 1, HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,
308 &naccepted);
309 cpl_test_error(CPL_ERROR_NONE);
310 cpl_test_abs(mode, 5.5, HDRL_EPS_TEST);
311 cpl_test_abs(mode_err, 0, HDRL_EPS_TEST);
312
313 hdrl_mode_clip_image(ima, 4, 6, 1, HDRL_MODE_MEDIAN, -1, &mode, &mode_err,
314 &naccepted);
315 cpl_test_error(CPL_ERROR_NONE);
316 cpl_test_abs(mode, 5, HDRL_EPS_TEST);
317 cpl_test_abs(mode_err, 0, HDRL_EPS_TEST);
318
319 cpl_image_delete(ima);
320 return cpl_error_get_code();
321}
322static cpl_error_code test_hdrl_mode_vector_one_value(void)
323{
324 cpl_msg_debug(cpl_func,"test_hdrl_mode_median");
325
326 double values[] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
327 5, 5, 5, 5, 5, 5, 5};
328
329 /*
330 double values[] = {1., 2., 3., 4., 5., 6., 7., 8., 9., 10.,
331 11., 12., 13., 14., 15., 16., 17.};
332
333 */
334 cpl_vector* vec = cpl_vector_wrap(17, values);
335
336
337 double mode = 0;
338 double mode_err = 0;
339 cpl_size naccepted = 0;
340
341 hdrl_mode_clip(vec, 3.5, 6., 1, HDRL_MODE_FIT, 0, &mode, &mode_err,
342 &naccepted);
343 cpl_test_error(CPL_ERROR_ILLEGAL_OUTPUT);
344
345
346
347 hdrl_mode_clip(vec, 3.5, 6.5, 1, HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,
348 &naccepted);
349 cpl_test_error(CPL_ERROR_NONE);
350 cpl_test_abs(mode, 5, HDRL_EPS_TEST);
351 cpl_test_abs(mode_err, 0.08574929257125441, HDRL_EPS_TEST);
352
353
354 // Fails HDRL_MODE_WEIGHTED
355 hdrl_mode_clip(vec, 0.0, 20., 1, HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,
356 &naccepted);
357 cpl_test_error(CPL_ERROR_NONE);
358 cpl_test_abs(mode, 5.5, HDRL_EPS_TEST);
359 cpl_test_abs(mode_err, 0.08574929257125441, HDRL_EPS_TEST);
360
361
362 cpl_vector_unwrap(vec);
363
364 return cpl_error_get_code();
365}
366
367static cpl_error_code test_hdrl_mode_median(void)
368{
369 cpl_msg_debug(cpl_func,"test_hdrl_mode_median");
370 double values[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
371 cpl_vector* vec = cpl_vector_wrap(10, values);
372
373 double mode = 0;
374 double mode_err = 0;
375
376 hdrl_mode_median(vec, 0, 2, 3, 0, &mode, &mode_err);
377 cpl_test_error(CPL_ERROR_NONE);
378 cpl_test_abs(mode, 1, HDRL_EPS_TEST);
379 cpl_test_abs(mode_err, 0, HDRL_EPS_TEST);
380 cpl_vector_unwrap(vec);
381 return cpl_error_get_code();
382}
383/* ---------------------------------------------------------------------------*/
389/* ---------------------------------------------------------------------------*/
390static cpl_error_code test_hdrl_mode_asymm(void)
391{
392 cpl_msg_debug(cpl_func,"test_hdrl_mode_asymm");
393 cpl_size nx = 17;
394 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
395 cpl_image_set(ima, 1, 1, 1.453698);
396 cpl_image_set(ima, 2, 1, 1.526955);
397 cpl_image_set(ima, 3, 1, 1.146273);
398 cpl_image_set(ima, 4, 1, 0.9416522);
399 cpl_image_set(ima, 5, 1, 1.059149);
400 cpl_image_set(ima, 6, 1, 0.468435);
401 cpl_image_set(ima, 7, 1, 0.4536197);
402 cpl_image_set(ima, 8, 1, 0.469264);
403 cpl_image_set(ima, 9, 1, 0.3145597);
404 cpl_image_set(ima, 10, 1, -0.03258576);
405
406 cpl_image_set(ima, 11, 1, -0.06351986);
407 cpl_image_set(ima, 12, 1, -0.009271647);
408 cpl_image_set(ima, 13, 1, 0.06780738);
409 cpl_image_set(ima, 14, 1, -0.1385294);
410 cpl_image_set(ima, 15, 1, 0.01233397);
411 cpl_image_set(ima, 16, 1, 0.04090551);
412 cpl_image_set(ima, 17, 1, 0.08584704);
413
414 cpl_image* err = cpl_image_duplicate(ima);
415 cpl_image_power(err, 0.5);
416 hdrl_image* hima = hdrl_image_create(ima, err);
417 double mode = 0;
418 double mode_err = 0;
419
420 cpl_size naccepted = 0;
421 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
422 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
423
424 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
425 cpl_error_reset();
426 /* this was making automatic switch to method weight
427 cpl_test_abs(mode, 0.032959079472357655, HDRL_EPS_TEST);
428 cpl_test_abs(mode_err, 0.19892636622913076, HDRL_EPS_TEST);
429 */
430
431 /* error_niter < 0 */
432 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
433 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
434
435 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
436 cpl_error_reset();
437 /* this was making automatic switch to method weight
438 cpl_test_abs(mode, 0.032959079472357655, HDRL_EPS_TEST);
439 */
440 cpl_test_zero(mode_err);
441 cpl_test_error(CPL_ERROR_NONE);
442
443
444 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
445 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
446 cpl_test_error(CPL_ERROR_NONE);
447 cpl_test_abs(mode, 0.032959079472357655, HDRL_EPS_TEST);
448 cpl_test_abs(mode_err, 0.19892636622913076, HDRL_EPS_TEST);
449
450
451 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), -0.7901858605612435,
452 2.1786116577852885, 1.3033128217198686,
453 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
454 cpl_test_error(CPL_ERROR_NONE);
455 cpl_test_abs(mode, 0.032959079472357655, HDRL_EPS_TEST);
456 cpl_test_abs(mode_err, 0.19892636622913076, HDRL_EPS_TEST);
457
458 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), -0.7901858605612435,
459 2.1786116577852885, 1.,
460 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
461 cpl_test_error(CPL_ERROR_NONE);
462 cpl_test_abs(mode, 0.09870302832764533, HDRL_EPS_TEST);
463 cpl_test_abs(mode_err, 0.384107387311211, HDRL_EPS_TEST);
464
465
466 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), -1.,
467 2.1786116577852885, 1.,
468 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
469 cpl_test_error(CPL_ERROR_NONE);
470 cpl_test_abs(mode, 0.5, HDRL_EPS_TEST);
471 cpl_test_abs(mode_err, 0.2549509756796392, HDRL_EPS_TEST);
472
473 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), -1.,
474 1., 1.,
475 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
476 cpl_test_error(CPL_ERROR_NONE);
477 cpl_test_abs(mode, 0.35714285714285715, HDRL_EPS_TEST);
478 cpl_test_abs(mode_err, 0.18239349930325996, HDRL_EPS_TEST);
479
480 /* error_niter < 0 */
481 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
482 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
483 cpl_test_error(CPL_ERROR_NONE);
484 cpl_test_abs(mode, 0.032959079472357655, HDRL_EPS_TEST);
485 cpl_test_zero(mode_err);
486
487 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
488 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
489 cpl_test_error(CPL_ERROR_NONE);
490 cpl_test_abs(mode, 0.054356445, HDRL_EPS_TEST);
491 cpl_test_abs(mode_err, 0.2236946, HDRL_EPS_TEST);
492
493
494 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), -0.7901858605612435,
495 2.1786116577852885, 1.3033128217198686,
496 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
497 cpl_test_error(CPL_ERROR_NONE);
498 cpl_test_abs(mode, 0.054356445, HDRL_EPS_TEST);
499 cpl_test_abs(mode_err, 0.2236946, HDRL_EPS_TEST);
500
501 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), -1,
502 2.1786116577852885, 1.3033128217198686,
503 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
504 cpl_test_error(CPL_ERROR_NONE);
505 cpl_test_abs(mode, 0.94165224, HDRL_EPS_TEST);
506 cpl_test_abs(mode_err, 0.46006256, HDRL_EPS_TEST);
507
508 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), -1,
509 2.1786116577852885, 1.,
510 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
511 cpl_test_error(CPL_ERROR_NONE);
512 cpl_test_abs(mode, 0.3145597, HDRL_EPS_TEST);
513 cpl_test_abs(mode_err, 0.30404693, HDRL_EPS_TEST);
514
515
516 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), -1,
517 3., 1.,
518 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
519 cpl_test_error(CPL_ERROR_NONE);
520 cpl_test_abs(mode, 0.3145597, HDRL_EPS_TEST);
521 cpl_test_abs(mode_err, 0.30404693, HDRL_EPS_TEST);
522
523
524 /* error_niter < 0 */
525
526 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
527 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
528 cpl_test_error(CPL_ERROR_NONE);
529 cpl_test_abs(mode, 0.054356445, HDRL_EPS_TEST);
530 cpl_test_zero(mode_err);
531
532
533 cpl_image_delete(err);
534
535 cpl_image_delete(ima);
536 hdrl_image_delete(hima);
537 return cpl_error_get_code();
538}
539
540/* ---------------------------------------------------------------------------*/
546/* ---------------------------------------------------------------------------*/
547static cpl_error_code test_hdrl_mode_test4r(void)
548{
549 cpl_size nx = 51;
550 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
551 cpl_image_set(ima, 1, 1, 10.00458);
552 cpl_image_set(ima, 2, 1, 9.010156);
553 cpl_image_set(ima, 3, 1, 6.991111);
554 cpl_image_set(ima, 4, 1, 7.01822);
555 cpl_image_set(ima, 5, 1, 6.010726);
556 cpl_image_set(ima, 6, 1, 6.003698);
557 cpl_image_set(ima, 7, 1, 4.988562);
558 cpl_image_set(ima, 8, 1, 5.005653);
559 cpl_image_set(ima, 9, 1, 4.000302);
560
561 cpl_image_set(ima, 10, 1, 3.987379);
562 cpl_image_set(ima, 11, 1, 3.996887);
563 cpl_image_set(ima, 12, 1, 4.01525);
564 cpl_image_set(ima, 13, 1, 4.014528);
565 cpl_image_set(ima, 14, 1, 3.012359);
566 cpl_image_set(ima, 15, 1, 2.999663);
567 cpl_image_set(ima, 16, 1, 2.991814);
568 cpl_image_set(ima, 17, 1, 3.005553);
569 cpl_image_set(ima, 18, 1, 2.989472);
570 cpl_image_set(ima, 19, 1, 3.01091);
571 cpl_image_set(ima, 20, 1, 1.99299);
572
573 cpl_image_set(ima, 21, 1, 1.9974);
574 cpl_image_set(ima, 22, 1, 1.992067);
575 cpl_image_set(ima, 23, 1, 2.017085);
576 cpl_image_set(ima, 24, 1, 1.980961);
577 cpl_image_set(ima, 25, 1, 1.997591);
578 cpl_image_set(ima, 26, 1, 1.992787);
579 cpl_image_set(ima, 27, 1, 1.990686);
580 cpl_image_set(ima, 28, 1, 0.9972966);
581 cpl_image_set(ima, 29, 1, 1.015236);
582 cpl_image_set(ima, 30, 1, 0.9991327);
583
584 cpl_image_set(ima, 31, 1, 0.9961795);
585 cpl_image_set(ima, 32, 1, 0.9818511);
586 cpl_image_set(ima, 33, 1, 0.9957231);
587 cpl_image_set(ima, 34, 1, 0.9882734);
588 cpl_image_set(ima, 35, 1, 1.013345);
589 cpl_image_set(ima, 36, 1, 0.008599423);
590 cpl_image_set(ima, 37, 1, 0.0006760373);
591 cpl_image_set(ima, 38, 1, -0.001155981);
592 cpl_image_set(ima, 39, 1, -0.00481371);
593 cpl_image_set(ima, 40, 1, -0.01359721);
594
595 cpl_image_set(ima, 41, 1, 0.004918799);
596 cpl_image_set(ima, 42, 1, 0.0004142628);
597 cpl_image_set(ima, 43, 1, -0.01527452);
598 cpl_image_set(ima, 44, 1, -1.006316);
599 cpl_image_set(ima, 45, 1, -0.9906352);
600 cpl_image_set(ima, 46, 1, -0.9964058);
601 cpl_image_set(ima, 47, 1, -0.9859128);
602 cpl_image_set(ima, 48, 1, -0.9941401);
603 cpl_image_set(ima, 49, 1, -2.000185);
604 cpl_image_set(ima, 50, 1, -1.997964);
605
606 cpl_image_set(ima, 51, 1, -2.984013);
607 cpl_image* err = NULL;
608 //cpl_image* err = cpl_image_duplicate(ima);
609 //cpl_image_power(err, 0.5);
610
611 hdrl_image* hima = hdrl_image_create(ima, err);
612 double mode = 0;
613 double mode_err = 0;
614 cpl_size naccepted = 0;
615
616 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
617 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
618 cpl_test_error(CPL_ERROR_NONE);
619 cpl_test_abs(mode, 3.601380572859801, HDRL_EPS_TEST);
620 cpl_test_abs(mode_err, 3.2971602106947917, HDRL_EPS_TEST);
621
622 /* error_niter < 0 */
623 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
624 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
625 cpl_test_error(CPL_ERROR_NONE);
626 cpl_test_abs(mode, 3.601380572859801, HDRL_EPS_TEST);
627 cpl_test_zero(mode_err);
628
629 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
630 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
631 cpl_test_error(CPL_ERROR_NONE);
632 cpl_test_abs(mode, 2.4773676405741756, HDRL_EPS_TEST);
633 cpl_test_abs(mode_err, 0.4347417015719646, HDRL_EPS_TEST);
634 /* error_niter < 0 */
635 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
636 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
637 cpl_test_error(CPL_ERROR_NONE);
638 cpl_test_abs(mode, 2.4773676405741756, HDRL_EPS_TEST);
639 cpl_test_zero(mode_err);
640
641 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
642 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
643 cpl_test_error(CPL_ERROR_NONE);
644 cpl_test_abs(mode, 1.9920673, HDRL_EPS_TEST);
645 cpl_test_abs(mode_err, 1.5278777, HDRL_EPS_TEST);
646 /* error_niter < 0 */
647 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
648 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
649 cpl_test_error(CPL_ERROR_NONE);
650 cpl_test_abs(mode, 1.9920673, HDRL_EPS_TEST);
651 cpl_test_zero(mode_err);
652
653 cpl_image_delete(err);
654 cpl_image_delete(ima);
655 hdrl_image_delete(hima);
656 return cpl_error_get_code();
657
658}
659/* ---------------------------------------------------------------------------*/
665/* ---------------------------------------------------------------------------*/
666static cpl_error_code test_hdrl_mode_test4(void)
667{
668
669 cpl_size nx = 51;
670 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
671 cpl_image_set(ima, 1, 1, 10);
672 cpl_image_set(ima, 2, 1, 9);
673 cpl_image_set(ima, 3, 1, 7);
674 cpl_image_set(ima, 4, 1, 7);
675 cpl_image_set(ima, 5, 1, 6);
676 cpl_image_set(ima, 6, 1, 6);
677 cpl_image_set(ima, 7, 1, 5);
678 cpl_image_set(ima, 8, 1, 5);
679 cpl_image_set(ima, 9, 1, 4);
680 cpl_image_set(ima, 10, 1, 4);
681
682 cpl_image_set(ima, 11, 1, 4);
683 cpl_image_set(ima, 12, 1, 4);
684 cpl_image_set(ima, 13, 1, 4);
685 cpl_image_set(ima, 14, 1, 3);
686 cpl_image_set(ima, 15, 1, 3);
687 cpl_image_set(ima, 16, 1, 3);
688 cpl_image_set(ima, 17, 1, 3);
689 cpl_image_set(ima, 18, 1, 3);
690 cpl_image_set(ima, 19, 1, 3);
691 cpl_image_set(ima, 20, 1, 2);
692
693 cpl_image_set(ima, 21, 1, 2);
694 cpl_image_set(ima, 22, 1, 2);
695 cpl_image_set(ima, 23, 1, 2);
696 cpl_image_set(ima, 24, 1, 2);
697 cpl_image_set(ima, 25, 1, 2);
698 cpl_image_set(ima, 26, 1, 2);
699 cpl_image_set(ima, 27, 1, 2);
700 cpl_image_set(ima, 28, 1, 1);
701 cpl_image_set(ima, 29, 1, 1);
702 cpl_image_set(ima, 30, 1, 1);
703
704 cpl_image_set(ima, 31, 1, 1);
705 cpl_image_set(ima, 32, 1, 1);
706 cpl_image_set(ima, 33, 1, 1);
707 cpl_image_set(ima, 34, 1, 1);
708 cpl_image_set(ima, 35, 1, 1);
709 cpl_image_set(ima, 36, 1, 0);
710 cpl_image_set(ima, 37, 1, 0);
711 cpl_image_set(ima, 38, 1, 0);
712 cpl_image_set(ima, 39, 1, 0);
713 cpl_image_set(ima, 40, 1, 0);
714
715 cpl_image_set(ima, 41, 1, 0);
716 cpl_image_set(ima, 42, 1, 0);
717 cpl_image_set(ima, 43, 1, 0);
718 cpl_image_set(ima, 44, 1, -1);
719 cpl_image_set(ima, 45, 1, -1);
720 cpl_image_set(ima, 46, 1, -1);
721 cpl_image_set(ima, 47, 1, -1);
722 cpl_image_set(ima, 48, 1, -1);
723 cpl_image_set(ima, 49, 1, -2);
724 cpl_image_set(ima, 50, 1, -2);
725
726 cpl_image_set(ima, 51, 1, -3);
727
728 cpl_image* err = cpl_image_duplicate(ima);
729 cpl_image_power(err, 0.5);
730
731 double mode = 0;
732 double mode_err = 0;
733 cpl_size naccepted = 0;
734 hdrl_image* hima = hdrl_image_create(ima, err);
735
736
737 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
738 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
739 cpl_test_error(CPL_ERROR_NONE);
740
741 cpl_test_abs(mode, 3.6175831623895225, HDRL_EPS_TEST);
742 cpl_test_abs(mode_err, 3.3132769022073894, HDRL_EPS_TEST);
743
744 /* error_niter < 0 */
745 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
746 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
747 cpl_test_error(CPL_ERROR_NONE);
748
749 cpl_test_abs(mode, 3.6175831623895225, HDRL_EPS_TEST);
750 cpl_test_zero(mode_err);
751
752 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
753 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
754 cpl_test_error(CPL_ERROR_NONE);
755 cpl_test_abs(mode, 2.488075996118219, HDRL_EPS_TEST);
756 cpl_test_abs(mode_err, 0.43686674174112927, HDRL_EPS_TEST);
757 /* error_niter < 0 */
758 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
759 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
760 cpl_test_error(CPL_ERROR_NONE);
761 cpl_test_abs(mode, 2.488075996118219, HDRL_EPS_TEST);
762 cpl_test_zero(mode_err);
763
764 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
765 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
766 cpl_test_error(CPL_ERROR_NONE);
767 cpl_test_abs(mode, 2.0, HDRL_EPS_TEST);
768 cpl_test_abs(mode_err, 1.5265421, HDRL_EPS_TEST);
769
770 /* error_niter < 0 */
771 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
772 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
773 cpl_test_error(CPL_ERROR_NONE);
774 cpl_test_abs(mode, 2.0, HDRL_EPS_TEST);
775 cpl_test_zero(mode_err);
776
777 cpl_image_delete(err);
778 cpl_image_delete(ima);
779 hdrl_image_delete(hima);
780 return cpl_error_get_code();
781
782}
783/* ---------------------------------------------------------------------------*/
789/* ---------------------------------------------------------------------------*/
790static cpl_error_code test_hdrl_mode_test3r(void)
791{
792 cpl_size nx = 38;
793 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
794 cpl_image_set(ima, 1, 1, 10.01704);
795 cpl_image_set(ima, 2, 1, 8.988381);
796 cpl_image_set(ima, 3, 1, 7.006527);
797 cpl_image_set(ima, 4, 1, 5.994881);
798 cpl_image_set(ima, 5, 1, 6.005347);
799 cpl_image_set(ima, 6, 1, 5.010838);
800 cpl_image_set(ima, 7, 1, 4.996699);
801 cpl_image_set(ima, 8, 1, 2.997336);
802 cpl_image_set(ima, 9, 1, 2.983613);
803 cpl_image_set(ima, 10, 1, 2.995615);
804
805 cpl_image_set(ima, 11, 1, 2.003428);
806 cpl_image_set(ima, 12, 1, 2.024438);
807 cpl_image_set(ima, 13, 1, 2.000071);
808 cpl_image_set(ima, 14, 1, 1.985059);
809 cpl_image_set(ima, 15, 1, 1.97843);
810 cpl_image_set(ima, 16, 1, 1.002653);
811 cpl_image_set(ima, 17, 1, 0.9947691);
812 cpl_image_set(ima, 18, 1, 1.000785);
813 cpl_image_set(ima, 19, 1, 1.008936);
814 cpl_image_set(ima, 20, 1, 0.9971861);
815
816 cpl_image_set(ima, 21, 1, 0.9904818);
817 cpl_image_set(ima, 22, 1, 0.9954762);
818 cpl_image_set(ima, 23, 1, 1.001084);
819 cpl_image_set(ima, 24, 1, -0.00915037);
820 cpl_image_set(ima, 25, 1, -0.001580715);
821 cpl_image_set(ima, 26, 1, -0.01343179);
822 cpl_image_set(ima, 27, 1, 0.009741801);
823 cpl_image_set(ima, 28, 1, -0.01869533);
824 cpl_image_set(ima, 29, 1, -0.004127814);
825 cpl_image_set(ima, 30, 1, 0.002222741);
826
827 cpl_image_set(ima, 31, 1, 0.01353268);
828 cpl_image_set(ima, 32, 1, -0.9949167);
829 cpl_image_set(ima, 33, 1, -0.9913741);
830 cpl_image_set(ima, 34, 1, -0.99041);
831 cpl_image_set(ima, 35, 1, -1.003971);
832 cpl_image_set(ima, 36, 1, -1.997081);
833 cpl_image_set(ima, 37, 1, -2.006046);
834 cpl_image_set(ima, 38, 1, -2.992429);
835
836
837 cpl_image* err = cpl_image_duplicate(ima);
838 cpl_image_power(err, 0.5);
839 hdrl_image* hima = hdrl_image_create(ima, err);
840 double mode = 0;
841 double mode_err = 0;
842 cpl_size naccepted = 0;
843
844 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
845 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
846 cpl_test_error(CPL_ERROR_NONE);
847
848 cpl_test_abs(mode, 1.5067609093941583, HDRL_EPS_TEST);
849 cpl_test_abs(mode_err, 1.197924149788573, HDRL_EPS_TEST);
850
851 /* error_niter < 0 */
852 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
853 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
854 cpl_test_error(CPL_ERROR_NONE);
855
856 cpl_test_abs(mode, 1.5067609093941583, HDRL_EPS_TEST);
857 cpl_test_zero(mode_err);
858
859 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
860 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
861 cpl_test_error(CPL_ERROR_NONE);
862 cpl_test_abs(mode, 0.40135648757242137, HDRL_EPS_TEST);
863 cpl_test_abs(mode_err, 0.3967385878378167, HDRL_EPS_TEST);
864
865 /* error_niter < 0 */
866 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
867 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
868 cpl_test_error(CPL_ERROR_NONE);
869 cpl_test_abs(mode, 0.40135648757242137, HDRL_EPS_TEST);
870 cpl_test_zero(mode_err);
871
872 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
873 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
874 cpl_test_error(CPL_ERROR_NONE);
875 cpl_test_abs(mode, 0.0059822705, HDRL_EPS_TEST);
876 cpl_test_abs(mode_err, 0.7660477, HDRL_EPS_TEST);
877
878 /* error_niter < 0 */
879 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
880 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
881 cpl_test_error(CPL_ERROR_NONE);
882 cpl_test_abs(mode, 0.0059822705, HDRL_EPS_TEST);
883 cpl_test_zero(mode_err);
884
885 cpl_image_delete(err);
886 cpl_image_delete(ima);
887 hdrl_image_delete(hima);
888 return cpl_error_get_code();
889
890}
891/* ---------------------------------------------------------------------------*/
897/* ---------------------------------------------------------------------------*/
898static cpl_error_code test_hdrl_mode_test3(void)
899{
900
901 cpl_size nx = 38;
902 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
903 cpl_image_set(ima, 1, 1, 10);
904 cpl_image_set(ima, 2, 1, 9);
905 cpl_image_set(ima, 3, 1, 7);
906 cpl_image_set(ima, 4, 1, 6);
907 cpl_image_set(ima, 5, 1, 6);
908 cpl_image_set(ima, 6, 1, 5);
909 cpl_image_set(ima, 7, 1, 5);
910 cpl_image_set(ima, 8, 1, 3);
911 cpl_image_set(ima, 9, 1, 3);
912 cpl_image_set(ima, 10, 1, 3);
913
914 cpl_image_set(ima, 11, 1, 2);
915 cpl_image_set(ima, 12, 1, 2);
916 cpl_image_set(ima, 13, 1, 2);
917 cpl_image_set(ima, 14, 1, 2);
918 cpl_image_set(ima, 15, 1, 2);
919 cpl_image_set(ima, 16, 1, 1);
920 cpl_image_set(ima, 17, 1, 1);
921 cpl_image_set(ima, 18, 1, 1);
922 cpl_image_set(ima, 19, 1, 1);
923 cpl_image_set(ima, 20, 1, 1);
924
925 cpl_image_set(ima, 21, 1, 1);
926 cpl_image_set(ima, 22, 1, 1);
927 cpl_image_set(ima, 23, 1, 1);
928 cpl_image_set(ima, 24, 1, 0);
929 cpl_image_set(ima, 25, 1, 0);
930 cpl_image_set(ima, 26, 1, 0);
931 cpl_image_set(ima, 27, 1, 0);
932 cpl_image_set(ima, 28, 1, 0);
933 cpl_image_set(ima, 29, 1, 0);
934 cpl_image_set(ima, 30, 1, 0);
935
936 cpl_image_set(ima, 31, 1, 0);
937 cpl_image_set(ima, 32, 1, -1);
938 cpl_image_set(ima, 33, 1, -1);
939 cpl_image_set(ima, 34, 1, -1);
940 cpl_image_set(ima, 35, 1, -1);
941 cpl_image_set(ima, 36, 1, -2);
942 cpl_image_set(ima, 37, 1, -2);
943 cpl_image_set(ima, 38, 1, -3);
944
945 cpl_image* err = cpl_image_duplicate(ima);
946 cpl_image_power(err, 0.5);
947 hdrl_image* hima = hdrl_image_create(ima, err);
948 double mode = 0;
949 double mode_err = 0;
950 cpl_size naccepted = 0;
951
952 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
953 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
954 cpl_test_error(CPL_ERROR_NONE);
955
956 cpl_test_abs(mode, 1.4324860586022403, HDRL_EPS_TEST);
957 cpl_test_abs(mode_err, 1.1801640248274137, HDRL_EPS_TEST);
958
959 /* error_niter < 0 */
960 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
961 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
962 cpl_test_error(CPL_ERROR_NONE);
963
964 cpl_test_abs(mode, 1.4324860586022403, HDRL_EPS_TEST);
965 cpl_test_zero(mode_err);
966
967
968 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
969 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
970 cpl_test_error(CPL_ERROR_NONE);
971 cpl_test_abs(mode, 0.3434700873077243, HDRL_EPS_TEST);
972 cpl_test_abs(mode_err, 0.39085664038884216, HDRL_EPS_TEST);
973
974 /* error_niter < 0 */
975 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
976 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
977 cpl_test_error(CPL_ERROR_NONE);
978 cpl_test_abs(mode, 0.3434700873077243, HDRL_EPS_TEST);
979 cpl_test_zero(mode_err);
980
981 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
982 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
983 cpl_test_error(CPL_ERROR_NONE);
984 cpl_test_abs(mode, 0.0, HDRL_EPS_TEST);
985 cpl_test_abs(mode_err, 0.76777196, HDRL_EPS_TEST);
986
987 /* error_niter < 0 */
988 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
989 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
990 cpl_test_error(CPL_ERROR_NONE);
991 cpl_test_abs(mode, 0.0, HDRL_EPS_TEST);
992 cpl_test_zero(mode_err);
993
994 cpl_image_delete(err);
995 cpl_image_delete(ima);
996 hdrl_image_delete(hima);
997 return cpl_error_get_code();
998
999}
1000
1001/* ---------------------------------------------------------------------------*/
1007/* ---------------------------------------------------------------------------*/
1008static cpl_error_code test_hdrl_mode_test2r(void)
1009{
1010 cpl_size nx = 32;
1011 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
1012 cpl_image_set(ima, 1, 1, 9.998852);
1013 cpl_image_set(ima, 2, 1, 9.005792);
1014 cpl_image_set(ima, 3, 1, 7.007078);
1015 cpl_image_set(ima, 4, 1, 5.99083);
1016 cpl_image_set(ima, 5, 1, 5.971509);
1017 cpl_image_set(ima, 6, 1, 4.996029);
1018 cpl_image_set(ima, 7, 1, 4.999716);
1019 cpl_image_set(ima, 8, 1, 2.984478);
1020 cpl_image_set(ima, 9, 1, 3.002251);
1021 cpl_image_set(ima, 10, 1, 2.991654);
1022
1023 cpl_image_set(ima, 11, 1, 2.004251);
1024 cpl_image_set(ima, 12, 1, 2.009505);
1025 cpl_image_set(ima, 13, 1, 1.99897);
1026 cpl_image_set(ima, 14, 1, 1.013377);
1027 cpl_image_set(ima, 15, 1, 0.9919196);
1028 cpl_image_set(ima, 16, 1, 1.011431);
1029 cpl_image_set(ima, 17, 1, 0.9922368);
1030 cpl_image_set(ima, 18, 1, 0.9986056);
1031 cpl_image_set(ima, 19, 1, 0.00585786);
1032 cpl_image_set(ima, 20, 1, 0.01751465);
1033
1034 cpl_image_set(ima, 21, 1, 0.003120024);
1035 cpl_image_set(ima, 22, 1, 0.007319644);
1036 cpl_image_set(ima, 23, 1, 0.01744251);
1037 cpl_image_set(ima, 24, 1, -0.01521505);
1038 cpl_image_set(ima, 25, 1, -0.001438193);
1039 cpl_image_set(ima, 26, 1, -0.02398127);
1040 cpl_image_set(ima, 27, 1, -0.9988093);
1041 cpl_image_set(ima, 28, 1, -0.9951036);
1042 cpl_image_set(ima, 29, 1, -1.011068);
1043 cpl_image_set(ima, 30, 1, -1.0117);
1044
1045 cpl_image_set(ima, 31, 1, -4.004215);
1046 cpl_image_set(ima, 32, 1, -2.987547);
1047
1048
1049 cpl_image* err = cpl_image_duplicate(ima);
1050 cpl_image_power(err, 0.5);
1051 hdrl_image* hima = hdrl_image_create(ima, err);
1052 double mode = 0;
1053 double mode_err = 0;
1054 cpl_size naccepted = 0;
1055
1056
1057 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1058 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
1059 cpl_test_error(CPL_ERROR_NONE);
1060
1061 cpl_test_abs(mode, 2.6150635557822244, HDRL_EPS_TEST);
1062 cpl_test_abs(mode_err, 2.3272304852994763, HDRL_EPS_TEST);
1063
1064 /* error_niter < 0 */
1065 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1066 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
1067 cpl_test_error(CPL_ERROR_NONE);
1068
1069 cpl_test_abs(mode, 2.6150635557822244, HDRL_EPS_TEST);
1070 cpl_test_zero(mode_err);
1071
1072 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1073 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
1074 cpl_test_error(CPL_ERROR_NONE);
1075 cpl_test_abs(mode, 1.0875377897613978, HDRL_EPS_TEST);
1076 cpl_test_abs(mode_err, 0.46097044101825074, HDRL_EPS_TEST);
1077
1078 /* error_niter < 0 */
1079 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1080 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
1081 cpl_test_error(CPL_ERROR_NONE);
1082 cpl_test_abs(mode, 1.0875377897613978, HDRL_EPS_TEST);
1083 cpl_test_zero(mode_err);
1084
1085 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1086 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
1087 cpl_test_error(CPL_ERROR_NONE);
1088 cpl_test_abs(mode, 0.017514654, HDRL_EPS_TEST);
1089 cpl_test_abs(mode_err, 1.2942566, HDRL_EPS_TEST);
1090
1091 /* error_niter < 0 */
1092 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1093 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
1094 cpl_test_error(CPL_ERROR_NONE);
1095 cpl_test_abs(mode, 0.017514654, HDRL_EPS_TEST);
1096 cpl_test_zero(mode_err);
1097
1098 cpl_image_delete(err);
1099 cpl_image_delete(ima);
1100 hdrl_image_delete(hima);
1101 return cpl_error_get_code();
1102
1103}
1104/* ---------------------------------------------------------------------------*/
1110/* ---------------------------------------------------------------------------*/
1111static cpl_error_code test_hdrl_mode_test2(void)
1112{
1113
1114 cpl_size nx = 32;
1115 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
1116 cpl_image_set(ima, 1, 1, 10);
1117 cpl_image_set(ima, 2, 1, 9);
1118 cpl_image_set(ima, 3, 1, 7);
1119 cpl_image_set(ima, 4, 1, 6);
1120 cpl_image_set(ima, 5, 1, 6);
1121 cpl_image_set(ima, 6, 1, 5);
1122 cpl_image_set(ima, 7, 1, 5);
1123 cpl_image_set(ima, 8, 1, 3);
1124 cpl_image_set(ima, 9, 1, 3);
1125 cpl_image_set(ima, 10, 1, 3);
1126
1127 cpl_image_set(ima, 11, 1, 2);
1128 cpl_image_set(ima, 12, 1, 2);
1129 cpl_image_set(ima, 13, 1, 2);
1130 cpl_image_set(ima, 14, 1, 1);
1131 cpl_image_set(ima, 15, 1, 1);
1132 cpl_image_set(ima, 16, 1, 1);
1133 cpl_image_set(ima, 17, 1, 1);
1134 cpl_image_set(ima, 18, 1, 1);
1135 cpl_image_set(ima, 19, 1, 0);
1136 cpl_image_set(ima, 20, 1, 0);
1137
1138 cpl_image_set(ima, 21, 1, 0);
1139 cpl_image_set(ima, 22, 1, 0);
1140 cpl_image_set(ima, 23, 1, 0);
1141 cpl_image_set(ima, 24, 1, 0);
1142 cpl_image_set(ima, 25, 1, 0);
1143 cpl_image_set(ima, 26, 1, 0);
1144 cpl_image_set(ima, 27, 1, -1);
1145 cpl_image_set(ima, 28, 1, -1);
1146 cpl_image_set(ima, 29, 1, -1);
1147 cpl_image_set(ima, 20, 1, -1);
1148
1149 cpl_image_set(ima, 31, 1, -4);
1150 cpl_image_set(ima, 32, 1, -3);
1151
1152 cpl_image* err = cpl_image_duplicate(ima);
1153 cpl_image_power(err, 0.5);
1154 hdrl_image* hima = hdrl_image_create(ima, err);
1155 double mode = 0;
1156 double mode_err = 0;
1157 cpl_size naccepted = 0;
1158
1159 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1160 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
1161 cpl_test_error(CPL_ERROR_NONE);
1162 cpl_test_abs(mode, 2.600666439655294, HDRL_EPS_TEST);
1163 cpl_test_abs(mode_err, 2.3206868514023493, HDRL_EPS_TEST);
1164
1165 /* error_niter < 0 */
1166 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1167 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
1168 cpl_test_error(CPL_ERROR_NONE);
1169 cpl_test_abs(mode, 2.600666439655294, HDRL_EPS_TEST);
1170 cpl_test_zero(mode_err);
1171
1172 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1173 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
1174 cpl_test_error(CPL_ERROR_NONE);
1175 cpl_test_abs(mode, 1.0774357228117637, HDRL_EPS_TEST);
1176 cpl_test_abs(mode_err, 0.4596742987485125, HDRL_EPS_TEST);
1177
1178 /* error_niter < 0 */
1179 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1180 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
1181 cpl_test_error(CPL_ERROR_NONE);
1182 cpl_test_abs(mode, 1.0774357228117637, HDRL_EPS_TEST);
1183 cpl_test_zero(mode_err);
1184
1185 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1186 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
1187 cpl_test_error(CPL_ERROR_NONE);
1188 cpl_test_abs(mode, 0.0, HDRL_EPS_TEST);
1189 cpl_test_abs(mode_err, 1.2945614, HDRL_EPS_TEST);
1190
1191 /* error_niter < 0 */
1192 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1193 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
1194 cpl_test_error(CPL_ERROR_NONE);
1195 cpl_test_abs(mode, 0.0, HDRL_EPS_TEST);
1196 cpl_test_zero(mode_err);
1197
1198 cpl_image_delete(err);
1199 cpl_image_delete(ima);
1200 hdrl_image_delete(hima);
1201 return cpl_error_get_code();
1202
1203}
1204
1205
1206/* ---------------------------------------------------------------------------*/
1212/* ---------------------------------------------------------------------------*/
1213static cpl_error_code test_hdrl_mode_test1r(void)
1214{
1215
1216 cpl_size nx = 27;
1217 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
1218 cpl_image_set(ima, 1, 1, 9.986042);
1219 cpl_image_set(ima, 2, 1, 9.011315);
1220 cpl_image_set(ima, 3, 1, 7.002415);
1221 cpl_image_set(ima, 4, 1, 5.996731);
1222 cpl_image_set(ima, 5, 1, 6.008002);
1223 cpl_image_set(ima, 6, 1, 5.004089);
1224 cpl_image_set(ima, 7, 1, 5.015304);
1225 cpl_image_set(ima, 8, 1, 3.016874);
1226 cpl_image_set(ima, 9, 1, 3.014342);
1227 cpl_image_set(ima, 10, 1, 2.993648);
1228
1229 cpl_image_set(ima, 11, 1, 1.992332);
1230 cpl_image_set(ima, 12, 1, 1.990101);
1231 cpl_image_set(ima, 13, 1, 1.992047);
1232 cpl_image_set(ima, 14, 1, 0.9840552);
1233 cpl_image_set(ima, 15, 1, 1.012001);
1234 cpl_image_set(ima, 16, 1, 1.014128);
1235 cpl_image_set(ima, 17, 1, 0.9949675);
1236 cpl_image_set(ima, 18, 1, 1.001209);
1237 cpl_image_set(ima, 19, 1, -0.01172797);
1238 cpl_image_set(ima, 20, 1, 0.002719025);
1239
1240 cpl_image_set(ima, 21, 1, 0.001201321);
1241 cpl_image_set(ima, 22, 1, 0.009442335);
1242 cpl_image_set(ima, 23, 1, 0.003206544);
1243 cpl_image_set(ima, 24, 1, -0.008347392);
1244 cpl_image_set(ima, 25, 1, -0.007489061);
1245 cpl_image_set(ima, 26, 1, -0.003378053);
1246 cpl_image_set(ima, 27, 1, 0.001325341);
1247
1248 cpl_image* err = cpl_image_duplicate(ima);
1249 cpl_image_power(err, 0.5);
1250 hdrl_image* hima = hdrl_image_create(ima, err);
1251 double mode = 0;
1252 double mode_err = 0;
1253 cpl_size naccepted = 0;
1254
1255 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1256 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
1257 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
1258 cpl_error_reset();
1259 /* this was making automatic switch to method weight
1260 cpl_test_abs(mode, 0.6902527626942991, HDRL_EPS_TEST);
1261 cpl_test_abs(mode_err, 0.6089455241031, HDRL_EPS_TEST);
1262 */
1263
1264 /* error_niter < 0 */
1265 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1266 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
1267 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
1268 cpl_error_reset();
1269 /* this was making automatic switch to method weight
1270 cpl_test_abs(mode, 0.6902527626942991, HDRL_EPS_TEST);
1271 */
1272 cpl_test_zero(mode_err);
1273
1274
1275
1276 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1277 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
1278 cpl_test_error(CPL_ERROR_NONE);
1279 cpl_test_abs(mode, 0.6902527626942991, HDRL_EPS_TEST);
1280 cpl_test_abs(mode_err, 0.6089455241031, HDRL_EPS_TEST);
1281
1282 /* error_niter < 0 */
1283 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1284 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
1285 cpl_test_error(CPL_ERROR_NONE);
1286 cpl_test_abs(mode, 0.6902527626942991, HDRL_EPS_TEST);
1287 cpl_test_zero(mode_err);
1288
1289 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1290 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
1291 cpl_test_error(CPL_ERROR_NONE);
1292 cpl_test_abs(mode, 0.0029627844, HDRL_EPS_TEST);
1293 cpl_test_abs(mode_err, 0.49867436, HDRL_EPS_TEST);
1294
1295 /* error_niter < 0 */
1296 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1297 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
1298 cpl_test_error(CPL_ERROR_NONE);
1299 cpl_test_abs(mode, 0.0029627844, HDRL_EPS_TEST);
1300 cpl_test_zero(mode_err);
1301
1302
1303 cpl_image_delete(err);
1304 cpl_image_delete(ima);
1305 hdrl_image_delete(hima);
1306 return cpl_error_get_code();
1307}
1308/* ---------------------------------------------------------------------------*/
1314/* ---------------------------------------------------------------------------*/
1315static cpl_error_code test_hdrl_mode_test1(void)
1316{
1317 cpl_size nx = 27;
1318 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
1319
1320 cpl_image_set(ima, 1, 1, 10);
1321 cpl_image_set(ima, 2, 1, 9);
1322 cpl_image_set(ima, 3, 1, 7);
1323 cpl_image_set(ima, 4, 1, 6);
1324 cpl_image_set(ima, 5, 1, 6);
1325 cpl_image_set(ima, 6, 1, 5);
1326 cpl_image_set(ima, 7, 1, 5);
1327 cpl_image_set(ima, 8, 1, 3);
1328 cpl_image_set(ima, 9, 1, 3);
1329 cpl_image_set(ima, 10, 1, 3);
1330
1331 cpl_image_set(ima, 11, 1, 2);
1332 cpl_image_set(ima, 12, 1, 2);
1333 cpl_image_set(ima, 13, 1, 2);
1334 cpl_image_set(ima, 14, 1, 1);
1335 cpl_image_set(ima, 15, 1, 1);
1336 cpl_image_set(ima, 16, 1, 1);
1337 cpl_image_set(ima, 17, 1, 1);
1338 cpl_image_set(ima, 18, 1, 1);
1339 cpl_image_set(ima, 19, 1, 0);
1340 cpl_image_set(ima, 20, 1, 0);
1341
1342 cpl_image_set(ima, 21, 1, 0);
1343 cpl_image_set(ima, 22, 1, 0);
1344 cpl_image_set(ima, 23, 1, 0);
1345 cpl_image_set(ima, 24, 1, 0);
1346 cpl_image_set(ima, 25, 1, 0);
1347 cpl_image_set(ima, 26, 1, 0);
1348 cpl_image_set(ima, 27, 1, 0);
1349
1350 cpl_image* err = cpl_image_duplicate(ima);
1351 cpl_image_power(err, 0.5);
1352 hdrl_image* hima = hdrl_image_create(ima, err);
1353 double mode = 0;
1354 double mode_err = 0;
1355 cpl_size naccepted = 0;
1356
1357 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1358 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
1359 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
1360 cpl_error_reset();
1361 /* this was making automatic switch to method weight
1362 cpl_test_abs(mode, 0.6899031999999996, HDRL_EPS_TEST);
1363 cpl_test_abs(mode_err, 0.59846865840335, HDRL_EPS_TEST);
1364 */
1365
1366 /* error_niter < 0 */
1367 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1368 HDRL_MODE_FIT, -1, &mode, &mode_err,&naccepted);
1369 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
1370 cpl_error_reset();
1371 /* this was making automatic switch to method weight
1372 cpl_test_abs(mode, 0.6899031999999996, HDRL_EPS_TEST);
1373 */
1374 cpl_test_zero(mode_err);
1375
1376 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1377 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
1378 cpl_test_error(CPL_ERROR_NONE);
1379 cpl_test_abs(mode, 0.6899031999999996, HDRL_EPS_TEST);
1380 cpl_test_abs(mode_err, 0.59846865840335, HDRL_EPS_TEST);
1381
1382 /* error_niter < 0 */
1383 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1384 HDRL_MODE_WEIGHTED, -1, &mode, &mode_err,&naccepted);
1385 cpl_test_error(CPL_ERROR_NONE);
1386 cpl_test_abs(mode, 0.6899031999999996, HDRL_EPS_TEST);
1387 cpl_test_zero(mode_err);
1388
1389 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1390 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
1391 cpl_test_error(CPL_ERROR_NONE);
1392 cpl_test_abs(mode, 0.0, HDRL_EPS_TEST);
1393 cpl_test_abs(mode_err, 0.49724513, HDRL_EPS_TEST);
1394
1395 /* error_niter < 0 */
1396 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1397 HDRL_MODE_MEDIAN, -1, &mode, &mode_err,&naccepted);
1398 cpl_test_error(CPL_ERROR_NONE);
1399 cpl_test_abs(mode, 0.0, HDRL_EPS_TEST);
1400 cpl_test_zero(mode_err);
1401
1402 cpl_image_delete(err);
1403 cpl_image_delete(ima);
1404 hdrl_image_delete(hima);
1405 return cpl_error_get_code();
1406
1407}
1408static cpl_error_code test_hdrl_mode_parameter_create_parlist(void)
1409{
1410 /* creates a proper HDRL mode parameter */
1411 hdrl_parameter * mode_def =
1412 hdrl_collapse_mode_parameter_create(1., 100., 1., HDRL_MODE_MEDIAN,0);
1413
1414 //char *name;
1415 //name = hdrl_join_string(".", 2, prefix, "mode");
1416 const char *prefix = "prefix";
1417 const char* base_context= "recipe";
1418
1419 cpl_parameterlist * pmode = NULL;
1420
1421 /* test improper inputs */
1422 pmode = hdrl_mode_parameter_create_parlist(NULL, prefix, mode_def);
1423 cpl_test_null(pmode);
1424 cpl_test_error(CPL_ERROR_NULL_INPUT);
1425
1426
1427 pmode = hdrl_mode_parameter_create_parlist(base_context, NULL, mode_def);
1428 cpl_test_null(pmode);
1429 cpl_test_error(CPL_ERROR_NULL_INPUT);
1430
1431
1432 pmode = hdrl_mode_parameter_create_parlist(base_context, prefix, NULL);
1433 cpl_test_null(pmode);
1434 cpl_test_error(CPL_ERROR_NULL_INPUT);
1435
1436 /* test proper inputs */
1437 pmode = hdrl_mode_parameter_create_parlist(base_context, prefix,mode_def);
1438 cpl_test_nonnull(pmode);
1439 cpl_test_error(CPL_ERROR_NONE);
1440
1441 hdrl_parameter_delete(mode_def);
1442 cpl_parameterlist_delete(pmode);
1443 return cpl_error_get_code();
1444}
1445
1446static cpl_error_code test_hdrl_mode_parameter_parse_parlist(void)
1447{
1448 /* creates a proper HDRL mode parameter */
1449 hdrl_parameter * mode_def =
1450 hdrl_collapse_mode_parameter_create(1., 100., 1., HDRL_MODE_MEDIAN,0);
1451
1452 //char *name;
1453 //name = hdrl_join_string(".", 2, prefix, "mode");
1454 const char *prefix = "mode";
1455 const char* base_context= "recipe";
1456 cpl_parameterlist * parlist =
1457 hdrl_mode_parameter_create_parlist(base_context, prefix,mode_def);
1458
1459 double histo_min = 0;
1460 double histo_max = 0;
1461 double bin_size = 0;
1462 cpl_size error_niter = 0;
1463 hdrl_mode_type method = HDRL_MODE_MEDIAN;
1464 hdrl_parameter* p = NULL;
1465
1466
1467 /* test invalid input */
1468 hdrl_mode_parameter_parse_parlist(NULL, prefix, &histo_min, &histo_max,
1469 &bin_size, &method, &error_niter);
1470 cpl_test_error(CPL_ERROR_NULL_INPUT);
1471
1472 hdrl_mode_parameter_parse_parlist(parlist, NULL, &histo_min, &histo_max,
1473 &bin_size, &method, &error_niter);
1474 cpl_test_error(CPL_ERROR_NULL_INPUT);
1475
1476
1477 /* test valid input */
1478 hdrl_mode_parameter_parse_parlist(parlist, base_context, &histo_min, &histo_max,
1479 &bin_size, &method, &error_niter);
1480 cpl_test_error(CPL_ERROR_NONE);
1481 p = hdrl_collapse_mode_parameter_create(histo_min, histo_max, bin_size,
1482 method, error_niter);
1483 cpl_test_error(CPL_ERROR_NONE);
1484 hdrl_parameter_delete(mode_def);
1486 cpl_parameterlist_delete(parlist);
1487 return cpl_error_get_code();
1488}
1489
1490static void
1491hdrl_write_qc (cpl_propertylist *plist, cpl_table *tab)
1492{
1493 cpl_propertylist_append_double (
1494 plist, "ESO QC MODE_MEDIAN MEAN",
1495 cpl_table_get_column_mean (tab, "mode_median"));
1496 cpl_propertylist_append_double (
1497 plist, "ESO QC MODE_MEDIAN_ERR MEAN",
1498 cpl_table_get_column_mean (tab, "mode_median_error"));
1499 cpl_propertylist_append_double (
1500 plist, "ESO QC MODE_WEIGHT MEAN",
1501 cpl_table_get_column_mean (tab, "mode_weight"));
1502 cpl_propertylist_append_double (
1503 plist, "ESO QC MODE_WEIGHT_ERR MEAN",
1504 cpl_table_get_column_mean (tab, "mode_weight_error"));
1505 cpl_propertylist_append_double (plist, "ESO QC MODE_FIT MEAN",
1506 cpl_table_get_column_mean (tab, "mode_fit"));
1507 cpl_propertylist_append_double (
1508 plist, "ESO QC MODE_FIT_ERR MEAN",
1509 cpl_table_get_column_mean (tab, "mode_fit_error"));
1510 cpl_propertylist_append_double (
1511 plist, "ESO QC MODE_MEDIAN MEDIAN",
1512 cpl_table_get_column_median (tab, "mode_median"));
1513 cpl_propertylist_append_double (
1514 plist, "ESO QC MODE_MEDIAN_ERR MEDIAN",
1515 cpl_table_get_column_median (tab, "mode_median_error"));
1516 cpl_propertylist_append_double (
1517 plist, "ESO QC MODE_WEIGHT MEDIAN",
1518 cpl_table_get_column_median (tab, "mode_weight"));
1519 cpl_propertylist_append_double (
1520 plist, "ESO QC MODE_WEIGHT_ERR MEDIAN",
1521 cpl_table_get_column_median (tab, "mode_weight_error"));
1522 cpl_propertylist_append_double (
1523 plist, "ESO QC MODE_FIT MEDIAN",
1524 cpl_table_get_column_median (tab, "mode_fit"));
1525 cpl_propertylist_append_double (
1526 plist, "ESO QC MODE_FIT_ERR MEDIAN",
1527 cpl_table_get_column_median (tab, "mode_fit_error"));
1528 cpl_propertylist_append_double (
1529 plist, "ESO QC MODE_MEDIAN STDEV",
1530 cpl_table_get_column_stdev (tab, "mode_median"));
1531 cpl_propertylist_append_double (
1532 plist, "ESO QC MODE_MEDIAN_ERR STDEV",
1533 cpl_table_get_column_stdev (tab, "mode_median_error"));
1534 cpl_propertylist_append_double (
1535 plist, "ESO QC MODE_WEIGHT STDEV",
1536 cpl_table_get_column_stdev (tab, "mode_weight"));
1537 cpl_propertylist_append_double (
1538 plist, "ESO QC MODE_WEIGHT_ERR STDEV",
1539 cpl_table_get_column_stdev (tab, "mode_weight_error"));
1540 cpl_propertylist_append_double (plist, "ESO QC MODE_FIT STDEV",
1541 cpl_table_get_column_stdev (tab, "mode_fit"));
1542 cpl_propertylist_append_double (
1543 plist, "ESO QC MODE_FIT_ERR STDEV",
1544 cpl_table_get_column_stdev (tab, "mode_fit_error"));
1545}
1546
1547
1548static cpl_error_code
1549test_hdrl_mode_montecarlo_exec(double expected,
1550 cpl_size iteration,
1551 double * mode_median,
1552 double * mode_median_error,
1553 double * mode_weight,
1554 double * mode_weight_error,
1555 double * mode_fit,
1556 double * mode_fit_error,
1557 hdrl_random_state * state)
1558{
1559
1560 cpl_size sx = 1;
1561 cpl_size sy = 250000;
1562 cpl_image* ima = cpl_image_new(sx, sy, CPL_TYPE_INT);
1563 int* pdata = cpl_image_get_data_int(ima);
1564 cpl_size size = sx * sy;
1565
1566 for(cpl_size i = 0; i < size; i++) {
1567
1568 pdata[i] = (int)hdrl_random_poisson(state, expected);
1569 }
1570
1571 double mean = cpl_image_get_mean(ima);
1572 double median = cpl_image_get_median(ima);
1573 double stdev = cpl_image_get_stdev(ima);
1574
1575 cpl_msg_debug(cpl_func, "Montecarlo Simulated image:");
1576 cpl_msg_debug(cpl_func, "Montecarlo mean: %g, median: %g, stdev: %g",
1577 mean, median, stdev);
1578
1579 hdrl_image* hima = hdrl_image_create(ima, NULL);
1580 double mode_loc = 0.;
1581 double mode_error_loc = 0.;
1582 cpl_size naccepted = 0;
1583 //--------------------------------------------------------------------
1584 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1585 HDRL_MODE_MEDIAN, 0, &mode_loc, &mode_error_loc,&naccepted);
1586
1587 cpl_test_error(CPL_ERROR_NONE);
1588
1589 /* Fill the final mode and error */
1590 *mode_median = mode_loc;
1591 *mode_median_error = mode_error_loc;
1592 //--------------------------------------------------------------------
1593
1594 //--------------------------------------------------------------------
1595 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1596 HDRL_MODE_WEIGHTED, 0, &mode_loc, &mode_error_loc,&naccepted);
1597
1598 cpl_test_error(CPL_ERROR_NONE);
1599
1600 /* Fill the final mode and error */
1601 *mode_weight = mode_loc;
1602 *mode_weight_error = mode_error_loc;
1603 //--------------------------------------------------------------------
1604
1605 //--------------------------------------------------------------------
1606 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1607 HDRL_MODE_FIT, 0, &mode_loc, &mode_error_loc,&naccepted);
1608 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1609 /* previous call may return error if fit method fail check conditions
1610 * in this case we reset error to CPL_ERROR_NONE */
1611 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
1612 cpl_error_reset();
1613 }
1614
1615 /* Fill the final mode and error */
1616 *mode_fit = mode_loc;
1617 *mode_fit_error = mode_error_loc;
1618 //--------------------------------------------------------------------
1619
1620 cpl_propertylist * plist = cpl_propertylist_new();
1621 cpl_propertylist_append_double(plist, "ESO QC LAMBDA", expected);
1622 cpl_propertylist_append_double(plist, "ESO QC MEAN" , mean);
1623 cpl_propertylist_append_double(plist, "ESO QC MEDIAN", median);
1624 cpl_propertylist_append_double(plist, "ESO QC STDEV" , stdev);
1625
1626 cpl_propertylist_append_double(plist, "ESO QC MODE MEDIAN", *mode_median);
1627 cpl_propertylist_append_double(plist, "ESO QC MODE WEIGHT", *mode_weight);
1628 cpl_propertylist_append_double(plist, "ESO QC MODE FIT" , *mode_fit);
1629 cpl_propertylist_append_double(plist, "ESO QC MODE MEDIAN ERR", *mode_median_error);
1630 cpl_propertylist_append_double(plist, "ESO QC MODE WEIGHT ERR", *mode_weight_error);
1631 cpl_propertylist_append_double(plist, "ESO QC MODE FIT ERR" , *mode_fit_error);
1632
1633 char * outname = cpl_sprintf("Simulation_Montecarlo_mode_%d_iter%04d.fits",
1634 (int)expected, (int)iteration);
1635
1636 /* Save the image */
1637 //cpl_propertylist_save(plist,outname,CPL_IO_CREATE);
1638 //cpl_image_save(ima, outname, CPL_TYPE_INT, plist, CPL_IO_EXTEND);
1639 //cpl_image_save(err, outname, CPL_TYPE_INT, plist, CPL_IO_EXTEND);
1640
1641 cpl_free(outname);
1642 cpl_propertylist_delete(plist);
1643
1644 /* clean memory */
1645 cpl_image_delete(ima);
1646 hdrl_image_delete(hima);
1647 return cpl_error_get_code();
1648}
1649
1650
1651static cpl_error_code test_hdrl_mode_bootstrap_exec(cpl_image * ima_in,
1652 cpl_size iteration,
1653 double * mode_median,
1654 double * mode_median_error,
1655 double * mode_weight,
1656 double * mode_weight_error,
1657 double * mode_fit,
1658 double * mode_fit_error,
1659 hdrl_random_state * state)
1660{
1661
1662 cpl_size sx = cpl_image_get_size_x(ima_in);
1663 cpl_size sy = cpl_image_get_size_x(ima_in);
1664 //cpl_type type = cpl_image_get_type(ima_in);
1665 cpl_image* ima_simul = cpl_image_new(sx, sy, CPL_TYPE_INT);
1666 int* pima_in = cpl_image_get_data_int(ima_in);
1667 int* pima_simul = cpl_image_get_data_int(ima_simul);
1668
1669 //hdrl_random_state * state = hdrl_random_state_new(1, NULL);
1670 const cpl_size N = sx * sy;
1671
1672 for (cpl_size i = 0; i < N; i++) {
1673 pima_simul[i] = pima_in[(cpl_size)hdrl_random_uniform_int64(state, 0, N - 1)];
1674 //cpl_size index = (cpl_size)(rand_0_to_1() * ((N-1) - 0) + 0);
1675 //pima_simul[i] = pima_in[index];
1676 //cpl_size upper = N - 1 ;
1677 //cpl_size lower = 0;
1678 //cpl_size index = (rand() % (upper - lower + 1)) + lower;
1679 //pima_simul[i] = pima_in[index];
1680 }
1681
1682 double mean = cpl_image_get_mean(ima_simul);
1683 double median = cpl_image_get_median(ima_simul);
1684 double stdev = cpl_image_get_stdev(ima_simul);
1685
1686 cpl_msg_debug(cpl_func,"Bootstrap Simulated image:");
1687 cpl_msg_debug(cpl_func,"Bootstrap mean: %g, median: %g, stdev: %g",
1688 mean, median, stdev);
1689
1690 hdrl_image* hima = hdrl_image_create(ima_simul, NULL);
1691 double mode_loc = 0.;
1692 double mode_error_loc = 0.;
1693 cpl_size naccepted = 0;
1694 //--------------------------------------------------------------------
1695 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1696 HDRL_MODE_MEDIAN, 0, &mode_loc, &mode_error_loc,&naccepted);
1697
1698 cpl_test_error(CPL_ERROR_NONE);
1699
1700 /* Fill the final mode and error */
1701 *mode_median = mode_loc;
1702 *mode_median_error = mode_error_loc;
1703 //--------------------------------------------------------------------
1704
1705 //--------------------------------------------------------------------
1706 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1707 HDRL_MODE_WEIGHTED, 0, &mode_loc, &mode_error_loc,&naccepted);
1708
1709
1710 cpl_test_error(CPL_ERROR_NONE);
1711
1712 /* Fill the final mode and error */
1713 *mode_weight = mode_loc;
1714 *mode_weight_error = mode_error_loc;
1715 //--------------------------------------------------------------------
1716
1717 //--------------------------------------------------------------------
1718 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 0,
1719 HDRL_MODE_FIT, 0, &mode_loc, &mode_error_loc,&naccepted);
1720 if(cpl_error_get_code() != CPL_ERROR_NONE) {
1721 /* previous call may return error if fit method fail check conditions
1722 * in this case we reset error to CPL_ERROR_NONE */
1723 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
1724 cpl_error_reset();
1725 }
1726 /* Fill the final mode and error */
1727 *mode_fit = mode_loc;
1728 *mode_fit_error = mode_error_loc;
1729 //--------------------------------------------------------------------
1730
1731 cpl_propertylist * plist = cpl_propertylist_new();
1732 cpl_propertylist_append_double(plist, "ESO QC MEAN" , mean);
1733 cpl_propertylist_append_double(plist, "ESO QC MEDIAN", median);
1734 cpl_propertylist_append_double(plist, "ESO QC STDEV" , stdev);
1735
1736 cpl_propertylist_append_double(plist, "ESO QC MODE MEDIAN", *mode_median);
1737 cpl_propertylist_append_double(plist, "ESO QC MODE WEIGHT", *mode_weight);
1738 cpl_propertylist_append_double(plist, "ESO QC MODE FIT" , *mode_fit);
1739 cpl_propertylist_append_double(plist, "ESO QC MODE MEDIAN ERR", *mode_median_error);
1740 cpl_propertylist_append_double(plist, "ESO QC MODE WEIGHT ERR", *mode_weight_error);
1741 cpl_propertylist_append_double(plist, "ESO QC MODE FIT ERR" , *mode_fit_error);
1742
1743 char * outname = cpl_sprintf("Simulation_Bootstrap_mode_iter%04d.fits",
1744 (int)iteration);
1745
1746 /* Save the image */
1747 //cpl_propertylist_save(plist,outname,CPL_IO_CREATE);
1748 //cpl_image_save(ima_simul, outname, CPL_TYPE_INT, plist, CPL_IO_EXTEND);
1749 //cpl_image_save(err, outname, CPL_TYPE_INT, plist, CPL_IO_EXTEND);
1750
1751 cpl_free(outname);
1752 cpl_propertylist_delete(plist);
1753
1754 /* clean memory */
1755 //hdrl_random_state_delete(state);
1756 cpl_image_delete(ima_simul);
1757 hdrl_image_delete(hima);
1758 return cpl_error_get_code();
1759}
1760
1761static cpl_error_code
1762test_hdrl_mode_general_montecarlo(double expected, cpl_size iterations,
1763 hdrl_random_state * state, double sigfactor,
1764 double relsigfactor, int savetodisk)
1765{
1766 double mode_median = 0.;
1767 double mode_median_error = 0.;
1768 double mode_weight = 0.;
1769 double mode_weight_error = 0.;
1770 double mode_fit = 0.;
1771 double mode_fit_error = 0.;
1772
1773 /* Simulate images with a poissonian flux distribution and save the results
1774 * into a table */
1775
1776 cpl_table * tab = cpl_table_new(iterations);
1777 cpl_table_new_column(tab, "lambda", CPL_TYPE_DOUBLE);
1778 cpl_table_new_column(tab, "mode_median", CPL_TYPE_DOUBLE);
1779 cpl_table_new_column(tab, "mode_median_error", CPL_TYPE_DOUBLE);
1780 cpl_table_new_column(tab, "mode_weight", CPL_TYPE_DOUBLE);
1781 cpl_table_new_column(tab, "mode_weight_error", CPL_TYPE_DOUBLE);
1782 cpl_table_new_column(tab, "mode_fit", CPL_TYPE_DOUBLE);
1783 cpl_table_new_column(tab, "mode_fit_error", CPL_TYPE_DOUBLE);
1784
1785 for (cpl_size i = 0; i < iterations; i++) {
1786 test_hdrl_mode_montecarlo_exec(expected,
1787 i,
1788 &mode_median,
1789 &mode_median_error,
1790 &mode_weight,
1791 &mode_weight_error,
1792 &mode_fit,
1793 &mode_fit_error,
1794 state);
1795 cpl_table_set_double(tab, "lambda", i, expected);
1796 cpl_table_set_double(tab, "mode_median", i, mode_median);
1797 cpl_table_set_double(tab, "mode_median_error", i, mode_median_error);
1798 cpl_table_set_double(tab, "mode_weight", i, mode_weight);
1799 cpl_table_set_double(tab, "mode_weight_error", i, mode_weight_error);
1800 cpl_table_set_double(tab, "mode_fit", i, mode_fit);
1801 cpl_table_set_double(tab, "mode_fit_error", i, mode_fit_error);
1802
1803 }
1804
1805 /* Check if the calculated values are compatible with the expectations
1806 * within sigfactor * standard-deviation of the the calculated errorbars */
1807
1808 cpl_test_abs(
1809 cpl_table_get_column_median(tab, "mode_median"), expected,
1810 cpl_table_get_column_stdev (tab, "mode_median") * sigfactor);
1811 cpl_test_abs(
1812 cpl_table_get_column_median(tab, "mode_weight"), expected,
1813 cpl_table_get_column_stdev (tab, "mode_weight") * sigfactor);
1814 cpl_test_abs(
1815 cpl_table_get_column_median(tab, "mode_fit"), expected,
1816 cpl_table_get_column_stdev (tab, "mode_fit") * sigfactor);
1817
1818 /* Check if the calculated standard deviation is not to large and in the
1819 * order of 1 percent */
1820 cpl_msg_debug(cpl_func, "lambda: %g", expected);
1821 double relerr_median = cpl_table_get_column_stdev (tab, "mode_median")
1822 / expected;
1823 double relerr_weight = cpl_table_get_column_stdev (tab, "mode_weight")
1824 / expected;
1825 double relerr_fit = cpl_table_get_column_stdev (tab, "mode_fit")
1826 / expected;
1827
1828 cpl_test(relerr_median < relsigfactor);
1829 cpl_test(relerr_weight < relsigfactor);
1830 //TODO: the following fails occasionally
1831 cpl_test(relerr_fit < relsigfactor);
1832
1833 if(savetodisk) {
1834 cpl_propertylist * plist = cpl_propertylist_new();
1835 hdrl_write_qc (plist, tab);
1836 char * outname = cpl_sprintf("Simultable_mode_%08d_montecarlo.fits",
1837 (int)expected);
1838 cpl_table_save(tab, plist, NULL, outname, CPL_IO_CREATE);
1839 cpl_free(outname);
1840 cpl_propertylist_delete(plist);
1841 }
1842
1843 cpl_table_delete(tab);
1844
1845 return cpl_error_get_code();
1846}
1847
1848static cpl_error_code
1849test_hdrl_mode_general_bootstrap(double expected, cpl_size iterations,
1850 hdrl_random_state * state, double sigfactor,
1851 double relsigfactor, int savetodisk)
1852{
1853
1854 double mode_median = 0.;
1855 double mode_median_error = 0.;
1856 double mode_weight = 0.;
1857 double mode_weight_error = 0.;
1858 double mode_fit = 0.;
1859 double mode_fit_error = 0.;
1860
1861 cpl_size sx = 500;
1862 cpl_size sy = 500;
1863 cpl_image* ima = cpl_image_new(sx, sy, CPL_TYPE_INT);
1864 int* pdata = cpl_image_get_data(ima);
1865 cpl_size size = sx * sy;
1866
1867 /* Simulate an image with a poissonian flux distribution */
1868 for(cpl_size i = 0; i < size; i++) {
1869 pdata[i] = (int)hdrl_random_poisson(state, expected);
1870 }
1871
1872 /* Derive new images by using the bootstrap method and save the results into
1873 * a table */
1874
1875 cpl_table * tab = cpl_table_new(iterations);
1876 cpl_table_new_column(tab, "lambda", CPL_TYPE_DOUBLE);
1877 cpl_table_new_column(tab, "mode_median", CPL_TYPE_DOUBLE);
1878 cpl_table_new_column(tab, "mode_median_error", CPL_TYPE_DOUBLE);
1879 cpl_table_new_column(tab, "mode_weight", CPL_TYPE_DOUBLE);
1880 cpl_table_new_column(tab, "mode_weight_error", CPL_TYPE_DOUBLE);
1881 cpl_table_new_column(tab, "mode_fit", CPL_TYPE_DOUBLE);
1882 cpl_table_new_column(tab, "mode_fit_error", CPL_TYPE_DOUBLE);
1883
1884 for (cpl_size i = 0; i < iterations; i++) {
1885 test_hdrl_mode_bootstrap_exec(ima, i,
1886 &mode_median, &mode_median_error,
1887 &mode_weight, &mode_weight_error,
1888 &mode_fit, &mode_fit_error,
1889 state);
1890
1891 cpl_table_set_double(tab, "lambda", i, expected);
1892 cpl_table_set_double(tab, "mode_median", i, mode_median);
1893 cpl_table_set_double(tab, "mode_median_error", i, mode_median_error);
1894 cpl_table_set_double(tab, "mode_weight", i, mode_weight);
1895 cpl_table_set_double(tab, "mode_weight_error", i, mode_weight_error);
1896 cpl_table_set_double(tab, "mode_fit", i, mode_fit);
1897 cpl_table_set_double(tab, "mode_fit_error", i, mode_fit_error);
1898 }
1899 cpl_test_error(CPL_ERROR_NONE);
1900
1901 /* Check if the calculated values are compatible with the expectations
1902 * within sigfactor * standard-deviation of the the calculated errorbars */
1903
1904 cpl_test_abs(
1905 cpl_table_get_column_median(tab, "mode_median"), expected,
1906 cpl_table_get_column_stdev (tab, "mode_median") * sigfactor);
1907 cpl_test_abs(
1908 cpl_table_get_column_median(tab, "mode_weight"), expected,
1909 cpl_table_get_column_stdev (tab, "mode_weight") * sigfactor);
1910 cpl_test_abs(
1911 cpl_table_get_column_median(tab, "mode_fit"), expected,
1912 cpl_table_get_column_stdev (tab, "mode_fit") * sigfactor);
1913
1914 /* Check if the calculated standard deviation is not to large and in the
1915 * order of 1 percent */
1916
1917 cpl_msg_debug(cpl_func, "lambda: %g", expected);
1918 double relerr_median = cpl_table_get_column_stdev (tab, "mode_median")
1919 / expected;
1920 double relerr_weight = cpl_table_get_column_stdev (tab, "mode_weight")
1921 / expected;
1922 double relerr_fit = cpl_table_get_column_stdev (tab, "mode_fit")
1923 / expected;
1924
1925 cpl_test(relerr_median < relsigfactor);
1926 cpl_test(relerr_weight < relsigfactor);
1927 cpl_test(relerr_fit < relsigfactor);
1928
1929 if(savetodisk) {
1930 cpl_propertylist * plist = cpl_propertylist_new();
1931 hdrl_write_qc (plist, tab);
1932 char * outname = cpl_sprintf("Simultable_mode_%08d_bootstrap.fits",
1933 (int)expected);
1934 cpl_table_save(tab, plist, NULL, outname, CPL_IO_CREATE);
1935 cpl_free(outname);
1936 cpl_propertylist_delete(plist);
1937 }
1938
1939 cpl_image_delete(ima);
1940 cpl_table_delete(tab);
1941 return cpl_error_get_code();
1942}
1943
1944
1945static cpl_error_code
1946test_hdrl_mode_bootstrap_results (double lambda, cpl_size iterations,
1947 hdrl_mode_type method, double binsize,
1948 double expected_error,
1949 double sigfactor)
1950{
1951
1952 cpl_size vec_size = 250000;
1953 cpl_vector * vec = cpl_vector_new(vec_size);
1954 double * pvec = cpl_vector_get_data(vec);
1955
1956 uint64_t seed[2] = {(uint64_t)1804289383, (uint64_t)846930886};
1957 hdrl_random_state * state = hdrl_random_state_new(1, seed);
1958
1959 /* Simulate a vector with a poissonian flux distribution */
1960 for(cpl_size i = 0; i < vec_size; i++) {
1961 pvec[i] = (double)hdrl_random_poisson(state, lambda);
1962 }
1963 hdrl_random_state_delete(state);
1964
1965 double mode_error = 0.;
1966
1967 hdrl_mode_bootstrap(vec, 10, 1, binsize, method, iterations,
1968 &mode_error);
1969 cpl_test_rel(expected_error, mode_error, sigfactor);
1970
1971 /*
1972 if(method == HDRL_MODE_MEDIAN){
1973 cpl_msg_debug(cpl_func, "MEDIAN: vector-mean: %.2g, mode-error: %g",
1974 cpl_vector_get_mean(vec), mode_error);
1975 } else if (method == HDRL_MODE_WEIGHTED) {
1976 cpl_msg_debug(cpl_func, "WEIGHTED: vector-mean: %.2g, mode-error: %g",
1977 cpl_vector_get_mean(vec), mode_error);
1978 } else {
1979 cpl_msg_debug(cpl_func, "FIT: vector-mean: %.2g, mode-error: %g",
1980 cpl_vector_get_mean(vec), mode_error);
1981 }
1982 */
1983
1984 cpl_vector_delete(vec);
1985
1986 return cpl_error_get_code();
1987}
1988static cpl_error_code
1989test_hdrl_mode_bootstrap_stability (double lambda, cpl_size iterations1,
1990 cpl_size iterations2,
1991 hdrl_mode_type method,
1992 double binsize,
1993 double difference)
1994{
1995
1996 cpl_size vec_size = 250000;
1997 cpl_vector * vec = cpl_vector_new(vec_size);
1998 double * pvec = cpl_vector_get_data(vec);
1999
2000 uint64_t seed[2] = {(uint64_t)1804289383, (uint64_t)846930886};
2001 hdrl_random_state * state = hdrl_random_state_new(1, seed);
2002
2003 /* Simulate a vector with a poissonian flux distribution */
2004 for(cpl_size i = 0; i < vec_size; i++) {
2005 pvec[i] = (double)hdrl_random_poisson(state, lambda);
2006 }
2007 hdrl_random_state_delete(state);
2008
2009 double mode_error1 = 0.;
2010 double mode_error2 = 0.;
2011
2012 hdrl_mode_bootstrap(vec, 10, 1, binsize, method, iterations1,
2013 &mode_error1);
2014 hdrl_mode_bootstrap(vec, 10, 1, binsize, method, iterations2,
2015 &mode_error2);
2016 cpl_test_rel(mode_error1, mode_error2, difference);
2017
2018 /*
2019 if(method == HDRL_MODE_MEDIAN){
2020 cpl_msg_debug(cpl_func, "MEDIAN: vector-mean: %.2g, mode-error1: %g,"
2021 " mode-error2: %g, ratio: %g",
2022 cpl_vector_get_mean(vec), mode_error1, mode_error2, mode_error1/mode_error2);
2023 } else if (method == HDRL_MODE_WEIGHTED) {
2024 cpl_msg_debug(cpl_func, "WEIGHTED: vector-mean: %.2g, mode-error1: %g, "
2025 "mode-error2: %g, ratio: %g",
2026 cpl_vector_get_mean(vec), mode_error1, mode_error2, mode_error1/mode_error2);
2027 } else if (method == HDRL_MODE_FIT) {
2028 cpl_msg_debug(cpl_func, "FIT: vector-mean: %.2g, mode-error1: %g, "
2029 "mode-error2: %g, ratio: %g",
2030 cpl_vector_get_mean(vec), mode_error1, mode_error2, mode_error1/mode_error2);
2031 } else {
2032 cpl_msg_debug(cpl_func,"wrong");
2033 }
2034 */
2035
2036 cpl_vector_delete(vec);
2037
2038 return cpl_error_get_code();
2039}
2040
2041static cpl_error_code test_hdrl_mode_image_threevalues(void)
2042{
2043 cpl_size nx = 12;
2044 cpl_image* ima = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
2045 cpl_image_set(ima, 1, 1, 1);
2046 cpl_image_set(ima, 2, 1, 2);
2047 cpl_image_set(ima, 3, 1, 2);
2048 cpl_image_set(ima, 4, 1, 3);
2049 cpl_image_set(ima, 5, 1, 3);
2050 cpl_image_set(ima, 6, 1, 3);
2051 cpl_image_set(ima, 7, 1, 3);
2052 cpl_image_set(ima, 8, 1, 3);
2053 cpl_image_set(ima, 9, 1, 3);
2054 cpl_image_set(ima, 10, 1, 4);
2055 cpl_image_set(ima, 11, 1, 4);
2056 cpl_image_set(ima, 12, 1, 5);
2057
2058 cpl_image* err = cpl_image_duplicate(ima);
2059 cpl_image_power(err, 0.5);
2060 hdrl_image* hima = hdrl_image_create(ima, err);
2061 double mode = 0;
2062 double mode_err = 0;
2063
2064 cpl_size naccepted = 0;
2065
2066 /* Symmetric histogram */
2067 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 1,
2068 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
2069 cpl_msg_warning(cpl_func, "HDRL_MODE_FIT: %g, %g", mode, mode_err);
2070 cpl_test_abs(mode, 3.0, HDRL_EPS_TEST);
2071 cpl_test_error(CPL_ERROR_NONE);
2072
2073 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 1,
2074 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
2075 cpl_msg_warning(cpl_func, "HDRL_MODE_MEDIAN: %g, %g", mode, mode_err);
2076 cpl_test_abs(mode, 3.0, HDRL_EPS_TEST);
2077 cpl_test_error(CPL_ERROR_NONE);
2078
2079 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 1,
2080 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
2081 cpl_msg_warning(cpl_func, "HDRL_MODE_WEIGHTED: %g, %g", mode, mode_err);
2082 cpl_test_abs(mode, 3.0, HDRL_EPS_TEST);
2083 cpl_test_error(CPL_ERROR_NONE);
2084
2085
2086 /* Asymmetric histogram */
2087
2088 /* Was cpl_image_set(ima, 10, 1, 4); */
2089 hdrl_image_set_pixel(hima, 10, 1, (hdrl_value){2.0, sqrt(2.)});
2090
2091 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 1,
2092 HDRL_MODE_FIT, 0, &mode, &mode_err,&naccepted);
2093 cpl_msg_warning(cpl_func, "HDRL_MODE_FIT: %g, %g", mode, mode_err);
2094 cpl_test(mode < 3.);
2095 cpl_test_error(CPL_ERROR_NONE);
2096
2097 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 1,
2098 HDRL_MODE_MEDIAN, 0, &mode, &mode_err,&naccepted);
2099 cpl_msg_warning(cpl_func, "HDRL_MODE_MEDIAN: %g, %g", mode, mode_err);
2100 cpl_test_abs(mode, 3.0, HDRL_EPS_TEST);
2101 cpl_test_error(CPL_ERROR_NONE);
2102
2103 hdrl_mode_clip_image(hdrl_image_get_image_const(hima), 0, -1, 1,
2104 HDRL_MODE_WEIGHTED, 0, &mode, &mode_err,&naccepted);
2105 cpl_msg_warning(cpl_func, "HDRL_MODE_WEIGHTED: %g, %g", mode, mode_err);
2106 cpl_test(mode < 3.);
2107 cpl_test_error(CPL_ERROR_NONE);
2108
2109 cpl_image_delete(err);
2110 cpl_image_delete(ima);
2111 hdrl_image_delete(hima);
2112 return cpl_error_get_code();
2113}
2114
2115/*----------------------------------------------------------------------------*/
2119/*----------------------------------------------------------------------------*/
2120int main(void)
2121{
2122 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
2123 test_hdrl_mode_vector_one_value();
2124 test_hdrl_mode_image_one_value();
2125 test_hdrl_mode_image_threevalues();
2126
2127 test_hdrl_mode_parameter_create_parlist();
2128 test_hdrl_mode_parameter_parse_parlist();
2129 test_hdrl_mode_nogoodpixels();
2130 test_hdrl_mode_onevalue();
2131 test_hdrl_mode_median();
2132 test_hdrl_mode_asymm();
2133 test_hdrl_mode_test1();
2134 test_hdrl_mode_test1r();
2135 test_hdrl_mode_test2();
2136 test_hdrl_mode_test2r();
2137 test_hdrl_mode_test3();
2138 test_hdrl_mode_test3r();
2139 test_hdrl_mode_test4();
2140 test_hdrl_mode_test4r();
2141
2142
2143 /* To ensure new random numbers each time the compiled program is called,
2144 * call srand(), e.g.
2145 * srand(time(NULL)) */
2146
2147 hdrl_random_state * state = NULL;
2148 uint64_t seed[2] = {(uint64_t)1804289383, (uint64_t)846930886};
2149
2150
2151 state = hdrl_random_state_new(1, seed);
2152 test_hdrl_mode(state, 0);
2153 hdrl_random_state_delete(state);
2154
2155 //lambda = 10000
2156
2157 state = hdrl_random_state_new(1, seed);
2158 test_hdrl_mode_general_montecarlo(10000., 100, state, 1.0, 0.01, 0);
2159 hdrl_random_state_delete(state);
2160
2161 state = hdrl_random_state_new(1, seed);
2162 test_hdrl_mode_general_bootstrap (10000., 100, state, 1.0, 0.01, 0);
2163 hdrl_random_state_delete(state);
2164
2165
2166 // lambda = 10.000
2167
2168 test_hdrl_mode_bootstrap_results (10000., 200, HDRL_MODE_MEDIAN, 10., 6.0, 0.30);
2169 test_hdrl_mode_bootstrap_results (10000., 200, HDRL_MODE_FIT, 10., 3.0, 0.30);
2170 test_hdrl_mode_bootstrap_results (10000., 200, HDRL_MODE_WEIGHTED, 10., 5.0, 0.30);
2171
2172#if defined HDRL_UNITTESTS_SLOW
2173 //lambda = 1000
2174 test_hdrl_mode_bootstrap_stability (1000, 800, 200, HDRL_MODE_MEDIAN, 3., 0.35);
2175 test_hdrl_mode_bootstrap_stability (1000, 800, 200, HDRL_MODE_FIT, 3., 0.35);
2176 test_hdrl_mode_bootstrap_stability (1000, 800, 200, HDRL_MODE_WEIGHTED, 3., 0.35);
2177
2178 //lambda = 10.000
2179 test_hdrl_mode_bootstrap_stability (10000, 800, 200, HDRL_MODE_MEDIAN, 10., 0.35);
2180 test_hdrl_mode_bootstrap_stability (10000, 800, 200, HDRL_MODE_FIT, 10., 0.35);
2181 test_hdrl_mode_bootstrap_stability (10000, 800, 200, HDRL_MODE_WEIGHTED, 10., 0.35);
2182#endif
2183
2184 //lambda = 10.000
2185 test_hdrl_mode_bootstrap_stability (10000, 400, 100, HDRL_MODE_MEDIAN, 10., 0.35);
2186 test_hdrl_mode_bootstrap_stability (10000, 400, 100, HDRL_MODE_FIT, 10., 0.35);
2187 test_hdrl_mode_bootstrap_stability (10000, 400, 100, HDRL_MODE_WEIGHTED, 10., 0.35);
2188
2189#if defined HDRL_UNITTESTS_SLOW
2190 //lambda = 100.000
2191 test_hdrl_mode_bootstrap_stability (100000, 800, 200, HDRL_MODE_MEDIAN, 30., 0.35);
2192 test_hdrl_mode_bootstrap_stability (100000, 800, 200, HDRL_MODE_FIT, 30., 0.35);
2193 test_hdrl_mode_bootstrap_stability (100000, 800, 200, HDRL_MODE_WEIGHTED, 30., 0.35);
2194#endif
2195
2196
2197 return cpl_test_end(0);
2198}
hdrl_parameter * hdrl_collapse_mode_parameter_create(double histo_min, double histo_max, double bin_size, hdrl_mode_type mode_method, cpl_size error_niter)
create a parameter object for the mode
cpl_error_code hdrl_image_set_pixel(hdrl_image *self, cpl_size xpos, cpl_size ypos, hdrl_value value)
set pixel values of hdrl_image
Definition: hdrl_image.c:594
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:157
hdrl_image * hdrl_image_create(const cpl_image *image, const cpl_image *error)
create a new hdrl_image from to existing images by copying them
Definition: hdrl_image.c:295
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:118
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter