8#include "tpcclibConfig.h"
29double func_av(
int parNr,
double *p,
void*);
31typedef struct FITDATA {
59static char *info[] = {
60 "Non-linear fitting of the parameters of compartmental model for",
63 "Usage: @P [Options] arterytac venatac [parfile]",
67 " All weights are set to 1.0 (no weighting); by default, weights in",
68 " output data file are used, if available.",
70 " Weight by output TAC sampling interval.",
72 " Fitted and measured TACs are plotted in specified SVG file.",
75 "If TAC files contain more than one TAC, only the first is used.",
77 "See also: sim_av, fit_xexp, convexpf",
79 "Keywords: input, curve fitting",
136 double b, c, d, w, z, dt2;
137 double cai, ca_last, t_last;
138 double ct1, ct1_last, ct2, ct2_last, ct3, ct3_last;
139 double ct1i, ct1i_last, ct2i, ct2i_last, ct3i, ct3i_last;
144 if(cvb==NULL)
return 2;
147 if(!(f>=0.0) || !(k1>=0.0) || k1>f)
return 3;
148 if(k3<=0.0) {k3=0.0;
if(k2<=0.0) k2=0.0;}
149 else if(k5<=0.0) {k5=0.0;
if(k4<=0.0) k4=0.0;}
150 else {
if(k6<=0.0) k6=0.0;}
153 t_last=0.0;
if(t[0]<t_last) t_last=t[0];
155 ct1_last=ct2_last=ct3_last=ct1i_last=ct2i_last=ct3i_last=0.0;
156 ct1=ct2=ct3=ct1i=ct2i=ct3i=0.0;
157 for(i=0; i<nr; i++) {
159 dt2=0.5*(t[i]-t_last);
165 cai+=(cab[i]+ca_last)*dt2;
167 b=ct1i_last+dt2*ct1_last;
168 c=ct2i_last+dt2*ct2_last;
169 d=ct3i_last+dt2*ct3_last;
170 w=k4 + k5 - (k5*k6*dt2)/(1.0+k6*dt2);
174 + k1*z*cai + (k3*k4*dt2 - (k2+k3)*z)*b
175 + k4*c + k4*k6*dt2*d/(1.0+k6*dt2)
176 ) / ( z*(1.0 + dt2*(k2+k3)) - k3*k4*dt2*dt2 );
177 ct1i = ct1i_last + dt2*(ct1_last+ct1);
179 ct2 = (k3*ct1i - w*c + k6*d/(1.0+k6*dt2)) / z;
180 ct2i = ct2i_last + dt2*(ct2_last+ct2);
182 ct3 = (k5*ct2i - k6*d) / (1.0 + k6*dt2);
183 ct3i = ct3i_last + dt2*(ct3_last+ct3);
186 double dct = k1*cab[i] - k2*ct1;
187 cvb[i] = cab[i] - dct/f;
192 if(ct!=NULL) {ct[i]=ct1+ct2+ct3;
if(fabs(ct[i])<1.0e-12) ct[i]=0.0;}
195 t_last=t[i]; ca_last=cab[i];
196 ct1_last=ct1; ct1i_last=ct1i;
197 ct2_last=ct2; ct2i_last=ct2i;
198 ct3_last=ct3; ct3i_last=ct3i;
210int main(
int argc,
char **argv)
212 int ai, help=0, version=0, verbose=1;
213 char afile[FILENAME_MAX], vfile[FILENAME_MAX], parfile[FILENAME_MAX], svgfile[FILENAME_MAX];
222 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
223 afile[0]=vfile[0]=parfile[0]=svgfile[0]=(char)0;
225 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
227 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
228 if(strcasecmp(cptr,
"W1")==0) {
230 }
else if(strcasecmp(cptr,
"WF")==0) {
232 }
else if(strncasecmp(cptr,
"SVG=", 4)==0 && strlen(cptr)>4) {
233 strlcpy(svgfile, cptr+4, FILENAME_MAX);
continue;
234 }
else if(strcasecmp(cptr,
"SER2")==0) {
236 }
else if(strcasecmp(cptr,
"MET")==0) {
239 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
248 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
253 if(ai<argc)
strlcpy(afile, argv[ai++], FILENAME_MAX);
254 if(ai<argc)
strlcpy(vfile, argv[ai++], FILENAME_MAX);
255 if(ai<argc)
strlcpy(parfile, argv[ai++], FILENAME_MAX);
257 fprintf(stderr,
"Error: invalid argument '%s'.\n", argv[ai]);
262 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
268 printf(
"afile := %s\n", afile);
269 printf(
"vfile := %s\n", vfile);
270 if(parfile[0]) printf(
"parfile := %s\n", parfile);
271 if(svgfile[0]) printf(
"svgfile := %s\n", svgfile);
272 printf(
"weights := %d\n",
weights);
273 printf(
"model := %d\n", model);
281 if(verbose>1) printf(
"reading TAC data\n");
285 double fitdur=1.0E+12;
287 &fitSampleNr, &atac, &vtac, &status);
294 printf(
"a.sampleNr := %d\n", atac.
sampleNr);
295 printf(
"v.sampleNr := %d\n", vtac.
sampleNr);
296 printf(
"fitSampleNr := %d\n", fitSampleNr);
299 printf(
"fitdur := %g s\n", fitdur);
301 if(fitSampleNr<4 || atac.
sampleNr<4) {
302 fprintf(stderr,
"Error: too few samples.\n");
315 for(
int i=0; i<vtac.
sampleNr; i++) vtac.
w[i]=1.0;
319 for(
int i=0; i<vtac.
sampleNr; i++) vtac.
w[i]=1.0;
332 if(verbose>1) printf(
"preparing for fitting\n");
340 fitdata.iy=atac.
c[0].
y;
341 fitdata.cy=atac.
c[1].
y;
345 fitdata.y=vtac.
c[0].
y;
346 fitdata.ysim=vtac.
c[1].
y;
352 if(verbose>2) printf(
"process initial parameter values\n");
353 int parNr=4;
if(model==1) parNr=6;
365 nlo.
xlower[0]=-0.03*fitdur;
410 if(verbose>2) printf(
"fixedNr := %d\n", fixedNr);
425 if(verbose>0) printf(
"fitting\n");
432 printf(
"measured and fitted TAC:\n");
434 printf(
"\t%g\t%g\t%g\n", vtac.
x[i], vtac.
c[0].
y[i], vtac.
c[1].
y[i]);
437 if(verbose>2) printf(
" wss := %g\n", nlo.
funval);
444 if(verbose>1) printf(
"plotting measured and fitted data\n");
445 for(
int i=0; i<atac.
sampleNr; i++) atac.
c[0].
y[i]=atac.
c[1].
y[i];
447 ret=
tacPlotFitSVG(&vtac, &atac,
"", nan(
""), nan(
""), nan(
""), nan(
""), svgfile, &status);
454 if(verbose>0) printf(
"Measured and fitted data plotted in %s\n", svgfile);
472double func_av(
int parNr,
double *p,
void *fdata)
474 FITDATA *d=(FITDATA*)fdata;
476 if(d->verbose>0) {printf(
"%s()\n", __func__); fflush(stdout);}
477 if(d->verbose>20) printf(
"p[]: %g %g %g %g\n", p[0], p[1], p[2], p[3]);
480 double delay, f, K1, k2, k3=0.0, k4=0.0;
492 for(
unsigned int i=0; i<d->in; i++) dx[i]=d->ix[i]+delay;
495 if(
simC3vb(d->ix, d->iy, d->in, f, K1, k2, k3, k4, 0.0, 0.0, d->cy, NULL)) {
496 fprintf(stderr,
"Error: cannot simulate.\n");
501 if(d->verbose>3) {fprintf(stdout,
"interpolation...\n"); fflush(stdout);}
502 if(
liInterpolate(dx, d->cy, d->in, d->x, d->ysim, NULL, NULL, d->n, 0, 1, 0)!=0) {
503 fprintf(stderr,
"Error: cannot interpolate.\n");
507 printf(
"\nData x\ty\tSimulated\n");
508 for(
unsigned int i=0; i<d->n; i++)
509 printf(
"%g\t%g\t%g\n", d->x[i], d->y[i], d->ysim[i]);
513 if(d->verbose>2) {fprintf(stdout,
"computing WSS...\n"); fflush(stdout);}
515 for(
unsigned i=0; i<d->n; i++) {
516 double v=d->ysim[i] - d->y[i];
520 for(
int i=0; i<parNr; i++) printf(
" %g", p[i]);
521 printf(
" -> %g\n", wss);
int simC3vb(double *t, double *cab, const int nr, double f, double k1, double k2, double k3, double k4, double k5, double k6, double *cvb, double *ct)
unsigned int drandSeed(short int seed)
Make and optionally set the seed for rand(), drand, drandRange, and drandGaussian().
int liInterpolate(double *x, double *y, const int nr, double *newx, double *newy, double *newyi, double *newyii, const int newnr, const int se, const int ee, const int verbose)
Linear interpolation and/or integration with trapezoidal method.
void nloptInit(NLOPT *nlo)
int nloptAllocate(NLOPT *nlo, unsigned int parNr)
unsigned int nloptFixedNr(NLOPT *d)
void nloptFree(NLOPT *nlo)
void nloptWrite(NLOPT *d, FILE *fp)
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
void tpcPrintBuild(const char *program, FILE *fp)
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
void statusInit(TPCSTATUS *s)
char * errorMsg(tpcerror e)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
double(* _fun)(int, double *, void *)
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int tacAllocateMore(TAC *tac, int tacNr)
int tacPlotFitSVG(TAC *tac1, TAC *tac2, const char *main_title, const double x1, const double x2, const double y1, const double y2, const char *fname, TPCSTATUS *status)
char * tacFormattxt(tacformat c)
int tacIsWeighted(TAC *tac)
int tacWByFreq(TAC *tac, isotope isot, TPCSTATUS *status)
int nloptITGO2(NLOPT *nlo, const int doLocal, unsigned int tgoNr, unsigned int sampleNr, unsigned int neighNr, TPCSTATUS *status)
Header file for libtpccm.
Header file for library libtpcextensions.
weights
Is data weighted, or are weight factors available with data?
@ WEIGHTING_OFF
Not weighted or weights not available (weights for all included samples are 1.0).
char * unitName(int unit_code)
Header file for library libtpcift.
@ ISOTOPE_UNKNOWN
Unknown.
Header file for libtpcli.
Header file for libtpclinopt.
Header file for library libtpcnlopt.
Header file for libtpcpar.
Header file for libtpcrand.
Header file for library libtpctac.
Header file for libtpctacmod.