5#include "tpcclibConfig.h"
42 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
43 if(verbose>0) printf(
"%s(tac, %g, %d)\n", __func__, dt, ti);
53 if(fabs(dt)<1.0E-12) {
54 if(verbose>1) printf(
" requested delay time is zero; nothing done.\n");
60 if(verbose>1) printf(
" data contains NaN(s).\n");
66 double xmin=nan(
""), xmax=nan(
"");
67 if(
tacXRange(tac, &xmin, &xmax)!=0 || dt>=xmax || dt<=-xmax) {
71 if(verbose>2) printf(
" xrange: %g - %g\n", xmin, xmax);
78 for(
int i=0; i<tac->
sampleNr; i++) tx[i]=tac->
x[i]+dt;
80 for(
int j=0; j<tac->
tacNr; j++)
if(ti<0 || ti==j) {
83 for(
int i=0; i<tac->
sampleNr; i++) ty[i]=tac->
c[j].
y[i];
85 if(
liInterpolate(tx, ty, tac->
sampleNr, tac->
x, tac->
c[j].
y, NULL, NULL, tac->
sampleNr, 0, 1,
104 statusSet(status, __func__, __FILE__, __LINE__, ret);
109 for(
int i=0; i<stac.
sampleNr; i++) stac.
x[i]+=dt;
111 for(
int j=0; j<tac->
tacNr; j++)
if(ti<0 || ti==j) {
113 NULL, NULL, tac->
sampleNr, 0, 1, verbose-10))
220 int verbose=0;
if(status!=NULL) verbose=status->
verbose;
222 printf(
"%s(inp, tis, %d, %g, %g, %g, %g, %d, ...)\n", __func__, ci, dtmin, dtmax, fitend, dtstep, mode);
229 if(ttac==NULL || (btac==NULL && tdata==NULL))
return(
TPCERROR_FAIL);
233 if(dt!=NULL) *dt=nan(
"");
240 if(b->
moveNr==0) printf(
" given temp data will be filled\n");
241 else printf(
" filled temp data will be used\n");
246 if(verbose>4) {printf(
" local temp data created\n"); fflush(stdout);}
250 if(btac!=NULL || b->
moveNr==0) {
252 if(!(dtmax>dtmin) || !(dtstep>0.001)) {
259 if(verbose>3) printf(
" moveNr := %d\n", b->
moveNr);
269 if(verbose>3) printf(
" TTAC range := %g - %g\n BTAC range := %g - %g\n",
286 if(verbose>3) printf(
"fitend=%g -> %g\n", fitend, b->
fitend);
287 b->
iNr=0;
for(
unsigned int i=0; i<(
unsigned int)btac->
sampleNr; i++)
if(btac->
x[i]<=b->
fitend) b->
iNr++;
else break;
288 b->
tNr=0;
for(
unsigned int i=0; i<(
unsigned int)ttac->
sampleNr; i++)
if(ttac->
x[i]<=b->
fitend) b->
tNr++;
else break;
290 if(b->
iNr<5 || b->
tNr<5) {
297 if(verbose>5) {printf(
" preparing delayed input data\n"); fflush(stdout);}
304 statusSet(status, __func__, __FILE__, __LINE__, ret);
318 statusSet(status, __func__, __FILE__, __LINE__, ret);
339 statusSet(status, __func__, __FILE__, __LINE__, ret);
343 if(btac->
isframe==0) dxp=dx;
else dxp=dx2;
344 for(
int di=0; di<b->
dtac.
tacNr-1; di++) {
345 for(
int i=0; i<btac->
sampleNr; i++) {
380 printf(
"integrated input curve 1:\n");
388 if(verbose>5) {printf(
" preparing data for LLSQ\n"); fflush(stdout);}
409 if(verbose>5) {printf(
" preparing tissue data\n"); fflush(stdout);}
428 printf(
"integrated tissue curves:\n");
437 if(verbose>5) {printf(
" LLSQ\n"); fflush(stdout);}
438 double ss, ssmin=nan(
"");
440 for(
int di=0; di<b->
dtac.
tacNr-1; di++) {
442 for(
int mi=0; mi<b->
llsq_m; mi++) {
469 for(
int mi=0; mi<b->
llsq_m; mi++) {
470 if(!isfinite(b->
llsq_b[mi])) failnr++;
471 if(!isfinite(b->
llsq_mat[mi])) failnr++;
477 if(failnr>0) printf(
"%d bad values\n", failnr);
479 if(verbose>9 && b->
llsq_m<100) {
480 printf(
"---- B x A ----\n");
481 for(
int mi=0; mi<b->
llsq_m; mi++) {
482 printf(
" %g ", b->
llsq_b[mi]);
483 for(
int ni=0; ni<b->
llsq_n; ni++)
490 if(ret<2 && isfinite(ss)) {
491 if(!isfinite(ssmin) || ssmin>ss) {ssmin=ss; mini=di;}
492 if(verbose>2) printf(
" %d %d %g %g\n", di, mini, ss, ssmin);
494 if(verbose>1) printf(
" failed NNLS\n");
498 if(verbose>1) fprintf(stderr,
"Error: all fits failed.\n");
502 if(dt!=NULL && mini>=0) *dt=b->
dtmin+mini*b->
dtstep;
int tacDelayFit(TAC *btac, TAC *ttac, int ci, double dtmin, double dtmax, double fitend, double dtstep, double *dt, int mode, int model, DELAYFITDATA *tdata, TPCSTATUS *status)
Fit time delay between PET tissue and plasma or blood curve.
void initDelayFitData(DELAYFITDATA *d)
Before first use, initiate the data structure for delay time estimation.
void freeDelayFitData(DELAYFITDATA *d)
After last use, free memory in the data structure for delay time estimation.
int tacDelay(TAC *tac, double dt, int ti, TPCSTATUS *status)
Move TAC y values (concentrations) in time, keeping sample times (x values) intact.
int liDerivate3(double *x, double *y, const int nr, double *d, double *dd, const int verbose)
Simplistic derivation of PET TAC using regression line over three points.
int liIntegrateFE(double *x1, double *x2, double *y, int nr, double *ie, double *iie, const int verbose)
Linear integration of PET TAC to frame end times.
int liIntegrate(double *x, double *y, const int nr, double *yi, const int se, const int verbose)
Linear integration of TAC with trapezoidal method.
int liDerivate(double *x, double *y, const int nr, double *d, double *dd, const int verbose)
Simplistic derivation of TAC as Δy divided by Δx, in relation to the previous point.
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.
int liInterpolateForPET(double *x, double *y, const int nr, double *newx1, double *newx2, double *newy, double *newyi, double *newyii, const int newnr, const int se, const int ee, const int verbose)
Linear TAC interpolation and/or integration to PET frames.
int nnls(double **a, int m, int n, double *b, double *x, double *rnorm, double *wp, double *zzp, int *indexp)
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
int verbose
Verbose level, used by statusPrint() etc.
int tacDuplicate(TAC *tac1, TAC *tac2)
Make a duplicate of TAC structure.
int tacAllocate(TAC *tac, int sampleNr, int tacNr)
int tacYNaNs(TAC *tac, const int i)
int tacFramesToSteps(TAC *inp, TAC *out, TPCSTATUS *status)
Transform TAC with frames into TAC with frames represented with stepwise changing dot-to-dot data.
int tacXRange(TAC *d, double *xmin, double *xmax)
Get the range of x values (times) in TAC structure.
Header file for library libtpcextensions.
@ TPCERROR_INVALID_XRANGE
Invalid sample time range.
@ TPCERROR_INVALID_VALUE
Invalid value.
@ TPCERROR_BAD_FIT
Fitting not successful.
@ TPCERROR_FAIL
General error.
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.
@ TPCERROR_INVALID_X
Invalid sample time.
@ TPCERROR_NO_DATA
File contains no data.
@ TPCERROR_MISSING_DATA
File contains missing values.
Header file for libtpcli.
Header file for libtpclinopt.
Header file for library libtpctac.
Header file for libtpctacmod.