26 long ft_size,
int zsize,
double scale,
double hi_res,
64 double fast_angle,
int action,
double wiener)
75 for ( mg_res = field->
mg, i=0; mg_res && i < micrograph_id; mg_res = mg_res->next, i++ ) ;
78 error_show(
"Error in mg_tomo_res_reconstruct", __FILE__, __LINE__);
79 cerr <<
"Micrograph number " << micrograph_id <<
" not found!" << endl << endl;
85 cout <<
"Skipping micrograph number %d!" << micrograph_id << endl << endl;
90 error_show(
"Error in mg_tomo_res_reconstruct", __FILE__, __LINE__);
91 cerr <<
"No micrograph image file name given for micrograph " << mg_res->
id << endl;
95 if ( hi_res < 2*mg_res->pixel_size[0] )
100 if ( pad_factor < 1 && mg_res->fft.length() > 0 ) p =
read_img(mg_res->
fft, 1, mg_res->
img_num);
106 if ( size[2] < 1 ) size[2] = p->
sizeX();
108 if ( size[2] > p->
sizeX() ) size[2] = p->
sizeX();
109 if ( size[2] > p->
sizeY() ) size[2] = p->
sizeY();
112 if ( ft_size < p->sizeX() ) ft_size = p->
sizeX();
113 if ( ft_size < p->sizeY() ) ft_size = p->
sizeY();
116 cout <<
"DEBUG mg_tomo_res_reconstruct: size=" << size <<
" ft_size=" << ft_size << endl;
146 long data_size = (long) prec->
size().
volume();
150 cout <<
"NLOO resolution test:" << endl;
151 cout <<
"Micrograph: " << mg_res->
id <<
" (" << mg_res->
img_num <<
")" << endl;
154 cout <<
"Maximum angular offset: " << fast_angle*180.0/
M_PI << endl;
155 cout <<
"Fourier transform size: " << ft_size <<
" x " << ft_size << endl;
156 cout <<
"Reconstruction size: " << size << endl;
157 cout <<
"Resolution limit: " << hi_res <<
" A" << endl;
158 cout <<
"Sampling ratio: " << sampling_ratio << endl;
160 cout <<
"CTF correction: " << action <<
" (wiener=" << wiener <<
")" << endl;
169 cout <<
"Reading image " << mg->
img_num <<
" (micrograph " << mg->
id <<
")" << endl;
175 error_show(
"mg_tomo_res_reconstruct", __FILE__, __LINE__);
204 cout <<
"Complete: " << nrec*100.0/nmg <<
"%\r";
213 error_show(
"Error in mg_tomo_res_reconstruct: No micrographs used in this reconstruction!", __FILE__, __LINE__);
218 cout <<
"Weighing reconstruction. " << endl;
220 for ( i=0; i<data_size; i++ ) {
221 if ( fom[i] ) prec->
set(i, prec->
complex(i) / fom[i]);
222 if ( fom2[i] ) prec2->
set(i, prec2->
complex(i) / fom2[i]);
223 fom[i] = fom[i]/(fom[i] + 1);
224 fom2[i] = fom2[i]/(fom2[i] + 1);
228 parr.push_back(prec);
229 parr.push_back(prec2);
269 double fast_angle,
int action,
double wiener,
double cutoff,
Bstring& psfile)
275 for ( mg_res = field->
mg, i=0; mg_res && i < micrograph_id; mg_res = mg_res->next, i++ ) ;
278 error_show(
"Error in mg_tomo_resolution", __FILE__, __LINE__);
279 cerr <<
"Micrograph number " << micrograph_id <<
" not found!" << endl << endl;
284 sampling_ratio, scale, size, fast_angle, action, wiener);
288 mg_res->
fom = resolution;
290 Bstring filename(parr[0]->file_name());
295 cout <<
"Resolution: " << setprecision(2) << resolution << endl << endl;
307 long i, j, k, l, m, nr(plot->
rows());
309 for ( i=nr, j=2*nr, k=3*nr, l=4*nr, m=5*nr; i<2*nr; i++, j++, k++, l++, m++ )
310 (*plot)[i] = ((*plot)[m]/(*plot)[l])*sqrt((*plot)[j]/(*plot)[k]);
348 double fast_angle,
int action,
double wiener,
double cutoff)
357 for ( mg = field->
mg; mg; mg = mg->
next )
if ( mg->
select ) nmg++;
371 Bplot* mgplot = NULL;
372 Bplot* tomoplot = NULL;
375 for ( mg = field->
mg; mg; mg = mg->
next, i++ )
if ( mg->
select ) {
377 sampling_ratio, scale, size, fast_angle, action, wiener);
379 cout <<
"Intensity: " << setprecision(2) << mg->
fom << endl << endl;
383 cout <<
"Resolution: " << setprecision(2) << mg->
fom << endl << endl;
388 tomoplot->
add(mgplot, 1);
404 label =
Bstring(cutoff,
"NLOO cutoff: %g");
412 plot.push_back(tiltplot);
413 plot.push_back(tomoplot);
432 double sampling_ratio,
double fast_angle,
double cutoff)
437 double scale(1), resolution;
438 Bfield* field, *field_res;
443 Bparticle* part = NULL, *part_res = NULL;
446 Bimage* ppart, *ppart_res = NULL;
452 partlist = rec->
part;
457 cerr <<
"No particle list found!" << endl << endl;
462 for ( mg = field->
mg; mg; mg = mg->
next ) nmg++;
465 cerr <<
"No micrographs found!" << endl << endl;
470 for ( mg = field->
mg; mg; mg = mg->
next )
471 if ( mg && mg->
part )
break;
472 if ( mg && mg->
part )
break;
475 if ( !mg || !mg->
part ) {
476 cerr <<
"No micrograph particles found!" << endl << endl;
480 double* res_avg =
new double[nmg];
481 double* res_std =
new double[nmg];
482 for ( i=0; i<nmg; i++ ) res_avg[i] = res_std[i] = 0;
486 if ( hi_res < 2*mg->pixel_size[0] ) hi_res = 2*mg->
pixel_size[0];
489 double rad_scale = realsize[0];
490 if ( rad_scale < realsize[1] ) rad_scale = realsize[1];
491 if ( rad_scale < realsize[2] ) rad_scale = realsize[2];
492 if ( boxsize[0] > 256 ) rad_scale *= 256.0/boxsize[0];
493 long maxrad = (long) (rad_scale/hi_res);
495 int ft_size = boxsize[0];
496 if ( pad_factor ) ft_size =
part_ft_size(boxsize[0], scale, pad_factor);
501 cout <<
"Calculating individual particle resolutions:" << endl;
502 cout <<
"Number of micrographs: " << nmg << endl;
503 cout <<
"Particle size: " << boxsize << endl;
504 cout <<
"Fourier transform size: " << ft_size <<
" x " << ft_size << endl;
505 cout <<
"High resolution limit: " << hi_res <<
" A" << endl;
506 cout <<
"FRC cutoff: " << cutoff << endl;
510 double s, sp, FRC, FRCp, fraction;
511 double* sum_r_2 =
new double[maxrad] ;
512 double* sum_r2_2 =
new double[maxrad] ;
513 double* sum_i_r =
new double[maxrad] ;
514 double* sum_i_r2 =
new double[maxrad] ;
516 for ( npart=0, recpart = partlist; recpart; recpart = recpart->
next ) {
518 cout <<
"Resolution for particle " << recpart->
id <<
":" << endl;
519 cout <<
"#mg\tAngle\tNLOO(" << cutoff <<
")" << endl;
522 for ( j=0; j<maxrad; j++ ) sum_r_2[j] = sum_r2_2[j] = sum_i_r[j] = sum_i_r2[j] = 0;
524 for ( i=0, field_res =
project->
field; field_res; field_res = field_res->
next )
if ( field_res->
select ) {
525 for ( mg_res = field_res->
mg; mg_res; mg_res = mg_res->
next )
if ( mg_res->
select ) {
526 for ( part_res = mg_res->
part; part_res; part_res = part_res->
next )
527 if ( recpart->
id == part_res->id )
break;
535 prec2 = prec->
copy();
541 for ( mg = field->
mg; mg; mg = mg->
next )
if ( mg->
select )
543 for ( part = mg->
part; part; part = part->
next )
544 if ( recpart->
id == part->
id )
break;
548 error_show(
"project_tomo_particle_resolution", __FILE__, __LINE__);
551 ppart = pmg->
extract(0, part->
loc - boxsize/2, boxsize);
560 ft_size, ppart->
sizeX(), scale, hi_res,
562 if ( mg == mg_res ) ppart_res = ppart;
567 vector<Bimage*> parr;
568 parr.push_back(ppart_res);
569 parr.push_back(prec);
570 parr.push_back(prec2);
576 for ( j=0; j<maxrad; j++ ) {
577 sum_r_2[j] += (*plot)[2*maxrad+j];
578 sum_r2_2[j] += (*plot)[3*maxrad+j];
579 sum_i_r[j] += (*plot)[4*maxrad+j];
580 sum_i_r2[j] += (*plot)[5*maxrad+j];
583 part_res->fom[0] = resolution;
584 res_avg[i] += resolution;
585 res_std[i] += resolution*resolution;
588 cout << i <<
tab << setprecision(2) << mg_res->
tilt_angle*180.0/
M_PI <<
tab << resolution << endl;
595 cout <<
"3D resolution: " << endl;
596 cout <<
"s(1/A)\tRes(A)\tNLOO3D" << endl;
599 resolution = 1/rad_scale;
600 for ( j=1; j<maxrad; j++ ) {
605 FRC = (sum_i_r2[j]*sqrt(sum_r_2[j]))/(sum_i_r[j]*sqrt(sum_r2_2[j]));
606 if ( FRC < 0 ) FRC = 0;
607 if ( FRC > cutoff ) resolution = 1/s;
608 if ( FRCp > cutoff && FRC <= cutoff ) {
609 fraction = (FRCp - cutoff)/(FRCp - FRC);
610 if ( sp ) resolution = (1-fraction)/sp + fraction/s;
614 cout << setprecision(4) << s <<
tab << setprecision(1) << 1/s <<
tab << setprecision(4) << FRC << endl;
616 cout <<
"NLOO3D resolution: " << resolution << endl << endl;
627 Bstring title(
"Particle resolution for a tilt series");
628 plot =
new Bplot(1, nmg, 3);
649 label =
Bstring(boxsize[0],
"Box size: %g") +
Bstring(boxsize[1],
" x %g");
652 double min(1e37), max(0);
655 cout <<
"Average resolution for " << npart <<
" particles:" << endl;
656 cout <<
"#mg\tAngle\tFRCavg\tFRCstd" << endl;
659 for ( mg = field->
mg; mg; mg = mg->
next, i++ ) {
661 res_std[i] = res_std[i]/npart - res_avg[i]*res_avg[i];
662 if ( res_std[i] > 0 ) res_std[i] = sqrt(res_std[i]);
664 if ( min > res_avg[i] - res_std[i] ) min = res_avg[i] - res_std[i];
665 if ( max < res_avg[i] + res_std[i] ) max = res_avg[i] + res_std[i];
667 (*plot)[i+nmg] = res_avg[i];
668 (*plot)[i+2*nmg] = res_std[i];
671 res_avg[i] <<
tab << res_std[i] << endl;
681 cout <<
"DEBUG project_tomo_particle_resolution: min=" << min <<
" max=" << max << endl;
713 long ft_size,
double scale,
double hi_res,
721 cout <<
"Packing an image into reciprocal space up to " << hi_res <<
" A resolution" << endl << endl;
723 long i, j, x, y, kx, ky;
724 long ix, iy, ixx, iyy, nover(0);
725 double w, dx, dy, d2;
733 int notinplane = 1 - inplane;
739 double maxrad = p->
sampling(0)[0]/hi_res;
740 double maxrad_sq = maxrad*maxrad;
741 double ymin = floor(-maxrad*p->
sizeY());
743 double xmin = floor(-maxrad*p->
sizeX());
752 for ( iv[1]=ymin; iv[1]<=ymax; iv[1]+=1 ) {
753 if ( iv[1] >= 0 ) y = (long)iv[1];
754 else y = (long)(iv[1] + p->
sizeY());
755 dy = iv[1]/p->
sizeY();
756 for ( iv[0]=xmin; iv[0]<=xmax; iv[0]+=1 ) {
757 if ( iv[0] >= 0 ) x = (long)iv[0];
758 else x = (long)(iv[0] + p->
sizeX());
759 dx = iv[0]/p->
sizeX();
761 if ( d2 <= maxrad_sq ) {
767 ix = (long) floor(m[0]);
768 iy = (long) floor(m[1]);
771 i = y*p->
sizeX() + x;
772 for ( ky=0; ky<2; ky++ ) {
774 if ( iyy < 0 ) iyy += prec->
sizeY();
776 for ( kx=0; kx<2; kx++ ) {
778 if ( ixx < 0 ) ixx += prec->
sizeX();
780 j = iyy*prec->
sizeX() + ixx;
800 long ft_size,
int zsize,
double scale,
double hi_res,
808 cout <<
"Packing an image into reciprocal space up to " << hi_res <<
" A resolution" << endl << endl;
810 long i, j, x, y, kx, ky;
811 long ix, iy, ixx, iyy;
821 int notinplane = 1 - inplane;
827 double maxrad = prec->
sampling(0)[0]/hi_res;
828 double maxrad_sq = maxrad*maxrad;
829 double ymin = floor(-maxrad*p->
sizeY());
831 double xmin = floor(-maxrad*p->
sizeX());
836 vt *= 1 - mat[2][2]/matr[2][2];
840 if ( mat[0][2] > 0 ) vt *= -1;
849 for ( iv[1]=ymin; iv[1]<=ymax; iv[1]+=1 ) {
850 if ( iv[1] >= 0 ) y = (long)iv[1];
851 else y = (long)(p->
sizeY() + iv[1]);
852 for ( iv[0]=xmin; iv[0]<=xmax; iv[0]+=1 ) {
853 if ( iv[0] >= 0 ) x = (long)iv[0];
854 else x = (long)(p->
sizeX() + iv[0]);
862 if ( d2 <= maxrad_sq ) {
863 ix = (long) floor(m[0]);
864 iy = (long) floor(m[1]);
867 i = y*p->
sizeX() + x;
868 for ( ky=0; ky<2; ky++ ) {
870 if ( iyy < 0 ) iyy += prec->
sizeY();
872 for ( kx=0; kx<2; kx++ ) {
874 if ( ixx < 0 ) ixx += prec->
sizeX();
876 j = iyy*prec->
sizeX() + ixx;
904 if ( parr.size() < 3 )
return NULL;
914 if ( sampling_ratio < 1 ) sampling_ratio = 1;
916 long i, j, x, y, z, iradius, iradius2, nt(0), nf(0);
918 double rx, ry, rz, radius, fraction, fraction2, v;
922 double rad_scale_max(realsize[0]/sampling_ratio);
923 double rad_scale = realsize[0];
924 if ( rad_scale < realsize[1] ) rad_scale = realsize[1];
925 if ( rad_scale < realsize[2] ) rad_scale = realsize[2];
928 if ( rad_scale > rad_scale_max ) rad_scale = rad_scale_max;
929 long maxrad = (long) (rad_scale/hi_res);
932 cout <<
"DEBUG img_calculate_FRC_curve: maxrad=" << maxrad << endl;
934 vector<double> n(maxrad);
935 vector<double> sum_i_2(maxrad);
936 vector<double> sum_r_2(maxrad);
937 vector<double> sum_r2_2(maxrad);
938 vector<double> sum_i_r(maxrad);
939 vector<double> sum_i_r2(maxrad);
941 for ( i=0; i<maxrad; i++ )
942 n[i] = sum_i_2[i] = sum_r_2[i] = sum_r2_2[i] = sum_i_r[i] = sum_i_r2[i] = 0;
946 for ( i=z=0; z<p->
sizeZ(); z++ ) {
948 if ( z > (p->
sizeZ() - 1)/2 ) zz -= p->
sizeZ();
949 rz = zz*freq_scale[2];
950 for ( y=0; y<p->
sizeY(); y++ ) {
952 if ( y > (p->
sizeY() - 1)/2 ) yy -= p->
sizeY();
953 ry = yy*freq_scale[1];
954 for ( x=0; x<p->
sizeX(); x++, i++ ) {
956 if ( x > (p->
sizeX() - 1)/2 ) xx -= p->
sizeX();
957 rx = xx*freq_scale[0];
958 radius = rad_scale*sqrt(rx*rx + ry*ry + rz*rz);
959 iradius = (long) radius;
960 iradius2 = iradius + 1;
962 if ( iradius2 < maxrad ) {
963 fraction = radius - iradius;
964 fraction2 = 1.0 - fraction;
966 sum_i_2[iradius] += fraction2*v;
967 sum_i_2[iradius2] += fraction*v;
968 v = (prec->
complex(i)).power();
969 sum_r_2[iradius] += fraction2*v;
970 sum_r_2[iradius2] += fraction*v;
971 v = (prec2->
complex(i)).power();
972 sum_r2_2[iradius] += fraction2*v;
973 sum_r2_2[iradius2] += fraction*v;
975 sum_i_r[iradius] += fraction2*v;
976 sum_i_r[iradius2] += fraction*v;
978 sum_i_r2[iradius] += fraction2*v;
979 sum_i_r2[iradius2] += fraction*v;
980 n[iradius] += fraction2;
981 n[iradius2] += fraction;
996 cout <<
"Pixel\ts\tResol\tsumI2\tsumR2\tsumRo2\tsumIR\tsumIRo\tFRC\tNum" << endl;
998 cout <<
"Pixel\ts\tResol\tFRC" << endl;
999 for ( i=1; i<maxrad; i++ ) {
1006 FRC = (sum_i_r2[i]/sum_i_r[i])*sqrt(sum_r_2[i]/sum_r2_2[i]);
1007 if ( FRC > 1 ) FRC = 1;
1008 if ( FRC < 0 ) FRC = 0;
1011 << setprecision(4) << s <<
tab
1012 << setprecision(2) << 1/s <<
tab
1013 << setprecision(0) << sum_i_2[i] <<
tab
1014 << sum_r_2[i] <<
tab << sum_r2_2[i] <<
tab
1015 << sum_i_r[i] <<
tab << sum_i_r2[i] <<
tab
1016 << setprecision(4) << FRC <<
tab
1017 << setprecision(0) << n[i] << endl;
1020 << setprecision(4) << s <<
tab
1021 << setprecision(2) << 1/s <<
tab
1022 << setprecision(4) << FRC << endl;
1027 (*plot)[j] = sum_r_2[i];
1029 (*plot)[j] = sum_r2_2[i];
1031 (*plot)[j] = sum_i_r[i];
1033 (*plot)[j] = sum_i_r2[i];
1037 cout <<
"Total voxels: " << nt << endl;
1038 cout <<
"Coverage: " << nf <<
" (" << nf*100.0/nt <<
" %)" << endl;
1052 double s, sp(0), FRC, FRCp(1), fraction, resolution(1000);
1053 long i, maxrad = (long) plot->
rows();
1055 for ( i=1; i<maxrad; i++ ) {
1057 FRC = (*plot)[i+maxrad];
1058 if ( FRC > cutoff ) resolution = 1/s;
1059 if ( FRCp > cutoff && FRC <= cutoff ) {
1060 fraction = (FRCp - cutoff)/(FRCp - FRC);
1061 if ( sp ) resolution = (1-fraction)/sp + fraction/s;
1083 plot->
title(
"Resolution");
1117 Bstring title(
"Micrograph resolution for a tilt series");
1120 for ( mg = field->
mg; mg; mg = mg->
next )
if ( mg->
select ) nmg++;
1135 for ( mg = field->
mg; mg; mg = mg->
next, i++ )
if ( mg->
select ) {
1137 (*plot)[i+nmg] = mg->
fom;