File: hdrl_spectrum1dlist-test.c

package info (click to toggle)
cpl-plugin-uves 6.1.3%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 23,136 kB
  • sloc: ansic: 171,056; sh: 4,368; python: 3,002; makefile: 1,319
file content (487 lines) | stat: -rw-r--r-- 16,082 bytes parent folder | download | duplicates (12)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
 * This file is part of the HDRL
 * Copyright (C) 2017 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 "hdrl.h"
#include "hdrl_spectrum_resample.h"

#include <math.h>


hdrl_spectrum1D * create_spectrum(double fx){
    cpl_image * flx = cpl_image_new(1, 1, HDRL_TYPE_DATA);
    cpl_array * wln = cpl_array_new(1, HDRL_TYPE_DATA);
    cpl_image_set(flx, 1, 1 , fx);
    cpl_array_set(wln, 0, 100);
    hdrl_spectrum1D * s = hdrl_spectrum1D_create_error_free(flx, wln,
            hdrl_spectrum1D_wave_scale_linear);

    cpl_array_delete(wln);
    cpl_image_delete(flx);

    return s;
}

void check_equal(const hdrl_spectrum1D * s1, const hdrl_spectrum1D * s2){

    const cpl_size sz = hdrl_spectrum1D_get_size(s1);
    cpl_test_eq(sz, hdrl_spectrum1D_get_size(s2));

    for(cpl_size i = 0; i < sz; ++i){
        int rej1 = 0, rej2 = 0;
        hdrl_value v1 = hdrl_spectrum1D_get_flux_value(s1, i, &rej1);
        hdrl_value v2 = hdrl_spectrum1D_get_flux_value(s2, i, &rej2);

        cpl_test_eq(rej1, rej2);
        cpl_test_rel(v1.data, v2.data, 1e-5);
        cpl_test_rel(v1.error, v2.error, 1e-5);
    }
}

void check_list_sequential(const hdrl_spectrum1Dlist * list,
        cpl_size idx, double mlx){

    for(cpl_size i = 0; i < hdrl_spectrum1Dlist_get_size(list); ++i){
        const hdrl_spectrum1D * s = hdrl_spectrum1Dlist_get_const(list, i);
        hdrl_value v  = hdrl_spectrum1D_get_flux_value(s, 0, NULL);
        cpl_test_rel(v.data, idx * mlx, 1e-10);
        idx++;
    }

}

void test_spectrum1dlist_wrap(void){
    hdrl_spectrum1D ** sl = cpl_calloc(6, sizeof(hdrl_spectrum1D*));

    sl[0] = create_spectrum(1);
    sl[1] = create_spectrum(2);
    sl[2] = create_spectrum(3);
    sl[3] = create_spectrum(4);
    sl[4] = create_spectrum(5);
    sl[5] = create_spectrum(6);

    hdrl_spectrum1Dlist * list = hdrl_spectrum1Dlist_wrap(sl, 6);

    cpl_test_eq(hdrl_spectrum1Dlist_get_size(list), 6);

    for(cpl_size i = 0; i < hdrl_spectrum1Dlist_get_size(list); ++i){
        const hdrl_spectrum1D * s = hdrl_spectrum1Dlist_get_const(list, i);
        cpl_test_eq_ptr(s, sl[i]);
    }

    hdrl_spectrum1Dlist_delete(list);
}

void test_spectrum1dlist(void){

    hdrl_spectrum1Dlist * list1 = hdrl_spectrum1Dlist_new();

    cpl_test_eq(0, hdrl_spectrum1Dlist_get_size(list1));

    hdrl_spectrum1D * s4 = create_spectrum(4);

    {
        hdrl_spectrum1D * s1 = create_spectrum(1);
        hdrl_spectrum1D * s11 = create_spectrum(1);
        hdrl_spectrum1D * s2 = create_spectrum(2);
        hdrl_spectrum1D * s3 = create_spectrum(3);
        hdrl_spectrum1D * s5 = create_spectrum(5);
        hdrl_spectrum1D * s6 = create_spectrum(6);

        hdrl_spectrum1Dlist_set(list1, s1, 0);
        /*Removed s1 when setting*/
        hdrl_spectrum1Dlist_set(list1, s11, 0);
        hdrl_spectrum1Dlist_set(list1, s2, 1);
        hdrl_spectrum1Dlist_set(list1, s3, 2);
        hdrl_spectrum1Dlist_set(list1, s4, 3);
        hdrl_spectrum1Dlist_set(list1, s5, 4);
        hdrl_spectrum1Dlist_set(list1, s6, 5);
    }

    cpl_test_eq(6, hdrl_spectrum1Dlist_get_size(list1));

    /* Check duplication */
    hdrl_spectrum1Dlist * list2 = hdrl_spectrum1Dlist_duplicate(list1);

    cpl_test_eq(hdrl_spectrum1Dlist_get_size(list1),
            hdrl_spectrum1Dlist_get_size(list2));

    for(cpl_size i = 0; i < hdrl_spectrum1Dlist_get_size(list1); ++i){
        const hdrl_spectrum1D * s1 = hdrl_spectrum1Dlist_get_const(list1, i);
        const hdrl_spectrum1D * s2 = hdrl_spectrum1Dlist_get_const(list2, i);

        check_equal(s1, s2);

        /*check that list1 and list2 point to different memory spaces*/
        cpl_test_noneq_ptr(s1, s2);
    }

    /*Check getters (mutability)*/
    for(cpl_size i = 0; i < hdrl_spectrum1Dlist_get_size(list1); ++i){
        hdrl_spectrum1D * s = hdrl_spectrum1Dlist_get(list2, i);
        hdrl_value v  = hdrl_spectrum1D_get_flux_value(s, 0, NULL);
        cpl_test_rel(v.data, (double)i + 1.0 , 1e-5);
        hdrl_spectrum1D_mul_scalar(s, (hdrl_value){5.0, 0.0});
    }

    for(cpl_size i = 0; i < hdrl_spectrum1Dlist_get_size(list1); ++i){
        hdrl_spectrum1D * s = hdrl_spectrum1Dlist_get(list2, i);
        hdrl_value v  = hdrl_spectrum1D_get_flux_value(s, 0, NULL);
        cpl_test_rel(v.data, ((double)i + 1.0) * 5.0 , 1e-5);
    }

    hdrl_spectrum1D * new_s4 = hdrl_spectrum1Dlist_unset(list1, 3);

    cpl_test_eq_ptr(new_s4, s4);

    cpl_test_eq(hdrl_spectrum1Dlist_get_size(list1), 5);

    const double flx_value[] = {1, 2, 3, 5, 6};
    for(cpl_size i = 0; i < hdrl_spectrum1Dlist_get_size(list1); ++i){

        const hdrl_spectrum1D * s = hdrl_spectrum1Dlist_get_const(list1, i);
        hdrl_value v  = hdrl_spectrum1D_get_flux_value(s, 0, NULL);
        cpl_test_rel(v.data, flx_value[i], 1e-5);
    }

    cpl_size i = 1;
    while(hdrl_spectrum1Dlist_get_size(list2)){
        hdrl_spectrum1D * s = hdrl_spectrum1Dlist_unset(list2, 0);
        check_list_sequential(list2, i + 1, 5.0);
        hdrl_value v  = hdrl_spectrum1D_get_flux_value(s, 0, NULL);
        cpl_test_rel(v.data, i * 5.0, 1e-5);
        hdrl_spectrum1D_delete(&s);
        i++;
    }

    hdrl_spectrum1Dlist_delete(list2);
    hdrl_spectrum1Dlist_delete(list1);
    hdrl_spectrum1D_delete(&s4);
}
 /*data[i] is both wavelength and flux. if data[i] the i-th pixel is rejected*/
hdrl_spectrum1D *
create_spectrum_long(const double * data, const cpl_size length){
    cpl_array * wlens = cpl_array_new(length, HDRL_TYPE_DATA);
    cpl_image * flx = cpl_image_new(length, 1, HDRL_TYPE_DATA);

    for(cpl_size i = 0; i < length; ++i){
        cpl_array_set(wlens, i, fabs(data[i]));

        if(data[i] >= 0)
            cpl_image_set(flx, i + 1, 1, data[i]);
        else
            cpl_image_reject(flx, i + 1, 1);
    }

    hdrl_spectrum1D * s = hdrl_spectrum1D_create_error_free(flx, wlens,
            hdrl_spectrum1D_wave_scale_linear);

    cpl_image_delete(flx);
    cpl_array_delete(wlens);
    return s;
}

/*check that bad pixels at the beginning or at the end of spectrum
 * do not contribute to the collapsed spectrum*/
void test_spectrum1dlist_collapse_badpix(void){

    hdrl_spectrum1Dlist * l = hdrl_spectrum1Dlist_new();

    double aValues1[4] = {1, 2, 3, 4};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues1, 4), 0);

    double aValues2[3] = {-1, 2, 4};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues2, 3), 1);

    double aValues3[3] = {1, 3, -4};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues3, 3), 2);

    cpl_array * wlenghts = cpl_array_new(6, HDRL_TYPE_DATA);
    for(cpl_size i = 0; i < cpl_array_get_size(wlenghts); ++i){
        cpl_array_set(wlenghts, i, i);
    }

    hdrl_parameter * stacking_par = hdrl_collapse_mean_parameter_create();
    hdrl_parameter * resampling_par =
            hdrl_spectrum1D_resample_interpolate_parameter_create(hdrl_spectrum1D_interp_linear);

    hdrl_spectrum1D * res = NULL;
    cpl_image * contrib = NULL;
    hdrl_imagelist * aligned_fluxes = NULL;

    hdrl_spectrum1Dlist_collapse(l, stacking_par, wlenghts, resampling_par, CPL_FALSE,
            &res, &contrib, &aligned_fluxes);

    int contribs[] = {2, 3, 3, 2};
    for(cpl_size i = 1; i < 5; ++i){
        int rej = 0;
        const double el = cpl_image_get(contrib, i + 1, 1, &rej);
        cpl_test_eq(rej, 0);
        cpl_test_eq(el, contribs[i - 1]);
    }

    int reject = 0;
    double el = cpl_image_get(contrib, 6, 1, &reject);
    cpl_test_eq(reject, 0);
    cpl_test_eq(el, 0);

    reject = 0;
    el = cpl_image_get(contrib, 1, 1, &reject);
    cpl_test_eq(reject, 0);
    cpl_test_eq(el, 0);

    const cpl_size sz = hdrl_spectrum1D_get_size(res);
    cpl_test_eq(cpl_array_get_size(wlenghts), sz);

    for(cpl_size i = 0; i < sz; ++i){
        int rej = 0;
        const hdrl_value v = hdrl_spectrum1D_get_flux_value(res, i, &rej);
        if(i == 0 || i == (sz - 1)){
            cpl_test_eq(rej, 1);
            continue;
        }
        cpl_test_rel(v.data, i, 1e-5);
        cpl_test_eq(rej, 0);
    }

    hdrl_spectrum1Dlist_delete(l);
    hdrl_spectrum1D_delete(&res);
    cpl_image_delete(contrib);
    hdrl_parameter_delete(stacking_par);
    hdrl_parameter_delete(resampling_par);
    hdrl_imagelist_delete(aligned_fluxes);
    cpl_array_delete(wlenghts);
}

/*Check that resamplking pixels having rejected neighbors in the original spectrum
 * do not contribute to the stacking*/
void test_spectrum1dlist_collapse_mark_rej_in_interpolation(void){

    hdrl_spectrum1Dlist * l = hdrl_spectrum1Dlist_new();

    double aValues1[4] = {1, 2, 3, 4};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues1, 4), 0);

    double aValues2[4] = {-1, 2, -3, 4};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues2, 4), 1);

    double aValues3[4] = {1,-2, 3, -4};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues3, 4), 2);

    cpl_array * wlenghts = cpl_array_new(6, HDRL_TYPE_DATA);
    for(cpl_size i = 0; i < cpl_array_get_size(wlenghts); ++i){
        cpl_array_set(wlenghts, i, i);
    }

    hdrl_parameter * stacking_par = hdrl_collapse_mean_parameter_create();
    hdrl_parameter * resampling_par =
            hdrl_spectrum1D_resample_interpolate_parameter_create(hdrl_spectrum1D_interp_linear);

    hdrl_spectrum1D * res = NULL;
    cpl_image * contrib = NULL;
    hdrl_imagelist * aligned_fluxes = NULL;

    hdrl_spectrum1Dlist_collapse(l, stacking_par, wlenghts, resampling_par, CPL_TRUE,
            &res, &contrib, &aligned_fluxes);

    int contribs[] = {2, 2, 2, 2};
    for(cpl_size i = 1; i < 5; ++i){
        int reject = 0;
        const double el = cpl_image_get(contrib, i + 1, 1, &reject);
        cpl_test_eq(reject, 0);
        cpl_test_eq(el, contribs[i - 1]);
    }

    int rej_contr = 0;
    double el = cpl_image_get(contrib, 6, 1, &rej_contr);
    cpl_test_eq(rej_contr, 0);
    cpl_test_eq(el, 0);

    rej_contr = 0;
    el = cpl_image_get(contrib, 1, 1, &rej_contr);
    cpl_test_eq(rej_contr, 0);
    cpl_test_eq(el, 0);

    const cpl_size sz = hdrl_spectrum1D_get_size(res);
    cpl_test_eq(cpl_array_get_size(wlenghts), sz);

    for(cpl_size i = 0; i < sz; ++i){
        int rej = 0;
        const hdrl_value v = hdrl_spectrum1D_get_flux_value(res, i, &rej);
        if(i == 0 || i == (sz - 1)){
            cpl_test_eq(rej, 1);
            continue;
        }
        cpl_test_rel(v.data, i, 1e-5);
        cpl_test_eq(rej, 0);
    }

    hdrl_spectrum1Dlist_delete(l);
    hdrl_spectrum1D_delete(&res);
    cpl_image_delete(contrib);
    hdrl_parameter_delete(stacking_par);
    hdrl_parameter_delete(resampling_par);
    hdrl_imagelist_delete(aligned_fluxes);
    cpl_array_delete(wlenghts);
}

/*check that shorted spectra are handled correctly*/
void test_spectrum1dlist_collapse_holes(void){

    hdrl_spectrum1Dlist * l = hdrl_spectrum1Dlist_new();

    double aValues1[4] = {1, 2, 3, 4};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues1, 4), 0);

    double aValues2[2] = {2, 4};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues2, 2), 1);

    double aValues3[2] = {1, 3};
    hdrl_spectrum1Dlist_set(l, create_spectrum_long(aValues3, 2), 2);

    cpl_array * wlenghts = cpl_array_new(6, HDRL_TYPE_DATA);
    for(cpl_size i = 0; i < cpl_array_get_size(wlenghts); ++i){
        cpl_array_set(wlenghts, i, i);
    }

    hdrl_parameter * stacking_par = hdrl_collapse_mean_parameter_create();
    hdrl_parameter * resampling_par =
            hdrl_spectrum1D_resample_interpolate_parameter_create(hdrl_spectrum1D_interp_linear);

    hdrl_spectrum1D * res = NULL;
    cpl_image * contrib = NULL;
    hdrl_imagelist * aligned_fluxes = NULL;
    hdrl_spectrum1Dlist_collapse(l, stacking_par, wlenghts, resampling_par,
            CPL_FALSE, &res, &contrib, &aligned_fluxes);

    int contribs[] = {2, 3, 3, 2};
    for(cpl_size i = 1; i < 5; ++i){
        int rej_contr = 0;
        const double el = cpl_image_get(contrib, i + 1, 1, &rej_contr);
        cpl_test_eq(rej_contr, 0);
        cpl_test_eq(el, contribs[i - 1]);
    }

    int rej_contrib = 0;
    double el = cpl_image_get(contrib, 6, 1, &rej_contrib);
    cpl_test_eq(rej_contrib, 0);
    cpl_test_eq(el, 0);

    rej_contrib = 0;
    el = cpl_image_get(contrib, 1, 1, &rej_contrib);
    cpl_test_eq(rej_contrib, 0);
    cpl_test_eq(el, 0);

    const cpl_size sz = hdrl_spectrum1D_get_size(res);
    cpl_test_eq(cpl_array_get_size(wlenghts), sz);

    for(cpl_size i = 0; i < sz; ++i){
        int rej = 0;
        const hdrl_value v = hdrl_spectrum1D_get_flux_value(res, i, &rej);
        if(i == 0 || i == (sz - 1)){
            cpl_test_eq(rej, 1);
            continue;
        }
        cpl_test_rel(v.data, i, 1e-5);
        cpl_test_eq(rej, 0);
    }

    hdrl_spectrum1Dlist_delete(l);
    hdrl_spectrum1D_delete(&res);
    cpl_image_delete(contrib);
    hdrl_imagelist_delete(aligned_fluxes);
    hdrl_parameter_delete(stacking_par);
    hdrl_parameter_delete(resampling_par);
    cpl_array_delete(wlenghts);
}

void test_error_and_reset(cpl_error_code expected){
    cpl_test_eq_error(expected, cpl_error_get_code());
    cpl_error_reset();
}

void test_spectrum1dlist_insert_duplication(void){

    hdrl_spectrum1Dlist * list1 = hdrl_spectrum1Dlist_new();

    cpl_test_eq(0, hdrl_spectrum1Dlist_get_size(list1));

    hdrl_spectrum1D * s1 = create_spectrum(1);
    hdrl_spectrum1D * s2 = create_spectrum(2);
    hdrl_spectrum1D * s3 = create_spectrum(3);
    hdrl_spectrum1D * s4 = create_spectrum(4);
    hdrl_spectrum1D * s5 = create_spectrum(5);
    hdrl_spectrum1D * s6 = create_spectrum(6);

    hdrl_spectrum1Dlist_set(list1, s1, 0);
    hdrl_spectrum1Dlist_set(list1, s2, 1);
    hdrl_spectrum1Dlist_set(list1, s3, 2);
    hdrl_spectrum1Dlist_set(list1, s4, 3);
    hdrl_spectrum1Dlist_set(list1, s5, 4);
    hdrl_spectrum1Dlist_set(list1, s6, 5);

    hdrl_spectrum1Dlist_set(list1, s1, 4);
    test_error_and_reset(CPL_ERROR_ILLEGAL_INPUT);

    hdrl_spectrum1Dlist_set(list1, s2, 3);
    test_error_and_reset(CPL_ERROR_ILLEGAL_INPUT);

    hdrl_spectrum1Dlist_set(list1, s3, 4);
    test_error_and_reset(CPL_ERROR_ILLEGAL_INPUT);

    hdrl_spectrum1Dlist_set(list1, s4, 5);
    test_error_and_reset(CPL_ERROR_ILLEGAL_INPUT);

    hdrl_spectrum1Dlist_set(list1, s5, 0);
    test_error_and_reset(CPL_ERROR_ILLEGAL_INPUT);

    hdrl_spectrum1Dlist_set(list1, s6, 2);
    test_error_and_reset(CPL_ERROR_ILLEGAL_INPUT);

    hdrl_spectrum1Dlist_delete(list1);
}

/*----------------------------------------------------------------------------*/
/**
  @brief   Unit tests of efficiency calculation module
 **/
/*----------------------------------------------------------------------------*/
int main(void)
{
    cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);

    test_spectrum1dlist();
    test_spectrum1dlist_wrap();
    test_spectrum1dlist_collapse_holes();
    test_spectrum1dlist_collapse_badpix();
    test_spectrum1dlist_collapse_mark_rej_in_interpolation();
    test_spectrum1dlist_insert_duplication();

    cpl_test_error(CPL_ERROR_NONE);

    return cpl_test_end(0);
}