CR2RE Pipeline Reference Manual 1.6.10
hdrl_sigclip-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
28#include "hdrl_sigclip.h"
29#include "hdrl_utils.h"
30#include <cpl.h>
31
32#include <math.h>
33#include <stdlib.h>
34
35#ifndef ARRAY_LEN
36#define ARRAY_LEN(a) sizeof((a))/sizeof((a)[0])
37#endif
38
39#ifndef SQR
40#define SQR(a) ((a) * (a))
41#endif
42
43
44/*----------------------------------------------------------------------------*/
48/*----------------------------------------------------------------------------*/
49static cpl_error_code hdrl_clip_kappa_sigma_test(void)
50{
51 double omean, omean_err, rej_low, rej_high;
52 cpl_size naccepted;
53
54 {
55 double * dpixels = cpl_calloc(9, sizeof(double));
56
57 cpl_vector * data = cpl_vector_wrap(9, dpixels);
58 cpl_vector * errors = cpl_vector_new(9);
59 cpl_vector_fill(errors, 1);
60
61 /* null optional out params */
62 hdrl_kappa_sigma_clip(data, errors, 3, 3, 3, CPL_FALSE,
63 &omean, NULL, NULL, NULL, NULL);
64 cpl_test_error(CPL_ERROR_NONE);
65
66 cpl_test_eq(omean, 0.);
67
68 /* all out params NULL makes no sense */
69 hdrl_kappa_sigma_clip(data, errors, 3, 3, 3, CPL_FALSE,
70 NULL, NULL, NULL, NULL, NULL);
71 cpl_test_error(CPL_ERROR_NULL_INPUT);
72
73 /* NULL data */
74 hdrl_kappa_sigma_clip(NULL, errors, 3, 3, 3, CPL_FALSE,
75 &omean, NULL, NULL, NULL, NULL);
76 cpl_test_error(CPL_ERROR_NULL_INPUT);
77
78 /* wrong iter */
79 hdrl_kappa_sigma_clip(data, errors, 3, 3, 0, CPL_TRUE,
80 &omean, NULL, NULL, NULL, NULL);
81 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
82
83 cpl_vector_delete(data);
84 cpl_vector_delete(errors);
85 }
86
87 {
88 double * dpixels = cpl_calloc(9, sizeof(double));
89
90 cpl_vector * data = cpl_vector_wrap(9, dpixels);
91 cpl_vector * errors = cpl_vector_new(9);
92 cpl_vector_fill(errors, 1);
93
94 hdrl_kappa_sigma_clip(data, errors, 3, 3, 3, CPL_FALSE,
95 &omean, &omean_err, &naccepted,
96 NULL, NULL);
97 cpl_test_error(CPL_ERROR_NONE);
98
99 cpl_test_eq(omean, 0.);
100 cpl_test_rel(omean_err, 1 / sqrt(9), 0.001);
101 cpl_test_eq(naccepted, 9);
102
103 cpl_vector_delete(data);
104 cpl_vector_delete(errors);
105 }
106
107 {
108 /* MAD sigma ~3 median 6, check that 1.5 and 10.5 which are closer to
109 * 6 -+ 3 * 1.5 than 4 and 6 but beyond the k*sig limit are not included */
110 double values[] = {1.5, 6., 4., 6., 4., 6., 4., 6., 10.5};
111 const int n = ARRAY_LEN(values);
112
113 cpl_vector * data = cpl_vector_wrap(n, values);
114 cpl_vector * errors = cpl_vector_new(n);
115 cpl_vector_fill(errors, 1);
116
117 hdrl_kappa_sigma_clip(data, errors, 1.5, 1.5, 1, CPL_FALSE,
118 &omean, &omean_err, &naccepted,
119 &rej_low, &rej_high);
120 cpl_test_error(CPL_ERROR_NONE);
121
122 cpl_test_rel(omean, (4. * 3 + 6 * 4.) / 7., 0.001);
123 cpl_test_rel(omean_err, 1 / sqrt(n - 2), 0.001);
124 cpl_test_rel(rej_low, 1.55, 0.02);
125 cpl_test_rel(rej_high, 10.44, 0.02);
126 cpl_test_eq(naccepted, n - 2);
127
128 cpl_vector_unwrap(data);
129 cpl_vector_delete(errors);
130 }
131
132 {
133 /* special case of one remaining pixels */
134 double values[] = {10.};
135 const int n = ARRAY_LEN(values);
136
137 cpl_vector * data = cpl_vector_wrap(n, values);
138 cpl_vector * errors = cpl_vector_new(n);
139 cpl_vector_fill(errors, 1);
140
141 hdrl_kappa_sigma_clip(data, errors, 2., 2., 1, CPL_FALSE,
142 &omean, &omean_err, &naccepted,
143 &rej_low, &rej_high);
144 cpl_test_error(CPL_ERROR_NONE);
145
146 cpl_test_rel(omean, values[0], 0.001);
147 cpl_test_rel(omean_err, 1, 0.001);
148 cpl_test_rel(rej_low, values[0], 0.02);
149 cpl_test_rel(rej_high, values[0], 0.02);
150 cpl_test_eq(naccepted, n);
151
152 cpl_vector_unwrap(data);
153 cpl_vector_delete(errors);
154 }
155
156 {
157 /* gaus mean 100 sigma 3.5 */
158 double values[] = {92, 93, 94, 94, 95, 95, 96, 96, 96, 97,
159 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
160 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
161 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
162 104, 105, 105, 106, 106, 107, 108 };
163 const int n = ARRAY_LEN(values);
164
165 cpl_vector * data = cpl_vector_wrap(n, values);
166 cpl_vector * errors = cpl_vector_new(n);
167 cpl_vector_fill(errors, 1);
168
169 /* kappa 2 included by iqr 92 and 108 */
170 hdrl_kappa_sigma_clip(data, errors, 2., 2., 3, CPL_FALSE,
171 &omean, &omean_err, &naccepted,
172 &rej_low, &rej_high);
173 cpl_test_error(CPL_ERROR_NONE);
174
175 cpl_test_rel(omean, 100., 0.001);
176 cpl_test_rel(omean_err, 1 / sqrt(n), 0.001);
177 /* sigma overestimated by iqr */
178 cpl_test_rel(rej_low, 91., 0.005);
179 cpl_test_rel(rej_high, 109, 0.005);
180 cpl_test_eq(naccepted, n);
181
182 cpl_vector_unwrap(data);
183 cpl_vector_delete(errors);
184 }
185
186 {
187 /* gaus mean 100 sigma 3.5, 2 sigma range, 2 outliers */
188 double values[] = {1, 150, 94, 94, 95, 95, 96, 96, 96, 97,
189 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
190 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
191 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
192 104, 105, 105, 106, 106, 107, 108 };
193 const int n = ARRAY_LEN(values);
194
195 cpl_vector * data = cpl_vector_wrap(n, values);
196 cpl_vector * errors = cpl_vector_new(n);
197 cpl_vector_fill(errors, 1);
198
199 hdrl_kappa_sigma_clip(data, errors, 3, 3, 3, CPL_FALSE,
200 &omean, &omean_err, &naccepted,
201 NULL, NULL);
202 cpl_test_error(CPL_ERROR_NONE);
203
204 cpl_test_rel(omean, 100., 0.005);
205 cpl_test_rel(omean_err, 1 / sqrt(n - 2), 0.001);
206 cpl_test_eq(naccepted, n - 2);
207
208 cpl_vector_unwrap(data);
209 cpl_vector_delete(errors);
210 }
211
212 /* test inputs are not modified */
213 {
214 double values[] = {54, 234. ,5,2, 343, 23 , 2, 0.21, 0.1232 , 1.2e3};
215 const int n = ARRAY_LEN(values);
216
217 cpl_vector * data = cpl_vector_wrap(n, values);
218 cpl_vector * errors = cpl_vector_duplicate(data);
219 cpl_vector * odata = cpl_vector_duplicate(data);
220 cpl_vector * oerrors = cpl_vector_duplicate(errors);
221
222 hdrl_kappa_sigma_clip(data, errors, 3, 3, 3, CPL_FALSE,
223 &omean, &omean_err, &naccepted,
224 NULL, NULL);
225 cpl_test_error(CPL_ERROR_NONE);
226
227 cpl_test_vector_abs(data, odata, FLT_EPSILON);
228 cpl_test_vector_abs(errors, oerrors, FLT_EPSILON);
229
230 cpl_vector_unwrap(data);
231 cpl_vector_delete(errors);
232 cpl_vector_delete(odata);
233 cpl_vector_delete(oerrors);
234 }
235
236
237 /* image test */
238 {
239 /* gaus mean 100 sigma 3.5, 2 sigma range, 2 outliers */
240 double values[] = {1, 150, 94, 94, 95, 95, 96, 96, 96, 97,
241 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
242 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
243 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
244 104, 105, 105, 106, 106, 107, 108, 100, 100};
245 const int n = ARRAY_LEN(values);
246
247 cpl_image * data = cpl_image_wrap(sqrt(n), sqrt(n), CPL_TYPE_DOUBLE,
248 values);
249 cpl_image * errors = cpl_image_new(sqrt(n), sqrt(n), CPL_TYPE_DOUBLE);
250 cpl_image_add_scalar(errors, 1);
251
252 hdrl_kappa_sigma_clip_image(data, errors, 3, 3, 3,
253 &omean, &omean_err, &naccepted,
254 NULL, NULL);
255 cpl_test_error(CPL_ERROR_NONE);
256
257 cpl_test_rel(omean, 100., 0.005);
258 cpl_test_rel(omean_err, 1 / sqrt(n - 2), 0.001);
259 cpl_test_eq(naccepted, n - 2);
260
261 cpl_image_unwrap(data);
262 cpl_image_delete(errors);
263 }
264
265 /* image test with bad pixels */
266 {
267 /* gaus mean 100 sigma 3.5, 2 sigma range, 2 outliers */
268 float values[] = {1, 150, 94, 94, 95, 95, 96, 96, 96, 97,
269 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
270 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
271 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
272 104, 105, 105, 106, 106, 107, 108, 100, 100};
273 const int n = sqrt(ARRAY_LEN(values));
274
275 cpl_image * data = cpl_image_wrap(n, n, CPL_TYPE_FLOAT, values);
276 cpl_image * errors = cpl_image_new(n, n, CPL_TYPE_FLOAT);
277 cpl_image_add_scalar(errors, 1);
278 /* set two bad pixels with really high error */
279 cpl_image_reject(data, n, n);
280 cpl_image_reject(data, n, n - 1);
281 cpl_image_set(errors, n, n, 2343.e30);
282 cpl_image_set(errors, n, n - 1, 2343.e30);
283 cpl_image_reject_from_mask(errors, cpl_image_get_bpm(data));
284
285 hdrl_kappa_sigma_clip_image(data, errors, 3, 3, 3,
286 &omean, &omean_err, &naccepted,
287 NULL, NULL);
288 cpl_test_error(CPL_ERROR_NONE);
289
290 cpl_test_rel(omean, 100., 0.005);
291 cpl_test_rel(omean_err, 1 / sqrt(n * n - 4), 0.001);
292 cpl_test_eq(naccepted, (n * n) - 4);
293
294 cpl_image_unwrap(data);
295 cpl_image_delete(errors);
296 }
297
298 /* test unequal bpms */
299 {
300 const int n = 5;
301 cpl_image * data = cpl_image_new(n, n, CPL_TYPE_FLOAT);
302 cpl_image * errors = cpl_image_new(n, n, CPL_TYPE_FLOAT);
303 cpl_image_reject(data, n, n);
304 cpl_image_reject(data, n, n - 1);
305
306 hdrl_kappa_sigma_clip_image(data, errors, 3, 3, 3,
307 &omean, &omean_err, &naccepted,
308 NULL, NULL);
309 cpl_test_error(CPL_ERROR_NONE);
310 cpl_image_delete(data);
311 cpl_image_delete(errors);
312 }
313
314 return cpl_error_get_code();
315}
316
317
318/*----------------------------------------------------------------------------*/
322/*----------------------------------------------------------------------------*/
323static cpl_error_code hdrl_clip_minmax_test(void)
324{
325 double omean, omean_err, nlow, nhigh, rej_low, rej_high;
326
327 cpl_size naccepted;
328
329 {
330 double * dpixels = cpl_calloc(9, sizeof(double));
331
332 cpl_vector * data = cpl_vector_wrap(9, dpixels);
333 cpl_vector * errors = cpl_vector_new(9);
334 cpl_vector_fill(errors, 1);
335
336 /* null optional out params */
337 hdrl_minmax_clip(data, errors, 3, 3, CPL_FALSE,
338 &omean, NULL, NULL, NULL, NULL);
339 cpl_test_error(CPL_ERROR_NONE);
340
341 cpl_test_eq(omean, 0.);
342
343 /* all out params NULL makes no sense */
344 hdrl_minmax_clip(data, errors, 3, 3, CPL_FALSE,
345 NULL, NULL, NULL, NULL, NULL);
346 cpl_test_error(CPL_ERROR_NULL_INPUT);
347
348 /* NULL data */
349 hdrl_minmax_clip(NULL, errors, 3, 3, CPL_TRUE,
350 &omean, NULL, NULL, NULL, &rej_high);
351 cpl_test_error(CPL_ERROR_NULL_INPUT);
352
353
354 cpl_vector_delete(data);
355 cpl_vector_delete(errors);
356 }
357
358 {
359 double * dpixels = cpl_calloc(9, sizeof(double));
360 cpl_vector * data = cpl_vector_wrap(9, dpixels);
361 cpl_vector * errors = cpl_vector_new(9);
362 cpl_vector_fill(errors, 1);
363 nlow = 3;
364 nhigh = 3;
365 hdrl_minmax_clip(data, errors, nlow, nhigh, CPL_FALSE,
366 &omean, &omean_err, &naccepted,
367 &rej_low, &rej_high);
368 cpl_test_error(CPL_ERROR_NONE);
369
370 cpl_test_eq(omean, 0.);
371 cpl_test_rel(omean_err, 1 / sqrt(9 - (nlow + nhigh)), 0.001);
372 cpl_test_eq(naccepted, 9 - (nlow + nhigh));
373 cpl_test_eq(rej_low, 0.);
374 cpl_test_eq(rej_high, 0.);
375
376 cpl_vector_delete(data);
377 cpl_vector_delete(errors);
378 }
379
380
381 {
382 /* special case of one remaining pixels */
383 double values[] = {10.};
384 const int n = ARRAY_LEN(values);
385
386 cpl_vector * data = cpl_vector_wrap(n, values);
387 cpl_vector * errors = cpl_vector_new(n);
388 cpl_vector_fill(errors, 1);
389 nlow = 1;
390 nhigh = 1;
391 hdrl_minmax_clip(data, errors, nlow, nhigh, CPL_FALSE,
392 &omean, &omean_err, &naccepted,
393 &rej_low, NULL);
394 cpl_test_error(CPL_ERROR_NONE);
395
396 cpl_test(isnan(omean));
397 cpl_test(isnan(omean_err));
398 cpl_test_eq(naccepted, 0);
399 cpl_test_eq(rej_low, 0); /* invalid */
400
401 cpl_vector_unwrap(data);
402 cpl_vector_delete(errors);
403 }
404
405 {
406 /* gaus mean 100 sigma 3.5 - special case - don't reject anything */
407 double values[] = {92, 93, 94, 94, 95, 95, 96, 96, 96, 97,
408 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
409 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
410 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
411 104, 105, 105, 106, 106, 107, 108 };
412 const int n = ARRAY_LEN(values);
413
414 cpl_vector * data = cpl_vector_wrap(n, values);
415 cpl_vector * errors = cpl_vector_new(n);
416 cpl_vector_fill(errors, 1);
417 nlow = 0;
418 nhigh = 0;
419
420 hdrl_minmax_clip(data, errors, nlow, nhigh, CPL_FALSE,
421 &omean, &omean_err, &naccepted,
422 &rej_low, &rej_high);
423 cpl_test_error(CPL_ERROR_NONE);
424
425 cpl_test_rel(omean, 100., n * HDRL_EPS_DATA);
426 cpl_test_rel(omean_err, 1 / sqrt(n -(nlow + nhigh)), 0.001);
427 cpl_test_eq(rej_low, 92.);
428 cpl_test_eq(rej_high, 108);
429 cpl_test_eq(naccepted, n - (nlow + nhigh));
430
431 cpl_vector_unwrap(data);
432 cpl_vector_delete(errors);
433 }
434
435 {
436 /* gaus mean 100 sigma 3.5 */
437 double values[] = {92, 93, 94, 94, 95, 95, 96, 96, 96, 97,
438 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
439 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
440 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
441 104, 105, 105, 106, 106, 107, 108 };
442 const int n = ARRAY_LEN(values);
443
444 cpl_vector * data = cpl_vector_wrap(n, values);
445 cpl_vector * errors = cpl_vector_new(n);
446 cpl_vector_fill(errors, 1);
447 nlow = 2;
448 nhigh = 2;
449
450 hdrl_minmax_clip(data, errors, nlow, nhigh, CPL_FALSE,
451 &omean, &omean_err, &naccepted,
452 &rej_low, &rej_high);
453 cpl_test_error(CPL_ERROR_NONE);
454
455 cpl_test_rel(omean, 100., 0.001);
456 cpl_test_rel(omean_err, 1 / sqrt(n -(nlow + nhigh)), 0.001);
457 /* sigma overestimated by iqr */
458 cpl_test_eq(rej_low, 94.);
459 cpl_test_eq(rej_high, 106);
460 cpl_test_eq(naccepted, n - (nlow + nhigh));
461
462 cpl_vector_unwrap(data);
463 cpl_vector_delete(errors);
464 }
465
466 {
467 /* gaus mean 100 sigma 3.5, 2 sigma range, 2 outliers */
468 double values[] = {1, 150, 94, 94, 95, 95, 96, 96, 96, 97,
469 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
470 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
471 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
472 104, 105, 105, 106, 106, 107, 108 };
473 const int n = ARRAY_LEN(values);
474
475 cpl_vector * data = cpl_vector_wrap(n, values);
476 cpl_vector * errors = cpl_vector_new(n);
477 cpl_vector_fill(errors, 1);
478 nlow = 3;
479 nhigh = 3;
480 hdrl_minmax_clip(data, errors, nlow, nhigh, CPL_FALSE,
481 &omean, &omean_err, &naccepted,
482 NULL, &rej_high);
483 cpl_test_error(CPL_ERROR_NONE);
484
485 cpl_test_rel(omean, 100., 0.005);
486 cpl_test_rel(omean_err, 1 / sqrt(n - (nlow + nhigh)), 0.001);
487 cpl_test_eq(naccepted, n - (nlow + nhigh));
488
489 cpl_vector_unwrap(data);
490 cpl_vector_delete(errors);
491 }
492
493 /* test inputs are not modified */
494 {
495 double values[] = {54, 234. ,5,2, 343, 23 , 2, 0.21, 0.1232 , 1.2e3};
496 const int n = ARRAY_LEN(values);
497
498 cpl_vector * data = cpl_vector_wrap(n, values);
499 cpl_vector * errors = cpl_vector_duplicate(data);
500 cpl_vector * odata = cpl_vector_duplicate(data);
501 cpl_vector * oerrors = cpl_vector_duplicate(errors);
502 nlow = 3;
503 nhigh = 3;
504
505 hdrl_minmax_clip(data, errors, nlow, nhigh, CPL_FALSE,
506 &omean, &omean_err, &naccepted,
507 &rej_low, &rej_high);
508 cpl_test_error(CPL_ERROR_NONE);
509 cpl_test_eq(rej_low, 2.);
510 cpl_test_eq(rej_high, 54.);
511
512 cpl_test_vector_abs(data, odata, 0.);
513 cpl_test_vector_abs(errors, oerrors, 0.);
514
515 cpl_vector_unwrap(data);
516 cpl_vector_delete(errors);
517 cpl_vector_delete(odata);
518 cpl_vector_delete(oerrors);
519 }
520
521
522 /* image test */
523 {
524 /* gaus mean 100 sigma 3.5, 2 sigma range, 3 outliers */
525 double values[] = {1, 150, 94, 94, 95, 95, 96, 96, 96, 97,
526 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
527 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
528 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
529 104, 105, 105, 106, 106, 107, 108, 100, -1000};
530 const int n = ARRAY_LEN(values);
531
532 cpl_image * data = cpl_image_wrap(sqrt(n), sqrt(n), CPL_TYPE_DOUBLE,
533 values);
534 cpl_image * errors = cpl_image_new(sqrt(n), sqrt(n), CPL_TYPE_DOUBLE);
535 cpl_image_add_scalar(errors, 1);
536 nlow = 2;
537 nhigh = 1;
538
539 hdrl_minmax_clip_image(data, errors, nlow, nhigh,
540 &omean, &omean_err, &naccepted,
541 NULL, NULL);
542 cpl_test_error(CPL_ERROR_NONE);
543
544 cpl_test_rel(omean, 100., 0.005);
545 cpl_test_rel(omean_err, 1 / sqrt(n - (nlow + nhigh)), 0.001);
546 cpl_test_eq(naccepted, n - (nlow + nhigh));
547
548 cpl_image_unwrap(data);
549 cpl_image_delete(errors);
550 }
551
552 /* image test with bad pixels */
553 {
554 /* gaus mean 100 sigma 3.5, 2 sigma range, 2 outliers */
555 float values[] = {1, 150, 94, 94, 95, 95, 96, 96, 96, 97,
556 97, 97, 97, 98, 98, 98, 98, 99, 99, 99,
557 99, 100, 100, 100, 100, 100, 101, 101, 101, 101,
558 102, 102, 102, 102, 103, 103, 103, 103, 104, 104,
559 104, 105, 105, 106, 106, 107, 108, 100, 100};
560 const int n = sqrt(ARRAY_LEN(values));
561
562 cpl_image * data = cpl_image_wrap(n, n, CPL_TYPE_FLOAT, values);
563 cpl_image * errors = cpl_image_new(n, n, CPL_TYPE_FLOAT);
564 cpl_image_add_scalar(errors, 1);
565 nlow = 1;
566 nhigh = 1;
567
568 /* set two bad pixels with really high error */
569 cpl_image_reject(data, n, n);
570 cpl_image_reject(data, n, n - 1);
571 cpl_image_set(errors, n, n, 2343.e30);
572 cpl_image_set(errors, n, n - 1, 2343.e30);
573 cpl_image_reject_from_mask(errors, cpl_image_get_bpm(data));
574
575 hdrl_minmax_clip_image(data, errors, nlow, nhigh,
576 &omean, &omean_err, &naccepted,
577 NULL, NULL);
578 cpl_test_error(CPL_ERROR_NONE);
579
580
581 cpl_test_rel(omean, 100., 0.005);
582 cpl_test_rel(omean_err, 1 / sqrt(n * n - (nlow + nhigh + 2)), 0.001);
583 cpl_test_eq(naccepted, (n * n) - (nlow + nhigh + 2));
584
585 cpl_image_unwrap(data);
586 cpl_image_delete(errors);
587 }
588
589 /* test unequal bpms */
590 {
591 const int n = 5;
592 cpl_image * data = cpl_image_new(n, n, CPL_TYPE_FLOAT);
593 cpl_image * errors = cpl_image_new(n, n, CPL_TYPE_FLOAT);
594 cpl_image_reject(data, n, n);
595 cpl_image_reject(data, n, n - 1);
596 nlow = 3;
597 nhigh = 3;
598
599 hdrl_minmax_clip_image(data, errors, nlow, nhigh,
600 &omean, &omean_err, &naccepted,
601 NULL, NULL);
602 cpl_test_error(CPL_ERROR_NONE);
603 cpl_image_delete(data);
604 cpl_image_delete(errors);
605 }
606 /* image test equal range of rejected values */
607 {
608 float values[] = {-5., -5., -5., 1., 5., 1., 5., 1., 5., 5.};
609 float errors[] = {50., 500., 100., 1., 5., 1., 500., 1., 200.};
610 const int n = sqrt(ARRAY_LEN(values));
611
612 cpl_image * data = cpl_image_wrap(n, n, CPL_TYPE_FLOAT, values);
613 cpl_image * errs = cpl_image_wrap(n, n, CPL_TYPE_FLOAT, errors);
614 cpl_image * odata = cpl_image_duplicate(data);
615 cpl_image * oerrs = cpl_image_duplicate(errs);
616
617 hdrl_minmax_clip_image(data, errs, 2, 2,
618 &omean, &omean_err, &naccepted,
619 NULL, NULL);
620 cpl_test_error(CPL_ERROR_NONE);
621
622
623 cpl_test_rel(omean, 0.6, 10 * HDRL_EPS_DATA);
624 cpl_test_rel(omean_err, sqrt((SQR(50.) + 1. * 3. + SQR(5.)) /
625 SQR(n * n - 4)),
626 20 * HDRL_EPS_ERROR);
627 cpl_test_eq(naccepted, (n * n) - (4));
628
629 /* select multiple smallest errors, low*/
630 hdrl_minmax_clip_image(data, errs, 1, 2,
631 &omean, &omean_err, &naccepted,
632 NULL, NULL);
633 cpl_test_error(CPL_ERROR_NONE);
634
635 /* select multiple smallest errors, high */
636 hdrl_minmax_clip_image(data, errs, 2, 1,
637 &omean, &omean_err, &naccepted,
638 NULL, NULL);
639 cpl_test_error(CPL_ERROR_NONE);
640
641 cpl_test_rel(omean, 8. / 6., 10 * HDRL_EPS_DATA);
642 cpl_test_rel(omean_err,
643 sqrt((SQR(50.) + SQR(200.) + 1. * 3. + SQR(5.)) /
644 SQR(n * n - (3))),
645 20 * HDRL_EPS_ERROR);
646 cpl_test_eq(naccepted, (n * n) - (3));
647
648 /* select multiple smallest errors, low + high */
649 hdrl_minmax_clip_image(data, errs, 1, 1,
650 &omean, &omean_err, &naccepted,
651 NULL, NULL);
652 cpl_test_error(CPL_ERROR_NONE);
653
654 cpl_test_rel(omean, 3. / 7., 10 * HDRL_EPS_DATA);
655 cpl_test_rel(omean_err,
656 sqrt((SQR(50.) + SQR(100.) + SQR(200.) + 3. + SQR(5.)) /
657 SQR(n * n - 2)),
658 20 * HDRL_EPS_ERROR);
659 cpl_test_eq(naccepted, (n * n) - (2));
660
661 /* 50 error not in equal range so we should get the 100. error */
662 values[0] = -5.0001;
663 cpl_image_set(odata, 1, 1, values[0]);
664 hdrl_minmax_clip_image(data, errs, 2, 2,
665 &omean, &omean_err, &naccepted,
666 NULL, NULL);
667 cpl_test_error(CPL_ERROR_NONE);
668
669 cpl_test_rel(omean, 0.6, 10 * HDRL_EPS_DATA);
670 cpl_test_rel(omean_err, sqrt((SQR(100.) + 1. * 3. + SQR(5.)) /
671 SQR(n * n - 4)),
672 20 * HDRL_EPS_ERROR);
673 cpl_test_eq(naccepted, (n * n) - (4));
674
675 /* check original have not been overwritten */
676 cpl_test_image_abs(data, odata, 0);
677 cpl_test_image_abs(errs, oerrs, 0);
678
679 cpl_image_delete(odata);
680 cpl_image_delete(oerrs);
681 cpl_image_unwrap(data);
682 cpl_image_unwrap(errs);
683 }
684
685 return cpl_error_get_code();
686}
687
688
689
690
691
692/*----------------------------------------------------------------------------*/
696/*----------------------------------------------------------------------------*/
697int main(void)
698{
699 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
700
701 hdrl_clip_kappa_sigma_test();
702 hdrl_clip_minmax_test();
703
704 return cpl_test_end(0);
705}