10#include "tpcclibConfig.h"
29double func_wcdf(
int parNr,
double *p,
void*);
30double func_wcdfd(
int parNr,
double *p,
void*);
34typedef struct FITDATA {
50static char *info[] = {
51 "Non-linear fitting of the Weibull CDF and Weibull PDF sum function to PET",
52 "time-activity curves (TACs).",
55 " when x<=dT, f(x) = 0 ",
56 " when x>dT, f(x) = A * (n/B) * ((x-dT)/B)^(n-1) * exp(-((x-dT)/B)^n) +",
57 " k * A * (1-exp(-((x-dT)/B)^n))",
58 ", where A>=0, B>0, n>0 (usually n>1), k>=0, and dT>=0.",
60 "Usage: @P [Options] tacfile [parfile]",
64 " WCDF function without WPDF (derivative of WCDF) is fitted:",
65 " when x>dT, f(x) = A * (1-exp(-((x-dT)/B)^n))",
67 " All weights are set to 1.0 (no weighting); by default, weights in",
68 " data file are used, if available.",
70 " Weight by sampling interval.",
72 " Specify the constraints for function parameters;",
73 " This file with default values can be created by giving this option",
74 " as the only command-line argument to this program.",
75 " Without file name the default values are printed on screen.",
77 " Delay time (dT) is constrained to specified value (>=0).",
79 " Parameter k is constrained to given value; setting k to zero causes",
80 " the function to approach zero.",
82 " Fitted and measured TACs are plotted in specified SVG file.",
85 "See also: fit_sinf, fit_feng, fit_suri, fit_ratf, fit2dat, extrapol",
87 "Keywords: TAC, curve fitting",
106int main(
int argc,
char **argv)
108 int ai, help=0, version=0, verbose=1;
109 char tacfile[FILENAME_MAX], parfile[FILENAME_MAX], svgfile[FILENAME_MAX];
110 char limfile[FILENAME_MAX];
112 unsigned int model=0;
113 double fixed_delay=nan(
"");
114 double fixed_k=nan(
"");
120 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
121 tacfile[0]=parfile[0]=svgfile[0]=limfile[0]=(char)0;
124 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
126 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(!*cptr)
continue;
127 if(strcasecmp(cptr,
"W1")==0) {
129 }
else if(strcasecmp(cptr,
"WF")==0) {
131 }
else if(strcasecmp(cptr,
"CDF")==0 || strcasecmp(cptr,
"WCDF")==0) {
133 }
else if(strcasecmp(cptr,
"CDFD")==0 || strcasecmp(cptr,
"WCDFD")==0) {
135 }
else if(strncasecmp(cptr,
"DELAY=", 6)==0 && strlen(cptr)>6) {
136 if(!
atofCheck(cptr+6, &fixed_delay) && fixed_delay>=0.0)
continue;
137 }
else if(strncasecmp(cptr,
"DT=", 3)==0 && strlen(cptr)>3) {
138 if(!
atofCheck(cptr+3, &fixed_delay) && fixed_delay>=0.0)
continue;
139 }
else if(strncasecmp(cptr,
"K=", 2)==0 && strlen(cptr)>2) {
140 if(!
atofCheck(cptr+2, &fixed_k) && fixed_k>=0.0)
continue;
141 }
else if(strncasecmp(cptr,
"SVG=", 4)==0 && strlen(cptr)>4) {
142 strlcpy(svgfile, cptr+4, FILENAME_MAX);
continue;
143 }
else if(strncasecmp(cptr,
"LIM=", 4)==0 && strlen(cptr)>4) {
144 strlcpy(limfile, cptr+4, FILENAME_MAX);
continue;
145 }
else if(strcasecmp(cptr,
"LIM")==0) {
146 strcpy(limfile,
"stdout");
continue;
148 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
157 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
162 if(ai<argc)
strlcpy(tacfile, argv[ai++], FILENAME_MAX);
163 if(ai<argc)
strlcpy(parfile, argv[ai++], FILENAME_MAX);
165 fprintf(stderr,
"Error: invalid argument '%s'.\n", argv[ai]);
171 if(tacfile[0]) printf(
"tacfile := %s\n", tacfile);
172 if(limfile[0]) printf(
"limfile := %s\n", limfile);
173 if(parfile[0]) printf(
"parfile := %s\n", parfile);
174 if(svgfile[0]) printf(
"svgfile := %s\n", svgfile);
175 if(!isnan(fixed_k)) printf(
"fixed_k := %g\n", fixed_k);
176 if(!isnan(fixed_delay)) printf(
"fixed_delay := %g\n", fixed_delay);
177 printf(
"model := %s\n",
modelCode(model));
178 printf(
"weights := %d\n",
weights);
183 fprintf(stderr,
"Error: cannot initiate parameter list.\n");
return(1);}
185 strcpy(plim.
n[0].
name,
"dT"); plim.
n[0].
lim1=0.0; plim.
n[0].
lim2=100.0;
186 strcpy(plim.
n[1].
name,
"A"); plim.
n[1].
lim1=1.0E-06; plim.
n[1].
lim2=1.0E+03;
187 strcpy(plim.
n[2].
name,
"B"); plim.
n[2].
lim1=1.0E-06; plim.
n[2].
lim2=20.0;
192 strcpy(plim.
n[3].
name,
"n"); plim.
n[3].
lim1=1.0E-03; plim.
n[3].
lim2=4.0;
196 if(limfile[0] && !tacfile[0]) {
198 if(strcasecmp(limfile,
"stdout")!=0 &&
fileExist(limfile)) {
199 fprintf(stderr,
"Error: parameter constraint file %s exists.\n", limfile);
204 if(verbose>1 && strcasecmp(limfile,
"stdout")!=0)
205 printf(
"writing parameter constraints file\n");
207 fprintf(stderr,
"Error: cannot write constraints file.\n");
210 if(strcasecmp(limfile,
"stdout")!=0)
211 fprintf(stdout,
"Parameter constraints written in %s\n", limfile);
217 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
220 if(strcasecmp(limfile,
"stdout")==0) limfile[0]=(char)0;
228 fprintf(stderr,
"Error: cannot read constraints file.\n");
234 if(!isnan(fixed_delay)) plim.
n[0].
lim1=plim.
n[0].
lim2=fixed_delay;
235 if(!isnan(fixed_k)) plim.
n[4].
lim1=plim.
n[4].
lim2=fixed_k;
243 if(verbose>1) printf(
"reading %s\n", tacfile);
251 printf(
"tacNr := %d\n", tac.
tacNr);
252 printf(
"sampleNr := %d\n", tac.
sampleNr);
255 printf(
"isframe := %d\n", tac.
isframe);
258 fprintf(stderr,
"Error: file contains no data.\n");
262 fprintf(stderr,
"Error: too few samples.\n");
267 fprintf(stderr,
"Error: data contains missing values.\n");
275 fprintf(stderr,
"Error: invalid data sample times.\n");
279 printf(
"xmin := %g\n", xmin);
280 printf(
"xmax := %g\n", xmax);
283 if(!limfile[0] && plim.
n[0].
lim2>plim.
n[0].
lim1 && xmax<plim.
n[0].
lim2/0.75)
284 plim.
n[0].
lim2=0.75*xmax;
290 for(
int i=0; i<tac.
sampleNr; i++) tac.
w[i]=1.0;
294 for(
int i=0; i<tac.
sampleNr; i++) tac.
w[i]=1.0;
306 if(verbose>1) printf(
"preparing space for parameters\n");
314 for(
int i=0; i<tac.
tacNr; i++) {
328 iftPut(&par.
h,
"program", buf, 0, NULL);
331 iftPut(&par.
h,
"datafile", tacfile, 0, NULL);
337 if(verbose>1) printf(
"preparing for fitting\n");
341 if(verbose>0) printf(
"fitting...\n");
342 for(
int ci=0; ci<tac.
tacNr; ci++) {
343 if(verbose>1) printf(
"TAC %d: %s\n", 1+ci, tac.
c[ci].
name);
345 double ymax;
int ymaxi;
346 if(
tacYRange(&tac, ci, NULL, &ymax, NULL, &ymaxi, NULL, NULL)!=0) {
347 fprintf(stderr,
"Error: invalid y (concentration) values.\n");
350 if(verbose>3) printf(
" ymax := %g\n", ymax);
356 fitdata.ymeas=tac.
c[ci].
y;
362 fprintf(stderr,
"Error: cannot initiate NLLS.\n");
368 for(
int i=0; i<par.
parNr; i++) {
372 for(
int i=0; i<par.
parNr; i++) {
393 printf(
"measured and fitted TAC:\n");
395 printf(
"\t%g\t%g\t%g\n", tac.
x[i], tac.
c[ci].
y[i], yfit[i]);
400 for(
int i=0; i<par.
parNr; i++) par.
r[ci].
p[i]=nlo.
xfull[i];
410 if(verbose>1) printf(
" saving %s\n", parfile);
411 FILE *fp=fopen(parfile,
"w");
413 fprintf(stderr,
"Error: cannot open file for writing.\n");
422 if(verbose>0) printf(
"parameters saved in %s\n", parfile);
431 if(verbose>1) printf(
"plotting measured and fitted data\n");
444 for(
int ci=0; ci<tac.
tacNr && ret==0; ci++)
456 for(
int ci=0; ci<tac.
tacNr && ret==0; ci++)
460 fprintf(stderr,
"Error: cannot calculate function values.\n");
466 ret=
tacPlotFitSVG(&tac, &ftac,
"", nan(
""), nan(
""), nan(
""), nan(
""), svgfile, &status);
473 if(verbose>0) printf(
"Measured and fitted data plotted in %s\n", svgfile);
489double func_wcdfd(
int parNr,
double *p,
void *fdata)
492 FITDATA *d=(FITDATA*)fdata;
495 for(
unsigned int i=0; i<d->n; i++) {
497 if(isnan(d->ymeas[i]) || isnan(d->x[i]))
continue;
498 double x=d->x[i]-p[0];
500 double et=exp(-pow(x/p[2], p[3]));
501 double wpdf=(p[3]/p[2]) * pow(x/p[2], p[3]-1.0) * et;
502 d->ysim[i]=p[1]*(wpdf + p[4]*(1.0-et));
504 v=d->ysim[i]-d->ymeas[i];
508 for(
unsigned int i=0; i<(
unsigned int)parNr; i++) printf(
" %g", p[i]);
509 printf(
" -> %g\n", wss);
514double func_wcdf(
int parNr,
double *p,
void *fdata)
517 FITDATA *d=(FITDATA*)fdata;
520 for(
unsigned int i=0; i<d->n; i++) {
522 if(isnan(d->ymeas[i]) || isnan(d->x[i]))
continue;
523 double x=d->x[i]-p[0];
525 d->ysim[i]=p[1]*(1.0 - exp(-pow(x/p[2], p[3])));
527 v=d->ysim[i]-d->ymeas[i];
531 for(
unsigned int i=0; i<(
unsigned int)parNr; i++) printf(
" %g", p[i]);
532 printf(
" -> %g\n", wss);
char * ctime_r_int(const time_t *t, char *buf)
Convert calendar time t into a null-terminated string of the form YYYY-MM-DD hh:mm:ss,...
int atofCheck(const char *s, double *v)
int fileExist(const char *filename)
int mfEvalY(const char *fid, const int parNr, const double *p, const int sampleNr, const double *x, double *y, const int verbose)
unsigned int drandSeed(short int seed)
Make and optionally set the seed for rand(), drand, drandRange, and drandGaussian().
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
unsigned int simSamples(double initStep, double maxStep, double endTime, int mode, double *x)
char * modelCode(const unsigned int i)
unsigned int modelParNr(const unsigned int code)
unsigned int modelCodeIndex(const char *s)
void nloptInit(NLOPT *nlo)
int nloptAllocate(NLOPT *nlo, unsigned int parNr)
void nloptFree(NLOPT *nlo)
void nloptWrite(NLOPT *d, FILE *fp)
int parAllocate(PAR *par, int parNr, int tacNr)
int parWrite(PAR *par, FILE *fp, parformat format, int extra, TPCSTATUS *status)
int parReadLimits(PAR *par, const char *fname, const int verbose)
void parListLimits(PAR *par, FILE *fp)
int parWriteLimits(PAR *par, const char *fname, const int verbose)
int parAllocateWithTAC(PAR *par, TAC *tac, int parNr, TPCSTATUS *status)
Allocate PAR based on data in TAC.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
void tpcProgramName(const char *program, int version, int copyright, char *prname, int n)
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 *)
IFT h
Optional (but often useful) header information.
char name[MAX_PARNAME_LEN+1]
char name[MAX_TACNAME_LEN+1]
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
int tacCopyTacchdr(TACC *d1, TACC *d2)
int tacCopyHdr(TAC *tac1, TAC *tac2)
Copy TAC header data from tac1 to tac2.
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)
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
char * tacFormattxt(tacformat c)
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
int tacSortByTime(TAC *d, TPCSTATUS *status)
int tacIsWeighted(TAC *tac)
unsigned int tacWSampleNr(TAC *tac)
int tacWByFreq(TAC *tac, isotope isot, TPCSTATUS *status)
int tacGetSampleInterval(TAC *d, double ilimit, double *minfdur, double *maxfdur)
Get the shortest and longest sampling intervals or frame lengths in TAC structure.
int tacXRange(TAC *d, double *xmin, double *xmax)
Get the range of x values (times) in TAC structure.
int tacYRange(TAC *d, int i, double *ymin, double *ymax, int *smin, int *smax, int *imin, int *imax)
Get the range of y values (concentrations) in TAC struct.
int nloptIATGO(NLOPT *nlo, const int doLocal, unsigned int maxIterNr, double neighFract, TPCSTATUS *status)
int nloptITGO2(NLOPT *nlo, const int doLocal, unsigned int tgoNr, unsigned int sampleNr, unsigned int neighNr, TPCSTATUS *status)
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 libtpcfileutil.
Header file for library libtpcift.
@ ISOTOPE_UNKNOWN
Unknown.
Header file for library libtpcnlopt.
Header file for libtpcpar.
@ PAR_FORMAT_FIT
Function fit format of Turku PET Centre.
@ PAR_FORMAT_TSV_UK
UK TSV (point as decimal separator).
Header file for libtpcrand.
Header file for library libtpctac.
@ TAC_FORMAT_UNKNOWN
Unknown format.
Header file for libtpctacmod.