8#include "tpcclibConfig.h"
27double *petmeas, *petsim;
30double wss_wo_penalty=0.0;
33double vbFunc(
int parNr,
double *p,
void*);
37static char *info[] = {
38 "Non-linear fitting of Vb as the only model parameter to blood and tissue,",
39 "time-activity curves (BTAC, and TTACs), to verify that goodness-of-fit",
40 "is better with compartmental models.",
41 "Sample times must be in minutes.",
43 "Usage: @P [Options] btacfile ttacfile endtime resultfile",
47 " Enter a lower limit for Vb; 0 by default.",
49 " Enter an upper limit for Vb; 100 by default.",
51 " Fitted regional TACs are written in DFT format.",
53 " Fitted and measured TACs are plotted in specified SVG file.",
56 "See also: fitk2, fitk3, fitk4, fitk5, fit_h2o, tacweigh, taccbv",
58 "Keywords: TAC, modelling, vascular fraction",
77int main(
int argc,
char **argv)
79 int ai, help=0, version=0, verbose=1;
80 int ri, fi, pi, m, n, ret;
81 char dfile[FILENAME_MAX], bfile[FILENAME_MAX], rfile[FILENAME_MAX], ffile[FILENAME_MAX];
82 char svgfile[FILENAME_MAX];
83 double fitdur, wss, aic;
89 def_pmin[0]=0.0; def_pmax[0]=1.0;
94 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
95 dfile[0]=bfile[0]=rfile[0]=ffile[0]=svgfile[0]=(char)0;
99 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
100 char *cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
102 if(strncasecmp(cptr,
"minVb=", 6)==0 && strlen(cptr)>3) {
105 if(minVb>=1.0) fprintf(stderr,
"Warning: min Vb was set to %g%%\n", 100.*minVb);
109 }
else if(strncasecmp(cptr,
"maxVb=", 6)==0 && strlen(cptr)>3) {
112 if(maxVb<0.05) fprintf(stderr,
"Warning: max Vb was set to %g%%\n", 100.*maxVb);
116 }
else if(strncasecmp(cptr,
"FIT=", 4)==0) {
117 strlcpy(ffile, cptr+4, FILENAME_MAX);
if(strlen(ffile)>0)
continue;
118 }
else if(strncasecmp(cptr,
"SVG=", 4)==0) {
119 strlcpy(svgfile, cptr+4, FILENAME_MAX);
if(strlen(svgfile)>0)
continue;
121 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
126 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
131 if(ai<argc) {
strlcpy(bfile, argv[ai++], FILENAME_MAX);}
132 if(ai<argc) {
strlcpy(dfile, argv[ai++], FILENAME_MAX);}
137 fprintf(stderr,
"Error: invalid fit time '%s'.\n", argv[ai]);
141 if(ai<argc) {
strlcpy(rfile, argv[ai++], FILENAME_MAX);}
143 fprintf(stderr,
"Error: invalid argument '%s'.\n", argv[ai]);
148 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
151 if(fitdur<=0) fitdur=1.0E+100;
152 if(def_pmin[0]>=def_pmax[0]) {
153 fprintf(stderr,
"Error: invalid limits for Vb.\n");
160 printf(
"bfile := %s\n", bfile);
161 printf(
"dfile :=%s\n", dfile);
162 printf(
"rfile := %s\n", rfile);
163 printf(
"ffile := %s\n", ffile);
164 printf(
"svgfile := %s\n", svgfile);
165 printf(
"fitdur := %g\n", fitdur);
166 printf(
"Vb: %g - %g\n", def_pmin[0], def_pmax[0]);
173 if(verbose>1) printf(
"reading tissue and input data\n");
177 &fitframeNr, &dft, &input, stdout, verbose-2, tmp);
179 fprintf(stderr,
"Error: %s\n", tmp);
182 if(fitframeNr<4 || input.
frameNr<4) {
183 fprintf(stderr,
"Error: too few samples in specified fit duration.\n");
190 fprintf(stdout,
"common_data_weights := %g", dft.
w[0]);
191 for(fi=1; fi<dft.
frameNr; fi++) fprintf(stdout,
", %g", dft.
w[fi]);
192 fprintf(stdout,
"\n");
199 if(verbose>1) printf(
"initializing result data\n");
202 fprintf(stderr,
"Error: cannot setup memory for results.\n");
220 pi=0; strcpy(res.
parname[pi],
"Vb"); strcpy(res.
parunit[pi],
"%");
221 pi++; strcpy(res.
parname[pi],
"WSS"); strcpy(res.
parunit[pi],
"");
222 pi++; strcpy(res.
parname[pi],
"AIC"); strcpy(res.
parunit[pi],
"");
229 fprintf(stdout,
"fitting regional TACs: ");
230 if(verbose>1) fprintf(stdout,
"\n");
233 for(ri=0; ri<dft.
voiNr; ri++) {
234 if(verbose>2) printf(
"\n %d %s:\n", ri, dft.
voi[ri].
name);
237 petmeas=dft.
voi[ri].
y; petsim=dft.
voi[ri].
y2;
240 pmin[0]=def_pmin[0]; pmax[0]=def_pmax[0];
242 printf(
" constraints :=");
243 for(pi=0; pi<parNr; pi++) printf(
" [%g,%g]", pmin[pi], pmax[pi]);
251 pmin, pmax, vbFunc, NULL, parNr, 8,
254 fprintf(stderr,
"\nError in optimization (%d).\n", ret);
258 printf(
" fitted_parameters :=");
259 for(pi=0; pi<parNr; pi++) printf(
" %g", res.
voi[ri].
parameter[pi]);
268 for(pi=n=0; pi<parNr; pi++)
if(pmax[pi]>pmin[pi]) n++;
269 if(verbose>2) printf(
"nr_of_fitted_parameters := %d\n", n);
270 for(fi=m=0; fi<fitframeNr; fi++)
if(dft.
w[fi]>0.0) m++;
271 if(verbose>2) printf(
"nr_of_fitted_samples := %d\n", m);
272 aic=
aicSS(wss, m, n);
279 if(dft.
voiNr>2 && verbose==1) {fprintf(stdout,
"."); fflush(stdout);}
283 if(verbose>0) {fprintf(stdout,
"\n"); fflush(stdout);}
286 for(ri=0; ri<res.
voiNr; ri++){
293 if(verbose>0) {
resPrint(&res); fprintf(stdout,
"\n");}
299 if(verbose>1) printf(
"saving results\n");
300 ret=
resWrite(&res, rfile, verbose-3);
302 fprintf(stderr,
"Error in writing '%s': %s\n", rfile,
reserrmsg);
306 if(verbose>0) fprintf(stdout,
"Model parameters written in %s\n", rfile);
312 if(svgfile[0] || ffile[0]) {
319 fprintf(stderr,
"Error: cannot save fitted curves.\n");
323 for(ri=0; ri<dft.
voiNr; ri++)
for(fi=0; fi<fitframeNr; fi++)
329 if(verbose>1) printf(
"saving SVG plot\n");
330 sprintf(tmp,
"Vb fit: ");
335 0.0, nan(
""), svgfile, verbose-8);
337 fprintf(stderr,
"Error (%d) in writing '%s'.\n", ret, svgfile);
341 if(verbose>0) printf(
"Plots written in %s\n", svgfile);
346 if(verbose>1) printf(
"saving fitted curves\n");
348 sprintf(dft2.
comments,
"# program := %s\n", tmp);
350 fprintf(stderr,
"Error in writing '%s': %s\n", ffile,
dfterrmsg);
354 if(verbose>0) printf(
"Fitted TACs written in %s\n", ffile);
370double vbFunc(
int parNr,
double *p,
void *fdata)
385 dft.
x1, dft.
x2, petsim, NULL, NULL, fitframeNr);
389 dft.
x, petsim, NULL, NULL, fitframeNr);
391 printf(
"error %d in interpolation\n", ret);
395 for(
int i=0; i<fitframeNr; i++) petsim[i]*=Vb;
398 for(
int i=0; i<fitframeNr; i++)
if(dft.
w[i]>0.0) {
399 double d=petmeas[i]-petsim[i];
404 if(0) {printf(
"%g => %g\n", pa[0], wss); fflush(stdout);}
double aicSS(double ss, const int n, const int k)
int modelCheckParameters(int par_nr, double *lower_p, double *upper_p, double *test_p, double *accept_p, double *penalty)
int atof_with_check(char *double_as_string, double *result_value)
double atof_dpi(char *str)
int dftdup(DFT *dft1, DFT *dft2)
int dftWrite(DFT *data, char *filename)
int res_allocate_with_dft(RES *res, DFT *dft)
int interpolate(double *x, double *y, int nr, double *newx, double *newy, double *newyi, double *newyii, int newnr)
Linear interpolation and integration.
int interpolate4pet(double *x, double *y, int nr, double *newx1, double *newx2, double *newy, double *newyi, double *newyii, int newnr)
Interpolate and integrate TAC to PET frames.
Header file for libtpccurveio.
int resWrite(RES *res, char *filename, int verbose)
#define DFT_TIME_STARTEND
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
size_t strlcpy(char *dst, const char *src, size_t dstsize)
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)
Header file for libtpcmodel.
int tgo(double *lowlim, double *uplim, double(*objf)(int, double *, void *), void *objfData, int dim, int neighNr, double *fmin, double *gmin, int samNr, int tgoNr, int verbose)
Header file for libtpcmodext.
int plot_fitrange_svg(DFT *dft1, DFT *dft2, char *main_title, double x1, double x2, double y1, double y2, char *fname, int verbose)
Header file for libtpcsvg.
char studynr[MAX_STUDYNR_LEN+1]
char comments[_DFT_COMMENT_LEN+1]
char parname[MAX_RESPARAMS][MAX_RESPARNAME_LEN+1]
char datafile[FILENAME_MAX]
char parunit[MAX_RESPARAMS][MAX_RESPARNAME_LEN+1]
char bloodfile[FILENAME_MAX]
double parameter[MAX_RESPARAMS]
char name[MAX_REGIONNAME_LEN+1]