CR2RE Pipeline Reference Manual 1.6.10
hdrl_prototyping-test.c
1/* $Id: hdrl_prototyping-test.c,v 1.0 2017-09-18 Exp $
2 *
3 * This file is part of the HDRL
4 * Copyright (C) 2013 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25/*-----------------------------------------------------------------------------
26 Includes
27 -----------------------------------------------------------------------------*/
28
29#include "hdrl_prototyping.h"
30
31#include <cpl.h>
32
33/*----------------------------------------------------------------------------*/
37/*----------------------------------------------------------------------------*/
38
39void test_prototyping_spatial_freq(void)
40{
41 int dim_X = 64;
42 int dim_Y = 64;
43 cpl_image *image = cpl_image_new(dim_X, dim_Y, CPL_TYPE_DOUBLE);
44
45
46 double gausfilt = 1.0;
47 int mirrorx = 10;
48 int mirrory = 10;
49
50
51 /* Function to calculate the low spatial frequency */
52 cpl_image *out1 = hdrl_get_spatial_freq(image, gausfilt, mirrorx, mirrory);
53 cpl_test_error(CPL_ERROR_NONE);
54 cpl_image_delete(out1);
55 cpl_image_delete(image);
56
57 image = cpl_image_new(1, 1, CPL_TYPE_INT);
58 cpl_image *out2 = hdrl_get_spatial_freq(image, gausfilt, 0, 0);
59 cpl_test_error(CPL_ERROR_NONE);
60 cpl_image_delete(image);
61 cpl_image_delete(out2);
62}
63
64
65void test_mime_image_polynomial_bkg(void)
66{
67 int dim_X = 10;
68 int dim_Y = 10;
69 cpl_image *image = NULL;
70 cpl_matrix *coeffs = NULL;
71 cpl_image *out = hdrl_mime_image_polynomial_bkg(image, dim_X, dim_Y, &coeffs);
72 cpl_test_error(CPL_ERROR_NULL_INPUT);
73 cpl_test_null(out);
74
75 image = cpl_image_new(dim_X, dim_Y, CPL_TYPE_DOUBLE);
76 out = hdrl_mime_image_polynomial_bkg(image, dim_X, dim_Y, &coeffs);
77 cpl_test_error(CPL_ERROR_NONE);
78 cpl_test_nonnull(out);
79 cpl_test_nonnull(coeffs);
80
81 cpl_image_delete(image);
82 cpl_image_delete(out);
83 cpl_matrix_delete(coeffs);
84}
85
86void test_mime_compute_polynomial_bkg(void){
87
88 cpl_imagelist *images = NULL;
89 cpl_imagelist *bkg_images = NULL;
90 int dim_X = 10;
91 int dim_Y = 10;
92 cpl_matrix *coeffs = NULL;
93
94
95 /* Test input wrong */
96
97 hdrl_mime_compute_polynomial_bkg(NULL, bkg_images, dim_X, dim_Y, &coeffs);
98 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
99
100 hdrl_mime_compute_polynomial_bkg(images, bkg_images, dim_X, dim_Y, &coeffs);
101 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
102
103 /* Normal execution */
104 cpl_image *image = cpl_image_new(dim_X, dim_Y, CPL_TYPE_DOUBLE);
105 images = cpl_imagelist_new();
106 cpl_imagelist_set(images, cpl_image_duplicate(image), 0);
107 cpl_imagelist_set(images, cpl_image_duplicate(image), 1);
108 /* hdrl_mime_compute_polynomial_bkg(images, bkg_images, dim_X, dim_Y, &coeffs);
109 cpl_test_error(CPL_ERROR_NULL_INPUT);
110 cpl_matrix_delete(coeffs);
111*/
112 bkg_images = cpl_imagelist_new();
113 cpl_imagelist_set(bkg_images, cpl_image_duplicate(image), 0);
114 cpl_imagelist_set(bkg_images, cpl_image_duplicate(image), 1);
115/* hdrl_mime_compute_polynomial_bkg(images, bkg_images, dim_X, dim_Y, &coeffs);
116 cpl_test_error(CPL_ERROR_TYPE_MISMATCH);
117 cpl_matrix_delete(coeffs);
118*/
119 /* Clean up */
120 cpl_image_delete(image);
121 cpl_imagelist_delete(images);
122 cpl_imagelist_delete(bkg_images);
123}
124
125void test_mime_legendre_polynomials_create(void){
126
127 int npoly = 0;
128 double a = 2;
129 double b = 2;
130 cpl_matrix *x = NULL;
131 cpl_matrix *legendre;
132
133
134 /* Test null inputs */
135
136 legendre = hdrl_mime_legendre_polynomials_create(npoly, a, b, x);
137 cpl_test_error(CPL_ERROR_NULL_INPUT);
138 cpl_test_null(legendre);
139
140 x = cpl_matrix_new(a, b);
141 legendre = hdrl_mime_legendre_polynomials_create(npoly, a, b, x);
142 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
143 cpl_test_null(legendre);
144 cpl_matrix_delete(x);
145
146
147 /* Normal execution */
148 npoly = 2;
149 a = 3;
150 b = 5;
151 x = cpl_matrix_new(a, b);
152 legendre = hdrl_mime_legendre_polynomials_create(npoly, a, b, x);
153 cpl_test_error(CPL_ERROR_NONE);
154 cpl_test_nonnull(legendre);
155 cpl_matrix_delete(legendre);
156 cpl_matrix_delete(x);
157
158}
159
160
161void test_mime_legendre_tensors_create(void)
162{
163 int nx = 2;
164 int ny = 2;
165 int npx = 1;
166 int npy = 1;
167
168 cpl_matrix *out;
169
170 /* Test wrong input */
171
172 out = hdrl_mime_legendre_tensors_create(0, ny, npx, npy);
173 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
174 cpl_test_null(out);
175
176 out = hdrl_mime_legendre_tensors_create(nx, 0, npx, npy);
177 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
178 cpl_test_null(out);
179
180 out = hdrl_mime_legendre_tensors_create(nx, ny, 0, npy);
181 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
182 cpl_test_null(out);
183
184 out = hdrl_mime_legendre_tensors_create(nx, ny, npx, 0 );
185 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
186 cpl_test_null(out);
187
188
189 /* Normal work */
190 out = hdrl_mime_legendre_tensors_create(nx, ny, npx, npy);
191 cpl_test_error(CPL_ERROR_NONE);
192 cpl_test_nonnull(out);
193 cpl_matrix_delete(out);
194}
195
196void test_mime_matrix_linspace_create(void)
197{
198 int n = 1;
199 double a = 2;
200 double b = 4;
201
202 /* Test wrong input */
203 cpl_matrix *out = hdrl_mime_matrix_linspace_create(n, a, b);
204 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
205 cpl_test_null(out);
206
207 /* Normal work */
208 n = 2;
210 cpl_test_error(CPL_ERROR_NONE);
211 cpl_test_nonnull(out);
212 cpl_matrix_delete(out);
213}
214
215void test_mime_matrix_copy_column(void)
216{
217 int j_1 = 1;
218 int j_2 = 1;
219 int nx = 2;
220 int ny = 4;
221 cpl_matrix *mat1 = cpl_matrix_new(nx, ny);
222 cpl_matrix *mat2 = cpl_matrix_new(nx, ny);
223
224 cpl_matrix *mat = cpl_matrix_new(ny, nx);
225
226
227 /* Test wrong input */
228
229 hdrl_mime_matrix_copy_column(NULL, j_1, mat2, j_2);
230 cpl_test_error(CPL_ERROR_NULL_INPUT);
231
232 hdrl_mime_matrix_copy_column(mat1, j_1, NULL, j_2);
233 cpl_test_error(CPL_ERROR_NULL_INPUT);
234
235 hdrl_mime_matrix_copy_column(mat1, j_1, mat, j_2);
236 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
237
238 hdrl_mime_matrix_copy_column(mat1, -1, mat2, j_2);
239 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
240
241 hdrl_mime_matrix_copy_column(mat1, 10, mat2, j_2);
242 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
243
244 hdrl_mime_matrix_copy_column(mat1, -1, mat2, j_2);
245 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
246
247 hdrl_mime_matrix_copy_column(mat1, j_1, mat2, -1);
248 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
249
250 hdrl_mime_matrix_copy_column(mat1, j_1, mat2, 10);
251 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
252
253
254 /* Normal work */
255 hdrl_mime_matrix_copy_column(mat1, j_1, mat2, j_2);
256 cpl_test_error(CPL_ERROR_NONE);
257
258
259 cpl_matrix_delete(mat );
260 cpl_matrix_delete(mat1);
261 cpl_matrix_delete(mat2);
262}
263
264
265void test_mime_linalg_pairwise_column_tensor_products_create(void)
266{
267 int nx = 4;
268 int ny = 4;
269
270 cpl_matrix *mat1 = NULL;
271 cpl_matrix *mat2 = NULL;
272
273 cpl_matrix *out;
274
275 /* Test wrong inputs */
277 cpl_test_error(CPL_ERROR_NULL_INPUT);
278 cpl_test_null(out);
279
280 mat1 = cpl_matrix_new(nx, ny);
282 cpl_test_error(CPL_ERROR_NULL_INPUT);
283 cpl_test_null(out);
284
285 /* Normal work */
286
287 mat2 = cpl_matrix_new(nx, ny);
289 cpl_test_nonnull(out);
290 cpl_matrix_delete(out);
291
292 /* Clean up */
293 cpl_matrix_delete(mat1);
294 cpl_matrix_delete(mat2);
295}
296
297void test_mime_linalg_tensor_products_columns_create(void)
298{
299 int nx = 2;
300 int ny = 4;
301 cpl_matrix *mat1 = cpl_matrix_new(nx, ny);
302 cpl_matrix *mat2 = cpl_matrix_new(nx, ny);
303
304 cpl_matrix *mat = cpl_matrix_new(ny, nx);
305
306
307 cpl_matrix *out;
308
309
310 /* Test wrong input */
311
313 cpl_test_error(CPL_ERROR_NULL_INPUT);
314 cpl_test_null(out);
315
317 cpl_test_error(CPL_ERROR_NULL_INPUT);
318 cpl_test_null(out);
319
321 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
322 cpl_test_null(out);
323
324
325 /* Normal work */
327 cpl_test_error(CPL_ERROR_NONE);
328 cpl_test_nonnull(out);
329 cpl_matrix_delete(out);
330
331
332 /* Clean up */
333 cpl_matrix_delete(mat );
334 cpl_matrix_delete(mat1);
335 cpl_matrix_delete(mat2);
336}
337
338void test_mime_tensor_weights_create(void)
339{
340 int nx = 2;
341 int ny = 2;
342
343 cpl_matrix *out;
344
345
346 /* Test wrong input */
347
349 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
350 cpl_test_null(out);
351
353 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
354 cpl_test_null(out);
355
356
357 /* Normal work */
359 cpl_test_error(CPL_ERROR_NONE);
360 cpl_test_nonnull(out);
361 cpl_matrix_delete(out);
362}
363
364void test_mime_matrix_mask_rows(void){
365
366 int nx = 2;
367 int ny = 8;
368 cpl_matrix *mat = cpl_matrix_new(nx, ny);
369 cpl_mask *mask = cpl_mask_new( nx, ny);
370 cpl_mask *mask1 = cpl_mask_new( ny, nx);
371
372
373 /* Test wrong input */
374
375 hdrl_mime_matrix_mask_rows(NULL, mask);
376 cpl_test_error(CPL_ERROR_NULL_INPUT);
377
379 cpl_test_error(CPL_ERROR_NULL_INPUT);
380
381 hdrl_mime_matrix_mask_rows(mat, mask1);
382 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
383
384
385 /* Normal work */
387 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
388
389
390 /* Clean up */
391 cpl_matrix_delete(mat);
392 cpl_mask_delete(mask);
393 cpl_mask_delete(mask1);
394}
395
396void test_mime_matrix_rescale_rows(void){
397
398 cpl_matrix *mat = cpl_matrix_new(2, 1);
399 cpl_matrix *d = cpl_matrix_new(2, 1);
400 cpl_matrix *dmat = cpl_matrix_new(2, 1);
401 cpl_matrix *d1 = cpl_matrix_new(2, 2);
402 cpl_matrix *dmat1 = cpl_matrix_new(1, 2);
403
404
405 /* Test wrong input */
406
407 hdrl_mime_matrix_rescale_rows(NULL, d, dmat);
408 cpl_test_error(CPL_ERROR_NULL_INPUT);
409
410 hdrl_mime_matrix_rescale_rows(mat, NULL, dmat);
411 cpl_test_error(CPL_ERROR_NULL_INPUT);
412
413 hdrl_mime_matrix_rescale_rows(mat, d, NULL);
414 cpl_test_error(CPL_ERROR_NULL_INPUT);
415
416 hdrl_mime_matrix_rescale_rows(mat, d1, dmat);
417 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
418
419 hdrl_mime_matrix_rescale_rows(mat, d, dmat1);
420 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
421
422
423 /* Normal work */
424 hdrl_mime_matrix_rescale_rows(mat, d, dmat);
425 cpl_test_error(CPL_ERROR_NONE);
426
427 cpl_matrix_delete(mat);
428 cpl_matrix_delete(d);
429 cpl_matrix_delete(d1);
430 cpl_matrix_delete(dmat);
431 cpl_matrix_delete(dmat1);
432}
433
434void test_mime_linalg_solve_tikhonov(void)
435{
436 cpl_matrix *mat = cpl_matrix_new(1,2);
437 cpl_matrix *rhs = cpl_matrix_new(1,2);
438 cpl_matrix *rhs1 = cpl_matrix_new(2,2);
439 double alpha = 2.;
440
441 cpl_matrix *out;
442
443 /* Test wrong input */
444
445 out = hdrl_mime_linalg_solve_tikhonov(NULL, rhs, alpha);
446 cpl_test_error(CPL_ERROR_NULL_INPUT);
447 cpl_test_null(out);
448
449 out = hdrl_mime_linalg_solve_tikhonov(mat, NULL, alpha);
450 cpl_test_error(CPL_ERROR_NULL_INPUT);
451 cpl_test_null(out);
452
453 out = hdrl_mime_linalg_solve_tikhonov(mat, rhs1, alpha);
454 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
455 cpl_test_null(out);
456
457
458 /* Normal work */
459 out = hdrl_mime_linalg_solve_tikhonov(mat, rhs, alpha);
460 cpl_test_error(CPL_ERROR_NONE);
461 cpl_test_nonnull(out);
462 cpl_matrix_delete(out);
463
464
465 /* Clean up */
466 cpl_matrix_delete(mat);
467 cpl_matrix_delete(rhs);
468 cpl_matrix_delete(rhs1);
469}
470
471void test_mime_linalg_normal_equations_create(void)
472{
473 cpl_matrix *mat = cpl_matrix_new(1,2);
474 double alpha = 2.;
475
476 cpl_matrix *out;
477
478 /* Test wrong input */
479
481 cpl_test_error(CPL_ERROR_NULL_INPUT);
482 cpl_test_null(out);
483
485 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
486 cpl_test_null(out);
487
488
489 /* Normal work */
491 cpl_test_error(CPL_ERROR_NONE);
492 cpl_test_nonnull(out);
493 cpl_matrix_delete(out);
494
495
496 /* Clean up */
497 cpl_matrix_delete(mat);
498}
499
500void test_mime_matrix_product_left_transpose_create(void)
501{
502 int nx = 2;
503 int ny = 4;
504 cpl_matrix *mat1 = cpl_matrix_new(nx, ny);
505 cpl_matrix *mat2 = cpl_matrix_new(nx, ny);
506
507 cpl_matrix *mat = cpl_matrix_new(ny, nx);
508
509
510 cpl_matrix *out;
511
512
513 /* Test wrong input */
514
516 cpl_test_error(CPL_ERROR_NULL_INPUT);
517 cpl_test_null(out);
518
520 cpl_test_error(CPL_ERROR_NULL_INPUT);
521 cpl_test_null(out);
522
524 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
525 cpl_test_null(out);
526
527
528 /* Normal work */
530 cpl_test_error(CPL_ERROR_NONE);
531 cpl_test_nonnull(out);
532 cpl_matrix_delete(out);
533
534
535 /* Clean up */
536 cpl_matrix_delete(mat );
537 cpl_matrix_delete(mat1);
538 cpl_matrix_delete(mat2);
539}
540
541void test_mime_matrix_product(void)
542{
543 cpl_matrix *mat1 = cpl_matrix_new(2, 3);
544 cpl_matrix *mat2 = cpl_matrix_new(3, 2);
545 cpl_matrix *mat = cpl_matrix_new(2, 3);
546
547 cpl_matrix *product = cpl_matrix_new(2, 2);
548 cpl_matrix *product_1 = cpl_matrix_new(3, 2);
549 cpl_matrix *product_2 = cpl_matrix_new(3, 2);
550
551
552 /* Test wrong input */
553
554 hdrl_mime_matrix_product(NULL, mat2, product);
555 cpl_test_error(CPL_ERROR_NULL_INPUT);
556
557 hdrl_mime_matrix_product(mat1, NULL, product);
558 cpl_test_error(CPL_ERROR_NULL_INPUT);
559
560 hdrl_mime_matrix_product(mat1, mat2, NULL);
561 cpl_test_error(CPL_ERROR_NULL_INPUT);
562
563 hdrl_mime_matrix_product(mat1, mat, product);
564 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
565
566 hdrl_mime_matrix_product(mat1, mat2, product_1);
567 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
568
569 hdrl_mime_matrix_product(mat1, mat2, product_2);
570 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
571
572 /* Normal work */
573 hdrl_mime_matrix_product(mat1, mat2, product);
574 cpl_test_error(CPL_ERROR_NONE);
575
576
577 /* Clean up */
578 cpl_matrix_delete(mat1);
579 cpl_matrix_delete(mat2);
580 cpl_matrix_delete(mat);
581 cpl_matrix_delete(product);
582 cpl_matrix_delete(product_1);
583 cpl_matrix_delete(product_2);
584}
585
586
587/*----------------------------------------------------------------------------*/
591/*----------------------------------------------------------------------------*/
592int main(void)
593{
594 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
595
596 test_prototyping_spatial_freq();
597
598 test_mime_image_polynomial_bkg();
599 test_mime_compute_polynomial_bkg();
600
601 test_mime_legendre_polynomials_create();
602 test_mime_legendre_tensors_create();
603 test_mime_matrix_linspace_create();
604 test_mime_matrix_copy_column();
605 test_mime_linalg_pairwise_column_tensor_products_create();
606 test_mime_linalg_tensor_products_columns_create();
607 test_mime_tensor_weights_create();
608
609 test_mime_matrix_mask_rows();
610 test_mime_matrix_rescale_rows();
611
612 test_mime_linalg_solve_tikhonov();
613 test_mime_linalg_normal_equations_create();
614
615 test_mime_matrix_product_left_transpose_create();
616 test_mime_matrix_product();
617
618
619 return cpl_test_end(0);
620}
cpl_error_code hdrl_mime_compute_polynomial_bkg(const cpl_imagelist *images, cpl_imagelist *bkg_images, int dim_X, int dim_Y, cpl_matrix **coeffs)
Fit smooth background for a list of images.
cpl_matrix * hdrl_mime_legendre_tensors_create(int nx, int ny, int npx, int npy)
Create tensor products of Legendre polynomials.
cpl_error_code hdrl_mime_matrix_mask_rows(cpl_matrix *mat, const cpl_mask *mask)
Fill matrix rows with zeros as indicated by a mask.
cpl_matrix * hdrl_mime_linalg_solve_tikhonov(const cpl_matrix *mat, const cpl_matrix *rhs, double alpha)
Solve an overdetermined linear system in the least-squares sense.
cpl_error_code hdrl_mime_matrix_copy_column(const cpl_matrix *mat1, int j_1, cpl_matrix *mat2, int j_2)
Copy a column from one matrix to another matrix.
cpl_matrix * hdrl_mime_linalg_pairwise_column_tensor_products_create(const cpl_matrix *mat1, const cpl_matrix *mat2)
Create selected pairwise tensor products of the columns of two matrices.
cpl_matrix * hdrl_mime_matrix_product_left_transpose_create(const cpl_matrix *mat1, const cpl_matrix *mat2)
Create the product of the transpose of a matrix with another matrix.
cpl_error_code hdrl_mime_matrix_rescale_rows(const cpl_matrix *mat, const cpl_matrix *d, cpl_matrix *dmat)
Multiply the rows of a matrix by given factors.
cpl_matrix * hdrl_mime_tensor_weights_create(int nx, int ny)
Create tensor product weights.
cpl_matrix * hdrl_mime_legendre_polynomials_create(int npoly, double a, double b, const cpl_matrix *x)
Create the Legendre polynomial basis on the interval (a,b).
cpl_image * hdrl_get_spatial_freq(cpl_image *ima, double gausfilt, int mirrorx, int mirrory)
Get low spatial frequency componenets from an image using the FFTW.
cpl_matrix * hdrl_mime_matrix_linspace_create(int n, double a, double b)
Create equally spaced nodes.
cpl_matrix * hdrl_mime_linalg_normal_equations_create(const cpl_matrix *mat, double alpha)
Create the matrix transpose(A) * A + alpha for given A and alpha.
cpl_matrix * hdrl_mime_linalg_tensor_products_columns_create(const cpl_matrix *mat1, const cpl_matrix *mat2)
Create the tensor products of the columns of two matrices.
cpl_error_code hdrl_mime_matrix_product(const cpl_matrix *mat1, const cpl_matrix *mat2, cpl_matrix *product)
Fill a matrix with the product of two given matrices.