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