CR2RE Pipeline Reference Manual 1.7.0
hdrl_cat_classify-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2017 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#include <cpl_test.h>
25
26#include "../hdrl_cat_classify.h"
27#include "../hdrl_cat_conf.h"
28#include "../hdrl_cat_table.h"
29
30
31#define NTEST 10
32
33
34int main(void)
35{
36 cpl_test_init(PACKAGE_BUGREPORT,CPL_MSG_WARNING);
37
38 /* Initialize */
39 double xpos[] = { 100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
40 double ypos[] = { 100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
41 double norm[] = {1000., 100., 200., 500., 550., 600., 650., 700., 750., 800.};
42
43 /* Generate a field with some stars and a confidence map */
44 cpl_image *bkg = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
45 cpl_image *im = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
46 cpl_image *cnf = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
47
48 double sigma = 2.;
49 double norm2 = 2. * CPL_MATH_PI * sigma * sigma;
50 cpl_image_fill_noise_uniform(bkg, -10., 10.);
51
52 double sky = 500.;
53 cpl_image_add_scalar(bkg, sky);
54
55 cpl_image_fill_noise_uniform(cnf, 99.9, 100.1);
56
57 double tot[NTEST];
58 for (cpl_size i = 0; i < NTEST; i++) {
59 cpl_image_fill_gaussian(im, xpos[i], ypos[i], norm[i] * norm2, sigma, sigma);
60 tot[i] = cpl_image_get_flux(im);
61 cpl_image_add(bkg, im);
62 }
63 cpl_image_delete(im);
64
65 hdrl_casu_fits *inf = hdrl_casu_fits_wrap(bkg);
66 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap(cnf);
67
68 /* Give it a WCS */
69 cpl_propertylist *pl = hdrl_casu_fits_get_ehu(inf);
70 cpl_propertylist_update_string(pl, "CTYPE1", "RA---TAN" );
71 cpl_propertylist_update_string(pl, "CTYPE2", "DEC--TAN" );
72 cpl_propertylist_update_double(pl, "CRVAL1", 30. );
73 cpl_propertylist_update_double(pl, "CRVAL2", 12. );
74 cpl_propertylist_update_double(pl, "CRPIX1", 512. );
75 cpl_propertylist_update_double(pl, "CRPIX2", 512. );
76 cpl_propertylist_update_double(pl, "CD1_1", -1. / 3600.);
77 cpl_propertylist_update_double(pl, "CD1_2", 0. );
78 cpl_propertylist_update_double(pl, "CD2_1", 0. );
79 cpl_propertylist_update_double(pl, "CD2_2", 1. / 3600.);
80
81 /* Run casu catalogue, TODO: check results */
82 hdrl_casu_result *res = cpl_calloc(1, sizeof(hdrl_casu_result));
83 cpl_test_eq(hdrl_catalogue_conf(inf, inconf, 5, 1.5, 0, 5., 1, 64, 6, 3., 1.,
84 HDRL_SATURATION_INIT, res), CPL_ERROR_NONE);
85 cpl_test_nonnull(res->catalogue);
86 cpl_image_delete(res->segmentation_map);
87 cpl_image_delete(res->background);
88
89 /* Check the results. Start by checking the number of rows and columns. Sort the table by X */
90 cpl_table *tab = hdrl_casu_tfits_get_table(res->catalogue);
91 cpl_test_nonnull(tab);
92 cpl_test_eq(cpl_table_get_ncol(tab), NCOLS);
93 cpl_test_eq(cpl_table_get_nrow(tab), NTEST);
94
95 pl = cpl_propertylist_new();
96 cpl_propertylist_append_bool(pl, "X_coordinate", 0);
97 cpl_table_sort(tab, pl);
98 cpl_propertylist_delete(pl);
99
100 /* Test the column content of the table */
101 int nl;
102 for (cpl_size i = 0; i < NTEST; i++) {
103
104 cpl_test_abs(xpos[i], cpl_table_get_double(tab, "X_coordinate", i, &nl), 0.2);
105 cpl_test_abs(ypos[i], cpl_table_get_double(tab, "Y_coordinate", i, &nl), 0.2);
106
107 double diff = fabs(cpl_table_get_double(tab, "Aper_flux_5", i, &nl) - tot[i])
108 / cpl_table_get_double(tab, "Aper_flux_5_err", i, &nl);
109 cpl_test_lt(diff, 1.6);
110 }
111
112 /*** Run classify and test the values of the classification ***/
113
114 /* Test 1 */
115 cpl_test_eq(hdrl_classify(res->catalogue, 5), CPL_ERROR_NONE);
116
117 pl = hdrl_casu_tfits_get_ehu(res->catalogue);
118 cpl_test_rel(cpl_propertylist_get_double(pl, "ESO QC IMAGE_SIZE"), 4.47, 0.02);
119 cpl_test_eq( cpl_propertylist_get_bool( pl, "ESO DRS CLASSIFD" ), 1 );
120 cpl_test_rel(cpl_propertylist_get_double(pl, "APCOR3" ), 0.132, 0.01);
121
122 for (cpl_size i = 0; i < NTEST; i++) {
123 double val = cpl_table_get(hdrl_casu_tfits_get_table(res->catalogue), "Classification", i, NULL);
124 cpl_test_rel(val, -1., 0.001);
125 }
126
127 /* Test 2 */
128 cpl_test_eq(hdrl_classify(res->catalogue, 10), CPL_ERROR_NONE);
129
130 /* Clean up */
132 hdrl_casu_fits_delete(inconf);
133 hdrl_casu_tfits_delete(res->catalogue);
134 cpl_free(res);
135
136
137 return cpl_test_end(0);
138}
cpl_error_code hdrl_catalogue_conf(hdrl_casu_fits *infile, hdrl_casu_fits *conf, cpl_size ipix, double threshold, cpl_size icrowd, double rcore, cpl_size bkg_subtr, cpl_size nbsize, cpl_size cattype, double filtfwhm, double gain, double saturation, hdrl_casu_result *res)
Do source extraction.
hdrl_casu_fits * hdrl_casu_fits_wrap(cpl_image *im)
Wrap an image in a hdrl_casu_fits wrapper.
cpl_propertylist * hdrl_casu_tfits_get_ehu(hdrl_casu_tfits *p)
Get the propertylist for the extension header for a given hdrl_casu_tfits image.
void hdrl_casu_tfits_delete(hdrl_casu_tfits *p)
Free all the workspace associated with a hdrl_casu_fits object.
void hdrl_casu_fits_delete(hdrl_casu_fits *p)
Free all the workspace associated with a hdrl_casu_fits object.
cpl_table * hdrl_casu_tfits_get_table(hdrl_casu_tfits *p)
Get the CPL table from the hdrl_casu_tfits object.
cpl_propertylist * hdrl_casu_fits_get_ehu(hdrl_casu_fits *p)
Get the propertylist for the extension header for a given hdrl_casu_fits image.
cpl_error_code hdrl_classify(hdrl_casu_tfits *catalogue, double minsize)
Do star/galaxy classification.