CR2RE Pipeline Reference Manual 1.6.2
hdrl_bpm_fit-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 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 "igam.c"
29#include "hdrl_bpm_fit.h"
30
31#include <cpl.h>
32#include <math.h>
33
34/*----------------------------------------------------------------------------*/
38/*----------------------------------------------------------------------------*/
39
40void test_invalid_parameter(void)
41{
42 hdrl_parameter * p;
43 /* invalid degree */
45 cpl_test_null(p);
46 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
48 cpl_test_null(p);
49 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
51 cpl_test_null(p);
52 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
53
54 /* invalid threshold */
56 cpl_test_null(p);
57 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
59 cpl_test_null(p);
60 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
62 cpl_test_null(p);
63 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
65 cpl_test_null(p);
66 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
67
68 /* NULL input */
70 cpl_test_error(CPL_ERROR_NULL_INPUT);
72 cpl_test_error(CPL_ERROR_NULL_INPUT);
74 cpl_test_error(CPL_ERROR_NULL_INPUT);
75}
76
77void test_parameter(void)
78{
79 hdrl_parameter * p;
80
83 cpl_test_abs(hdrl_bpm_fit_parameter_get_pval(p), 10., 0);
84 cpl_test_lt((hdrl_bpm_fit_parameter_get_rel_chi_low(p)), 0);
87
90 cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_chi_low(p), 10.);
92 cpl_test_lt(hdrl_bpm_fit_parameter_get_pval(p), 0);
96
99 cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_coef_low(p), 10.);
101 cpl_test_lt(hdrl_bpm_fit_parameter_get_pval(p), 0);
104
106}
107
108void test_parameterlist(void)
109{
110 hdrl_parameter * p;
111 hdrl_parameter * def;
112 cpl_parameterlist * parlist;
113
115 parlist = hdrl_bpm_fit_parameter_create_parlist("RECIPE", "bpm_fit", def);
117 p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
118 cpl_test_null(p);
119 cpl_test_error(CPL_ERROR_NULL_INPUT);
120 cpl_parameterlist_delete(parlist);
121
123 parlist = hdrl_bpm_fit_parameter_create_parlist("RECIPE", "bpm_fit", def);
125 p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
126 cpl_test_null(p);
127 cpl_test_error(CPL_ERROR_NULL_INPUT);
128 cpl_parameterlist_delete(parlist);
129
131 parlist = hdrl_bpm_fit_parameter_create_parlist("RECIPE", "bpm_fit", def);
133 cpl_test_error(CPL_ERROR_NONE);
134 cpl_test_eq(cpl_parameterlist_get_size(parlist), 6);
135
136 p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.invalid");
137 cpl_test_null(p);
138 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
139
140 p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
141 cpl_test_error(CPL_ERROR_NONE);
142 cpl_test_eq(cpl_parameterlist_get_size(parlist), 6);
143 cpl_test_eq(hdrl_bpm_fit_parameter_get_degree(p), 2);
144 cpl_test_abs(hdrl_bpm_fit_parameter_get_pval(p), 0.1, 0);
146 cpl_parameterlist_delete(parlist);
147
149 parlist = hdrl_bpm_fit_parameter_create_parlist( "RECIPE", "bpm_fit", def);
151 cpl_test_error(CPL_ERROR_NONE);
152 cpl_test_eq(cpl_parameterlist_get_size(parlist), 6);
153 p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
154 cpl_test_eq(hdrl_bpm_fit_parameter_get_degree(p), 2);
155 cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_chi_low(p), 3.);
156 cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_chi_high(p), 2.);
158 cpl_parameterlist_delete(parlist);
159
161 parlist = hdrl_bpm_fit_parameter_create_parlist("RECIPE", "bpm_fit", def);
163 cpl_test_error(CPL_ERROR_NONE);
164 cpl_test_eq(cpl_parameterlist_get_size(parlist), 6);
165 p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
166 cpl_test_eq(hdrl_bpm_fit_parameter_get_degree(p), 2);
167 cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_coef_low(p), 2.);
170 cpl_parameterlist_delete(parlist);
171}
172
173void test_bpm_fit(void)
174{
175 int rej;
176 hdrl_imagelist * hl = hdrl_imagelist_new();
177 cpl_vector * sample = cpl_vector_new(10);
178 for (size_t i = 0; i < 10; i++) {
179 hdrl_image * himg = hdrl_image_new(13, 4);
180 hdrl_image_add_scalar(himg, (hdrl_value){i + 1, sqrt(i + 1)});
181 hdrl_image_set_pixel(himg, 1, 1, (hdrl_value){1.01, 1.});
182 hdrl_imagelist_set(hl, himg, i);
183 cpl_vector_set(sample, i, i);
184 }
185
186 hdrl_parameter * p;
187 cpl_image * out_mask;
188
190 hdrl_bpm_fit_compute(p, hl, sample, &out_mask);
191 cpl_test_error(CPL_ERROR_NONE);
192 cpl_test_eq(cpl_image_get_flux(out_mask), 0.);
193 cpl_image_delete(out_mask);
195
197 hdrl_bpm_fit_compute(p, hl, sample, &out_mask);
198 cpl_test_error(CPL_ERROR_NONE);
199 cpl_test_eq(cpl_image_get(out_mask, 1, 1, &rej), 3);
200 cpl_image_delete(out_mask);
202
205 (hdrl_value){5.1 , sqrt(5.1)});
206 hdrl_bpm_fit_compute(p, hl, sample, &out_mask);
207 cpl_test_error(CPL_ERROR_NONE);
208 cpl_test_eq(cpl_image_get_flux(out_mask), 1.);
209 cpl_test_eq(cpl_image_get(out_mask, 1, 1, &rej), 1.);
210 cpl_image_delete(out_mask);
212
214 cpl_vector_delete(sample);
215}
216
217void test_igam(void){
218
219 cpl_test_error(CPL_ERROR_NONE);
220
221 /* Fail test */
222 cpl_test(isnan(igamc(-1, 1)));
223 cpl_test(isnan(igamc(1, -1)));
224 cpl_test(isnan(igamc(-1, -1)));
225
226 cpl_test(!isnan(igamc(1, 1)));
227
228
229 /* Normal test */
230 double a = 1.;
231 double x = 2.;
232
233 double res1 = igamc(a, x);
234 cpl_test_eq(res1, 0.);
235
236 double res2 = igamc(x, a);
237 cpl_test_eq(res2, 0.);
238
239 double res3 = igam(a, x);
240 cpl_test_eq(res3, 0.);
241
242 double res4 = igam(x, a);
243 cpl_test_eq(res4, 0.);
244
245
246 double res11 = igamc(0., 0.);
247 cpl_test(res11 != 0.);
248
249 double res12 = igamc(0., 1.);
250 cpl_test(res12 != 0.);
251
252 double res13 = igamc(1., 0.);
253 cpl_test(res13 != 0.);
254
255
256 double res21 = igamc(0., 1e10);
257 cpl_test(res21 != 0.);
258
259 double res22 = igamc(1., 1e20);
260 cpl_test_eq(res22, 0.);
261
262 double res23 = igamc(1e10, 0.);
263 cpl_test_eq(res23, 1.);
264
265 double res24 = igamc(1e20, 1.);
266 cpl_test_eq(res24, 1.);
267
268
269 cpl_test_error(CPL_ERROR_NONE);
270}
271
272
273/*----------------------------------------------------------------------------*/
277/*----------------------------------------------------------------------------*/
278int main(void)
279{
280 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
281
282 test_invalid_parameter();
283 test_parameter();
284 test_parameterlist();
285 test_bpm_fit();
286
287 test_igam();
288
289
290 return cpl_test_end(0);
291}
hdrl_parameter * hdrl_bpm_fit_parameter_create_rel_coef(int degree, double rel_coef_low, double rel_coef_high)
create bpm_fit parameter with relative coefficient bpm treshold
Definition: hdrl_bpm_fit.c:176
hdrl_parameter * hdrl_bpm_fit_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse a parameterlist to create input parameters for the BPM_FIT.
Definition: hdrl_bpm_fit.c:442
double hdrl_bpm_fit_parameter_get_rel_chi_high(const hdrl_parameter *p)
get relative chi distribution upper threshold
Definition: hdrl_bpm_fit.c:308
double hdrl_bpm_fit_parameter_get_rel_coef_high(const hdrl_parameter *p)
get relative fit coefficient distribution upper threshold
Definition: hdrl_bpm_fit.c:338
int hdrl_bpm_fit_parameter_get_degree(const hdrl_parameter *p)
get degree of polynomial fit of parameter
Definition: hdrl_bpm_fit.c:263
cpl_parameterlist * hdrl_bpm_fit_parameter_create_parlist(const char *base_context, const char *prefix, const hdrl_parameter *defaults)
Create a parameter list for the BPM_FIT computation.
Definition: hdrl_bpm_fit.c:363
hdrl_parameter * hdrl_bpm_fit_parameter_create_pval(int degree, double pval)
create bpm_fit parameter with p-value bpm treshold
Definition: hdrl_bpm_fit.c:142
double hdrl_bpm_fit_parameter_get_pval(const hdrl_parameter *p)
get p-value bpm treshold
Definition: hdrl_bpm_fit.c:278
double hdrl_bpm_fit_parameter_get_rel_chi_low(const hdrl_parameter *p)
get relative chi distribution lower threshold
Definition: hdrl_bpm_fit.c:293
hdrl_parameter * hdrl_bpm_fit_parameter_create_rel_chi(int degree, double rel_chi_low, double rel_chi_high)
create bpm_fit parameter with relative chi bpm treshold
Definition: hdrl_bpm_fit.c:158
cpl_boolean hdrl_bpm_fit_parameter_check(const hdrl_parameter *self)
Check that the parameter is a bpm_fit parameter.
Definition: hdrl_bpm_fit.c:190
double hdrl_bpm_fit_parameter_get_rel_coef_low(const hdrl_parameter *p)
get relative fit coefficient distribution lower threshold
Definition: hdrl_bpm_fit.c:323
cpl_error_code hdrl_bpm_fit_compute(const hdrl_parameter *par, const hdrl_imagelist *data, const cpl_vector *sample_pos, cpl_image **out_mask)
compute bad pixel map based on fitting a stack of images
Definition: hdrl_bpm_fit.c:587
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_error_code hdrl_image_add_scalar(hdrl_image *self, hdrl_value value)
Elementwise addition of a scalar to an image.
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
Definition: hdrl_image.c:311
cpl_error_code hdrl_imagelist_set(hdrl_imagelist *himlist, hdrl_image *himg, cpl_size pos)
Insert an image into an imagelist.
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter