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

Weights for PET data modelling. More...

#include "libtpcmodext.h"

Go to the source code of this file.

Functions

int dftWeightByFreq (DFT *dft)
 
int imgSetWeights (IMG *img, int wmet, int verbose)
 
int dftWSampleNr (DFT *tac)
 

Detailed Description

Weights for PET data modelling.

Author
Vesa Oikonen

Definition in file weight_model.c.

Function Documentation

◆ dftWeightByFreq()

int dftWeightByFreq ( DFT * dft)

Add weights based on sample frequency or frame length.

Existing weights are overwritten.

Returns
Returns 0 when successful, otherwise <>0.
See also
dftRead, dftSortByFrame, sifWeight, sifWeightByFrames, dftDecayCorrection, imgSetWeights, dftWSampleNr
Parameters
dftSamples/frames must be sorted by sample time, but duplicate samples are allowed

Definition at line 20 of file weight_model.c.

24 {
25 int fi, fi1, fi2;
26 double f, t, t1, t2, sumw=0.0;
27
28 if(dft==NULL || dft->frameNr<1) return 1;
29 if(dft->frameNr==1) {
30 dft->w[0]=1.0;
31 dft->isweight=1;
32 return 0;
33 }
34
35 if(dft->timetype==DFT_TIME_STARTEND) { /* weights by frame lengths */
36
37 for(fi=0; fi<dft->frameNr; fi++) {
38 dft->w[fi]=dft->x2[fi]-dft->x1[fi];
39 sumw+=dft->w[fi];
40 }
41 /* scale weights so that sum of weights equals sample number */
42 sumw/=(double)dft->frameNr;
43 for(fi=0; fi<dft->frameNr; fi++) dft->w[fi]/=sumw;
44
45 } else if(dft->timetype==DFT_TIME_MIDDLE) { /* weights by sample distance */
46
47 for(fi=0; fi<dft->frameNr; fi++) {
48 t=t1=t2=dft->x[fi]; //printf("fi=%d t=%g\n", fi, t);
49 /* Find the closest sample time before this one */
50 for(fi1=fi; fi1>=0; fi1--) {t1=dft->x[fi1]; if(t1<t) break;}
51 /* Find the closest sample time after this one */
52 for(fi2=fi; fi2<dft->frameNr; fi2++) {t2=dft->x[fi2]; if(t2>t) break;}
53 //printf(" t1=%g t2=%g\n", t1, t2);
54 /* Mean sample distance */
55 f=0.0;
56 if(t1<t) f+=t-t1; else f+=t2-t;
57 if(t2>t) f+=t2-t; else f+=t-t1;
58 f*=0.5; if(f<=0.0) f=1.0;
59 /* Set initial weight */
60 //printf(" f=%g\n", f);
61 dft->w[fi]=f; sumw+=f;
62 }
63 /* Scale weights */
64 sumw/=(double)dft->frameNr;
65 for(fi=0; fi<dft->frameNr; fi++) dft->w[fi]/=sumw;
66
67 } else
68 return 2;
69
70 dft->isweight=1;
71 return(0);
72}
#define DFT_TIME_MIDDLE
#define DFT_TIME_STARTEND
int timetype
double * w
double * x1
double * x2
int frameNr
int isweight
double * x

◆ dftWSampleNr()

int dftWSampleNr ( DFT * tac)

Get the number of samples in DFT that have weight > 0. Missing (NaN) sample values are included as long as weight is not missing. If weights are not set, then nr of all samples is returned.

Returns
The number of samples with weight above zero.
See also
dftWeightByFreq, aicSS, parFreeNr
Parameters
tacPointer to the DFT struct.

Definition at line 147 of file weight_model.c.

150 {
151 if(tac==NULL || tac->voiNr<1 || tac->frameNr<1) return(0);
152 if(tac->isweight==0) return(tac->frameNr);
153 int n=0;
154 for(int i=0; i<tac->frameNr; i++) if(tac->w[i]>0.0) n++;
155 return(n);
156}
int voiNr

◆ imgSetWeights()

int imgSetWeights ( IMG * img,
int wmet,
int verbose )

Add weights based on average voxel values and frame lengths, just frame lengths, or set all weights to 1 (no weighting).

Existing weights are overwritten. Decay correction is not considered in calculation of weights.

Returns
Returns 0 when successful, otherwise <>0.
See also
dftWeightByFreq, sifWeight, sifWeightByFrames
Parameters
imgPointer to IMG struct; image data is used to calculate relational frame weights, which are also written into IMG struct.
wmetWeighting method: 0=based on mean voxel values times frame lengths; 1=based on frame lengths, 2=set weights to 1 (no weighting).
verboseVerbose level; if zero, then only warnings are printed into stderr

Definition at line 85 of file weight_model.c.

94 {
95 if(verbose>0) printf("imgSetWeights(*img, %d, ...)\n", wmet);
96 /* Check the function input */
97 if(img==NULL) return(1);
98 if(img->status!=IMG_STATUS_OCCUPIED) return(2);
99 if(img->dimt<1 || img->dimx<1 || img->dimy<1 || img->dimz<1) return(3);
100 if(wmet<0 || wmet>2) return(4);
101 img->isWeight=0;
102
103 /* If one time frame, then weight is 1 */
104 if(img->dimt==1) {
105 img->weight[0]=1.0;
106 if(wmet==0 || wmet==1) img->isWeight=1;
107 return(0);
108 }
109
110 int fi, ret;
111 double f, fm;
112
113 if(wmet==2) { /* If weights are to be set to 1 (removed) */
114 for(fi=0; fi<img->dimt; fi++) img->weight[fi]=1.0;
115 return(0);
116 }
117
118 if(wmet==0) { /* weight based on frame mean values and durations */
119 SIF sif;
120 sifInit(&sif);
121 ret=sifAllocateWithIMG(&sif, img, 1, verbose); if(ret) return(100+ret);
122 sifModerateTrues(&sif, 100.0);
123 sifWeight(&sif, 0.0); if(verbose>2) sifPrint(&sif);
124 for(fi=0; fi<img->dimt; fi++) img->weight[fi]=sif.weights[fi];
125 sifEmpty(&sif);
126 } else if(wmet==1) { /* weight based on frame durations */
127 for(fi=0, fm=0.0; fi<img->dimt; fi++) {
128 f=img->end[fi]-img->start[fi]; fm+=f;
129 img->weight[fi]=f;
130 }
131 /* scale weights so that sum of weights equals sample number */
132 if(fm>0.0) f=(double)img->dimt/fm; else f=1.0;
133 for(fi=0; fi<img->dimt; fi++) img->weight[fi]*=f;
134 }
135 img->isWeight=1;
136 return(0);
137}
void sifModerateTrues(SIF *sif, double limit)
Definition weight.c:131
#define IMG_STATUS_OCCUPIED
void sifInit(SIF *data)
Definition sif.c:17
void sifPrint(SIF *data)
Definition sifio.c:234
void sifWeight(SIF *data, double halflife)
Calculate weights for frames in SIF data based on true counts. Weights are normalized to have an aver...
Definition weight.c:28
void sifEmpty(SIF *data)
Definition sif.c:33
int sifAllocateWithIMG(SIF *sif, IMG *img, int doCounts, int verbose)
Definition misc_model.c:418
unsigned short int dimx
char status
unsigned short int dimt
float * weight
float * start
unsigned short int dimz
unsigned short int dimy
float * end
char isWeight
double * weights