GIRAFFE Pipeline Reference Manual

gimodel.c

00001 /* $Id: gimodel.c,v 1.14 2008/01/08 14:14:58 rpalsa Exp $
00002  *
00003  * This file is part of the GIRAFFE Pipeline
00004  * Copyright (C) 2002-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /*
00022  * $Author: rpalsa $
00023  * $Date: 2008/01/08 14:14:58 $
00024  * $Revision: 1.14 $
00025  * $Name: giraffe-2_8_8 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #  include <config.h>
00030 #endif
00031 
00032 #include <math.h>
00033 #include <string.h>
00034 
00035 #include <cxtypes.h>
00036 #include <cxmemory.h>
00037 #include <cxmessages.h>
00038 #include <cxstrutils.h>
00039 
00040 #include <cpl_error.h>
00041 
00042 #include "gierror.h"
00043 #include "gimodels.h"
00044 
00045 
00054 inline static void
00055 _giraffe_model_set_flag(GiModel *self, cxint idx, cxbool value)
00056 {
00057 
00058     cx_assert(self != NULL);
00059 
00060     if (self->parameters.flags == NULL) {
00061         self->parameters.flags = cx_calloc(self->parameters.count,
00062                                            sizeof(cxint));
00063     }
00064 
00065     if (value == TRUE) {
00066         if (self->parameters.flags[idx] == 0) {
00067             self->parameters.flags[idx] = 1;
00068             self->fit.nfree += 1;
00069         }
00070     }
00071     else {
00072         if (self->parameters.flags[idx] == 1) {
00073             self->parameters.flags[idx] = 0;
00074             self->fit.nfree -= 1;
00075         }
00076     }
00077 
00078     return;
00079 
00080 }
00081 
00082 
00083 inline static cxbool
00084 _giraffe_model_get_flag(const GiModel *self, cxint idx)
00085 {
00086 
00087     cx_assert(self != NULL);
00088 
00089     if (self->parameters.flags == NULL) {
00090         return FALSE;
00091     }
00092 
00093     return self->parameters.flags[idx] == 0 ? FALSE : TRUE;
00094 
00095 }
00096 
00097 
00098 inline static cxdouble
00099 _giraffe_compute_rsquare(cxdouble rss, cpl_matrix *y, cxint n)
00100 {
00101 
00102     register cxint i;
00103 
00104     register cxdouble my = 0.;
00105     register cxdouble sy = 0.;
00106     register cxdouble ss = 0.;
00107 
00108     cxdouble r = 0.;
00109     cxdouble *_y = cpl_matrix_get_data(y);
00110 
00111 
00112     if (n < 1) {
00113         return 0.;
00114     }
00115 
00116     for (i = 0; i < n; i++) {
00117         my  += _y[i];
00118     }
00119     my /= n;
00120 
00121 
00122     for (i = 0; i < n; i++) {
00123         sy = _y[i] - my;
00124         ss += sy * sy;
00125     }
00126 
00127     r = rss / ss;
00128 
00129     if (isnan(r)) {
00130         return 0.;
00131     }
00132 
00133     return 1. - r;
00134 
00135 }
00136 
00137 
00138 inline static cxint
00139 _giraffe_model_fit(GiModel *self, cpl_matrix *x, cpl_matrix *y,
00140                    cpl_matrix *sigma, cxint ndata, cxint start,
00141                    cxint stride)
00142 {
00143 
00144     cxint status = 0;
00145 
00146     cxdouble _chisq = 0.;
00147 
00148     GiFitParams setup;
00149 
00150 
00151     if ((cpl_matrix_get_nrow(x) != cpl_matrix_get_nrow(y)) ||
00152         (cpl_matrix_get_nrow(x) != cpl_matrix_get_nrow(sigma))) {
00153             return -128;
00154         }
00155 
00156     if (cpl_matrix_get_ncol(x) != self->arguments.count) {
00157         return -128;
00158     }
00159 
00160 
00161     /*
00162      * Check data selection range
00163      */
00164 
00165     if (cpl_matrix_get_nrow(y) <= start + stride * (ndata - 1)) {
00166         return -255;
00167     }
00168 
00169 
00170     /*
00171      * Setup the fit
00172      */
00173 
00174     setup.iterations = self->fit.setup.iterations;
00175     setup.tests = self->fit.setup.tests;
00176     setup.dchisq = self->fit.setup.delta;
00177 
00178     if (self->fit.covariance != NULL) {
00179         cpl_matrix_set_size(self->fit.covariance, self->parameters.count,
00180                             self->parameters.count);
00181         cpl_matrix_fill(self->fit.covariance, 0.);
00182     }
00183     else {
00184         self->fit.covariance = cpl_matrix_new(self->parameters.count,
00185                                               self->parameters.count);
00186     }
00187 
00188 
00189     /*
00190      * Fit the data
00191      */
00192 
00193     giraffe_error_push();
00194 
00195     status = giraffe_nlfit(x, y, sigma, ndata, self->parameters.values,
00196                            self->parameters.limits, self->parameters.flags,
00197                            self->parameters.count, self->fit.covariance,
00198                            &_chisq, self->model, &setup);
00199 
00200     if (status < 0) {
00201 
00202         if (cpl_error_get_code() == CPL_ERROR_NONE) {
00203             giraffe_error_pop();
00204         }
00205 
00206         return status;
00207 
00208     }
00209 
00210     if (cpl_error_get_code() != CPL_ERROR_NONE) {
00211         return -255;
00212     }
00213 
00214     giraffe_error_pop();
00215 
00216     self->fit.df = ndata - self->fit.nfree;
00217     self->fit.iterations = status;
00218     self->fit.chisq = _chisq;
00219     self->fit.rsquare = _giraffe_compute_rsquare(self->fit.chisq, y, ndata);
00220 
00221     return 0;
00222 
00223 }
00224 
00225 
00226 GiModel *
00227 giraffe_model_new(const cxchar *name)
00228 {
00229 
00230     register cxint i = 0;
00231 
00232     GiModel *self = NULL;
00233 
00234 
00235     if (name == NULL) {
00236         return NULL;
00237     }
00238 
00239     while (giraffe_models[i].name != NULL) {
00240 
00241         if (strcmp(name, giraffe_models[i].name) == 0) {
00242 
00243             self = cx_calloc(1, sizeof(GiModel));
00244 
00245             giraffe_error_push();
00246 
00247             giraffe_models[i].ctor(self, &giraffe_models[i]);
00248 
00249             if (cpl_error_get_code() != CPL_ERROR_NONE) {
00250 
00251                 giraffe_model_delete(self);
00252                 self = NULL;
00253 
00254                 break;
00255 
00256             }
00257 
00258             break;
00259 
00260         }
00261 
00262         ++i;
00263 
00264     }
00265 
00266     self->fit.setup.iterations = 0;
00267     self->fit.setup.tests = 0;
00268     self->fit.setup.delta = 0.;
00269 
00270     self->fit.iterations = 0;
00271     self->fit.nfree = 0;
00272     self->fit.df = 0;
00273     self->fit.covariance = NULL;
00274 
00275     return self;
00276 
00277 }
00278 
00279 
00280 GiModel *
00281 giraffe_model_clone(const GiModel *other)
00282 {
00283 
00284     GiModel *self = NULL;
00285 
00286 
00287     if (other != NULL) {
00288 
00289         self = giraffe_model_new(other->name);
00290 
00291         /*
00292          * The model constructor creates already an a property list and
00293          * a matrix as container for the argument and parameter names and
00294          * values. We get rid of them here before we create the copies.
00295          * This way is simpler than copying each argument and parameter
00296          * individually.
00297          */
00298 
00299         cpl_propertylist_delete(self->arguments.names);
00300         self->arguments.names =
00301             cpl_propertylist_duplicate(other->arguments.names);
00302 
00303         cpl_matrix_delete(self->arguments.values);
00304         self->arguments.values =
00305             cpl_matrix_duplicate(other->arguments.values);
00306 
00307         self->arguments.count = other->arguments.count;
00308 
00309         cx_assert(cpl_propertylist_get_size(self->arguments.names) ==
00310                   self->arguments.count);
00311         cx_assert(cpl_matrix_get_nrow(self->arguments.values) *
00312                   cpl_matrix_get_ncol(self->arguments.values) ==
00313                   self->arguments.count);
00314 
00315 
00316         cpl_propertylist_delete(self->parameters.names);
00317         self->parameters.names =
00318             cpl_propertylist_duplicate(other->parameters.names);
00319 
00320         cpl_matrix_delete(self->parameters.values);
00321         self->parameters.values =
00322             cpl_matrix_duplicate(other->parameters.values);
00323 
00324         self->parameters.count = other->parameters.count;
00325 
00326         cx_assert(cpl_propertylist_get_size(self->parameters.names) ==
00327                   self->parameters.count);
00328         cx_assert(cpl_matrix_get_nrow(self->parameters.values) *
00329                   cpl_matrix_get_ncol(self->parameters.values) ==
00330                   self->parameters.count);
00331 
00332         self->fit.setup = other->fit.setup;
00333         self->fit.iterations = other->fit.iterations;
00334         self->fit.nfree = other->fit.nfree;
00335         self->fit.df = other->fit.df;
00336 
00337         if (other->fit.covariance == NULL) {
00338             self->fit.covariance = NULL;
00339         }
00340         else {
00341             self->fit.covariance =
00342                 cpl_matrix_duplicate(other->fit.covariance);
00343         }
00344 
00345     }
00346 
00347     return self;
00348 
00349 }
00350 
00351 void
00352 giraffe_model_delete(GiModel *self)
00353 {
00354 
00355     if (self) {
00356 
00357         register cxint i = 0;
00358 
00359         while (giraffe_models[i].name != NULL) {
00360 
00361             if (strcmp(self->name, giraffe_models[i].name) == 0) {
00362                 giraffe_models[i].dtor(self);
00363                 cx_free(self);
00364 
00365                 break;
00366             }
00367 
00368             ++i;
00369 
00370         }
00371 
00372     }
00373 
00374     return;
00375 
00376 }
00377 
00378 
00379 const cxchar *
00380 giraffe_model_get_name(const GiModel *self)
00381 {
00382 
00383     cx_assert(self != NULL);
00384     return self->name;
00385 
00386 }
00387 
00388 
00389 GiModelType
00390 giraffe_model_get_type(const GiModel *self)
00391 {
00392 
00393     cx_assert(self != NULL);
00394     return self->type;
00395 
00396 }
00397 
00398 
00399 cxsize
00400 giraffe_model_count_arguments(const GiModel *self)
00401 {
00402 
00403     cx_assert(self != NULL);
00404     return self->arguments.count;
00405 
00406 }
00407 
00408 
00409 cxsize
00410 giraffe_model_count_parameters(const GiModel *self)
00411 {
00412 
00413     cx_assert(self != NULL);
00414     return self->parameters.count;
00415 
00416 }
00417 
00418 
00419 const cxchar*
00420 giraffe_model_argument_name(const GiModel* self, cxsize position)
00421 {
00422 
00423     const cpl_property* p = NULL;
00424 
00425 
00426     cx_assert(self != NULL);
00427 
00428     p = cpl_propertylist_get(self->arguments.names, position);
00429     if (p == NULL) {
00430         return NULL;
00431     }
00432 
00433     return cpl_property_get_name(p);
00434 
00435 }
00436 
00437 
00438 const cxchar*
00439 giraffe_model_parameter_name(const GiModel* self, cxsize position)
00440 {
00441 
00442     const cpl_property* p = NULL;
00443 
00444 
00445     cx_assert(self != NULL);
00446 
00447     p = cpl_propertylist_get(self->parameters.names, position);
00448     if (p == NULL) {
00449         return NULL;
00450     }
00451 
00452     return cpl_property_get_name(p);
00453 
00454 }
00455 
00456 
00457 cxint
00458 giraffe_model_set_argument(GiModel *self, const cxchar *name, cxdouble value)
00459 {
00460 
00461     const cxchar *const fctid = "giraffe_model_set_argument";
00462 
00463 
00464     cx_assert(self != NULL);
00465 
00466     if (name == NULL) {
00467         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00468         return 1;
00469     }
00470 
00471     if (!cpl_propertylist_has(self->arguments.names, name)) {
00472         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00473         return 1;
00474     }
00475     else {
00476 
00477         register cxint idx = cpl_propertylist_get_int(self->arguments.names,
00478                                                       name);
00479 
00480         cpl_matrix_set(self->arguments.values, idx, 0, value);
00481 
00482     }
00483 
00484     return 0;
00485 
00486 }
00487 
00488 
00489 cxdouble
00490 giraffe_model_get_argument(const GiModel *self, const cxchar *name)
00491 {
00492 
00493     const cxchar *const fctid = "giraffe_model_get_argument";
00494 
00495     register cxint idx;
00496 
00497 
00498     cx_assert(self != NULL);
00499 
00500     if (name == NULL) {
00501         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00502         return 0.;
00503     }
00504 
00505     if (!cpl_propertylist_has(self->arguments.names, name)) {
00506         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00507         return 0.;
00508     }
00509 
00510 
00511     idx = cpl_propertylist_get_int(self->arguments.names, name);
00512 
00513     return cpl_matrix_get(self->arguments.values, idx, 0);
00514 
00515 }
00516 
00517 
00518 cxint
00519 giraffe_model_set_parameter(GiModel *self, const cxchar *name,
00520                             cxdouble value)
00521 {
00522 
00523     const cxchar *const fctid = "giraffe_model_set_parameter";
00524 
00525 
00526     cx_assert(self != NULL);
00527 
00528     if (name == NULL) {
00529         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00530         return 1;
00531     }
00532 
00533     if (!cpl_propertylist_has(self->parameters.names, name)) {
00534         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00535         return 1;
00536     }
00537     else {
00538 
00539         register cxint idx = cpl_propertylist_get_int(self->parameters.names,
00540                                                       name);
00541 
00542         cpl_matrix_set(self->parameters.values, idx, 0, value);
00543 
00544     }
00545 
00546     return 0;
00547 
00548 }
00549 
00550 
00551 cxdouble
00552 giraffe_model_get_parameter(const GiModel *self, const cxchar *name)
00553 {
00554 
00555     const cxchar *const fctid = "giraffe_model_get_parameter";
00556 
00557     register cxint idx;
00558 
00559 
00560     cx_assert(self != NULL);
00561 
00562     if (name == NULL) {
00563         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00564         return 0.;
00565     }
00566 
00567     if (!cpl_propertylist_has(self->parameters.names, name)) {
00568         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00569         return 0.;
00570     }
00571 
00572 
00573     idx = cpl_propertylist_get_int(self->parameters.names, name);
00574 
00575     return cpl_matrix_get(self->parameters.values, idx, 0);
00576 
00577 }
00578 
00579 
00580 cxint
00581 giraffe_model_freeze_parameter(GiModel *self, const cxchar *name)
00582 {
00583 
00584     const cxchar *const fctid = "giraffe_model_freeze_parameter";
00585 
00586 
00587     if (self == NULL) {
00588         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00589         return 1;
00590     }
00591 
00592     if (name == NULL) {
00593         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00594         return 1;
00595     }
00596 
00597     if (!cpl_propertylist_has(self->parameters.names, name)) {
00598         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00599         return 1;
00600     }
00601     else {
00602 
00603         register cxint idx = cpl_propertylist_get_int(self->parameters.names,
00604                                                       name);
00605 
00606         _giraffe_model_set_flag(self, idx, FALSE);
00607 
00608     }
00609 
00610     return 0;
00611 
00612 }
00613 
00614 
00615 cxint
00616 giraffe_model_thaw_parameter(GiModel *self, const cxchar *name)
00617 {
00618 
00619     const cxchar *const fctid = "giraffe_model_thaw_parameter";
00620 
00621 
00622     cx_assert(self != NULL);
00623 
00624     if (name == NULL) {
00625         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00626         return 1;
00627     }
00628 
00629     if (!cpl_propertylist_has(self->parameters.names, name)) {
00630         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00631         return 1;
00632     }
00633     else {
00634 
00635         register cxint idx = cpl_propertylist_get_int(self->parameters.names,
00636                                                       name);
00637 
00638         _giraffe_model_set_flag(self, idx, TRUE);
00639 
00640     }
00641 
00642     return 0;
00643 
00644 }
00645 
00646 
00647 cxbool
00648 giraffe_model_frozen_parameter(const GiModel *self, const cxchar *name)
00649 {
00650 
00651     const cxchar *const fctid = "giraffe_model_frozen_parameter";
00652 
00653     register cxint idx;
00654 
00655 
00656     cx_assert(self != NULL);
00657 
00658     if (name == NULL) {
00659         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00660         return FALSE;
00661     }
00662 
00663     if (!cpl_propertylist_has(self->parameters.names, name)) {
00664         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00665         return FALSE;
00666     }
00667 
00668 
00669     idx = cpl_propertylist_get_int(self->parameters.names, name);
00670 
00671     return _giraffe_model_get_flag(self, idx) == FALSE;
00672 
00673 }
00674 
00675 
00676 cxint
00677 giraffe_model_freeze(GiModel *self)
00678 {
00679 
00680     register cxint i;
00681 
00682 
00683     cx_assert(self != NULL);
00684 
00685     for (i = 0; i < cpl_propertylist_get_size(self->parameters.names); i++) {
00686 
00687         const cpl_property *p = cpl_propertylist_get(self->parameters.names,
00688                                                      i);
00689 
00690         if (p == NULL) {
00691             return 1;
00692         }
00693 
00694         _giraffe_model_set_flag(self, cpl_property_get_int(p), FALSE);
00695 
00696     }
00697 
00698     return 0;
00699 
00700 }
00701 
00702 
00703 cxint
00704 giraffe_model_thaw(GiModel *self)
00705 {
00706 
00707     register cxint i;
00708 
00709 
00710     cx_assert(self != NULL);
00711 
00712     for (i = 0; i < cpl_propertylist_get_size(self->parameters.names); i++) {
00713 
00714         const cpl_property *p = cpl_propertylist_get(self->parameters.names,
00715                                                      i);
00716 
00717         if (p == NULL) {
00718             return 1;
00719         }
00720 
00721         _giraffe_model_set_flag(self, cpl_property_get_int(p), TRUE);
00722 
00723     }
00724 
00725     return 0;
00726 
00727 }
00728 
00729 
00730 cxint
00731 giraffe_model_evaluate(const GiModel *self, cxdouble *result, cxint *status)
00732 {
00733 
00734     const cxchar *const fctid = "giraffe_model_evaluate";
00735 
00736     cxdouble _result = 0.;
00737     cxdouble *arg = NULL;
00738     cxdouble *par = NULL;
00739 
00740 
00741     cx_assert(self != NULL);
00742 
00743     arg = cpl_matrix_get_data(self->arguments.values);
00744 
00745     if (arg == NULL) {
00746         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00747         return 2;
00748     }
00749 
00750     par = cpl_matrix_get_data(self->parameters.values);
00751 
00752     if (par == NULL) {
00753         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00754         return 3;
00755     }
00756 
00757     giraffe_error_push();
00758 
00759     self->model(&_result, arg, par, self->parameters.count, NULL, NULL);
00760 
00761     if (cpl_error_get_code() != CPL_ERROR_NONE) {
00762 
00763         if (status) {
00764             *status = 1;
00765         }
00766 
00767         return 4;
00768 
00769     }
00770 
00771     giraffe_error_pop();
00772 
00773     *result = _result;
00774     *status = 0;
00775 
00776     return 0;
00777 
00778 }
00779 
00780 
00781 cxint
00782 giraffe_model_set_iterations(GiModel *self, cxint iterations)
00783 {
00784 
00785     cx_assert(self != NULL);
00786 
00787     if (iterations < 1) {
00788         return 1;
00789     }
00790 
00791     self->fit.setup.iterations = iterations;
00792 
00793     return 0;
00794 
00795 }
00796 
00797 
00798 cxint
00799 giraffe_model_get_iterations(const GiModel *self)
00800 {
00801 
00802     cx_assert(self != NULL);
00803 
00804     return self->fit.setup.iterations;
00805 
00806 }
00807 
00808 
00809 cxint
00810 giraffe_model_set_tests(GiModel *self, cxint tests)
00811 {
00812 
00813     cx_assert(self != NULL);
00814 
00815     if (tests < 1) {
00816         return 1;
00817     }
00818 
00819     self->fit.setup.tests = tests;
00820 
00821     return 0;
00822 
00823 }
00824 
00825 
00826 cxint
00827 giraffe_model_get_tests(const GiModel *self)
00828 {
00829 
00830     cx_assert(self != NULL);
00831 
00832     return self->fit.setup.tests;
00833 
00834 }
00835 
00836 
00837 cxint
00838 giraffe_model_set_delta(GiModel *self, cxdouble delta)
00839 {
00840 
00841     cx_assert(self != NULL);
00842 
00843     if (delta < 0.) {
00844         return 1;
00845     }
00846 
00847     self->fit.setup.delta = delta;
00848 
00849     return 0;
00850 
00851 }
00852 
00853 
00854 cxdouble
00855 giraffe_model_get_delta(const GiModel *self)
00856 {
00857 
00858     cx_assert(self != NULL);
00859 
00860     return self->fit.setup.delta;
00861 
00862 }
00863 
00864 
00865 cxint
00866 giraffe_model_get_position(const GiModel *self)
00867 {
00868 
00869     cx_assert(self != NULL);
00870 
00871     if (self->fit.iterations <= 0) {
00872         return -1;
00873     }
00874 
00875     return self->fit.iterations;
00876 
00877 }
00878 
00879 
00880 cxdouble
00881 giraffe_model_get_variance(const GiModel *self, const cxchar *name)
00882 {
00883 
00884     const cxchar *const fctid = "giraffe_model_get_variance";
00885 
00886 
00887     cxdouble variance = 0.;
00888 
00889 
00890     cx_assert(self != NULL);
00891 
00892     if (name == NULL) {
00893         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00894         return variance;
00895     }
00896 
00897     if (!cpl_propertylist_has(self->parameters.names, name)) {
00898         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00899         return variance;
00900     }
00901     else {
00902 
00903         if (self->fit.covariance == NULL) {
00904             cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00905             return variance;
00906         }
00907         else {
00908 
00909             register cxint idx =
00910                 cpl_propertylist_get_int(self->parameters.names, name);
00911 
00912             variance = cpl_matrix_get(self->fit.covariance, idx, idx);
00913 
00914         }
00915 
00916     }
00917 
00918     return variance;
00919 
00920 }
00921 
00922 
00923 cxdouble
00924 giraffe_model_get_sigma(const GiModel *self, const cxchar *name)
00925 {
00926 
00927     const cxchar *const fctid = "giraffe_model_get_sigma";
00928 
00929 
00930     cxdouble sigma = 0.;
00931 
00932 
00933     cx_assert(self != NULL);
00934 
00935     if (name == NULL) {
00936         cpl_error_set(fctid, CPL_ERROR_NULL_INPUT);
00937         return sigma;
00938     }
00939 
00940     if (!cpl_propertylist_has(self->parameters.names, name)) {
00941         cpl_error_set(fctid, CPL_ERROR_ILLEGAL_INPUT);
00942         return sigma;
00943     }
00944     else {
00945 
00946         if (self->fit.covariance == NULL) {
00947             cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00948             return sigma;
00949         }
00950         else {
00951 
00952             register cxint idx =
00953                 cpl_propertylist_get_int(self->parameters.names, name);
00954 
00955             sigma = cpl_matrix_get(self->fit.covariance, idx, idx);
00956 
00957             if (isnan(sigma) || sigma < 0.) {
00958                 sigma = 0.;
00959             }
00960             else {
00961                 sigma = sqrt(sigma);
00962             }
00963 
00964         }
00965 
00966     }
00967 
00968     return sigma;
00969 
00970 }
00971 
00972 
00973 cxint
00974 giraffe_model_get_df(const GiModel *self)
00975 {
00976 
00977     cx_assert(self != NULL);
00978 
00979     return self->fit.df;
00980 
00981 }
00982 
00983 
00984 cxdouble
00985 giraffe_model_get_chisq(const GiModel *self)
00986 {
00987 
00988     cx_assert(self != NULL);
00989 
00990     return self->fit.chisq;
00991 
00992 }
00993 
00994 
00995 cxdouble
00996 giraffe_model_get_rsquare(const GiModel *self)
00997 {
00998 
00999     cx_assert(self != NULL);
01000 
01001     return self->fit.rsquare;
01002 
01003 }
01004 
01005 
01006 cxint
01007 giraffe_model_fit(GiModel *self, cpl_matrix *x, cpl_matrix *y,
01008                   cpl_matrix *sigma)
01009 {
01010 
01011     cxint ndata = 0;
01012 
01013 
01014     cx_assert(self != NULL);
01015 
01016     if (x == NULL || y == NULL) {
01017         return -128;
01018     }
01019 
01020     if (sigma == NULL) {
01021         return -128;
01022     }
01023 
01024     ndata = cpl_matrix_get_nrow(y);
01025 
01026     return _giraffe_model_fit(self, x, y, sigma, ndata, 0, 1);
01027 
01028 }
01029 
01030 
01031 cxint
01032 giraffe_model_fit_sequence(GiModel *self, cpl_matrix *x, cpl_matrix *y,
01033                            cpl_matrix *sigma, cxint ndata, cxint start,
01034                            cxint stride)
01035 {
01036 
01037     cx_assert(self != NULL);
01038 
01039     /* FIXME: Currently only (start == 0 && stride == 1) is supported! */
01040     cx_assert((start == 0) || (stride == 1));
01041 
01042 
01043     if (x == NULL || y == NULL) {
01044         return -128;
01045     }
01046 
01047     if (sigma == NULL) {
01048         return -128;
01049     }
01050 
01051     if ((start < 0) || (stride < 0)) {
01052         return -128;
01053     }
01054 
01055     return _giraffe_model_fit(self, x, y, sigma, ndata, 0, 1);
01056 
01057 }

This file is part of the GIRAFFE Pipeline Reference Manual 2.8.8.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Fri Mar 4 10:50:27 2011 by doxygen 1.6.3 written by Dimitri van Heesch, © 1997-2004