95 Bimage* pftmask,
double hi_res,
double aligned_res,
double sampling_ratio)
97 if ( sampling_ratio <= 0 ) sampling_ratio = 1;
105 double rad_scale = realsize.x()/sampling_ratio;
106 if ( rad_scale < realsize.y()/sampling_ratio ) rad_scale = realsize.y()/sampling_ratio;
107 if ( rad_scale < realsize.z()/sampling_ratio ) rad_scale = realsize.z()/sampling_ratio;
111 if ( hi_res > realsize.x()/2.0 ) hi_res = realsize.x()/2.0;
115 long maxrad = (long) (2 + rad_scale/hi_res);
116 long alnrad = ( aligned_res > hi_res )? (
long) (rad_scale/aligned_res): maxrad;
119 Bstring title(
"Resolution"), txt;
124 cerr <<
"Error: File " << p->
file_name() <<
" must be a real space map!" << endl;
129 cerr <<
"Error: File " << pmod->
file_name() <<
" must be a real space map!" << endl;
142 if ( pc == NULL )
return plot;
154 cout <<
"DEBUG img_resolution: FFT done" << endl;
157 float* fom = pc->
FOM();
159 long i, j, c, n, x, y, z, xx, yy, zz, ix, iy, iz, iradius, iradius2, use_this=1;
160 double radius, fraction, fraction2, I1, I2, Idif, Isum;
161 double dphi, dphi2, cos_dphi, F1F2re, Id2;
166 cout <<
"Determining resolution from images " << p->
file_name() <<
" and " << pmod->
file_name() << endl;
167 cout <<
"Resolution limit: " << hi_res <<
" A (" << maxrad <<
")" << endl;
168 if ( aligned_res > hi_res )
169 cout <<
"Aligned resolution: " << aligned_res <<
" A (" << alnrad <<
")" << endl;
170 cout <<
"Sampling ratio: " << sampling_ratio << endl << endl;
173 double* num =
new double[maxrad];
174 double* F1 =
new double[maxrad];
175 double* F2 =
new double[maxrad];
176 double* F1F2 =
new double[maxrad];
177 double* Fsum =
new double[maxrad];
178 double* Fsumphi =
new double[maxrad];
179 double* SSNR =
new double[maxrad];
180 double* RAB =
new double[maxrad];
182 for ( n=c=0; n<pc->
images(); n++, c+=ncol ) {
218 for ( i=0; i<maxrad; i++ ) {
219 num[i] = F1[i] = F2[i] = F1F2[i] = Fsum[i] = Fsumphi[i] = 0;
220 SSNR[i] = RAB[i] = 0;
222 for ( z=0; z<pc->
sizeZ(); z++ ) {
224 if ( z > (pc->
sizeZ() - 1)/2 ) zz -= pc->
sizeZ();
225 rz = zz*freq_scale[2];
227 if ( iz < 0 ) iz += pc->
sizeZ();
228 for ( y=0; y<pc->
sizeY(); y++ ) {
230 if ( y > (pc->
sizeY() - 1)/2 ) yy -= pc->
sizeY();
231 ry = yy*freq_scale[1];
233 if ( iy < 0 ) iy += pc->
sizeY();
234 for ( x=0; x<pc->
sizeX()/2; x++ ) {
238 if ( x > (pc->
sizeX() - 1)/2 ) xx -= pc->
sizeX();
239 rx = xx*freq_scale[0];
241 if ( ix < 0 ) ix += pc->
sizeX();
242 radius = rad_scale*sqrt(rx*rx + ry*ry + rz*rz);
243 iradius = (int) radius;
244 iradius2 = iradius + 1;
245 if ( iradius2 >= maxrad ) use_this = 0;
246 if ( pftmask && (*pftmask)[i] < 1 ) use_this = 0;
248 fraction = radius - iradius;
249 fraction2 = 1.0 - fraction;
255 Idif = (image -
model).power();
256 dphi = image.
phi() - model.
phi();
257 if ( !isfinite(dphi) ) {
258 cerr << x <<
" " << y <<
" " << z <<
": dphi not finite: ";
259 cerr <<
"image = " << image.
real() <<
" + " << image.
imag()
260 <<
"i model = " << model.
real() <<
" + " << model.
imag() <<
"i" << endl;
265 num[iradius] += fraction2;
266 num[iradius2] += fraction;
267 F1[iradius] += fraction2*I1;
268 F1[iradius2] += fraction*I1;
269 F2[iradius] += fraction2*I2;
270 F2[iradius2] += fraction*I2;
274 cerr << x <<
" " << y <<
" " << z <<
": I1 not finite" << endl;
276 cerr << x <<
" " << y <<
" " << z <<
": I2 not finite" << endl;
278 F1F2[iradius] += fraction2*F1F2re;
279 F1F2[iradius2] += fraction*F1F2re;
281 Fsum[iradius] += fraction2*Isum;
282 Fsum[iradius2] += fraction*Isum;
284 Fsumphi[iradius] += fraction2*Id2;
285 Fsumphi[iradius2] += fraction*Id2;
287 SSNR[iradius] += fraction2*Idif;
288 SSNR[iradius2] += fraction*Idif;
290 RAB[iradius] += fraction2*Idif;
291 RAB[iradius2] += fraction*Idif;
294 cos_dphi = F1F2re/(I1*I2);
298 fom[i] = (1 - Idif/Isum)*cos_dphi;
300 fom[i] = (1 - Idif/Isum)*cos_dphi*
301 (1 - exp(invsigma2*Isum*Isum));
303 if ( fom[i] < 0 ) fom[i] = 0;
314 SSNR[0] = 0.09*F2[0];
315 double FSC, DPR, FSCsum(0), nsum(0), naln(0), nval(0), FSCaln(0), FSCval(0);
316 for ( i=0, j=c*maxrad; i<maxrad; i++, j++ ) {
318 if ( i == 0 ) FSC = 1;
319 else if ( F1[i]*F2[i] >
SMALLFLOAT ) FSC = F1F2[i]/sqrt(F1[i]*F2[i]);
321 FSCaln += FSC*num[i];
324 FSCval += FSC*num[i];
328 if ( FSC > 1 ) FSC = 1;
329 FSCsum += FSC*num[i];
331 if ( Fsum[i] >
SMALLFLOAT ) DPR = sqrt(Fsumphi[i]/Fsum[i]);
334 if ( F1[i]*F2[i] >
SMALLFLOAT ) SSNR[i] = F1F2[i]/(sqrt(F1[i]*F2[i]) - F1F2[i]);
336 if ( Fsum[i] >
SMALLFLOAT ) RAB[i] /= Fsum[i];
338 (*plot)[j] = i/rad_scale;
339 (*plot)[maxrad+j] = FSC;
340 (*plot)[2*maxrad+j] = DPR*180.0/
M_PI;
341 (*plot)[3*maxrad+j] = SSNR[i];
342 (*plot)[4*maxrad+j] = RAB[i];
344 FSCval /= 2*naln - FSCaln;
345 if ( FSCval < 0 ) FSCval = 0;
347 cout <<
"Validation ratio: " << FSCval << endl;
349 txt =
Bstring(pow(4.0*
M_PI/(3.0*FSCsum), 1.0/3.0),
"Information volume radius: %g A");
351 if ( aligned_res > hi_res ) {
352 txt =
Bstring(FSCval,
"Validation ratio: %g");
358 cout <<
"DEBUG img_resolution: page number = " << plot->
page(0).
number() <<
" columns = " << plot->
page(0).
columns() << endl;
372 cout <<
"DEBUG img_resolution: Maximum FOM = " << pc->
FOM_maximum() << endl;
395 long i, j, n, c, ncol(5), nrow(plot->
rows()), nimg(plot->
pages());
397 double *s, *FSC, *DPR, *SSNR, *RAB;
401 if ( cutoff[1] < 5 ) cutoff[1] *= 180.0/
M_PI;
404 double* res_est =
new double[nimg*4];
405 for ( n=0; n<nimg*4; n++ ) res_est[n] = 0;
407 for ( n=c=0; n<nimg; n++, c+=ncol ) {
415 cout <<
"Resolution cutoff levels: " << cutoff[0] <<
" "
416 << cutoff[1] <<
" " << cutoff[2] <<
" " << cutoff[3] << endl << endl;
418 cout <<
"Resolution measures for image " << n << endl;
420 cout <<
"Resolution curves:" << endl;
421 cout <<
"s(1/A)\tRes(A)\t" << label <<
"\tDPR\tSSNR\tRA+B" << endl;
424 for ( i=0, j=c*nrow; i<nrow; i++, j++ ) {
427 if ( FSC[i-1] >= cutoff[0] && FSC[i] < cutoff[0] )
428 res_est[n*4+0] = res + (res_1 - res)*(cutoff[0] - FSC[i])/(FSC[i-1] - FSC[i]);
429 if ( DPR[i-1] <= cutoff[1] && DPR[i] > cutoff[1] )
430 res_est[n*4+1] = res + (res_1 - res)*(cutoff[1] - DPR[i])/(DPR[i-1] - DPR[i]);
431 if ( SSNR[i-1] >= cutoff[2] && SSNR[i] < cutoff[2] )
432 res_est[n*4+2] = res + (res_1 - res)*(cutoff[2] - SSNR[i])/(SSNR[i-1] - SSNR[i]);
433 if ( RAB[i-1] <= cutoff[3] && RAB[i] > cutoff[3])
434 res_est[n*4+3] = res + (res_1 - res)*(cutoff[3] - RAB[i])/(RAB[i-1] - RAB[i]);
438 cout << showpoint << fixed << setprecision(5) << setw(7) << s[i] <<
tab
439 << setprecision(3) << setw(7) << res <<
tab
440 << setw(7) << FSC[i] <<
tab
441 << setw(7) << DPR[i] <<
tab
442 << setw(7) << SSNR[i] <<
tab
443 << setw(7) << RAB[i] << endl;
445 txt = label +
Bstring(cutoff[0],
"(%g): ") +
Bstring(res_est[n*4],
"%g A");
447 txt = Bstring(cutoff[1],
"DPR(%g): ") + Bstring(res_est[n*4+1],
"%g A");
450 if (
verbose & VERB_PROCESS ) {
451 cout <<
"Resolution estimates:\t" << n <<
tab
452 << setprecision(4) << res_est[n*4] <<
tab << res_est[n*4+1] <<
tab
453 << res_est[n*4+2] <<
tab << res_est[n*4+3] << endl << endl;
454 cout << *(plot->
page(n).
text()) << endl;
478 long i, j, k, n, c, ncol(2), nrow(plot->
rows()), nimg(plot->
pages());
485 double cutoff[4] = {0.143,0.3,0.5,0.9};
486 double* res_est =
new double[nimg*4];
487 for ( n=0; n<nimg*4; n++ ) res_est[n] = 0;
489 for ( n=c=0; n<nimg; n++, c+=ncol ) {
494 cout <<
"Resolution cutoff levels: " << cutoff[0] <<
" "
495 << cutoff[1] <<
" " << cutoff[2] <<
" " << cutoff[3] << endl << endl;
497 cout <<
"Resolution measures for image " << n << endl;
499 cout <<
"Resolution curves:" << endl;
500 cout <<
"s(1/A)\tRes(A)\t" << label << endl;
503 for ( i=0, j=c*nrow; i<nrow; i++, j++ ) {
506 for ( k=0; k<4; k++ )
507 if ( FSC[i-1] >= cutoff[k] && FSC[i] < cutoff[k] )
508 res_est[n*4+k] = res + (res_1 - res)*(cutoff[k] - FSC[i])/(FSC[i-1] - FSC[i]);
512 cout << showpoint << fixed << setprecision(5) << setw(7) << s[i] <<
tab
513 << setprecision(3) << setw(7) << res <<
tab
514 << setw(7) << FSC[i] << endl;
516 for ( k=0; k<4; k++ ) {
517 txt = label +
Bstring(cutoff[k],
"(%g): ") +
Bstring(res_est[n*4+k],
"%g A");
521 if (
verbose & VERB_PROCESS ) {
522 cout <<
"Resolution estimates:\t" << n <<
tab
523 << setprecision(2) << res_est[n*4] <<
tab << res_est[n*4+1] <<
tab
524 << res_est[n*4+2] <<
tab << res_est[n*4+3] << endl << endl;
525 cout << *(plot->
page(n).
text()) << endl;
553 Bstring& real_mask_file,
Bstring& ft_mask_file,
double resolution,
554 double aligned_res,
Vector3<float> sampling,
double sampling_ratio,
557 if ( resolution <= 0 ) resolution = 0.1;
567 error_show(
"img_resolution_from_two_images", __FILE__, __LINE__);
573 error_show(
"img_resolution_from_two_images", __FILE__, __LINE__);
577 unsigned long nimg = p->
images();
582 double* res_est = NULL;
585 cout <<
"Img\tFSC\tDPR\tSSNR\tR+AB" << endl;
594 if ( real_mask_file.
length() ) {
595 prealmask =
read_img(real_mask_file, 1, -1);
597 error_show(
"img_resolution_from_two_images", __FILE__, __LINE__);
601 if ( ft_mask_file.
length() ) {
602 pftmask =
read_img(ft_mask_file, 1, -1);
604 error_show(
"img_resolution_from_two_images", __FILE__, __LINE__);
608 if ( sampling.
x() > 0 ) {
615 plot =
img_resolution(p, pmap, prealmask, pftmask, resolution, aligned_res, sampling_ratio);
621 for ( i=0; i<nimg; i++ ) {
623 for ( j=0; j<4; j++ ) cout <<
tab << res_est[i*4+j];
635 if ( real_mask_file.
length() ) {
636 prealmask =
read_img(real_mask_file, 1, 0);
638 error_show(
"img_resolution_from_two_images", __FILE__, __LINE__);
642 if ( ft_mask_file.
length() ) {
643 pftmask =
read_img(ft_mask_file, 1, 0);
645 error_show(
"img_resolution_from_two_images", __FILE__, __LINE__);
649 for ( i=0; i<nimg; i++ ) {
651 if ( sampling.
x() > 0 ) {
658 plot =
img_resolution(p, pmap, prealmask, pftmask, resolution, aligned_res, sampling_ratio);
664 for ( j=0; j<4; j++ ) cout <<
tab << res_est[j];
671 }
else if ( p->
sizeZ() == 1 ) {
672 if ( proj_file.
length() ) {
681 if ( real_mask_file.
length() ) {
682 prealmask =
read_img(real_mask_file, 1, -1);
684 error_show(
"img_resolution_from_two_images", __FILE__, __LINE__);
688 if ( ft_mask_file.
length() ) {
689 pftmask =
read_img(ft_mask_file, 1, -1);
691 error_show(
"img_resolution_from_two_images", __FILE__, __LINE__);
695 for ( i=0; i<nimg; i++ ) {
697 if ( sampling.
x() > 0 ) {
712 plot =
img_resolution(p, pmap, prealmask, pftmask, resolution, aligned_res, sampling_ratio);
718 for ( j=0; j<4; j++ ) cout <<
tab << res_est[j];
722 if ( pf ) pf->
replace(i, proj);
729 if ( prealmask )
delete prealmask;
730 if ( pftmask )
delete pftmask;
void change_type(char letter)
Get the data type indicated by a single letter code.
int replace(Bimage *img)
Replaces the data with that from the given image.
void view(double x, double y, double z, double a)
Header file for image class.
Bplot * img_resolution(Bimage *p, Bimage *pmod, Bimage *prealmask, Bimage *pftmask, double hi_res, double aligned_res, double sampling_ratio)
Calculates resolution measures.
Header file for general utilities.
int ps_plot(Bstring &filename, Bplot *plot)
Generates postscript graphs.
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
CompoundType compound_type()
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
Library routines to read and write FSC curves.
void write_tsv(Bstring &filename)
double average(size_t cc, double xf, double yf, double zf, size_t nn, double iscale)
Averages in the xy plane when scaling is less than 1.
size_t statistics()
Calculates the statistics for an image.
double * img_resolution_display(Bplot *plot, float *cutoff)
Calculates the resolution estimates from a plot generated by img_resolution.
double FOM_find_maximum()
Calculates the maximum in the FOM block.
Bimage * rotate_project(Matrix3 mat, Vector3< double > translate, double radial_cutoff)
Rotates a 3D map and projects it along the z-axis.
General image parameter class.
Vector3< double > real_size()
int rescale_to_avg_std(double nuavg=0, double nustd=1)
Rescales the image data to a given average and standard deviation.
float * FOM_initialize()
Allocates and initializes the FOM block.
Bimage * read_img(const char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Complex< double > complex(size_t j)
Returns a complex value at the given index.
void multiply(size_t j, Complex< double > cv)
void file_name(Bstring s)
Header file for trigonometry functions.
void origin(double x, double y, double z)
void set(size_t j, double v)
Sets a single value at the given index.
double standard_deviation()
int write_img(const char *filename, Bimage *p)
General driver function to write multiple image formats This is the only image writing function that ...
void add_text(Bstring &s)
Header file for postscript output functions.
int img_resolution_from_two_images(Bstring &image_file, Bstring &ref_file, Bstring &real_mask_file, Bstring &ft_mask_file, double resolution, double aligned_res, Vector3< float > sampling, double sampling_ratio, float *cutoff, Bstring &ps_file, Bstring &xml_file, Bstring &tsv_file, Bstring &proj_file)
Calculates the resolution from two images.
Library routines to estimate resolution.
Bimage * pack_two_in_complex(Bimage *p)
Packs two real images into one complex image.
FourierType fourier_type()
ostream & tab(ostream &out)
void xml_write_fsc(Bstring &filename, Bplot *plot)