00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #define PROGRAM_NAME "plug_nlfit"
00045 #define PROGRAM_AUTHOR "B. Douglas Ward"
00046 #define PROGRAM_DATE "10 May 2000"
00047
00048
00049
00050
00051 #include "afni.h"
00052
00053 #ifndef ALLOW_PLUGINS
00054 # error "Plugins not properly set up -- see machdep.h"
00055 #endif
00056
00057
00058 #include <math.h>
00059 #include <stdlib.h>
00060
00061 #include "mrilib.h"
00062
00063 #include "matrix.h"
00064 #include "simplex.h"
00065 #include "NLfit.h"
00066
00067 #include "matrix.c"
00068 #include "simplex.c"
00069 #include "NLfit.c"
00070
00071
00072
00073
00074 static float DELT = 1.0;
00075 static int inTR = 0 ;
00076 static float dsTR = 0.0 ;
00077
00078
00079
00080
00081
00082
00083
00084
00085 static char helpstring[] =
00086 " Purpose: Control the 'NLfit' and 'NLerr' 1D functions.\n"
00087 "\n"
00088 " Control: Ignore = Number of points to ignore at start \n"
00089 " of each timeseries. \n"
00090 " NRandom = Number of random test points. \n"
00091 " NBest = Find opt. soln. for these best test points. \n"
00092 " \n"
00093 " Models: Noise = Label of noise model to use. \n"
00094 " Signal = Label of signal model to use. \n"
00095 " Noise Constr = Relative or Absolute noise constraints. \n"
00096 " \n"
00097 " Noise: Parameter = Index of noise model parameter. \n"
00098 " Min Constr = Minimum value for noise model parameter. \n"
00099 " Max Constr = Maximum value for noise model parameter. \n"
00100 " \n"
00101 " Signal: Parameter = Index of signal model parameter.\n"
00102 " Min Constr = Minimum value for signal model parameter. \n"
00103 " Max Constr = Maximum value for signal model parameter. \n"
00104 " \n"
00105 " Time Scale: Reference = Internal or External time reference. \n"
00106 " File = External time reference file name. \n"
00107 "Author -- BD Ward"
00108 ;
00109
00110
00111
00112
00113
00114 char * NL_main( PLUGIN_interface * ) ;
00115
00116 void NL_fitter( int nt, double to, double dt, float * vec, char ** label ) ;
00117 void NL_error ( int nt, double to, double dt, float * vec, char ** label ) ;
00118 void NL_worker( int nt, double dt, float * vec, int dofit, char ** label ) ;
00119
00120
00121
00122 static PLUGIN_interface * global_plint = NULL ;
00123 static int initialize=1 ;
00124
00125
00126
00127 char * constr_types[2] = {"Relative", "Absolute"};
00128 char * time_refs[3] = {"Internal", "External" , "-inTR" };
00129 int plug_ignore = 3;
00130 int plug_nrand = 100;
00131 int plug_nbest = 5;
00132 int plug_nabs = 0;
00133 int plug_timeref = 0;
00134 char plug_tfilename[MAX_NAME_LENGTH] = "";
00135
00136
00137 int num_noise_models;
00138 int plug_noise_index;
00139 char * noise_labels[MAX_MODELS];
00140 vfp plug_nmodel[MAX_MODELS];
00141 int plug_r[MAX_MODELS];
00142 char * noise_plabels[MAX_MODELS][MAX_PARAMETERS];
00143 float plug_min_nconstr[MAX_MODELS][MAX_PARAMETERS];
00144
00145 float plug_max_nconstr[MAX_MODELS][MAX_PARAMETERS];
00146
00147
00148
00149 int num_signal_models;
00150 int plug_signal_index;
00151 char * signal_labels[MAX_MODELS];
00152 vfp plug_smodel[MAX_MODELS];
00153 int plug_p[MAX_MODELS];
00154 char * signal_plabels[MAX_MODELS][MAX_PARAMETERS];
00155 float plug_min_sconstr[MAX_MODELS][MAX_PARAMETERS];
00156
00157 float plug_max_sconstr[MAX_MODELS][MAX_PARAMETERS];
00158
00159
00160
00161
00162
00163
00164
00165
00166 void initialize_options
00167 (
00168 int * im1,
00169 char ** nname,
00170 char ** sname,
00171 vfp * nmodel,
00172 vfp * smodel,
00173 int * r,
00174 int * p,
00175 char *** npname,
00176 char *** spname,
00177 float ** min_nconstr,
00178 float ** max_nconstr,
00179 float ** min_sconstr,
00180 float ** max_sconstr,
00181 int * nabs,
00182 int * nrand,
00183 int * nbest,
00184 float * rms_min,
00185 char ** tfilename
00186 )
00187
00188 {
00189 int ip;
00190 int ok;
00191 char message[MAX_NAME_LENGTH];
00192
00193
00194 *im1 = 1;
00195 *nrand = plug_nrand;
00196 *nbest = plug_nbest;
00197 *nabs = plug_nabs;
00198 *rms_min = 0.0;
00199 *tfilename = plug_tfilename;
00200
00201 *nname = noise_labels[plug_noise_index];
00202 *sname = signal_labels[plug_signal_index];
00203
00204 *nmodel = plug_nmodel[plug_noise_index];
00205 *smodel = plug_smodel[plug_signal_index];
00206
00207 *r = plug_r[plug_noise_index];
00208 *p = plug_p[plug_signal_index];
00209
00210 *npname = noise_plabels[plug_noise_index];
00211 *spname = signal_plabels[plug_signal_index];
00212
00213
00214 *min_nconstr = (float *) malloc (sizeof(float) * (*r));
00215 if (*min_nconstr == NULL)
00216 NLfit_error ("Unable to allocate memory for min_nconstr");
00217 *max_nconstr = (float *) malloc (sizeof(float) * (*r));
00218 if (*max_nconstr == NULL)
00219 NLfit_error ("Unable to allocate memory for max_nconstr");
00220 *min_sconstr = (float *) malloc (sizeof(float) * (*p));
00221 if (*min_sconstr == NULL)
00222 NLfit_error ("Unable to allocate memory for min_sconstr");
00223 *max_sconstr = (float *) malloc (sizeof(float) * (*p));
00224 if (*max_sconstr == NULL)
00225 NLfit_error ("Unable to allocate memory for max_sconstr");
00226
00227
00228 for (ip = 0; ip < (*r); ip++)
00229 {
00230 (*min_nconstr)[ip] = plug_min_nconstr[plug_noise_index][ip];
00231 (*max_nconstr)[ip] = plug_max_nconstr[plug_noise_index][ip];
00232 }
00233
00234 for (ip = 0; ip < (*p); ip++)
00235 {
00236 (*min_sconstr)[ip] = plug_min_sconstr[plug_signal_index][ip];
00237 (*max_sconstr)[ip] = plug_max_sconstr[plug_signal_index][ip];
00238 }
00239
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 void check_for_valid_inputs ()
00249 {
00250 }
00251
00252
00253
00254
00255 void initialize_program
00256 (
00257 int * im1,
00258 char ** nname,
00259 char ** sname,
00260 vfp * nmodel,
00261 vfp * smodel,
00262 int * r,
00263 int * p,
00264 char *** npname,
00265 char *** spname,
00266 float ** min_nconstr,
00267 float ** max_nconstr,
00268 float ** min_sconstr,
00269 float ** max_sconstr,
00270 int * nabs,
00271 int * nrand,
00272 int * nbest,
00273 float * rms_min,
00274
00275 float ** par_rdcd,
00276 float ** par_full,
00277 float ** tpar_full,
00278
00279 int ts_length,
00280 char ** tfilename,
00281 float *** x_array,
00282
00283 float ** fit
00284 )
00285
00286 {
00287 int dimension;
00288 int ip;
00289 int it;
00290 MRI_IMAGE * im, * flim;
00291
00292 int nt;
00293 float * tar;
00294
00295
00296
00297 initialize_options (im1, nname, sname, nmodel, smodel, r, p, npname, spname,
00298 min_nconstr, max_nconstr, min_sconstr, max_sconstr,
00299 nabs, nrand, nbest, rms_min, tfilename);
00300
00301
00302 check_for_valid_inputs ();
00303
00304
00305
00306 *x_array = (float **) malloc (sizeof(float *) * ts_length);
00307 if (*x_array == NULL)
00308 NLfit_error ("Unable to allocate memory for x_array");
00309 for (it = 0; it < ts_length; it++)
00310 {
00311 (*x_array)[it] = (float *) malloc (sizeof(float) * 3);
00312 if ((*x_array)[it] == NULL)
00313 NLfit_error ("Unable to allocate memory for x_array[it]");
00314 }
00315
00316
00317 if (!plug_timeref)
00318 {
00319 static float old_DELT = -1.0 ;
00320 DELT = (inTR && dsTR > 0.0) ? dsTR : 1.0 ;
00321 if( DELT != old_DELT ){
00322 old_DELT = DELT ;
00323 printf("NLfit: switch to TR = %g\n",DELT) ;
00324 }
00325
00326 for (it = 0; it < ts_length; it++)
00327 {
00328 (*x_array)[it][0] = 1.0;
00329 (*x_array)[it][1] = it * DELT;
00330 (*x_array)[it][2] = (it * DELT) * (it * DELT);
00331 }
00332 }
00333 else
00334 {
00335 flim = mri_read_1D (*tfilename);
00336 if (flim == NULL)
00337 NLfit_error ("Unable to read time reference file \n");
00338 nt = flim -> nx;
00339 if (nt < ts_length)
00340 NLfit_error ("Time reference array is too short");
00341 tar = MRI_FLOAT_PTR(flim) ;
00342 for (it = 0; it < ts_length; it++)
00343 {
00344 (*x_array)[it][0] = 1.0;
00345 (*x_array)[it][1] = tar[it] ;
00346 (*x_array)[it][2] = tar[it] * tar[it];
00347 }
00348 mri_free (flim);
00349 }
00350
00351 dimension = (*r) + (*p);
00352
00353
00354 *par_rdcd = (float *) malloc (sizeof(float) * dimension);
00355 if (*par_rdcd == NULL)
00356 NLfit_error ("Unable to allocate memory for par_rdcd");
00357 *par_full = (float *) malloc (sizeof(float) * dimension);
00358 if (*par_full == NULL)
00359 NLfit_error ("Unable to allocate memory for par_full");
00360 *tpar_full = (float *) malloc (sizeof(float) * dimension);
00361 if (*tpar_full == NULL)
00362 NLfit_error ("Unable to allocate memory for tpar_full");
00363 *fit = (float *) malloc (sizeof(float) * (ts_length));
00364 if (*fit == NULL)
00365 NLfit_error ("Unable to allocate memory for fit");
00366
00367 }
00368
00369
00370
00371
00372
00373
00374
00375 void terminate_program
00376 (
00377 int r,
00378 int p,
00379 int ts_length,
00380 float *** x_array,
00381 float ** par_rdcd,
00382 float ** min_nconstr,
00383 float ** max_nconstr,
00384 float ** par_full,
00385 float ** tpar_full,
00386 float ** min_sconstr,
00387 float ** max_sconstr
00388 )
00389
00390 {
00391 int ip;
00392 int it;
00393
00394
00395
00396 if (*min_nconstr != NULL) { free (*min_nconstr); *min_nconstr = NULL; }
00397 if (*max_nconstr != NULL) { free (*max_nconstr); *max_nconstr = NULL; }
00398 if (*min_sconstr != NULL) { free (*min_sconstr); *min_sconstr = NULL; }
00399 if (*max_sconstr != NULL) { free (*max_sconstr); *max_sconstr = NULL; }
00400
00401
00402
00403 for (it = 0; it < ts_length; it++)
00404 if ((*x_array)[it] != NULL)
00405 { free ((*x_array)[it]); (*x_array)[it] = NULL; }
00406 if (*x_array != NULL) { free (*x_array); *x_array = NULL; }
00407
00408
00409
00410 if (*par_rdcd != NULL) { free (*par_rdcd); *par_rdcd = NULL; }
00411 if (*par_full != NULL) { free (*par_full); *par_full = NULL; }
00412 if (*tpar_full != NULL) { free (*tpar_full); *tpar_full = NULL; }
00413
00414 }
00415
00416
00417
00418
00419 float * nlfit
00420 (
00421 int ts_length,
00422 float * ts_array,
00423 char ** label
00424 )
00425
00426 {
00427 float * fit;
00428
00429
00430 int nabs;
00431 int nrand;
00432 int nbest;
00433 float rms_min;
00434
00435
00436 int im1;
00437 float ** x_array = NULL;
00438 char * tfilename = NULL;
00439
00440
00441 char * nname = NULL;
00442 vfp nmodel;
00443 int r;
00444 char ** npname = NULL;
00445 float * par_rdcd = NULL;
00446 float sse_rdcd;
00447 float * min_nconstr = NULL;
00448 float * max_nconstr = NULL;
00449
00450
00451 char * sname = NULL;
00452 vfp smodel;
00453 int p;
00454 char ** spname = NULL;
00455 float * par_full = NULL;
00456 float sse_full;
00457 float * tpar_full = NULL;
00458 float freg;
00459 float rmsreg;
00460 float rsqr;
00461 float smax;
00462 float tmax;
00463 float pmax;
00464 float area;
00465 float parea;
00466 float * min_sconstr = NULL;
00467 float * max_sconstr = NULL;
00468
00469 int novar;
00470
00471
00472
00473 initialize_program (&im1, &nname, &sname, &nmodel, &smodel,
00474 &r, &p, &npname, &spname,
00475 &min_nconstr, &max_nconstr, &min_sconstr, &max_sconstr,
00476 &nabs, &nrand, &nbest, &rms_min,
00477 &par_rdcd, &par_full, &tpar_full,
00478 ts_length, &tfilename, &x_array, &fit);
00479
00480
00481
00482 calc_reduced_model (ts_length, r, x_array, ts_array,
00483 par_rdcd, &sse_rdcd);
00484
00485
00486
00487 calc_full_model (nmodel, smodel, r, p,
00488 min_nconstr, max_nconstr, min_sconstr, max_sconstr,
00489 ts_length, x_array, ts_array, par_rdcd, sse_rdcd,
00490 nabs, nrand, nbest, rms_min, par_full, &sse_full, &novar);
00491
00492
00493
00494 full_model (nmodel, smodel, par_full, par_full + r,
00495 ts_length, x_array, fit);
00496
00497
00498
00499 analyze_results (nmodel, smodel, r, p, novar,
00500 min_nconstr, max_nconstr, min_sconstr, max_sconstr,
00501 ts_length, x_array,
00502 par_rdcd, sse_rdcd, par_full, sse_full,
00503 &rmsreg, &freg, &rsqr, &smax, &tmax, &pmax, &area, &parea,
00504 tpar_full);
00505
00506
00507
00508 report_results (nname, sname, r, p, npname, spname, ts_length,
00509 par_rdcd, sse_rdcd, par_full, sse_full, tpar_full,
00510 rmsreg, freg, rsqr, smax, tmax, pmax, area, parea, label);
00511 printf ("\nVoxel Results: \n");
00512 printf ("%s \n", *label);
00513
00514
00515
00516 terminate_program (r, p, ts_length, &x_array,
00517 &par_rdcd, &min_nconstr, &max_nconstr,
00518 &par_full, &tpar_full,
00519 &min_sconstr, &max_sconstr);
00520
00521 return (fit);
00522
00523 }
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541 DEFINE_PLUGIN_PROTOTYPE
00542
00543 PLUGIN_interface * PLUGIN_init( int ncall )
00544 {
00545 int ii ;
00546 PLUGIN_interface * plint ;
00547 int ok;
00548
00549 NLFIT_MODEL_array * model_array = NULL;
00550 int im;
00551 int ip;
00552
00553 char message[MAX_NAME_LENGTH];
00554
00555
00556 if( ncall > 0 ) return NULL ;
00557
00558 jump_on_NLfit_error = 1 ;
00559 if( setjmp(NLfit_error_jmpbuf) != 0 ){
00560 jump_on_NLfit_error = 0 ;
00561 fprintf(stderr,"\n*** Can't load NLfit plugin! ***\n");
00562 return NULL ;
00563 }
00564
00565
00566
00567
00568
00569 plint = PLUTO_new_interface( "NLfit & NLerr" ,
00570 "Control NLfit and NLerr Functions" ,
00571 helpstring ,
00572 PLUGIN_CALL_VIA_MENU , NL_main ) ;
00573
00574 PLUTO_add_hint( plint , "Control NLfit and NLerr Functions" ) ;
00575
00576 global_plint = plint ;
00577
00578 PLUTO_set_sequence( plint , "A:funcs:fitting" ) ;
00579
00580 PLUTO_set_runlabels( plint , "Set+Keep" , "Set+Close" ) ;
00581
00582
00583 model_array = NLFIT_get_many_MODELs ();
00584 if ((model_array == NULL) || (model_array->num == 0))
00585 #if 1
00586 { PLUTO_report( plint , "Found no models!") ;
00587 jump_on_NLfit_error = 0 ; return NULL ; }
00588 #else
00589 NLfit_error ("Unable to locate any models");
00590 #endif
00591
00592 #if 1
00593 else
00594 { char str[64] ;
00595 sprintf(str,"Found %d models",model_array->num) ;
00596 PLUTO_report( plint , str ) ;
00597 }
00598 #endif
00599
00600
00601 ii = 0;
00602 for (im = 0; im < model_array->num; im++)
00603 {
00604 if (model_array->modar[im]->interface->model_type == MODEL_NOISE_TYPE)
00605 {
00606 noise_labels[ii] = (char *) malloc (sizeof(char)*MAX_NAME_LENGTH);
00607 strncpy (noise_labels[ii], model_array->modar[im]->interface->label,
00608 MAX_NAME_LENGTH);
00609
00610 plug_nmodel[ii] = model_array->modar[im]->interface->call_func;
00611 if (plug_nmodel[ii] == NULL)
00612 {
00613 sprintf (message, "Noise model %s improperly defined. \n",
00614 noise_labels[ii]);
00615 NLfit_error (message);
00616 }
00617
00618 plug_r[ii] = model_array->modar[im]->interface->params;
00619 if ((plug_r[ii] < 0) || (plug_r[ii] > MAX_PARAMETERS))
00620 {
00621 sprintf (message,
00622 "Illegal number of parameters for noise model %s",
00623 noise_labels[ii]);
00624 NLfit_error (message);
00625 }
00626
00627 for (ip = 0; ip < plug_r[ii]; ip++)
00628 {
00629 noise_plabels[ii][ip] =
00630 (char *) malloc (sizeof(char)*MAX_NAME_LENGTH);
00631 strncpy (noise_plabels[ii][ip],
00632 model_array->modar[im]->interface->plabel[ip],
00633 MAX_NAME_LENGTH);
00634 plug_min_nconstr[ii][ip] =
00635 model_array->modar[im]->interface->min_constr[ip];
00636 plug_max_nconstr[ii][ip] =
00637 model_array->modar[im]->interface->max_constr[ip];
00638 if (plug_min_nconstr[ii][ip] > plug_max_nconstr[ii][ip])
00639 NLfit_error
00640 ("Must have noise parameter min cnstrnts <= max cnstrnts");
00641 }
00642 ii += 1;
00643 }
00644 }
00645 num_noise_models = ii;
00646 if (num_noise_models <= 0)
00647 NLfit_error ("Unable to locate any noise models");
00648 plug_noise_index = 1;
00649
00650
00651
00652 ii = 0;
00653 for (im = 0; im < model_array->num; im++)
00654 {
00655 if (model_array->modar[im]->interface->model_type == MODEL_SIGNAL_TYPE)
00656 {
00657 signal_labels[ii] = (char *) malloc (sizeof(char)*MAX_NAME_LENGTH);
00658 strncpy (signal_labels[ii],
00659 model_array->modar[im]->interface->label,
00660 MAX_NAME_LENGTH);
00661
00662 plug_smodel[ii] = model_array->modar[im]->interface->call_func;
00663 if (plug_smodel[ii] == NULL)
00664 {
00665 sprintf (message, "Signal model %s improperly defined. \n",
00666 signal_labels[ii]);
00667 NLfit_error (message);
00668 }
00669
00670 plug_p[ii] = model_array->modar[im]->interface->params;
00671 if ((plug_p[ii] < 0) || (plug_p[ii] > MAX_PARAMETERS))
00672 {
00673 sprintf (message,
00674 "Illegal number of parameters for signal model %s",
00675 signal_labels[ii]);
00676 NLfit_error (message);
00677 }
00678
00679
00680 for (ip = 0; ip < plug_p[ii]; ip++)
00681 {
00682 signal_plabels[ii][ip] =
00683 (char *) malloc (sizeof(char)*MAX_NAME_LENGTH);
00684 strncpy (signal_plabels[ii][ip],
00685 model_array->modar[im]->interface->plabel[ip],
00686 MAX_NAME_LENGTH);
00687 plug_min_sconstr[ii][ip] =
00688 model_array->modar[im]->interface->min_constr[ip];
00689 plug_max_sconstr[ii][ip] =
00690 model_array->modar[im]->interface->max_constr[ip];
00691 if (plug_min_sconstr[ii][ip] > plug_max_sconstr[ii][ip])
00692 NLfit_error
00693 ("Must have signal parameter min cnstrnts <= max cnstrnts");
00694 }
00695 ii += 1;
00696 }
00697 }
00698 num_signal_models = ii;
00699 if (num_signal_models <= 0)
00700 NLfit_error ("Unable to locate any signal models");
00701 plug_signal_index = 0;
00702
00703
00704
00705 PLUTO_add_option (plint , "Control" , "Control" , TRUE);
00706 PLUTO_add_number (plint , "Ignore" , 0,20,0,
00707 plug_ignore , FALSE );
00708 PLUTO_add_number (plint , "NRandom" , 10,99999,0,
00709 plug_nrand , TRUE );
00710 PLUTO_add_number (plint , "NBest" , 1,10,0,
00711 plug_nbest , FALSE);
00712
00713
00714
00715 PLUTO_add_option (plint , "Models" , "Models" , TRUE);
00716 PLUTO_add_string (plint, "Noise Model", num_noise_models, noise_labels,
00717 plug_noise_index);
00718 PLUTO_add_string (plint, "Signal Model", num_signal_models, signal_labels,
00719 plug_signal_index);
00720 PLUTO_add_string (plint, "Noise Constr", 2, constr_types, 0);
00721
00722
00723
00724 PLUTO_add_option (plint, "Noise", "Noise", FALSE);
00725 PLUTO_add_number (plint, "Parameter" , 0, MAX_PARAMETERS, 0, 0, FALSE);
00726 PLUTO_add_number (plint, "Min Constr", -99999, 99999, 0, 0, TRUE);
00727 PLUTO_add_number (plint, "Max Constr", -99999, 99999, 0, 0, TRUE);
00728
00729
00730
00731 PLUTO_add_option (plint, "Signal", "Signal", FALSE);
00732 PLUTO_add_number (plint, "Parameter", 0, MAX_PARAMETERS, 0, 0, FALSE);
00733 PLUTO_add_number (plint, "Min Constr", -99999, 99999, 0, 0, TRUE);
00734 PLUTO_add_number (plint, "Max Constr", -99999, 99999, 0, 0, TRUE);
00735
00736
00737
00738 PLUTO_add_option (plint, "Time Scale", "Time Scale", FALSE);
00739 PLUTO_add_string (plint, "Reference", 3, time_refs, 0);
00740 PLUTO_add_string (plint, "File", 0, NULL, 19);
00741
00742
00743
00744
00745 PLUTO_register_1D_funcstr ("NLfit" , NL_fitter);
00746 PLUTO_register_1D_funcstr ("NLerr" , NL_error);
00747
00748
00749
00750 #if 0
00751 DESTROY_MODEL_ARRAY (model_array);
00752 #endif
00753
00754 jump_on_NLfit_error = 0 ; return plint ;
00755 }
00756
00757
00758
00759
00760
00761
00762
00763 char * NL_main( PLUGIN_interface * plint )
00764 {
00765 char * str ;
00766 int ii, ival, ip ;
00767 float * tsar ;
00768 float min_constr, max_constr;
00769 MRI_IMAGE * im;
00770
00771
00772
00773
00774 PLUTO_next_option(plint) ;
00775
00776 plug_ignore = PLUTO_get_number(plint) ;
00777 plug_nrand = PLUTO_get_number(plint) ;
00778 plug_nbest = PLUTO_get_number(plint) ;
00779
00780
00781
00782
00783 do
00784 {
00785 str = PLUTO_get_optiontag(plint) ;
00786 if( str == NULL ) break ;
00787
00788 if( strcmp(str,"Models") == 0 )
00789 {
00790 str = PLUTO_get_string(plint) ;
00791 for (ii = 0; ii < num_noise_models; ii++)
00792 if (strcmp (str, noise_labels[ii]) == 0)
00793 plug_noise_index = ii;
00794
00795 str = PLUTO_get_string(plint) ;
00796 for (ii = 0; ii < num_signal_models; ii++)
00797 if (strcmp (str, signal_labels[ii]) == 0)
00798 plug_signal_index = ii;
00799
00800 str = PLUTO_get_string(plint);
00801 if (strcmp (str, "Absolute") == 0)
00802 plug_nabs = 1;
00803 else
00804 plug_nabs = 0;
00805 }
00806
00807 else if( strcmp(str,"Noise") == 0 )
00808 {
00809 ival = PLUTO_get_number(plint);
00810 min_constr = PLUTO_get_number(plint);
00811 max_constr = PLUTO_get_number(plint);
00812 if (min_constr > max_constr)
00813 return "**********************************\n"
00814 " Require min constr <= max constr \n"
00815 "**********************************" ;
00816 plug_min_nconstr[plug_noise_index][ival] = min_constr;
00817 plug_max_nconstr[plug_noise_index][ival] = max_constr;
00818 }
00819
00820 else if( strcmp(str,"Signal") == 0 )
00821 {
00822 ival = PLUTO_get_number(plint);
00823 min_constr = PLUTO_get_number(plint);
00824 max_constr = PLUTO_get_number(plint);
00825 if (min_constr > max_constr)
00826 return "**********************************\n"
00827 " Require min constr <= max constr \n"
00828 "**********************************" ;
00829 plug_min_sconstr[plug_signal_index][ival] = min_constr;
00830 plug_max_sconstr[plug_signal_index][ival] = max_constr;
00831 }
00832
00833 else if( strcmp(str,"Time Scale") == 0 )
00834 {
00835 str = PLUTO_get_string(plint);
00836 if (strcmp (str, "External") == 0){
00837 plug_timeref = 1;
00838 str = PLUTO_get_string(plint);
00839 im = mri_read_1D (str);
00840 if (im == NULL)
00841 return "************************************\n"
00842 " Unable to read time reference file \n"
00843 "************************************" ;
00844 mri_free(im);
00845 strcpy (plug_tfilename, str);
00846
00847 } else if( strcmp(str,"-inTR") == 0 ){
00848 inTR = 1 ;
00849 plug_timeref = 0;
00850
00851 } else {
00852 plug_timeref = 0;
00853 inTR = 0 ;
00854 }
00855
00856 }
00857
00858 else
00859 {
00860 return "************************\n"
00861 "Illegal optiontag found!\n"
00862 "************************" ;
00863 }
00864 } while(1) ;
00865
00866
00867
00868
00869 printf ("\n\n");
00870 printf ("Program: %s \n", PROGRAM_NAME);
00871 printf ("Author: %s \n", PROGRAM_AUTHOR);
00872 printf ("Date: %s \n", PROGRAM_DATE);
00873 printf ("\n");
00874
00875
00876
00877 printf ("\nControls: \n");
00878 printf ("Ignore = %5d \n", plug_ignore);
00879 printf ("Num Random = %5d \n", plug_nrand);
00880 printf ("Num Best = %5d \n", plug_nbest);
00881 printf ("Noise Constr = %s \n", constr_types[plug_nabs]);
00882 printf ("\nNoise Model = %s \n", noise_labels[plug_noise_index]);
00883 for (ip = 0; ip < plug_r[plug_noise_index]; ip++)
00884 {
00885 printf ("gn[%d]: min =%10.3f max =%10.3f %s \n",
00886 ip, plug_min_nconstr[plug_noise_index][ip],
00887 plug_max_nconstr[plug_noise_index][ip],
00888 noise_plabels[plug_noise_index][ip]);
00889 }
00890 printf ("\nSignal Model = %s \n", signal_labels[plug_signal_index]);
00891 for (ip = 0; ip < plug_p[plug_signal_index]; ip++)
00892 {
00893 printf ("gs[%d]: min =%10.3f max =%10.3f %s \n",
00894 ip, plug_min_sconstr[plug_signal_index][ip],
00895 plug_max_sconstr[plug_signal_index][ip],
00896 signal_plabels[plug_signal_index][ip]);
00897 }
00898
00899 if (plug_timeref)
00900 printf ("\nExternal Time Reference = %s \n", plug_tfilename);
00901 else if( inTR )
00902 printf ("\n-inTR Time Reference\n") ;
00903 else
00904 printf ("\nInternal Time Reference \n");
00905
00906
00907
00908
00909 initialize = 1 ;
00910
00911
00912 return NULL ;
00913 }
00914
00915
00916
00917
00918 void NL_fitter( int nt , double to , double dt , float * vec, char ** label )
00919 {
00920 NL_worker( nt , dt , vec , TRUE, label ) ;
00921 return ;
00922 }
00923
00924
00925
00926
00927 void NL_error( int nt , double to , double dt , float * vec, char ** label )
00928 {
00929 NL_worker( nt , dt , vec , FALSE, label ) ;
00930 return ;
00931 }
00932
00933
00934
00935
00936 void NL_worker( int nt , double dt , float * vec , int dofit, char ** label )
00937 {
00938 float * fit;
00939 int ii, nlen;
00940 float val;
00941
00942
00943 nlen = nt - plug_ignore;
00944
00945 dsTR = dt ;
00946
00947
00948
00949 fit = nlfit (nlen, vec+plug_ignore, label);
00950
00951 for (ii = 0; ii < plug_ignore; ii++)
00952 if (dofit)
00953 vec[ii] = fit[0];
00954 else
00955 vec[ii] = vec[plug_ignore] - fit[0];
00956
00957 for (ii=plug_ignore; ii < nt; ii++)
00958 {
00959 if (dofit)
00960 vec[ii] = fit[ii-plug_ignore];
00961 else
00962 vec[ii] = vec[ii] - fit[ii-plug_ignore] ;
00963 }
00964
00965 free(fit) ;
00966 return ;
00967 }
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978