TPCCLIB
Loading...
Searching...
No Matches
fittime.c File Reference

Check and set fit duration from data. More...

#include "libtpcmodext.h"

Go to the source code of this file.

Functions

int fittime_from_dft (DFT *dft, double *startTime, double *endTime, int *first, int *last, int verbose)
 
int fittime_from_img (IMG *img, double *fittime, int verbose)
 
int check_times_dft_vs_img (IMG *img, DFT *dft, int verbose)
 
int check_times_dft_vs_dft (DFT *dft1, DFT *dft2, int verbose)
 
int copy_times_from_img_to_dft (IMG *img, DFT *dft, int verbose)
 
int getActualSamplenr (DFT *dft, int ri)
 
double dftEndtime (DFT *dft)
 
double imgEndtime (IMG *img)
 
int dftMatchTimeunits (DFT *dft1, DFT *dft2, int *tunit2, int verbose)
 

Detailed Description

Check and set fit duration from data.

Author
Vesa Oikonen

Definition in file fittime.c.

Function Documentation

◆ check_times_dft_vs_dft()

int check_times_dft_vs_dft ( DFT * dft1,
DFT * dft2,
int verbose )

Check whether sample times are the same (or very close to) in two DFT structs. Data sets are not edited. If DFT structs contain different sample number, then only common nr of samples are compared.

See also
check_times_dft_vs_img, copy_times_from_img_to_dft
Returns
Returns 0 in case of no match, 1 if times do match, and <1 in case of an error.
Parameters
dft1Pointer to first DFT data; times can be both seconds or minutes, if unit is correctly set.
dft2Pointer to second DFT data; times can be both seconds or minutes, if unit is correctly set; sample nr may be different than in dft1.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 169 of file fittime.c.

177 {
178 int fi, n, smaller_frameNr=0;
179 double f, ts1, ts2;
180 double accepted_timedif=2.2; // sec
181
182 if(verbose>0) printf("%s(*img, *dft)\n", __func__);
183 if(dft1==NULL || dft2==NULL) return -1;
184 if(dft1->frameNr<=0 || dft2->frameNr<=0) return 0;
185
186 /* Which has less samples? */
187 if(dft1->frameNr<dft2->frameNr) smaller_frameNr=dft1->frameNr;
188 else smaller_frameNr=dft2->frameNr;
189
190 /* Convert times to sec if necessary (and possible) */
191 ts1=ts2=1.0;
192 if(dft1->timeunit!=TUNIT_UNKNOWN && dft1->timeunit!=TUNIT_UNKNOWN) {
193 if(dft1->timeunit==TUNIT_MIN) ts1=60.0;
194 if(dft2->timeunit==TUNIT_MIN) ts2=60.0;
195 }
196 if(verbose>1) {
197 printf("dft1->timetype := %d\n", dft1->timetype);
198 printf("dft2->timetype := %d\n", dft2->timetype);
199 if(verbose>2) {
200 printf("time range 1 := %g - %g %s\n", dft1->x[0],
201 dft1->x[dft1->frameNr-1], petTunit(dft1->timeunit));
202 printf("time range 2 := %g - %g %s\n", dft2->x[0],
203 dft2->x[dft2->frameNr-1], petTunit(dft2->timeunit));
204 }
205 }
206
207 /* With short study the accepted time difference must be shorter */
208 f=0.01*dft1->x2[dft1->frameNr-1]*ts1;
209 if(accepted_timedif>f) accepted_timedif=f;
210 if(verbose>1) printf("accepted_timedif := %g [s]\n", accepted_timedif);
211
212 /* Compare sample times frame-by-frame */
213 for(fi=0, n=0; fi<smaller_frameNr; fi++) {
214 if(dft1->timetype==DFT_TIME_MIDDLE) { // check frame mid times
215 f=fabs(dft1->x[fi]*ts1-dft2->x[fi]*ts2);
216 if(verbose>10) printf("timedif[%d] := %g\n", fi, f);
217 if(verbose>12) printf(" %g vs %g\n", ts1*dft1->x[fi], ts2*dft2->x[fi]);
218 if(f>accepted_timedif) n++;
219 continue;
220 }
222 f=fabs(dft1->x1[fi]*ts1-dft2->x1[fi]*ts2);
223 if(verbose>10) printf("timedif[%d] := %g\n", fi, f);
224 if(verbose>12) printf(" %g vs %g\n", ts1*dft1->x1[fi], ts2*dft2->x1[fi]);
225 if(f>accepted_timedif) {n++; continue;}
226 }
227 if(dft1->timetype==DFT_TIME_END || dft1->timetype==DFT_TIME_STARTEND) {
228 f=fabs(dft1->x2[fi]*ts1-dft2->x2[fi]*ts2);
229 if(verbose>10) printf("timedif[%d] := %g\n", fi, f);
230 if(verbose>12) printf(" %g vs %g\n", ts1*dft1->x2[fi], ts2*dft2->x2[fi]);
231 if(f>accepted_timedif) n++;
232 }
233 }
234 if(verbose>2) printf("nr of different frame times := %d\n", n);
235
236 if(n==0) return 1; else return 0;
237}
#define DFT_TIME_MIDDLE
#define DFT_TIME_END
#define DFT_TIME_START
#define DFT_TIME_STARTEND
char * petTunit(int tunit)
Definition petunits.c:226
int timetype
int timeunit
double * x1
double * x2
int frameNr
double * x

Referenced by dftInterpolate(), dftInterpolateForIMG(), and dftInterpolateInto().

◆ check_times_dft_vs_img()

int check_times_dft_vs_img ( IMG * img,
DFT * dft,
int verbose )

Check whether DFT sample times are the same (or very close to) as the frame times in IMG. This would suggest that DFT data originates from the same or similar PET scan. Specified data sets are not edited. If frame nr is different, then only the common frames are compared.

See also
check_times_dft_vs_dft, copy_times_from_img_to_dft, dftEndtime, imgEndtime
Returns
Returns 0 in case of no match, 1 if times do match, and <1 in case of an error.
Parameters
imgPointer to IMG data; times must be in sec as usual.
dftPointer to DFT data; times can be both seconds or minutes, if unit is correctly set; sample nr may be different than in IMG.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 109 of file fittime.c.

117 {
118 int smaller_dimt=0;
119 double f, ts;
120 double accepted_timedif=2.2; // sec
121
122 if(verbose>0) printf("%s(*img, *dft)\n", __func__);
123 if(img==NULL || dft==NULL) return -1;
124 if(img->dimt<=0 || dft->frameNr<=0) return 0;
125
126 /* Get the smaller frame nr */
127 if(img->dimt<dft->frameNr) smaller_dimt=img->dimt; else smaller_dimt=dft->frameNr;
128
129 /* With short study the accepted time difference must be shorter */
130 f=0.01*img->end[img->dimt-1]; if(accepted_timedif>f) accepted_timedif=f;
131 if(verbose>1) printf("accepted_timedif := %g [s]\n", accepted_timedif);
132
133 /* Convert DFT times to sec if necessary */
134 if(dft->timeunit==TUNIT_MIN) ts=60.0; else ts=1.0;
135
136 /* Compare sample times frame-by-frame */
137 int n=0;
138 for(int fi=0; fi<smaller_dimt; fi++) {
139 if(dft->timetype==DFT_TIME_MIDDLE) { // check frame mid times
140 f=fabs(img->mid[fi]-dft->x[fi]*ts);
141 if(verbose>10) printf("timedif[%d] := %g\n", fi, f);
142 if(f>accepted_timedif) n++;
143 continue;
144 }
146 f=fabs(img->start[fi]-dft->x1[fi]*ts);
147 if(verbose>10) printf("timedif[%d] := %g\n", fi, f);
148 if(f>accepted_timedif) {n++; continue;}
149 }
151 f=fabs(img->end[fi]-dft->x2[fi]*ts);
152 if(verbose>10) printf("timedif[%d] := %g\n", fi, f);
153 if(f>accepted_timedif) n++;
154 }
155 }
156 if(verbose>2) printf("nr of different frame times := %d\n", n);
157
158 if(n==0) return 1; else return 0;
159}
unsigned short int dimt
float * start
float * end
float * mid

Referenced by img_logan(), and img_patlak().

◆ copy_times_from_img_to_dft()

int copy_times_from_img_to_dft ( IMG * img,
DFT * dft,
int verbose )

Copies frame times (especially start and end times, but also mid times) from an IMG data into DFT data, and sets DFT 'header' to indicate that frame start and end times are present.

See also
check_times_dft_vs_img, dftEndtime, imgEndtime, dftMatchTimeunits
Returns
Returns 0 if successful and <>0 in case of an error.
Parameters
imgPointer to IMG data; times must be in sec as usual.
dftPointer to DFT data; times can be both seconds or minutes, if unit is correctly set; sample nr may be smaller than in IMG.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 246 of file fittime.c.

254 {
255 int times_changed=0;
256
257 if(verbose>0) printf("%s(*img, *dft)\n", __func__);
258 if(img==NULL || dft==NULL) return 1;
259 if(img->dimt<=0 || dft->frameNr<=0) return 0;
260
261 /* If more DFT samples, then that is an error */
262 if(img->dimt<dft->frameNr) return 2;
263
264 /* Convert DFT times to sec if necessary */
265 if(dft->timeunit==TUNIT_MIN) {dftMin2sec(dft); times_changed=1;}
266
267 /* Copy the frame times frame-by-frame */
268 for(int fi=0; fi<dft->frameNr; fi++) {
269 dft->x1[fi]=img->start[fi];
270 dft->x2[fi]=img->end[fi];
271 dft->x[fi]=img->mid[fi];
272 }
274
275 /* Convert DFT times back to min if necessary */
276 if(times_changed!=0) dftSec2min(dft);
277
278 return 0;
279}
void dftSec2min(DFT *dft)
Definition dftunit.c:160
void dftMin2sec(DFT *dft)
Definition dftunit.c:145

Referenced by dftInterpolateForIMG(), img_logan(), and img_patlak().

◆ dftEndtime()

double dftEndtime ( DFT * dft)

Get TAC end time. Sample times are assumed to be sorted to increasing order.

See also
imgEndtime, fittime_from_dft, getActualSamplenr
Returns
Returns the TAC end time, not converting the time units.
Parameters
dftPointer to DFT TAC structure.

Definition at line 315 of file fittime.c.

318 {
319 if(dft==NULL || dft->frameNr<1) return(0.0);
320 for(int fi=dft->frameNr-1; fi>=0; fi--) {
321 if(dft->timetype==DFT_TIME_MIDDLE) {
322 if(!isnan(dft->x[fi])) return(dft->x[fi]);
323 } else if(dft->timetype==DFT_TIME_STARTEND) {
324 if(!isnan(dft->x2[fi])) return(dft->x2[fi]);
325 } else if(dft->timetype==DFT_TIME_START) {
326 if(!isnan(dft->x[fi])) return(dft->x[fi]);
327 } else if(dft->timetype==DFT_TIME_END) {
328 if(!isnan(dft->x[fi])) return(dft->x[fi]);
329 } else
330 return(0.0);
331 }
332 return(0.0);
333}

Referenced by imgReadModelingData().

◆ dftMatchTimeunits()

int dftMatchTimeunits ( DFT * dft1,
DFT * dft2,
int * tunit2,
int verbose )

Make sure that time units in two DFT structs are the same, converting units when necessary, optionally saving original units so that units can be converted back to what they were using dftTimeunitConversion().

See also
dftTimeunitConversion, dftEndtime, fittime_from_dft
Returns
Returns 0 if the was no need for time unit conversion, 1 if time units in dft2 were converted, and <0 if units were not identified or in case of an error.
Parameters
dft1Pointer to DFT struct 1.
dft2Pointer to DFT struct 2; sample time units are changed to match the data in dft1.
tunit2Pointer for original time unit in DFT 2; enter NULL if not needed.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 358 of file fittime.c.

367 {
368 if(verbose>0) printf("%s()\n", __func__);
369 if(dft1==NULL || dft2==NULL) return(-1);
370 /* Save original units if required */
371 if(tunit2!=NULL) *tunit2=dft2->timeunit;
372 /* Check that time units are available */
373 if(dft1->timeunit!=TUNIT_MIN && dft1->timeunit!=TUNIT_SEC) {
374 if(verbose>0) printf(" unknown time units in dft1\n");
375 return(-2);
376 }
377 if(dft2->timeunit!=TUNIT_MIN && dft2->timeunit!=TUNIT_SEC) {
378 if(verbose>0) printf(" unknown time units in dft2\n");
379 return(-3);
380 }
381 /* Check if they are the same */
382 if(dft1->timeunit==dft2->timeunit) {
383 if(verbose>1) printf(" time units are the same in dft1 and dft2.\n");
384 return(0);
385 }
386 /* Conversion to dft2 */
387 if(verbose>1) printf(" time units in dft2 converted from %s to %s\n",
388 petTunit(dft2->timeunit), petTunit(dft1->timeunit));
389 int ret=dftTimeunitConversion(dft2, dft1->timeunit);
390 if(ret) {
391 if(verbose>0) printf(" time unit conversion failed\n");
392 return(-10-ret);
393 }
394 return(1);
395}
int dftTimeunitConversion(DFT *dft, int tunit)
Definition dftunit.c:119

Referenced by dftInterpolateCheckEnd(), and dftInterpolateCheckStart().

◆ fittime_from_dft()

int fittime_from_dft ( DFT * dft,
double * startTime,
double * endTime,
int * first,
int * last,
int verbose )

Reset user-defined fit time range to comply with DFT data.

See also
fittime_from_img, getActualSamplenr, dftEndtime, dftReadModelingData
Returns
Returns the number of samples included in the fit range, or <0 in case of an error.
Parameters
dftPointer to DFT containing (regional tissue) data; times can be in minutes or seconds, as long as units are defined.
startTimePointer containing originally the requested fit start time (min). This is changed to contain the time of the first included frame. Unit must be minutes. Initially, set to <0 to start from the beginning of the data.
endTimePointer containing originally the requested fit end time (min). This is changed to contain the time of the last included frame. Unit must be minutes. Initially, set to <0 or to a very large value to reach to the end of data.
firstFunction writes the index of the first included sample (frame) here.
lastFunction writes the index of the last included sample (frame) here.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 16 of file fittime.c.

37 {
38 int fi, dataNr;
39
40 if(verbose>0) printf("%s(*dft, %g, %g, first, last)\n", __func__, *startTime, *endTime);
41 if(dft==NULL || startTime==NULL || endTime==NULL) return(-1);
42 if(!isfinite(*startTime)) *startTime=-1.0;
43 if(!isfinite(*endTime)) *endTime=-1.0;
44 *first=*last=0;
45 if(dft->frameNr<=0) {*startTime=*endTime=0.0; return 0;}
46 /* Change start and end times to seconds if necessary */
47 if(dft->timeunit==TUNIT_SEC) {*startTime*=60.; *endTime*=60.;}
48 /* Check that data range is not outside required range */
49 if(dft->x[dft->frameNr-1]<*startTime || dft->x[0]>*endTime) {
50 *startTime=*endTime=0.0; return 0;}
51 /* Get first and last data point */
52 for(fi=0, *first=dft->frameNr; fi<dft->frameNr; fi++)
53 if(dft->x[fi]>=*startTime) {*first=fi; break;}
54 for(*last=fi=*first; fi<dft->frameNr; fi++)
55 if(dft->x[fi]<=*endTime) *last=fi; else break;
56 if(*first>=dft->frameNr) {*startTime=*endTime=0.0; return 0;}
57 /* Correct fit range to frame start and end times */
58 *startTime=(dft->timetype==DFT_TIME_STARTEND?dft->x1[*first]:dft->x[*first]);
59 *endTime=(dft->timetype==DFT_TIME_STARTEND?dft->x2[*last]:dft->x[*last]);
60 /* Calculate the number of data points in the fit range */
61 dataNr=(*last)-(*first)+1;
62 /* Change start and end times back to minutes if necessary */
63 if(dft->timeunit==TUNIT_SEC) {*startTime/=60.; *endTime/=60.;}
64 return dataNr;
65}

Referenced by dftReadModelingData(), and imgReadModelingData().

◆ fittime_from_img()

int fittime_from_img ( IMG * img,
double * fittime,
int verbose )

Get the IMG frame end time of the last frame that is inside (mid time before) the specified maximum fittime.

See also
fittime_from_dft, imgEndtime, imgReadModelingData
Returns
Returns the number of IMG frames included in the fittime, or <0 in case of an error.
Parameters
imgPointer to IMG
fittimePointer containing originally the fit time maximum, after this the last included IMG frame end time. Unit must be seconds. Initially, set to <0 or to a very large value to include all frames.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 74 of file fittime.c.

83 {
84 if(verbose>0) printf("%s(*img, %g)\n", __func__, *fittime);
85 if(img==NULL || fittime==NULL) return(-1);
86 if(!isfinite(*fittime)) *fittime=-1.0;
87 if(img->dimt<=0) {*fittime=0.0; return 0;}
88 if(*fittime<0) {*fittime=img->end[img->dimt-1]; return img->dimt;}
89 int fitdimt=0;
90 for(int fi=0; fi<img->dimt; fi++)
91 if(img->mid[fi]>*fittime) break; else fitdimt++;
92 *fittime=img->end[fitdimt-1];
93 if(verbose>1) {
94 printf(" fitdimt := %d\n", fitdimt);
95 printf(" fittime := %g\n", *fittime);
96 }
97 return fitdimt;
98}

Referenced by imgReadModelingData().

◆ getActualSamplenr()

int getActualSamplenr ( DFT * dft,
int ri )

Returns the actual TAC sample number, not including NaNs, samples with negative x, duplicate samples, or samples with zero weights (if data is weighted).

See also
dftEndtime, fittime_from_dft
Returns
Returns sample number.
Parameters
dftPointer to TAC data in DFT struct; must be sorted by increasing x.
riRegion index [0..voiNr-1].

Definition at line 288 of file fittime.c.

293 {
294 int n, fi;
295 double last_x=-1.0E+99;
296
297 if(dft==NULL || dft->frameNr<1 || ri<0 || ri>=dft->voiNr) return 0;
298 for(fi=n=0; fi<dft->frameNr; fi++) {
299 if(isnan(dft->x[fi])) continue;
300 if(isnan(dft->voi[ri].y[fi])) continue;
301 if(dft->x[fi]<0.0) continue;
302 if(dft->isweight!=0 && dft->w[fi]<=0.0) continue;
303 if(dft->x[fi]==last_x) continue;
304 n++; last_x=dft->x[fi];
305 }
306 return n;
307}
Voi * voi
double * w
int voiNr
int isweight
double * y

◆ imgEndtime()

double imgEndtime ( IMG * img)

Get IMG end time. Frame times are assumed to be sorted to increasing order.

See also
dftEndtime, fittime_from_img
Returns
Returns the last frame end time. By default, times are in sec.
Parameters
imgPointer to IMG structure.

Definition at line 339 of file fittime.c.

342 {
343 if(img==NULL || img->dimt<1) return(0.0);
344 for(int fi=img->dimt-1; fi>=0; fi--)
345 if(!isnan(img->end[fi])) return(img->end[fi]);
346 return(0.0);
347}

Referenced by imgReadModelingData().