31 "Usage: dimgstats [options] input.images",
32 "---------------------------------------",
33 "Calculates statistical measures within a set of images or between sets of images.",
34 "Corresponding pixels are compared--i.e. pixel m in image 1 is compared to pixel m",
38 " Variance var = Sum[ (rho - avg_rho)(rho - avg_rho) ] / (n - 1)",
39 " numerator portion Vn = Sum[ (rho - avg_rho)(rho - avg_rho) ]",
40 " Standard deviation std = sqrt( var )",
41 " Student's t-test (t) SD = sqrt[(Vn1 + Vn2)(1/m + 1/n) / (m + n - 2) ]",
42 " approx. equal var t = ( avg1 - avg2 ) / SD",
43 " unequal var t = ( avg1 - avg2 ) / sqrt[ var1/m + var2/n ]",
44 " F-test F = var1 / var2",
45 "avg = average (mean), m = number of data points in set 1, n = number in set 2,",
46 "rho = value of a given pixel at a given (x,y,z) coordinate",
47 "SD = standard error of the difference of the means, sqrt = square root",
48 "Sum = summation of.",
50 "See Milligan and Flicker (1987) J. Cell Biol. 105:29-39 for a reference",
51 "to the use of the Student's t-test in 3D electron microscopy.",
53 "Any number of input images may be given and may include the wild card \"*\".",
54 "(However, VMS does not support Unix style usage of wild cards)",
55 "All images are converted to floating point.",
58 "-Ttest equal Do a Student's t-test to compare two sets (equal/unequal).",
59 "-Ftest Do a F-test to compare two sets.",
60 "-average Compute an average image for each set.",
61 "-stdev Compute a standard deviation image for each set.",
62 "-variance Compute a variance image for each set.",
63 "-significance Compute significance-level (probability) image.",
64 "-rescale -0.1,5.2 Rescale input images to average, standard deviation.",
67 "-datatype u Force writing of a new data type",
68 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
69 "-sets 10,12 Number of input files in each set (default = single set).",
70 "-verbose 7 Verbosity of output",
71 "-weights 0.1,0.3,0.9 Weights for input files (default 1 for every file)",
74 "-output name.img base name for output image(s), suffix added based on action",
81 int main(
int argc,
char **argv)
98 int sttflag[2] = {0,0};
101 double nuavg(0), nustd(0);
102 vector<double> weight, weight2;
105 Bimage *p1 = NULL, *p2 = NULL;
114 for ( curropt = option; curropt; curropt = curropt->
next ) {
115 if ( curropt->
tag ==
"Ttest" ) {
117 if ( curropt->
value[0] ==
'e' ) sttflag[0] = 1;
118 if ( curropt->
value[0] ==
'u' ) sttflag[1] = 1;
120 if ( curropt->
tag ==
"Ftest" ) {
121 fflag = 1; avgvar = 1;
123 if ( curropt->
tag ==
"average" ) {
124 avgflag = 1; avgvar = 1;
126 if ( curropt->
tag ==
"stdev" ) {
127 stdev = 1; avgvar = 1;
129 if ( curropt->
tag ==
"variance" ) {
130 variance = 1; avgvar = 1;
132 if ( curropt->
tag ==
"significance" )
134 if ( curropt->
tag ==
"rescale" )
135 if ( curropt->
values(nuavg, nustd) < 2 )
136 cerr <<
"-rescale: Both average and standard deviation must be specified" << endl;
137 if ( curropt->
tag ==
"datatype" )
139 if ( curropt->
tag ==
"sampling" )
140 sam = curropt->
scale();
141 if ( curropt->
tag ==
"sets" ) {
142 if ( curropt->
values(set1, set2) < 2 )
143 cerr <<
"-sets: Two integers must be specified" << endl;
144 else if ( set1<=0 || set2<=0 )
145 cerr <<
"-sets: Each set must have at least one member." << endl;
147 if ( curropt->
tag ==
"weights" ) {
148 weight_string = curropt->
value;
149 if ( weight_string.
length() < 1 )
150 cerr <<
"-weights: Weights for all files must be specified" << endl;
154 if ( curropt->
tag ==
"output" )
161 if ( optind >= argc ) {
162 cerr <<
"No input files!" << endl;
165 if ( (sttflag[0] || sttflag[1] || fflag) && (set1 == 0) ) {
166 cerr <<
"You must designate two image sets (option \"-sets\") for the t-test or F-test!" << endl;
173 while ( optind < argc ) {
174 if ( !set1 || ( set1 && nfiles < set1 ) )
181 if ( set1 < 1 && set2 < 1 )
184 if ( (set1+set2) != nfiles ) {
185 cerr <<
"The number of files (" << nfiles <<
") does not agree with the numbers designated for sets 1 ("
186 << set1 <<
") and 2 (" << set2 <<
")." << endl;
194 if ( weight.size() < nfiles ) {
195 cerr <<
"Error: The number of weights must be equal to the number of input files" << endl;
199 for ( i=0; i<nfiles; i++ ) weight.push_back(1.0);
203 for ( i=set1; i<nfiles; i++ ) weight2.push_back(weight[i]);
211 if ( filebase.
length() ) {
217 if ( p2 ) p2->change_type(nudatatype);
250 if ( filebase.
length() ) {
256 outfile = filebase.
pre_rev(
'.') +
"_ftest_sig." + filebase.
post_rev(
'.');
266 for ( i=0; i<2; i++ )
if ( sttflag[i] ) {
271 if ( filebase.
length() ) {
272 if ( i == 0 ) outfile = filebase.
pre_rev(
'.') +
"_stte." + filebase.
post_rev(
'.');
273 if ( i == 1 ) outfile = filebase.
pre_rev(
'.') +
"_sttu." + filebase.
post_rev(
'.');
278 if ( i == 0 ) outfile = filebase.
pre_rev(
'.') +
"_stte_sig." + filebase.
post_rev(
'.');
279 if ( i == 1 ) outfile = filebase.
pre_rev(
'.') +
"_sttu_sig." + filebase.
post_rev(
'.');
290 if ( filebase.
length() ) {
325 cerr <<
"Error: No FOM block found in " << p->
file_name() << endl;
332 for ( i=0; i<datasize; i++ ) {
334 cerr <<
"Cannot take the square root of the FOM of this image because a value is less than zero." << endl;
338 fom[i] = sqrt(fom[i]);
379 for ( i=0; i<n; i++ ) ws += weight[i];
381 double dof = ws*(1 - 1.0L/n);
382 double hdof = 0.5*dof;
385 for ( i=0; i<datasize; i++ ) avg_var += var[i];
389 cout <<
"Student's t test:" << endl;
390 cout <<
"Number: " << n << endl;
391 cout <<
"Average variance: " << avg_var << endl;
392 cout <<
"Degrees of freedom: " << dof << endl << endl;
397 for ( i=0; i<datasize; i++ ) {
399 if ( d < avg_var ) d = avg_var;
401 pt->
set(i, (*p)[i]/d);
402 siglev[i] =
betai(hdof, 0.5, dof/(dof + (*pt)[i]*(*pt)[i]));
449 cout << endl <<
"Student's t-test, assuming same variance in sets 1 and 2" << endl;
450 cout <<
"Set 1 contains " << set1 <<
" images" << endl;
451 cout <<
"Set 2 contains " << set2 <<
" images" << endl;
455 for ( i=0, j=0, ws1=0; i<set1; i++, j++ ) ws1 += weight[j];
456 for ( i=0, ws2=0; i<set2; i++, j++ ) ws2 += weight[j];
458 double inv_weight_sum = (1.0L / ws1) + (1.0L / ws2);
460 double dof1 = ws1*(1.0L - 1.0L/set1);
461 double dof2 = ws2*(1.0L - 1.0L/set2);
462 double dof = dof1 + dof2;
463 double hdof = 0.5*dof;
467 long zero_count1 = 0, zero_count2 = 0;
468 for ( i=0; i<datasize; i++ ) {
469 Vn1 = var1[i] * dof1;
470 Vn2 = var2[i] * dof2;
471 SD = sqrt( ( (Vn1 + Vn2) / dof ) * inv_weight_sum );
473 pt->
set(i, ( (*p1)[i] - (*p2)[i] ) / SD);
474 if ( (*pt)[i] == 0 ) zero_count2++;
475 sig[i] =
betai(hdof, 0.5L, dof/(dof + (*pt)[i]*(*pt)[i]) );
484 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;
485 cout << zero_count2 <<
" pixels had standard deviation > 0 and t = 0." << endl;
486 cout << datasize <<
" = total pixels in image" << endl;
531 cout << endl <<
"Student's t-test, assuming unequal variance in sets 1 and 2" << endl;
532 cout <<
"Set 1 contains " << set1 <<
" images" << endl;
533 cout <<
"Set 2 contains " << set2 <<
" images" << endl;
537 for ( i=0, j=0, ws1=0; i<set1; i++, j++ ) ws1 += weight[j];
538 for ( i=0, ws2=0; i<set2; i++, j++ ) ws2 += weight[j];
540 double dof1 = ws1*(1.0L - 1.0L/set1);
541 double dof2 = ws2*(1.0L - 1.0L/set2);
545 long zero_count1 = 0, zero_count2 = 0;
546 for ( i=0; i<datasize; i++ ) {
547 if ( var1[i] > 0 || var2[i] > 0 ) {
550 pt->
set(i, ( (*p1)[i] - (*p2)[i] ) / sqrt(Vn1 + Vn2));
551 if ( (*pt)[i] == 0 ) zero_count2++;
552 dof = (Vn1 + Vn2)*(Vn1 + Vn2) / (Vn1*Vn1/dof1 + Vn2*Vn2/dof2);
553 sig[i] =
betai(0.5L*dof, 0.5L, dof/(dof + (*pt)[i]*(*pt)[i]) );
562 cout << zero_count1 <<
" pixels had zero variance and t was set to zero." << endl;
563 cout << zero_count2 <<
" pixels had variance not equal to 0 and t = 0." << endl;
564 cout << datasize <<
" = total pixels in image" << endl;
605 cout << endl <<
"F-test" << endl;
606 cout <<
"Set 1 contains " << set1 <<
" images" << endl;
607 cout <<
"Set 2 contains " << set2 <<
" images" << endl;
611 for ( i=0, j=0, ws1=0; i<set1; i++, j++ ) ws1 += weight[j];
612 for ( i=0, ws2=0; i<set2; i++, j++ ) ws2 += weight[j];
614 double dof1 = ws1*(1.0L - 1.0L/set1);
615 double dof2 = ws2*(1.0L - 1.0L/set2);
618 for ( i=0; i<datasize; i++ ) {
624 if ( var1[i] > var2[i] ) {
625 pf->
set(i, var1[i] / var2[i]);
629 pf->
set(i, var2[i] / var1[i]);
633 sig[i] = 2*
betai(0.5L*df2, 0.5L*df1, df2/(df2 + df1*(*pf)[i]) );
634 if ( sig[i] > 1 ) sig[i] = 2 - sig[i];
638 cout << j <<
" pixels in set 1 or set 2 had zero variance, and the F-values were set to zero." << endl;
639 cout << datasize <<
" = total pixels in image" << endl;