File: hdrl_der_snr-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 (300 lines) | stat: -rw-r--r-- 8,984 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
/*
 * 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_DER_SNR.h"
#include "hdrl_spectrum.h"
#include "hdrl_random.h"

#include <cpl.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>



/*----------------------------------------------------------------------------*/
/**
 * @defgroup hdrl_der_snr_test
 */
/*----------------------------------------------------------------------------*/

static inline cpl_image * get_noisy_flux(const cpl_image * img);

#define ARR_SIZE(a) ((cpl_size)((sizeof((a)) / sizeof((a[0])))))

static inline
cpl_image * get_error(const int* data, const int* map, cpl_size length){
    /* map[j] is the position in unsorted  where the j-th element is going to
     * be inserted*/
    cpl_array * wav = cpl_array_new(length, HDRL_TYPE_DATA);
    cpl_image * flux = cpl_image_new(length, 1, HDRL_TYPE_DATA);

    for(cpl_size j = 0; j < length; ++j){
       cpl_image_set(flux, map[j] + 1, 1, data[j]);
       cpl_array_set(wav, map[j], j + 1);
    }

    const hdrl_data_t *flux_p =
           (const hdrl_data_t *)cpl_image_get_data_const(flux);

    const cpl_binary *msk  = NULL;
    cpl_image *flux_e = estimate_noise_DER_SNR(flux_p, msk, wav, length, 5);

    cpl_image_delete(flux);
    cpl_array_delete(wav);

    return flux_e;
}

void test_DER_SNR_sort(void){

    int data[] = {
            1, 5, 10, 5, 23, 1, 8, 17, 21, 7, 11, 13, 5, 99, 12, 4,
            1, 5, 10, 5, 23, 1, 8, 17, 21, 7, 11, 13, 5, 99, 12, 4
    };

    int map1[ARR_SIZE(data)] = {0};
    int map2[ARR_SIZE(data)] = {0};

    const cpl_size length = ARR_SIZE(data);

    for(cpl_size i = 0; i < length; ++i){
        map1[i] = i;
        map2[i] = i;
    }

    for(cpl_size i = 0; i < length; ++i){

          const cpl_size source = i;
          const cpl_size dest = ((double)rand() / (double)RAND_MAX) * (length - 1);

          cpl_test(dest >= 0 && dest < length);

          const int data_s = map1[source];
          const int data_d = map1[dest];

          map1[source] = data_d;
          map1[dest] = data_s;
      }

    cpl_image * flux_e_unsorted = get_error(data, map1, length);
    cpl_image * flux_e_sorted = get_error(data, map2, length);

    /* map1[i] is the position in unsorted  where the i-th element of sorted
     * is inserted.*/
    for(cpl_size i = 0; i < length; ++i){
        int rej1;
        const double data1 = cpl_image_get(flux_e_sorted, i + 1, 1, &rej1);

        int rej2;
        const double data2 = cpl_image_get(flux_e_unsorted, map1[i] + 1, 1, &rej2);

        cpl_test_eq(rej1, rej2);
        cpl_test_abs(data1, data2, 1e-3);
    }

    cpl_image_delete(flux_e_sorted);
    cpl_image_delete(flux_e_unsorted);
}

void test_DER_SNR(void){

    int data[] = {
            1, 5, 10, 5, 23, 1, 8, 17, 21, 7, 11, 13, 5, 99, 12, 4,
            1, 5, 10, 5, 23, 1, 8, 17, 21, 7, 11, 13, 5, 99, 12, 4
    };

    cpl_array *  wav = cpl_array_new(ARR_SIZE(data), HDRL_TYPE_DATA);
    cpl_image * flux = cpl_image_new(ARR_SIZE(data), 1, HDRL_TYPE_DATA);

    for(cpl_size i = 0; i < ARR_SIZE(data); ++i){
        cpl_image_set(flux, i +1, 1, data[i]);
        cpl_array_set(wav, i, i + 1);
    }

    const hdrl_data_t * flux_p =
            (const hdrl_data_t *)cpl_image_get_data_const(flux);

    const cpl_binary *msk = NULL;
    hdrl_data_t err =
            estimate_noise_window(flux_p, msk, 0,
                    ARR_SIZE(data) - 1, ARR_SIZE(data));
    cpl_test_abs(err, 12.105, 1e-3);

    cpl_image * flux_e = estimate_noise_DER_SNR(flux_p, msk, wav,
            ARR_SIZE(data), 5);

    cpl_test_eq(cpl_image_get_size_x(flux), cpl_image_get_size_x(flux_e));
    cpl_test_eq(cpl_image_get_size_y(flux), cpl_image_get_size_y(flux_e));

    int rej = 0;
    err = cpl_image_get(flux_e, 7, 1, &rej);
    cpl_test_abs(err, 13.921, 1e-3);

    err = cpl_image_get(flux_e, 6, 1, &rej);
    cpl_test_abs(err, 13.921, 1e-3);

    err = cpl_image_get(flux_e, 1, 1, &rej);
    cpl_test_abs(err, 2.421, 1e-3);

    cpl_image_delete(flux_e);


    /*bad pixel is bad also in error*/
    cpl_image_reject(flux, 2, 1);

    flux_p = (const hdrl_data_t *)cpl_image_get_data_const(flux);
    const cpl_mask *mask = cpl_image_get_bpm_const(flux);
    msk  = cpl_mask_get_data_const(mask);

    flux_e = estimate_noise_DER_SNR(flux_p, msk, wav, ARR_SIZE(data), 5);

    cpl_test(cpl_image_is_rejected(flux_e, 2, 1));

    /*bad pixels in window are skipped*/
    err = cpl_image_get(flux_e, 6, 1, &rej);
    cpl_test_abs(err, 14.829, 1e-3);
    cpl_test_eq(rej, 0);

    /*good pixel surrounded by bad pixels so that there is no pixel to calculate
     * error, becomes bad*/
    cpl_image_delete(flux_e);

    for(cpl_size i = 1; i <= 11; i++)
    {
        if(i == 6) continue;
        cpl_image_reject(flux, i, 1);
    }

    flux_p = (const hdrl_data_t *)cpl_image_get_data_const(flux);
    mask = cpl_image_get_bpm_const(flux);
    msk  = cpl_mask_get_data_const(mask);

    flux_e = estimate_noise_DER_SNR(flux_p, msk, wav, ARR_SIZE(data), 5);
    cpl_test(cpl_image_is_rejected(flux_e, 6, 1));
    cpl_test(!cpl_image_is_rejected(flux, 6, 1));

    cpl_image_delete(flux_e);
    cpl_image_delete(flux);
    cpl_array_delete(wav);
}

void test_DER_SNR_performance(void){

    static const int sz = 2000;
    static const double delta = 3.14 / 2000.0;
    static const double peak = 1e3;
    static const int n_iter = 100;

    cpl_image * flux = cpl_image_new(sz, 1, HDRL_TYPE_DATA);
    cpl_array * lambdas = cpl_array_new(sz, HDRL_TYPE_DATA);

    for(cpl_size i = 0; i < sz; i++){
        double x = delta * (i+1);
        cpl_array_set(lambdas, i, x);
        cpl_image_set(flux, i + 1, 1, peak * sin(x));
    }

    cpl_image * std_dev_theo = cpl_image_power_create(flux, 1./2.);
    cpl_image * DER_SNR_avg = cpl_image_new(sz, 1,HDRL_TYPE_DATA);

    for(cpl_size i = 0; i < n_iter; ++i){
        cpl_image * noisy_flux_i = get_noisy_flux(flux);

        const hdrl_data_t * noisy_flux =
                (const hdrl_data_t *)cpl_image_get_data_const(noisy_flux_i);

        const cpl_binary *msk = NULL;
        cpl_image * e = estimate_noise_DER_SNR(noisy_flux, msk, lambdas, sz, 5);

        cpl_image_divide_scalar(e, n_iter);
        cpl_image_add(DER_SNR_avg, e);
        cpl_image_delete(e);

        cpl_image_delete(noisy_flux_i);
    }

    cpl_image * ratio = cpl_image_divide_create(std_dev_theo, DER_SNR_avg);
    double avg_ratio = cpl_image_get_absflux(ratio) / sz;

    cpl_test_leq(avg_ratio, 1.1);
    cpl_test_leq(.9, avg_ratio);

    cpl_image_delete(flux);
    cpl_image_delete(DER_SNR_avg);
    cpl_array_delete(lambdas);
    cpl_image_delete(ratio);
    cpl_image_delete(std_dev_theo);
}

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

    srand (time(NULL));

    test_DER_SNR();
    test_DER_SNR_sort();
    test_DER_SNR_performance();

    return cpl_test_end(0);
}

/*----------------------------------------------------------------------------*/
/**
  @brief   Private functions
 **/
/*----------------------------------------------------------------------------*/
static inline
cpl_image * get_noisy_flux(const cpl_image * img){

    cpl_size sx = cpl_image_get_size_x(img);
    cpl_size sy = cpl_image_get_size_y(img);

    cpl_image * to_ret = cpl_image_new(sx, sy, HDRL_TYPE_DATA);

    hdrl_random_state * rng = hdrl_random_state_new(1, NULL);

    for(cpl_size x = 1; x <= sx; x++){
        for(cpl_size y = 1; y <= sy; y++){
            int rej = 0;
            double clean_value = cpl_image_get(img, x, y, &rej);
            double noisy_value = hdrl_random_poisson(rng, clean_value);
            cpl_image_set(to_ret, x, y, noisy_value);
        }
    }

    hdrl_random_state_delete(rng);
    return to_ret;
}