CR2RE Pipeline Reference Manual 1.6.8
hdrl_catalogue-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2013,2014 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.h"
29
30#include <cpl.h>
31#include <math.h>
32#include <string.h>
33
34/*----------------------------------------------------------------------------*/
38/*----------------------------------------------------------------------------*/
39
40/*----------------------------------------------------------------------------*/
45/*----------------------------------------------------------------------------*/
46static cpl_error_code hdrl_catalogue_test_compute(void)
47{
48 cpl_image * img, * cnf, * flat_img, * peak_img;
49 cpl_wcs * wcs;
50 hdrl_parameter * par;
51 hdrl_catalogue_result * r;
52 cpl_size nx = 1001, ny = 753;
53
54 img = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
55 cnf = cpl_image_new(nx, ny, CPL_TYPE_INT);
56
57 /* Create a completely flat image with all pixels set to 1.0 */
58 flat_img = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
59 cpl_test_nonnull(flat_img);
60 cpl_image_add_scalar(flat_img, 1.0);
61 cpl_test_error(CPL_ERROR_NONE);
62
63 /* Create an image with all pixels set to 1.0 and a single sharp Gaussian
64 peak in the image at coordinate (100, 100). */
65 peak_img = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
66 cpl_test_nonnull(peak_img);
67 cpl_image_fill_gaussian(peak_img, 100, 100, 1000.0, 1.0, 1.0);
68 cpl_test_error(CPL_ERROR_NONE);
69 cpl_image_add_scalar(peak_img, 1.0);
70 cpl_test_error(CPL_ERROR_NONE);
71
72 cpl_propertylist * pl = cpl_propertylist_new();
73 cpl_propertylist_update_string(pl, "CTYPE1", "RA---TAN");
74 cpl_propertylist_update_string(pl, "CTYPE2", "DEC--TAN");
75 cpl_propertylist_update_double(pl, "CRVAL1", 30.0);
76 cpl_propertylist_update_double(pl, "CRVAL2", 12.0);
77 cpl_propertylist_update_double(pl, "CRPIX1", 512.0);
78 cpl_propertylist_update_double(pl, "CRPIX2", 512.0);
79 cpl_propertylist_update_double(pl, "CD1_1", -1.0 / 3600);
80 cpl_propertylist_update_double(pl, "CD1_2", 0.0);
81 cpl_propertylist_update_double(pl, "CD2_1", 0.0);
82 cpl_propertylist_update_double(pl, "CD2_2", 1.0 / 3600);
83 cpl_propertylist_update_int(pl, "NAXIS1", nx);
84 cpl_propertylist_update_int(pl, "NAXIS2", ny);
85 wcs = cpl_wcs_new_from_propertylist(pl);
86 cpl_propertylist_delete(pl);
87
88 par = hdrl_catalogue_parameter_create(3, 2.5, CPL_FALSE, 3., CPL_TRUE, 64,
89 2., 2.0, HDRL_SATURATION_INIT,
90 HDRL_CATALOGUE_ALL);
91
92 /* check null input errors */
93 for (size_t i = 0; i < 2; i++) {
94 for (size_t j = 0; j < 2; j++) {
95 for (size_t k = 0; k < 2; k++) {
96 for (size_t l = 0; l < 2; l++) {
97 /* img and par are enough, rest optional */
98 if (i && l)
99 continue;
100 r = hdrl_catalogue_compute(i ? img : NULL,
101 j ? cnf : NULL,
102 k ? wcs : NULL,
103 l ? par : NULL);
104 cpl_test_null(r);
105 cpl_test_error(CPL_ERROR_NULL_INPUT);
107 }
108 }
109 }
110 }
111
112 /* zero image */
113 // TODO mem-leaks
114 //r = hdrl_catalogue_compute(img , NULL, NULL, par);
115 //cpl_test_null(r);
116 //cpl_test_error(CPL_ERROR_TODO);
117
118 /* check basic object detection and results */
120 par = hdrl_catalogue_parameter_create(5, 1.5, CPL_FALSE, 5., CPL_TRUE, 64,
121 3., 1.0, HDRL_SATURATION_INIT,
122 HDRL_CATALOGUE_ALL);
123 cpl_image *bkg = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
124 double sigma = 2., sky = 500.;
125 double norm2 = 2.0 * CPL_MATH_PI * sigma * sigma;
126 cpl_image_add_scalar(cnf, 100.);
127 double xpos[] = {100.0,200.0,300.0,400.0,500.0,600.0,700.0,800.0,900.0,
128 950.0};
129 double ypos[] = {100.0,200.0,300.0,400.0,550.0,600.0,650.0,700.0,230.0,
130 170.0};
131 double norm[] = {1000.0,100.0,200.0,500.0,550.0,600.0,650.0,700.0,
132 750.0,800.0};
133 size_t nobj = sizeof(xpos) / sizeof(xpos[0]);
134 for (size_t i = 0; i < nobj; i++) {
135 cpl_image_fill_gaussian(bkg, xpos[i], ypos[i], norm[i] * norm2, sigma,
136 sigma);
137 cpl_image_add(img, bkg);
138 }
139 cpl_image_fill_noise_uniform(bkg, -10.0, 10.0);
140 cpl_image_add_scalar(bkg, sky);
141 cpl_image_add(img, bkg);
142 cpl_image_delete(bkg);
143
144 r = hdrl_catalogue_compute(img , NULL, NULL, par);
145 cpl_test_nonnull(r);
146 cpl_test_error(CPL_ERROR_NONE);
147 cpl_test_nonnull(r->catalogue);
148 cpl_test_eq(cpl_table_get_nrow(r->catalogue), nobj);
149 cpl_test_nonnull(r->segmentation_map);
150 cpl_test_eq(cpl_image_get_max(r->segmentation_map), nobj);
151 cpl_test_eq(cpl_image_get_min(r->segmentation_map), 0);
152 cpl_test_nonnull(r->background);
153 cpl_test_abs(cpl_image_get_mean(r->background), sky, 5);
154 cpl_test_nonnull(r->qclist);
155 // TODO check required keys
157
158 /* smoke test no background subtraction */
159 hdrl_parameter * bpar =
160 hdrl_catalogue_parameter_create(5, 1.5, CPL_FALSE, 5., CPL_FALSE, 64,
161 3., 1.0, HDRL_SATURATION_INIT,
162 HDRL_CATALOGUE_ALL);
163 cpl_image * imgcor = cpl_image_subtract_scalar_create(img, sky);
164 r = hdrl_catalogue_compute(imgcor, NULL, NULL, bpar);
165 cpl_test_nonnull(r);
166 cpl_test_error(CPL_ERROR_NONE);
167 cpl_test_nonnull(r->catalogue);
168 cpl_test_eq(cpl_table_get_nrow(r->catalogue), nobj);
169 cpl_test_nonnull(r->segmentation_map);
170 cpl_test_eq(cpl_image_get_max(r->segmentation_map), nobj);
171 cpl_test_eq(cpl_image_get_min(r->segmentation_map), 0);
172 cpl_test_null(r->background);
173 cpl_test_nonnull(r->qclist);
174 cpl_image_delete(imgcor);
177
178 /* test too big nbsize */
179 bpar = hdrl_catalogue_parameter_create(3, 2.5, CPL_FALSE, 3., CPL_TRUE,
180 nx + 23, 2., 2.0,
181 HDRL_SATURATION_INIT,
182 HDRL_CATALOGUE_ALL);
183
184 /* Check parameter */
185 cpl_test(hdrl_catalogue_parameter_check(bpar));
186
187
188 /* Change option */
189 hdrl_catalogue_parameter_set_option(NULL, HDRL_CATALOGUE_ALL);
190 cpl_test_error(CPL_ERROR_NULL_INPUT);
191
192 hdrl_catalogue_parameter_set_option(bpar, HDRL_CATALOGUE_BKG);
193 cpl_test_error(CPL_ERROR_NONE);
194
195 hdrl_catalogue_parameter_set_option(bpar, HDRL_CATALOGUE_ALL);
196 cpl_test_error(CPL_ERROR_NONE);
197
198
199 /* Create ParameterList */
200 cpl_parameterlist *plCat;
201
202 plCat = hdrl_catalogue_parameter_create_parlist(NULL, "catalogue", bpar);
203 cpl_test_error(CPL_ERROR_NULL_INPUT);
204 cpl_test_null(plCat);
205
206 plCat = hdrl_catalogue_parameter_create_parlist("test", NULL, bpar);
207 cpl_test_error(CPL_ERROR_NULL_INPUT);
208 cpl_test_null(plCat);
209
210 plCat = hdrl_catalogue_parameter_create_parlist("test", "catalogue", NULL);
211 cpl_test_error(CPL_ERROR_NULL_INPUT);
212 cpl_test_null(plCat);
213
214 plCat = hdrl_catalogue_parameter_create_parlist("test", "catalogue", bpar);
215 cpl_test_error(CPL_ERROR_NONE);
216 cpl_test_nonnull(plCat);
217
218
219 /* Parse ParameterList */
220 hdrl_parameter *check;
221
222 check = hdrl_catalogue_parameter_parse_parlist(NULL, "test.catalogue");
223 cpl_test_error(CPL_ERROR_NULL_INPUT);
224 cpl_test_null(check);
225
226 check = hdrl_catalogue_parameter_parse_parlist(plCat, NULL);
227 cpl_test_error(CPL_ERROR_NULL_INPUT);
228 cpl_test_null(check);
229
230 check = hdrl_catalogue_parameter_parse_parlist(plCat, "test.catalogue");
231 cpl_test_error(CPL_ERROR_NONE);
232 cpl_test_nonnull(check);
233
234
235 cpl_parameterlist_delete(plCat);
237
238
239
240 /* Compute */
241 r = hdrl_catalogue_compute(img , NULL, NULL, bpar);
242 cpl_test_nonnull(r);
243 cpl_test_error(CPL_ERROR_NONE);
246
247 /* test bad confidence */
248 cpl_image_subtract_scalar(cnf, 200);
249 r = hdrl_catalogue_compute(img , cnf, NULL, par);
250 cpl_test_null(r);
251 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
252 cpl_image_add_scalar(cnf, 200);
253
254 /* test double confidence */
255 cpl_image * dcnf = cpl_image_cast(cnf, CPL_TYPE_DOUBLE);
256 r = hdrl_catalogue_compute(img , dcnf, NULL, par);
257 cpl_test_nonnull(r);
258 cpl_test_nonnull(r->catalogue);
259 cpl_test_nonnull(r->segmentation_map);
260 cpl_test_nonnull(r->background);
261 cpl_test_error(CPL_ERROR_NONE);
263
264 /* smoketest image with bad pixels */
265 cpl_image_reject(img, 60, 23);
266 r = hdrl_catalogue_compute(img , NULL, NULL, par);
267 cpl_test_nonnull(r);
268 cpl_test_error(CPL_ERROR_NONE);
270
271 /* smoketest image with bad pixels and confidence */
272 r = hdrl_catalogue_compute(img , cnf, NULL, par);
273 cpl_test_nonnull(r);
274 cpl_test_nonnull(r->catalogue);
275 cpl_test_nonnull(r->segmentation_map);
276 cpl_test_nonnull(r->background);
277 cpl_test_error(CPL_ERROR_NONE);
279
280 /* smoketest image with bad pixels and double confidence */
281 r = hdrl_catalogue_compute(img , dcnf, NULL, par);
282 cpl_test_nonnull(r);
283 cpl_test_nonnull(r->catalogue);
284 cpl_test_nonnull(r->segmentation_map);
285 cpl_test_nonnull(r->background);
286 cpl_test_error(CPL_ERROR_NONE);
288 cpl_image_delete(dcnf);
289
290 /* smoketest image and confidence */
291 cpl_image_accept_all(img);
292 r = hdrl_catalogue_compute(img , cnf, NULL, par);
293 cpl_test_nonnull(r);
294 cpl_test_error(CPL_ERROR_NONE);
296
297 /* smoketest image, confidence and wcs */
298 cpl_image_accept_all(img);
299 r = hdrl_catalogue_compute(img , cnf, wcs, par);
300 cpl_test_nonnull(r);
301 cpl_test_nonnull(r->catalogue);
302 cpl_test_nonnull(r->segmentation_map);
303 cpl_test_nonnull(r->background);
304 cpl_test_error(CPL_ERROR_NONE);
306
307 /* smoketest double image, confidence and wcs */
308 cpl_image * dimg = cpl_image_cast(img, CPL_TYPE_DOUBLE);
309 cpl_image_accept_all(img);
310 r = hdrl_catalogue_compute(dimg , cnf, wcs, par);
311 cpl_test_nonnull(r);
312 cpl_test_nonnull(r->catalogue);
313 cpl_test_nonnull(r->segmentation_map);
314 cpl_test_nonnull(r->background);
315 cpl_test_error(CPL_ERROR_NONE);
316 cpl_image_delete(dimg);
318
319 /* test no segmap and background */
321 par = hdrl_catalogue_parameter_create(5, 1.5, CPL_FALSE, 5., CPL_TRUE, 64,
322 3., 1.0, HDRL_SATURATION_INIT,
323 HDRL_CATALOGUE_CAT_COMPLETE);
324 r = hdrl_catalogue_compute(img , cnf, wcs, par);
325 cpl_test_nonnull(r);
326 cpl_test_nonnull(r->catalogue);
327 cpl_test_null(r->segmentation_map);
328 cpl_test_null(r->background);
329 cpl_test_error(CPL_ERROR_NONE);
331
332 /* test no segmap */
334 par = hdrl_catalogue_parameter_create(5, 1.5, CPL_FALSE, 5., CPL_TRUE, 64,
335 3., 1.0, HDRL_SATURATION_INIT,
336 HDRL_CATALOGUE_CAT_COMPLETE |
337 HDRL_CATALOGUE_BKG);
338 r = hdrl_catalogue_compute(img , cnf, wcs, par);
339 cpl_test_nonnull(r);
340 cpl_test_nonnull(r->catalogue);
341 cpl_test_null(r->segmentation_map);
342 cpl_test_nonnull(r->background);
343 cpl_test_error(CPL_ERROR_NONE);
345
346 /* test no background */
348 par = hdrl_catalogue_parameter_create(5, 1.5, CPL_FALSE, 5., CPL_TRUE, 64,
349 3., 1.0, HDRL_SATURATION_INIT,
350 HDRL_CATALOGUE_CAT_COMPLETE |
351 HDRL_CATALOGUE_SEGMAP);
352 r = hdrl_catalogue_compute(img , cnf, wcs, par);
353 cpl_test_nonnull(r);
354 cpl_test_error(CPL_ERROR_NONE);
355 cpl_test_nonnull(r->catalogue);
356 cpl_test_nonnull(r->segmentation_map);
357 cpl_test_null(r->background);
359
360 /* test no catalogue */
362 par = hdrl_catalogue_parameter_create(5, 1.5, CPL_FALSE, 5., CPL_TRUE, 64,
363 3., 1.0, HDRL_SATURATION_INIT,
364 HDRL_CATALOGUE_SEGMAP |
365 HDRL_CATALOGUE_BKG);
366 r = hdrl_catalogue_compute(img , cnf, wcs, par);
367 cpl_test_nonnull(r);
368 cpl_test_nonnull(r->catalogue);
369 cpl_test_eq(cpl_table_get_nrow(r->catalogue), 0);
370 cpl_test_nonnull(r->segmentation_map);
371 cpl_test_nonnull(r->background);
372 cpl_test_error(CPL_ERROR_NONE);
374
375 /* Test pathological case with a completely flat image. */
377 par = hdrl_catalogue_parameter_create(5, 1.5, CPL_FALSE, 5., CPL_TRUE, 64,
378 3., 1.0, HDRL_SATURATION_INIT,
379 HDRL_CATALOGUE_CAT_COMPLETE |
380 HDRL_CATALOGUE_SEGMAP |
381 HDRL_CATALOGUE_BKG);
382 cpl_test_nonnull(par);
383 r = hdrl_catalogue_compute(flat_img , NULL, NULL, par);
384 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
385 cpl_test_nonnull(r);
387
388 /* Test pathological case with a single sharp peak in the image. */
390 par = hdrl_catalogue_parameter_create(5, 1.5, CPL_FALSE, 5., CPL_TRUE, 64,
391 3., 1.0, HDRL_SATURATION_INIT,
392 HDRL_CATALOGUE_CAT_COMPLETE |
393 HDRL_CATALOGUE_SEGMAP |
394 HDRL_CATALOGUE_BKG);
395 cpl_test_nonnull(par);
396 r = hdrl_catalogue_compute(peak_img , NULL, NULL, par);
397 cpl_test_nonnull(r);
398 cpl_test_error(CPL_ERROR_NONE);
399 cpl_test_nonnull(r->catalogue);
400 cpl_test_nonnull(r->segmentation_map);
401 cpl_test_nonnull(r->background);
403
404 cpl_image_delete(flat_img);
405 cpl_image_delete(peak_img);
406 cpl_image_delete(img);
407 cpl_image_delete(cnf);
408 cpl_wcs_delete(wcs);
410 return cpl_error_get_code();
411}
412
413
414/*----------------------------------------------------------------------------*/
418/*----------------------------------------------------------------------------*/
419int main(void)
420{
421 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
422
423 hdrl_catalogue_test_compute();
424 cpl_test_error(CPL_ERROR_NONE);
425
426
427 return cpl_test_end(0);
428}
hdrl_catalogue_result * hdrl_catalogue_compute(const cpl_image *image_, const cpl_image *confidence_map, const cpl_wcs *wcs, hdrl_parameter *param_)
build object catalog
void hdrl_catalogue_result_delete(hdrl_catalogue_result *result)
delete hdrl parameter result object
hdrl_parameter * hdrl_catalogue_parameter_create(int obj_min_pixels, double obj_threshold, cpl_boolean obj_deblending, double obj_core_radius, cpl_boolean bkg_estimate, int bkg_mesh_size, double bkg_smooth_fwhm, double det_eff_gain, double det_saturation, hdrl_catalogue_options resulttype)
Creates catalogue Parameters object.
cpl_boolean hdrl_catalogue_parameter_check(const hdrl_parameter *self)
Check that the parameter is a catalogue parameter.
hdrl_parameter * hdrl_catalogue_parameter_parse_parlist(const cpl_parameterlist *parlist, const char *prefix)
Parse parameter list to create input parameters for the catalogue.
cpl_parameterlist * hdrl_catalogue_parameter_create_parlist(const char *base_context, const char *prefix, hdrl_parameter *defaults)
Create parameter list for the catalogue computation.
cpl_error_code hdrl_catalogue_parameter_set_option(hdrl_parameter *par, hdrl_catalogue_options opt)
set result option of catalogue parameter
void hdrl_parameter_destroy(hdrl_parameter *obj)
deep delete of a parameter
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter