Bsoft  1.9
Bernard's software package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_stat_test.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "math_util.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
37 Bimage* img_students_t_test(int n, Bimage* p, float* weight)
38 {
39  unsigned long i;
40 
41  Bimage* pt = new Bimage(Float, TSimple, p->size(), 1);
42  pt->origin(p->image->origin());
43  pt->sampling(p->sampling());
44 
45  float* var = (float *) p->FOM();
46 
47  float* siglev = pt->FOM_initialize();
48 
49  unsigned long datasize = p->data_size();
50  double ws = 0, d;
51 
52  for ( i=0; i<n; i++ ) ws += weight[i];
53 
54  double dof = ws*(1 - 1.0L/n);
55  double hdof = 0.5*dof;
56  double avg_var = 0;
57 
58  for ( i=0; i<datasize; i++ ) avg_var += var[i];
59  avg_var /= datasize;
60 
61  if ( verbose ) {
62  cout << "Student's t test:" << endl;
63  cout << "Number: " << n << endl;
64  cout << "Average variance: " << avg_var << endl;
65  cout << "Degrees of freedom: " << dof << endl << endl;
66  }
67 
68  avg_var *= avg_var;
69 
70  for ( i=0; i<datasize; i++ ) {
71  d = var[i]/n;
72  if ( d < avg_var ) d = avg_var;
73  d = sqrt(d);
74  pt->set(i, (*p)[i]/d);
75  siglev[i] = betai(hdof, 0.5, dof/(dof + (*pt)[i]*(*pt)[i]));
76  }
77 
78  return pt;
79 }
80 
106 Bimage* img_students_t_test_equal(int set1, int set2, Bimage* p1, Bimage* p2, float* weight)
107 {
108  unsigned long i, j;
109 
110  // Set arrays and image parameters
111  Bimage* pt = new Bimage(Float, TSimple, p1->size(), 1);
112  pt->origin(p1->image->origin());
113  pt->sampling(p1->sampling());
114 
115  unsigned long datasize = p1->sizeX()*p1->sizeY()*p1->sizeZ()*p1->images();
116 
117  float* var1 = p1->FOM(); // pointers to variance data
118  float* var2 = p2->FOM();
119  float* sig = pt->FOM_initialize();
120 
121  // Compute Student's t-test
122  if ( verbose ) {
123  cout << endl << "Student's t-test, assuming same variance in sets 1 and 2" << endl;
124  cout << "Set 1 contains " << set1 << " images" << endl;
125  cout << "Set 2 contains " << set2 << " images" << endl;
126  }
127 
128  double ws1, ws2;
129  for ( i=0, j=0, ws1=0; i<set1; i++, j++ ) ws1 += weight[j];
130  for ( i=0, ws2=0; i<set2; i++, j++ ) ws2 += weight[j];
131 
132  double inv_weight_sum = (1.0L / ws1) + (1.0L / ws2);
133 
134  double dof1 = ws1*(1.0L - 1.0L/set1); // degrees of freedom
135  double dof2 = ws2*(1.0L - 1.0L/set2);
136  double dof = dof1 + dof2;
137  double hdof = 0.5*dof;
138  double SD;
139  double Vn1, Vn2;
140 
141  long zero_count1 = 0, zero_count2 = 0; // Counts numbers of zero values in t-test
142  for ( i=0; i<datasize; i++ ) {
143  Vn1 = var1[i] * dof1; // Compute numerator of variance from the variance
144  Vn2 = var2[i] * dof2;
145  SD = sqrt( ( (Vn1 + Vn2) / dof ) * inv_weight_sum ); // Standard error of the difference of the means
146  if ( SD > 0 ) { // test for zero in denominator
147  pt->set(i, ( (*p1)[i] - (*p2)[i] ) / SD); // t-test value
148  if ( (*pt)[i] == 0 ) zero_count2++;
149  sig[i] = betai(hdof, 0.5L, dof/(dof + (*pt)[i]*(*pt)[i]) ); // significance level
150  } else {
151  pt->set(i, 0);
152  sig[i] = 1;
153  zero_count1++;
154  }
155  }
156 
157  if ( verbose & VERB_FULL ) {
158  cout << zero_count1 << " pixels had a zero value for the standard error of the differences of the means, and t was set to zero." << endl;
159  cout << zero_count2 << " pixels had standard deviation > 0 and t = 0." << endl;
160  cout << datasize << " = total pixels in image" << endl;
161  }
162 
163  return pt;
164 }
165 
166 
167 
189 Bimage* img_students_t_test_unequal(int set1, int set2, Bimage* p1, Bimage* p2, float* weight)
190 {
191  unsigned long i, j;
192 
193  // Set arrays and image parameters
194  Bimage* pt = new Bimage(Float, TSimple, p1->sizeX(), p1->sizeY(), p1->sizeZ(), 1);
195  pt->origin(p1->image->origin());
196  pt->sampling(p1->sampling());
197 
198  unsigned long datasize = p1->sizeX()*p1->sizeY()*p1->sizeZ()*p1->images();
199 
200  float* var1 = p1->FOM(); // pointers to variance data
201  float* var2 = p2->FOM();
202  float* sig = pt->FOM_initialize(); // initialize figure-of-merit subimage structure, for significance level
203 
204  // Compute Student's t-test
205  if ( verbose ) {
206  cout << endl << "Student's t-test, assuming unequal variance in sets 1 and 2" << endl;
207  cout << "Set 1 contains " << set1 << " images" << endl;
208  cout << "Set 2 contains " << set2 << " images" << endl;
209  }
210 
211  double ws1, ws2;
212  for ( i=0, j=0, ws1=0; i<set1; i++, j++ ) ws1 += weight[j];
213  for ( i=0, ws2=0; i<set2; i++, j++ ) ws2 += weight[j];
214 
215  double dof1 = ws1*(1.0L - 1.0L/set1); // degrees of freedom
216  double dof2 = ws2*(1.0L - 1.0L/set2);
217  double dof;
218  double Vn1, Vn2;
219 
220  long zero_count1 = 0, zero_count2 = 0; // Counts numbers of zero values in t-test
221  for ( i=0; i<datasize; i++ ) {
222  if ( var1[i] > 0 || var2[i] > 0 ) { // test for zero in denominator
223  Vn1 = var1[i]/ws1;
224  Vn2 = var2[i]/ws2;
225  pt->set(i, ( (*p1)[i] - (*p2)[i] ) / sqrt(Vn1 + Vn2));
226  if ( (*pt)[i] == 0 ) zero_count2++;
227  dof = (Vn1 + Vn2)*(Vn1 + Vn2) / (Vn1*Vn1/dof1 + Vn2*Vn2/dof2);
228  sig[i] = betai(0.5L*dof, 0.5L, dof/(dof + (*pt)[i]*(*pt)[i]) ); // significance level
229  } else {
230  pt->set(i, 0);
231  sig[i] = 1;
232  zero_count1++;
233  }
234  }
235 
236  if ( verbose & VERB_FULL ) {
237  cout << zero_count1 << " pixels had zero variance and t was set to zero." << endl;
238  cout << zero_count2 << " pixels had variance not equal to 0 and t = 0." << endl;
239  cout << datasize << " = total pixels in image" << endl;
240  }
241 
242  return pt;
243 }
244 
264 Bimage* img_f_test(int set1, int set2, Bimage* p1, Bimage* p2, float* weight)
265 {
266  unsigned long i, j;
267 
268  // Set image parameters and arrays
269  Bimage* pf = new Bimage(Float, TSimple, p1->size(), 1);
270  pf->origin(p1->image->origin());
271  pf->sampling(p1->sampling());
272 
273  unsigned long datasize = p1->sizeX()*p1->sizeY()*p1->sizeZ()*p1->images();
274 
275  float* var1 = (float *) p1->FOM(); // pointers to variance data
276  float* var2 = (float *) p2->FOM();
277  float* sig = pf->FOM_initialize();
278 
279  // Compute F-test
280  if ( verbose ) {
281  cout << endl << "F-test" << endl;
282  cout << "Set 1 contains " << set1 << " images" << endl;
283  cout << "Set 2 contains " << set2 << " images" << endl;
284  }
285 
286  double ws1, ws2;
287  for ( i=0, j=0, ws1=0; i<set1; i++, j++ ) ws1 += weight[j];
288  for ( i=0, ws2=0; i<set2; i++, j++ ) ws2 += weight[j];
289 
290  double dof1 = ws1*(1.0L - 1.0L/set1);
291  double dof2 = ws2*(1.0L - 1.0L/set2);
292  double df1, df2; // degrees of freedom, sets 1 and 2
293 
294  for ( i=0; i<datasize; i++ ) {
295  if ( var1[i] < SMALLFLOAT || var2[i] < SMALLFLOAT ) {
296  pf->set(i, 0);
297  sig[i] = 1;
298  j++;
299  } else {
300  if ( var1[i] > var2[i] ) { // make f the ratio of the larger variance to the smaller one
301  pf->set(i, var1[i] / var2[i]);
302  df1 = dof1;
303  df2 = dof2;
304  } else {
305  pf->set(i, var2[i] / var1[i]);
306  df1 = dof2;
307  df2 = dof1;
308  }
309  sig[i] = 2*betai(0.5L*df2, 0.5L*df1, df2/(df2 + df1*(*pf)[i]) ); // significance level
310  if ( sig[i] > 1 ) sig[i] = 2 - sig[i];
311  }
312  }
313  if ( verbose & VERB_FULL ) {
314  cout << j << " pixels in set 1 or set 2 had zero variance, and the F-values were set to zero." << endl;
315  cout << datasize << " = total pixels in image" << endl;
316  }
317 
318  return pf;
319 }
320 
void sizeZ(size_t nz)
Definition: Bimage.h:332
Header file for image class.
#define SMALLFLOAT
Definition: utilities.h:43
Header file for general utilities.
Bimage * img_students_t_test(int n, Bimage *p, float *weight)
Applies the Student's t-test to one set of images.
Vector3< size_t > size() const
Definition: Bimage.h:327
Definition: Bimage.h:51
General image parameter class.
Definition: Bimage.h:152
float * FOM_initialize()
Allocates and initializes the FOM block.
Definition: Bimage.cpp:677
void sampling(double s)
Definition: Bimage.h:440
Bimage * img_f_test(int set1, int set2, Bimage *p1, Bimage *p2, float *weight)
Applies the F-test to two sets of images.
float * FOM()
Definition: Bimage.h:239
double betai(double a, double b, double x)
Calculates the incomplete beta function Ix(a,b).
Definition: math_util.cpp:339
void origin(double x, double y, double z)
Definition: Bimage.h:132
void set(size_t j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:977
size_t images()
Definition: Bimage.h:438
Bimage * img_students_t_test_equal(int set1, int set2, Bimage *p1, Bimage *p2, float *weight)
Applies the Student's t-test to two sets of images, assumes the two distributions have approximately ...
size_t data_size()
Definition: Bimage.h:225
void sizeX(size_t nx)
Definition: Bimage.h:330
void sizeY(size_t ny)
Definition: Bimage.h:331
#define VERB_FULL
Definition: utilities.h:103
int verbose
Bsub_image * image
Definition: Bimage.h:183
void origin(double ox, double oy, double oz)
Definition: Bimage.h:483
Header file for general utilities.
Bimage * img_students_t_test_unequal(int set1, int set2, Bimage *p1, Bimage *p2, float *weight)
Applies the Student's t-test to two sets of images, assumes the two distributions have unequal varian...