10#include "tpcclibConfig.h"
23int FIT2DAT_MAXNR=50000;
27static char *info[] = {
28 "Calculates the PET time-activity curves (TACs) using parameters of",
29 "a mathematical function, which have been fitted to the data previously.",
31 "Usage: @P [Options] fitfile tacfile",
33 "Fitfile must be in the DFT fit format; it contains the parameters",
34 "of the functions fitted to one or more PET TACs. Fitfile specifications:",
35 "https://www.turkupetcentre.net/petanalysis/format_tpc_fit.html",
38 " -i Integrals of functions from zero to sample time are calculated;",
39 " not available for all functions.",
41 " Function values are calculated at frame mid times (default), or as",
42 " means between frame start and end times, when possible.",
45 " The sample times, where function values are to be calculated, can",
46 " be specified with one of the following options:",
48 " autointerpolate the data from 0 to stop time, or to fit range end time",
49 " (default), with decreasing sample frequency.",
50 " -C=<start,stop,step>",
51 " specify the first and last sample time, and the sample frequence.",
53 " specify a TAC file, which contains the time framing information.",
55 " specify the number of sample points between fit start and end times.",
57 " specify the sample times individually.",
59 " Specified nr of random x values between low and up are generated.",
61 " Specified nr of random x values between low and up are generated",
62 " with distribution biased towards low absolute values.",
64 "See also: simframe, fit_ppf, fit_sinf, fit_sigm, metabcor, fit2res",
66 "Keywords: TAC, simulation, input, interpolation, extrapolation",
85int main(
int argc,
char **argv)
87 int ai, help=0, version=0, verbose=1;
88 int ri, fi, xnr=0, rnr=0, integr=0, ret;
92 char *cptr, fitfile[FILENAME_MAX], datafile[FILENAME_MAX],
93 xfile[FILENAME_MAX], tmp[FILENAME_MAX];
94 double numbers[FIT2DAT_MAXNR], a, b, c, d, aiendtime=-9.9;
105 if(argc==1) {
tpcPrintUsage(argv[0], info, stderr);
return(1);}
106 fitfile[0]=datafile[0]=xfile[0]=(char)0;
109 for(ai=1; ai<argc; ai++)
if(*argv[ai]==
'-') {
110 cptr=argv[ai]+1;
if(*cptr==
'-') cptr++;
if(cptr==NULL)
continue;
112 if(strcasecmp(cptr,
"I")==0) {
114 }
else if(strcasecmp(cptr,
"MID")==0) {
115 useFrames=0;
continue;
116 }
else if(strcasecmp(cptr,
"FRAMES")==0) {
117 useFrames=1;
continue;
118 }
else if(strncasecmp(cptr,
"X", 1)==0) {
119 cptr++;
if(*cptr==
'=') cptr++;
120 if(xnr>0 || strlen(xfile)) {fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
122 strcpy(tmp, cptr); cptr=strtok(tmp,
";,:|");
123 while(cptr!=NULL && xnr<FIT2DAT_MAXNR) {
124 numbers[xnr++]=
atof_dpi(cptr); cptr=strtok(NULL,
";,:|");}
127 for(ri=0; ri<xnr; ri++)
for(fi=ri+1; fi<xnr; fi++)
128 if(numbers[ri]>numbers[fi]) {d=numbers[fi]; numbers[fi]=numbers[ri]; numbers[ri]=d;}
130 }
else if(strncasecmp(cptr,
"C=", 2)==0) {
132 if(xnr>0 || strlen(xfile)) {fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
134 a=b=c=0.; strcpy(tmp, cptr); cptr=strtok(tmp,
";,");
135 if(cptr!=NULL) {a=
atof_dpi(cptr); cptr=strtok(NULL,
";,:|");}
136 if(cptr!=NULL) {b=
atof_dpi(cptr); cptr=strtok(NULL,
";,:|");}
138 if(a>=b || c<1.0E-14) {fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
140 for(xnr=0; v<(b+0.2*c) && xnr<FIT2DAT_MAXNR; v=a+c*(double)xnr) numbers[xnr++]=v;
142 }
else if(strncasecmp(cptr,
"R=", 2)==0) {
144 if(xnr>0 || rnr>0 || strlen(xfile)) {
145 fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
147 a=b=0.; xnr=0; strcpy(tmp, cptr); cptr=strtok(tmp,
";,");
148 if(cptr!=NULL) {a=
atof_dpi(cptr); cptr=strtok(NULL,
";,:|");}
149 if(cptr!=NULL) {b=
atof_dpi(cptr); cptr=strtok(NULL,
";,:|");}
150 if(cptr!=NULL) {xnr=atoi(cptr);}
151 if(a>=b || xnr<=0 || xnr>FIT2DAT_MAXNR) {
152 fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
155 }
else if(strncasecmp(cptr,
"RST=", 4)==0) {
157 if(xnr>0 || rnr>0 || strlen(xfile)) {
158 fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
160 a=b=0.; xnr=0; strcpy(tmp, cptr); cptr=strtok(tmp,
";,");
161 if(cptr!=NULL) {a=
atof_dpi(cptr); cptr=strtok(NULL,
";,:|");}
162 if(cptr!=NULL) {b=
atof_dpi(cptr); cptr=strtok(NULL,
";,:|");}
163 if(cptr!=NULL) {xnr=atoi(cptr);}
164 if(a>=b || xnr<=0 || xnr>FIT2DAT_MAXNR) {
165 fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
168 }
else if(strncasecmp(cptr,
"F=", 2)==0) {
170 if(xnr>0 || strlen(xfile)) {fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
174 }
else if(strncasecmp(cptr,
"N=", 2)==0) {
176 if(xnr>0 || strlen(xfile)) {fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
179 if(rnr<2) {fprintf(stderr,
"Error: invalid nr of x values.\n");
return 1;}
181 }
else if(strcasecmp(cptr,
"A")==0) {
182 aiendtime=0.0;
continue;
183 }
else if(strncasecmp(cptr,
"A=", 2)==0) {
185 if(xnr>0 || strlen(xfile)) {fprintf(stderr,
"Error: invalid x value(s).\n");
return(1);}
187 aiendtime=
atof_dpi(cptr);
if(aiendtime>0.0)
continue;
189 fprintf(stderr,
"Error: invalid option '%s'.\n", argv[ai]);
194 if(help==2) {
tpcHtmlUsage(argv[0], info,
"");
return(0);}
199 for(; ai<argc; ai++) {
200 if(!fitfile[0]) {strcpy(fitfile, argv[ai]);
continue;}
201 else if(!datafile[0]) {strcpy(datafile, argv[ai]);
continue;}
203 fprintf(stderr,
"Error: too many arguments: '%s'.\n", argv[ai]);
210 fprintf(stderr,
"Error: missing command-line argument; use option --help\n");
213 if(xnr<1 && rnr<1 && !strlen(xfile) && aiendtime<0.0) {
216 if(integr) useFrames=0;
220 printf(
"fitfile := %s\n", fitfile);
221 printf(
"datafile := %s\n", datafile);
222 printf(
"xfile := %s\n", xfile);
223 printf(
"integr := %d\n", integr);
224 printf(
"useFrames := %d\n", useFrames);
226 for(ri=0; ri<xnr; ri++) printf(
" %g", numbers[ri]);
228 printf(
"rnr := %d\n", rnr);
229 printf(
"aiendtime := %g\n", aiendtime);
232 printf(
"Available functions:\n");
233 for(ri=1; ri<2000; ri++) {
242 if(verbose>1) printf(
"Reading fitfile\n");
243 if((ret=
fitRead(fitfile, &fit, verbose-2))) {
244 fprintf(stderr,
"Error %d in reading '%s': %s\n", ret, fitfile,
fiterrmsg);
255 if(verbose>1) printf(
"Reading x values from file %s\n", xfile);
257 fprintf(stderr,
"Error in reading '%s': %s\n", xfile,
dfterrmsg);
261 if(verbose>2) printf(
"Allocating more memory.\n");
263 fprintf(stderr,
"Memory allocation error.\n");
266 for(ri=0; ri<dft.
voiNr; ri++)
for(fi=0; fi<dft.
frameNr; fi++)
271 fprintf(stderr,
"Memory allocation error.\n");
275 for(fi=0; fi<xnr; fi++) dft.
x[fi]=numbers[fi];
279 fprintf(stderr,
"Memory allocation error.\n");
284 for(ri=1; ri<fit.
voiNr; ri++) {
288 c=(b-a)/(
double)(rnr-1);
for(fi=0; fi<rnr; fi++) dft.
x[fi]=a+c*(
double)fi;
290 }
else if(aiendtime>=0.0) {
293 for(ri=1; ri<fit.
voiNr; ri++) {
298 if(aiendtime<=0.0 || aiendtime<=a) aiendtime=b;
300 c=0.0; d=0.02; rnr=1;
301 while(c+d<aiendtime && rnr<9999) {c+=d; d*=1.05; rnr++;}
302 d=aiendtime-c; c=aiendtime; rnr++;
305 fprintf(stderr,
"Memory allocation error.\n");
311 c=0.0; d=0.02; fi=0; dft.
x[fi++]=c;
312 while(c+d<aiendtime && rnr<9999) {c+=d; d*=1.05; dft.
x[fi++]=c;}
313 d=aiendtime-c; c=aiendtime; dft.
x[fi++]=c; dft.
frameNr=fi;
316 fprintf(stderr,
"Error: invalid arguments.\n");
322 for(ri=0; ri<dft.
voiNr; ri++) {
334 printf(
"X values:\n");
336 printf(
" %03d: %8.4f - %8.4f mid %8.4f\n", fi+1, dft.
x1[fi], dft.
x2[fi], dft.
x[fi]);
337 else for(fi=0; fi<dft.
frameNr; fi++)
338 printf(
" %03d: mid %8.4f\n", fi+1, dft.
x[fi]);
348 for(ri=0; ri<fit.
voiNr && !ret; ri++) {
351 }
else if(useFrames) {
352 for(ri=0; ri<fit.
voiNr && !ret; ri++) {
358 for(ri=0; ri<fit.
voiNr && !ret; ri++) {
363 fprintf(stderr,
"Error: cannot evaluate function.\n");
370 sprintf(tmp,
"# Ta := %g\n# Ti := %g\n", fit.
voi[0].
p[0], fit.
voi[0].
p[1]);
381 if(!strcasecmp(cptr,
".tac") || !strcasecmp(cptr,
".bld"))
385 if(strlen(datafile)) ret=
dftWrite(&dft, datafile);
386 else {
dftWrite(&dft,
"stdout"); ret=0;}
388 fprintf(stderr,
"Error in writing output: %s\n",
dfterrmsg);
391 if(verbose>0) fprintf(stdout,
" %s written.\n", datafile);
double atof_dpi(char *str)
int dftAddmem(DFT *dft, int voiNr)
int dftSetmem(DFT *data, int frameNr, int voiNr)
int dftRead(char *filename, DFT *data)
int dftWrite(DFT *data, char *filename)
char * filenameGetExtension(char *s)
Get the last extension of a filename.
int rand_range(int nr, double *d, double low, double up, int type)
unsigned int drandSeed(short int seed)
Make and optionally set the seed for rand(), drand, drandRange, and drandGaussian().
Header file for libtpccurveio.
int fitEvaltac(FitVOI *r, double *x, double *y, int dataNr)
int fitRead(char *filename, FIT *fit, int verbose)
int fitIntegralEvaltac(FitVOI *r, double *x, double *yi, int dataNr)
int fitFunctionformat(int type, char *str)
#define DFT_TIME_STARTEND
int fitEvaltacframes(FitVOI *r, double *x1, double *x2, double *y, int dataNr)
int fitFunctionname(int type, char *str)
Header file for libtpcmisc.
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
#define MAX_REGIONNAME_LEN
size_t strlcpy(char *dst, const char *src, size_t dstsize)
int tpcHtmlUsage(const char *program, char *text[], const char *path)
int studynr_from_fname(char *fname, char *studynr)
void tpcPrintBuild(const char *program, FILE *fp)
#define MAX_REGIONSUBNAME_LEN
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Header file for libtpcmodel.
char studynr[MAX_STUDYNR_LEN+1]
char comments[_DFT_COMMENT_LEN+1]
char unit[MAX_UNITS_LEN+1]
char unit[MAX_UNITS_LEN+1]
char place[MAX_REGIONSUBNAME_LEN+1]
char hemisphere[MAX_REGIONSUBNAME_LEN+1]
char voiname[MAX_REGIONSUBNAME_LEN+1]
char name[MAX_REGIONNAME_LEN+1]
char voiname[MAX_REGIONSUBNAME_LEN+1]
char name[MAX_REGIONNAME_LEN+1]
char hemisphere[MAX_REGIONSUBNAME_LEN+1]
char place[MAX_REGIONSUBNAME_LEN+1]