1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
* This file is part of the HDRL
* Copyright (C) 2013 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include "igam.c"
#include "hdrl_bpm_fit.h"
#include <cpl.h>
#include <math.h>
/*----------------------------------------------------------------------------*/
/**
@defgroup hdrl_bpm_fit_test Testing of the HDRL bpm_fit
*/
/*----------------------------------------------------------------------------*/
void test_invalid_parameter(void)
{
hdrl_parameter * p;
/* invalid degree */
p = hdrl_bpm_fit_parameter_create_pval(-1, 0.1);
cpl_test_null(p);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
p = hdrl_bpm_fit_parameter_create_rel_chi(-1, 0.1, 0.1);
cpl_test_null(p);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
p = hdrl_bpm_fit_parameter_create_rel_coef(-1, 0.1, 0.1);
cpl_test_null(p);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
/* invalid threshold */
p = hdrl_bpm_fit_parameter_create_pval(1, -0.1);
cpl_test_null(p);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
p = hdrl_bpm_fit_parameter_create_pval(1, 100.1);
cpl_test_null(p);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
p = hdrl_bpm_fit_parameter_create_rel_chi(1, -0.1, 0.1);
cpl_test_null(p);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
p = hdrl_bpm_fit_parameter_create_rel_coef(1, -0.1, -0.1);
cpl_test_null(p);
cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
/* NULL input */
hdrl_bpm_fit_parameter_get_pval(NULL);
cpl_test_error(CPL_ERROR_NULL_INPUT);
hdrl_bpm_fit_parameter_get_rel_chi_low(NULL);
cpl_test_error(CPL_ERROR_NULL_INPUT);
hdrl_bpm_fit_parameter_get_rel_coef_high(NULL);
cpl_test_error(CPL_ERROR_NULL_INPUT);
}
void test_parameter(void)
{
hdrl_parameter * p;
p = hdrl_bpm_fit_parameter_create_pval(1, 10.);
cpl_test(hdrl_bpm_fit_parameter_check(p));
cpl_test_abs(hdrl_bpm_fit_parameter_get_pval(p), 10., 0);
cpl_test_lt((hdrl_bpm_fit_parameter_get_rel_chi_low(p)), 0);
cpl_test_lt((hdrl_bpm_fit_parameter_get_rel_coef_high(p)), 0);
hdrl_parameter_delete(p);
p = hdrl_bpm_fit_parameter_create_rel_chi(1, 10., 5.);
cpl_test(hdrl_bpm_fit_parameter_check(p));
cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_chi_low(p), 10.);
cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_chi_high(p), 5.);
cpl_test_lt(hdrl_bpm_fit_parameter_get_pval(p), 0);
cpl_test_lt(hdrl_bpm_fit_parameter_get_rel_coef_low(p), 0);
cpl_test_lt(hdrl_bpm_fit_parameter_get_rel_coef_high(p), 0);
hdrl_parameter_delete(p);
p = hdrl_bpm_fit_parameter_create_rel_coef(1, 10., 3.);
cpl_test(hdrl_bpm_fit_parameter_check(p));
cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_coef_low(p), 10.);
cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_coef_high(p), 3.);
cpl_test_lt(hdrl_bpm_fit_parameter_get_pval(p), 0);
cpl_test_lt(hdrl_bpm_fit_parameter_get_rel_chi_low(p), 0);
cpl_test_lt(hdrl_bpm_fit_parameter_get_rel_chi_high(p), 0);
hdrl_parameter_delete(p);
}
void test_parameterlist(void)
{
hdrl_parameter * p;
hdrl_parameter * def;
cpl_parameterlist * parlist;
def = hdrl_bpm_fit_parameter_create_pval(-2, 0.1) ;
parlist = hdrl_bpm_fit_parameter_create_parlist("RECIPE", "bpm_fit", def);
hdrl_parameter_delete(def);
p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
cpl_test_null(p);
cpl_test_error(CPL_ERROR_NULL_INPUT);
cpl_parameterlist_delete(parlist);
def = hdrl_bpm_fit_parameter_create_pval(2, -1) ;
parlist = hdrl_bpm_fit_parameter_create_parlist("RECIPE", "bpm_fit", def);
hdrl_parameter_delete(def);
p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
cpl_test_null(p);
cpl_test_error(CPL_ERROR_NULL_INPUT);
cpl_parameterlist_delete(parlist);
def = hdrl_bpm_fit_parameter_create_pval(2, 0.1) ;
parlist = hdrl_bpm_fit_parameter_create_parlist("RECIPE", "bpm_fit", def);
hdrl_parameter_delete(def);
cpl_test_error(CPL_ERROR_NONE);
cpl_test_eq(cpl_parameterlist_get_size(parlist), 6);
p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.invalid");
cpl_test_null(p);
cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
cpl_test_error(CPL_ERROR_NONE);
cpl_test_eq(cpl_parameterlist_get_size(parlist), 6);
cpl_test_eq(hdrl_bpm_fit_parameter_get_degree(p), 2);
cpl_test_abs(hdrl_bpm_fit_parameter_get_pval(p), 0.1, 0);
hdrl_parameter_delete(p);
cpl_parameterlist_delete(parlist);
def = hdrl_bpm_fit_parameter_create_rel_chi(2, 3., 2.) ;
parlist = hdrl_bpm_fit_parameter_create_parlist( "RECIPE", "bpm_fit", def);
hdrl_parameter_delete(def);
cpl_test_error(CPL_ERROR_NONE);
cpl_test_eq(cpl_parameterlist_get_size(parlist), 6);
p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
cpl_test_eq(hdrl_bpm_fit_parameter_get_degree(p), 2);
cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_chi_low(p), 3.);
cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_chi_high(p), 2.);
hdrl_parameter_delete(p);
cpl_parameterlist_delete(parlist);
def = hdrl_bpm_fit_parameter_create_rel_coef(2, 2., 3.) ;
parlist = hdrl_bpm_fit_parameter_create_parlist("RECIPE", "bpm_fit", def);
hdrl_parameter_delete(def);
cpl_test_error(CPL_ERROR_NONE);
cpl_test_eq(cpl_parameterlist_get_size(parlist), 6);
p = hdrl_bpm_fit_parameter_parse_parlist(parlist, "RECIPE.bpm_fit");
cpl_test_eq(hdrl_bpm_fit_parameter_get_degree(p), 2);
cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_coef_low(p), 2.);
cpl_test_eq(hdrl_bpm_fit_parameter_get_rel_coef_high(p), 3.);
hdrl_parameter_delete(p);
cpl_parameterlist_delete(parlist);
}
void test_bpm_fit(void)
{
int rej;
hdrl_imagelist * hl = hdrl_imagelist_new();
cpl_vector * sample = cpl_vector_new(10);
for (size_t i = 0; i < 10; i++) {
hdrl_image * himg = hdrl_image_new(13, 4);
hdrl_image_add_scalar(himg, (hdrl_value){i + 1, sqrt(i + 1)});
hdrl_image_set_pixel(himg, 1, 1, (hdrl_value){1.01, 1.});
hdrl_imagelist_set(hl, himg, i);
cpl_vector_set(sample, i, i);
}
hdrl_parameter * p;
cpl_image * out_mask;
p = hdrl_bpm_fit_parameter_create_pval(1, 0.1);
hdrl_bpm_fit_compute(p, hl, sample, &out_mask);
cpl_test_error(CPL_ERROR_NONE);
cpl_test_eq(cpl_image_get_flux(out_mask), 0.);
cpl_image_delete(out_mask);
hdrl_parameter_delete(p);
p = hdrl_bpm_fit_parameter_create_rel_coef(1, 1., 1.);
hdrl_bpm_fit_compute(p, hl, sample, &out_mask);
cpl_test_error(CPL_ERROR_NONE);
cpl_test_eq(cpl_image_get(out_mask, 1, 1, &rej), 3);
cpl_image_delete(out_mask);
hdrl_parameter_delete(p);
p = hdrl_bpm_fit_parameter_create_rel_chi(1, 1., 1.);
hdrl_image_add_scalar(hdrl_imagelist_get(hl, 4),
(hdrl_value){5.1 , sqrt(5.1)});
hdrl_bpm_fit_compute(p, hl, sample, &out_mask);
cpl_test_error(CPL_ERROR_NONE);
cpl_test_eq(cpl_image_get_flux(out_mask), 1.);
cpl_test_eq(cpl_image_get(out_mask, 1, 1, &rej), 1.);
cpl_image_delete(out_mask);
hdrl_parameter_delete(p);
hdrl_imagelist_delete(hl);
cpl_vector_delete(sample);
}
void test_igam(void){
cpl_test_error(CPL_ERROR_NONE);
/* Fail test */
cpl_test(isnan(igamc(-1, 1)));
cpl_test(isnan(igamc(1, -1)));
cpl_test(isnan(igamc(-1, -1)));
cpl_test(!isnan(igamc(1, 1)));
/* Normal test */
double a = 1.;
double x = 2.;
double res1 = igamc(a, x);
cpl_test_eq(res1, 0.);
double res2 = igamc(x, a);
cpl_test_eq(res2, 0.);
double res3 = igam(a, x);
cpl_test_eq(res3, 0.);
double res4 = igam(x, a);
cpl_test_eq(res4, 0.);
double res11 = igamc(0., 0.);
cpl_test(res11 != 0.);
double res12 = igamc(0., 1.);
cpl_test(res12 != 0.);
double res13 = igamc(1., 0.);
cpl_test(res13 != 0.);
double res21 = igamc(0., 1e10);
cpl_test(res21 != 0.);
double res22 = igamc(1., 1e20);
cpl_test_eq(res22, 0.);
double res23 = igamc(1e10, 0.);
cpl_test_eq(res23, 1.);
double res24 = igamc(1e20, 1.);
cpl_test_eq(res24, 1.);
cpl_test_error(CPL_ERROR_NONE);
}
/*----------------------------------------------------------------------------*/
/**
@brief Unit tests of BPM module
**/
/*----------------------------------------------------------------------------*/
int main(void)
{
cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
test_invalid_parameter();
test_parameter();
test_parameterlist();
test_bpm_fit();
test_igam();
return cpl_test_end(0);
}
|