File: montecarlo.c

package info (click to toggle)
cpl-plugin-fors 5.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,664 kB
  • ctags: 3,906
  • sloc: ansic: 68,347; cpp: 16,320; sh: 11,458; python: 1,123; makefile: 823
file content (480 lines) | stat: -rw-r--r-- 15,250 bytes parent folder | download
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
/* $Id: montecarlo.c,v 1.5 2013-04-24 14:14:13 cgarcia Exp $
 *
 * This file is part of the FORS Data Reduction Pipeline
 * Copyright (C) 2002-2010 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
 */

/*
 * $Author: cgarcia $
 * $Date: 2013-04-24 14:14:13 $
 * $Revision: 1.5 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <cpl.h>
#include <moses.h>
#include <fors_dfs.h>

static int montecarlo_create(cpl_plugin *);
static int montecarlo_exec(cpl_plugin *);
static int montecarlo_destroy(cpl_plugin *);
static int montecarlo(cpl_parameterlist *, cpl_frameset *);

static char montecarlo_description[] =
"This recipe is used to test the mos_montecarlo_polyfit() function.\n"
"It accepts a table with columns x, y, y_err, derives the best polynomial\n"
"fit y = p(x), and produces a table with the polynomial 1-sigma accuracy\n"
"on the given set of x coordinates.\n"
"Input files:\n\n"
"  DO category:               Type:       Explanation:         Required:\n"
"  TABLE                      Raw         Table to evaluate       Y\n\n"
"Output files:\n\n"
"  DO category:               Data type:  Explanation:\n"
"  MODEL_ERROR                FITS image  Model error at different x\n\n";

#define montecarlo_exit(message)               \
{                                             \
if ((const char *)message != NULL) cpl_msg_error(recipe, message);  \
cpl_table_delete(table);                  \
cpl_table_delete(points);                  \
cpl_table_delete(model_error);                  \
cpl_polynomial_delete(p);                    \
cpl_msg_indent_less();                        \
return -1;                                    \
}

#define montecarlo_exit_memcheck(message)       \
{                                               \
if ((const char *)message != NULL) cpl_msg_info(recipe, message);     \
cpl_table_delete(table);                    \
cpl_table_delete(points);                  \
cpl_table_delete(model_error);                  \
cpl_polynomial_delete(p);                    \
cpl_msg_indent_less();                          \
return 0;                                       \
}


/**
 * @brief    Build the list of available plugins, for this module. 
 *
 * @param    list    The plugin list
 *
 * @return   0 if everything is ok, -1 otherwise
 *
 * Create the recipe instance and make it available to the application 
 * using the interface. This function is exported.
 */

int cpl_plugin_get_info(cpl_pluginlist *list)
{
    cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe );
    cpl_plugin *plugin = &recipe->interface;

    cpl_plugin_init(plugin,
                    CPL_PLUGIN_API,
                    FORS_BINARY_VERSION,
                    CPL_PLUGIN_TYPE_RECIPE,
                    "montecarlo",
                    "Test function mos_montecarlo_polyfit()",
                    montecarlo_description,
                    "Carlo Izzo",
                    PACKAGE_BUGREPORT,
    "This file is currently part of the FORS Instrument Pipeline\n"
    "Copyright (C) 2002-2010 European Southern Observatory\n\n"
    "This program is free software; you can redistribute it and/or modify\n"
    "it under the terms of the GNU General Public License as published by\n"
    "the Free Software Foundation; either version 2 of the License, or\n"
    "(at your option) any later version.\n\n"
    "This program is distributed in the hope that it will be useful,\n"
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
    "GNU General Public License for more details.\n\n"
    "You should have received a copy of the GNU General Public License\n"
    "along with this program; if not, write to the Free Software Foundation,\n"
    "Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n",
                    montecarlo_create,
                    montecarlo_exec,
                    montecarlo_destroy);

    cpl_pluginlist_append(list, plugin);
    
    return 0;
}


/**
 * @brief    Setup the recipe options    
 *
 * @param    plugin  The plugin
 *
 * @return   0 if everything is ok
 *
 * Defining the command-line/configuration parameters for the recipe.
 */

static int montecarlo_create(cpl_plugin *plugin)
{
    cpl_recipe    *recipe;
    cpl_parameter *p;


    /* 
     * Check that the plugin is part of a valid recipe 
     */

    if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
        recipe = (cpl_recipe *)plugin;
    else 
        return -1;

    /* 
     * Create the parameters list in the cpl_recipe object 
     */

    recipe->parameters = cpl_parameterlist_new(); 


    /*
     * Name of input table columns
     */

    p = cpl_parameter_new_value("fors.montecarlo.x",
                                CPL_TYPE_STRING,
                                "Name of independent variable column",
                                "fors.montecarlo",
                                "x");
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "x");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    p = cpl_parameter_new_value("fors.montecarlo.y",
                                CPL_TYPE_STRING,
                                "Name of dependent variable column",
                                "fors.montecarlo",
                                "y");
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "y");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    p = cpl_parameter_new_value("fors.montecarlo.sigma",
                                CPL_TYPE_STRING,
                                "Name of error column on dependent variable",
                                "fors.montecarlo",
                                "");
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "sigma");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    /*
     * Order of fitting polynomial
     */

    p = cpl_parameter_new_value("fors.montecarlo.order",
                                CPL_TYPE_INT,
                                "Order of fitting polynomial",
                                "fors.montecarlo",
                                1);
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "order");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    /*
     * Origin of x for fit
     */

    p = cpl_parameter_new_value("fors.montecarlo.zero",
                                CPL_TYPE_DOUBLE,
                                "Origin of x for fit",
                                "fors.montecarlo",
                                0.0);
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "zero");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    /*
     * Start x for evaluation
     */

    p = cpl_parameter_new_value("fors.montecarlo.start",
                                CPL_TYPE_DOUBLE,
                                "Start x for evaluation",
                                "fors.montecarlo",
                                0.0);
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "start");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    /*
     * End x for evaluation
     */

    p = cpl_parameter_new_value("fors.montecarlo.end",
                                CPL_TYPE_DOUBLE,
                                "End x for evaluation",
                                "fors.montecarlo",
                                0.0);
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "end");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    /*
     * Evaluation sampling
     */
    
    p = cpl_parameter_new_value("fors.montecarlo.step",
                                CPL_TYPE_DOUBLE,
                                "x sampling interval",
                                "fors.montecarlo",
                                0.0);
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "step");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    /*
     * Statistical sample
     */

    p = cpl_parameter_new_value("fors.montecarlo.trials",
                                CPL_TYPE_INT,
                                "Size of statistical sample",
                                "fors.montecarlo",
                                100);
    cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "trials");
    cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
    cpl_parameterlist_append(recipe->parameters, p);

    return 0;
}


/**
 * @brief    Execute the plugin instance given by the interface
 *
 * @param    plugin  the plugin
 *
 * @return   0 if everything is ok
 */

static int montecarlo_exec(cpl_plugin *plugin)
{
    cpl_recipe *recipe;
    
    if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
        recipe = (cpl_recipe *)plugin;
    else 
        return -1;

    return montecarlo(recipe->parameters, recipe->frames);
}


/**
 * @brief    Destroy what has been created by the 'create' function
 *
 * @param    plugin  The plugin
 *
 * @return   0 if everything is ok
 */

static int montecarlo_destroy(cpl_plugin *plugin)
{
    cpl_recipe *recipe;
    
    if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
        recipe = (cpl_recipe *)plugin;
    else 
        return -1;

    cpl_parameterlist_delete(recipe->parameters); 

    return 0;
}


/**
 * @brief    Interpret the command line options and execute the data processing
 *
 * @param    parlist     The parameters list
 * @param    frameset    The set-of-frames
 *
 * @return   0 if everything is ok
 */

static int montecarlo(cpl_parameterlist *parlist, cpl_frameset *frameset)
{

    const char *recipe = "montecarlo";


    /*
     * Input parameters
     */

    const char *x;
    const char *y;
    const char *sigma;
    int         order;
    double      start;
    double      end;
    double      step;
    int         trials;
    double      zero;

    /*
     * CPL objects
     */

    cpl_table      *table = NULL;
    cpl_table      *model_error = NULL;
    cpl_table      *points = NULL;
    cpl_polynomial *p = NULL;

    /*
     * Auxiliary variables
     */

    char   *version = "0.1";
    char   *table_tag = "TABLE";
    char   *model_error_tag = "MODEL_ERROR";
    double *xdata;
    int     ntables;
    int     i, count;


    cpl_msg_set_indentation(2);


    /* 
     * Get configuration parameters
     */

    cpl_msg_info(recipe, "Recipe %s configuration parameters:", recipe);
    cpl_msg_indent_more();

    x = dfs_get_parameter_string(parlist, "fors.montecarlo.x", NULL);
    y = dfs_get_parameter_string(parlist, "fors.montecarlo.y", NULL);
    sigma = dfs_get_parameter_string(parlist, "fors.montecarlo.sigma", NULL);
    order = dfs_get_parameter_int(parlist, "fors.montecarlo.order", NULL);
    if (order < 0)
        montecarlo_exit("Invalid polynomial order");
    start = dfs_get_parameter_double(parlist, "fors.montecarlo.start", NULL);
    end = dfs_get_parameter_double(parlist, "fors.montecarlo.end", NULL);
    if (end <= start)
        montecarlo_exit("Invalid interval");
    step = dfs_get_parameter_double(parlist, "fors.montecarlo.step", NULL);
    if (step >= end - start || step <= 0.0)
        montecarlo_exit("Invalid step");
    trials = dfs_get_parameter_int(parlist, "fors.montecarlo.trials", NULL);
    if (trials < 2)
        montecarlo_exit("At least 2 trials should be performed");
    zero = dfs_get_parameter_double(parlist, "fors.montecarlo.zero", NULL);

    if (cpl_error_get_code())
        montecarlo_exit("Failure getting the configuration parameters");

    ntables = cpl_frameset_count_tags(frameset, table_tag);
    if (ntables == 0) {
        cpl_msg_error(recipe, "Missing required input: %s", table_tag);
        montecarlo_exit(NULL);
    }
    if (ntables > 1) {
        cpl_msg_error(recipe, "Too many in input: %s", table_tag);
        montecarlo_exit(NULL);
    }

    table = dfs_load_table(frameset, table_tag, 1);
    points = cpl_table_new(cpl_table_get_nrow(table));

    if (cpl_table_has_column(table, x)) {
        cpl_table_move_column(points, x, table);
        if (!cpl_table_has_column(points, "x")) {
            cpl_table_name_column(points, x, "x");
        }
    }
    else {
        cpl_msg_error(recipe, "Missing column: %s", x);
        montecarlo_exit(NULL);
    }

    cpl_table_subtract_scalar(points, "x", zero);

    if (cpl_table_has_column(table, y)) {
        cpl_table_move_column(points, y, table);
        if (!cpl_table_has_column(points, "y")) {
            cpl_table_name_column(points, y, "y");
        }
    }
    else {
        cpl_msg_error(recipe, "Missing column: %s", y);
        montecarlo_exit(NULL);
    }

    if (sigma[0] != '\0') {
        if (cpl_table_has_column(table, sigma)) {
            cpl_table_move_column(points, sigma, table);
            if (!cpl_table_has_column(points, "y_err")) {
                cpl_table_name_column(points, sigma, "y_err");
            }
        }
        else {
            cpl_msg_error(recipe, "Missing column: %s", sigma);
            montecarlo_exit(NULL);
        }
    }

    cpl_table_delete(table); table = NULL;
    cpl_table_erase_invalid(points);

    count = 1;
    while (start + step*count <= end)
        count++;

    model_error = cpl_table_new(count);
    cpl_table_new_column(model_error, "x", CPL_TYPE_DOUBLE);
    cpl_table_fill_column_window_double(model_error, "x", 0, count, 0.0);
    xdata = cpl_table_get_data_double(model_error, "x");

    for (i = 0; i < count; i++)
        xdata[i] = start + step*i - zero;

    p = mos_montecarlo_polyfit(points, model_error, trials, order);

    if (cpl_error_get_code() != CPL_ERROR_NONE) {
        cpl_msg_error(recipe, "%s", cpl_error_get_message());
        montecarlo_exit(NULL);
    }

    for (i = 0; i < count; i++)
        xdata[i] += zero;

    cpl_polynomial_delete(p); p = NULL;
    cpl_table_delete(points); points = NULL;

    if (dfs_save_table(frameset, model_error, model_error_tag, NULL,
                       parlist, recipe, version))
        montecarlo_exit(NULL);

    cpl_table_delete(model_error); model_error = NULL;

    return 0;
}