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

Functions for setting weight factors based on SIF. More...

#include "libtpcimgio.h"

Go to the source code of this file.

Functions

void sifWeight (SIF *data, double halflife)
 Calculate weights for frames in SIF data based on true counts. Weights are normalized to have an average of 1.0.
void sifWeightByFrames (SIF *data, double halflife)
 Calculate weights for frames in SIF data based on frame lengths. Weights are normalized to have an average of 1.0.
void sifWeightNorm (SIF *d)
void sifModerateTrues (SIF *sif, double limit)
void sifModerateWeights (SIF *sif, double limit)
int sifExistentCounts (SIF *sif)

Detailed Description

Functions for setting weight factors based on SIF.

Author
Vesa Oikonen

Definition in file weight.c.

Function Documentation

◆ sifExistentCounts()

int sifExistentCounts ( SIF * sif)

Verify that SIF contains prompts and randoms.

Returns
Returns 0 if neither prompts or randoms can be found, 1 or 2 if prompts or randoms can be found, and 3 if both prompts and randoms are there.
See also
sifInit, sifRead, sifWeight
Parameters
sifPointer to SIF struct

Definition at line 207 of file weight.c.

210 {
211 int fi, p=0, r=0;
212 double v1, v2;
213 if(sif==NULL || sif->frameNr<1) return 0;
214 /* If just one frame, then value > 0 is fine */
215 if(sif->frameNr==1) {
216 if(sif->prompts[0]>0.00000001) p=1;
217 if(sif->randoms[0]>0.00000001) r=2;
218 return(p+r);
219 }
220 /* Otherwise, check also that frames have different count level */
221 for(fi=1; fi<sif->frameNr; fi++) {
222 v1=sif->prompts[fi]-sif->prompts[fi-1]; if(fabs(v1)>0.001) p=1;
223 v2=sif->randoms[fi]-sif->randoms[fi-1]; if(fabs(v2)>0.001) r=2;
224 if((p+r)>2) break;
225 }
226 return(p+r);
227}
double * prompts
int frameNr
double * randoms

◆ sifModerateTrues()

void sifModerateTrues ( SIF * sif,
double limit )

Moderate the trues in SIF.

True values in SIF are used to calculate weight factors for time frames. If trues are very low in certain frames, the weight factors in other frames may become very low. This function finds the maximum trues, and adds max/limit to each trues value, if min trues < max trues / limit. Negative trues are always eliminated.

See also
sifWeight, sifModerateWeights, sifRead, sifWrite, sifWeightNorm
Parameters
sifPointer to SIF in which the trues are moderated
limitMax trues / limit is added to all trues values; 100.0 might be good

Definition at line 131 of file weight.c.

136 {
137 if(SIF_TEST) printf("sifModerateTrues(*sif, %g)\n", limit);
138
139 if(sif==NULL || sif->frameNr<2) return;
140 if(limit<=1.0) return;
141
142 int fi;
143 double w, f;
144 for(w=f=sif->trues[0], fi=1; fi<sif->frameNr; fi++) {
145 if(sif->trues[fi]>w) w=sif->trues[fi];
146 else if(sif->trues[fi]<f) f=sif->trues[fi];
147 }
148 if(f*limit<w) {
149 for(w/=limit, fi=0; fi<sif->frameNr; fi++)
150 if(sif->trues[fi]>0.0) sif->trues[fi]+=w; else sif->trues[fi]=w;
151 } else {
152 for(fi=0; fi<sif->frameNr; fi++)
153 if(sif->trues[fi]<0.0) sif->trues[fi]=0.0;
154 }
155}
int SIF_TEST
Definition sif.c:6
double * trues

Referenced by imgSetWeights().

◆ sifModerateWeights()

void sifModerateWeights ( SIF * sif,
double limit )

Moderate the weights in SIF.

This function finds the maximum weight, and adds max/limit to each weight value (except if weight is 0), if min weight < max/limit. Negative weights are set to zero.

See also
sifWeight, sifModerateTrues, sifRead, sifWrite, sifWeightNorm
Parameters
sifPointer to SIF in which the weights are moderated
limitMax weight / limit is added to all weights; 100.0 might be good

Definition at line 167 of file weight.c.

172 {
173 if(SIF_TEST) printf("sifModerateWeights(*sif, %g)\n", limit);
174
175 if(sif==NULL || sif->frameNr<2) return;
176 if(limit<=1.0) return;
177
178 int fi;
179 double w, f;
180 w=f=nan("");
181 for(fi=0; fi<sif->frameNr; fi++) {
182 if(isnan(sif->weights[fi])) continue;
183 if(sif->weights[fi]<=0.0) {sif->weights[fi]=0.0; continue;}
184 if(isnan(w)) w=sif->weights[fi];
185 if(isnan(f)) f=sif->weights[fi];
186 if(sif->weights[fi]>w) w=sif->weights[fi];
187 if(sif->weights[fi]<f) f=sif->weights[fi];
188 }
189 if(isnan(w) || isnan(f)) return;
190
191 if(f*limit<w) {
192 for(w/=limit, fi=0; fi<sif->frameNr; fi++)
193 if(sif->weights[fi]>0.0) sif->weights[fi]+=w;
194 }
195}
double * weights

◆ sifWeight()

void sifWeight ( SIF * data,
double halflife )

Calculate weights for frames in SIF data based on true counts. Weights are normalized to have an average of 1.0.

Weights are calculated from formula weight=(frame duration)^2 / (trues in a frame). Before calling this routine, trues must be calculated as total counts - randoms. Counts in SIF are not corrected for physical decay. Therefore, isotope halflife must be known, if weights are to be calculated for decay corrected TACs. Isotope halflife must be set to 0, if weights are used for TACs that are not corrected for decay.

Reference: Mazoyer BM, Huesman RH, Budinger TF, Knittel BL. Dynamic PET data analysis. J Comput Assist Tomogr 1986; 10:645-653.

See also
sifInit, sifRead, sifWrite, sifEmpty, sifModerateTrues, sifModerateWeights, sifExistentCounts, sifWeightByFrames, dftWeightByFreq, dftDecayCorrection
Parameters
dataPointer to SIF data.
halflifeHalflife (in seconds) of the isotope; If halflife is 0, the weights are calculated for non-decay corrected data. If halflife is >0, the weights are calculated using decay corrected trues, but trues data in SIF are not changed.

Definition at line 28 of file weight.c.

36 {
37 int i;
38 double f, d;
39
40 if(SIF_TEST) printf("sifWeight(*sif, %g)\n", halflife);
41 /* Calculate weights */
42 for(i=0; i<data->frameNr; i++) {
43 if(data->trues[i]<1.0) data->trues[i]=1.0;
44 f=data->x2[i]-data->x1[i]; if(f<=0.0) f=1.0;
45 if(halflife<=1.0E-8)
46 d=1.0;
47 else
48 d=exp( ((data->x1[i]+data->x2[i])/2.0)*0.693147/halflife );
49 data->weights[i]=(f*f)/(d*data->trues[i]);
50 /*printf("%3d %g %g\n", i, data->trues[i], data->weights[i]);*/
51 }
52
53 /* Scale weights so that average weight is 1.0 */
54 sifWeightNorm(data);
55
56 return;
57}
double * x1
double * x2
void sifWeightNorm(SIF *d)
Definition weight.c:105

Referenced by imgSetWeights().

◆ sifWeightByFrames()

void sifWeightByFrames ( SIF * data,
double halflife )

Calculate weights for frames in SIF data based on frame lengths. Weights are normalized to have an average of 1.0.

Weights are calculated from the simple formula weight=(frame duration), if isotope halflife is set to 0, and using formula weight=(frame duration)*exp(-t*ln(2)/halflife), if isotope halflife is given (less weight for late frames, which may be more suitable for PET data that is corrected for physical decay).

See also
sifInit, sifRead, sifWrite, sifEmpty, sifWeight, dftWeightByFreq
Parameters
dataPointer to SIF data.
halflifeHalflife (in seconds) of the isotope; If halflife is >0, the late frames are given less weight.

Definition at line 72 of file weight.c.

78 {
79 int i;
80 double f, d;
81
82 if(SIF_TEST) printf("sifWeightByFrames(*sif, %g)\n", halflife);
83 /* Calculate weights */
84 for(i=0; i<data->frameNr; i++) {
85 f=data->x2[i]-data->x1[i]; if(f<=0.0) f=1.0;
86 if(halflife<=1.0E-8)
87 d=1.0;
88 else
89 d=exp( -((data->x1[i]+data->x2[i])/2.0)*0.693147/halflife );
90 data->weights[i]=f*d;
91 }
92
93 /* Scale weights so that average weight is 1.0 */
94 sifWeightNorm(data);
95
96 return;
97}

◆ sifWeightNorm()

void sifWeightNorm ( SIF * d)

Scale weights in SIF data so that average weight is 1.0, and the sum of weights equals the nr of frames.

See also
sifInit, sifRead, sifWrite, sifEmpty, sifWeight, sifWeightByFrames
Parameters
dPointer to SIF data.

Definition at line 105 of file weight.c.

108 {
109 if(SIF_TEST) printf("sifWeightNorm(*sif)\n");
110 int i;
111 double f=0.0;
112 for(i=0; i<d->frameNr; i++) f+=d->weights[i];
113 f/=(double)d->frameNr;
114 for(i=0; i<d->frameNr; i++) d->weights[i]/=f;
115 return;
116}

Referenced by sifWeight(), and sifWeightByFrames().