20 cerr <<
"Error: File " <<
file_name() <<
" must be a real space map!" << endl;
25 cerr <<
"Error: File " << p->
file_name() <<
" must be a real space map!" << endl;
30 cout <<
"Determining resolution from images " <<
file_name() <<
" and " << p->
file_name() << endl;
34 if ( pc == NULL )
return pc;
41 cout <<
"DEBUG Bimage::resolution_prepare: FFT done" << endl;
49 cerr <<
"Error: File " <<
file_name() <<
" must be a real space map!" << endl;
54 cerr <<
"Error: File " << p->
file_name() <<
" must be a real space map!" << endl;
59 cout <<
"Determining resolution from images " <<
file_name() <<
" and " << p->
file_name() << endl;
63 if ( pc == NULL )
return pc;
70 cout <<
"DEBUG Bimage::resolution_prepare: FFT done" << endl;
94 if ( sampling_ratio <= 0 ) sampling_ratio = 1;
105 long maxrad = (long) (2 + rad_scale/hi_res);
110 cout <<
"DEBUG Bimage::fsc_dpr: FFT done" << endl;
112 long i, j, xx, yy, zz, ix, iy, iz, iradius, iradius2;
113 double radius, fraction, fraction2, I1, I2, Isum, F1F2re, dphi, Id2;
117 cout <<
"Resolution limit: " << hi_res <<
" A (" << maxrad <<
")" << endl;
118 cout <<
"Sampling ratio: " << sampling_ratio << endl << endl;
121 double* F1 =
new double[maxrad];
122 double* F2 =
new double[maxrad];
123 double* Fsum =
new double[maxrad];
124 double* FSC =
new double[maxrad];
125 double* DPR =
new double[maxrad];
127 for ( i=0; i<maxrad; i++ )
128 F1[i] = F2[i] = Fsum[i] = FSC[i] = DPR[i] = 0;
130 for ( zz=0; zz<z; zz++ ) {
132 if ( rz > (z - 1)/2 ) rz -= z;
135 if ( iz < 0 ) iz += z;
136 for ( yy=0; yy<y; yy++ ) {
138 if ( ry > (y - 1)/2 ) ry -= y;
141 if ( iy < 0 ) iy += y;
142 for ( xx=0; xx<x/2; xx++ ) {
143 i = (zz*y + yy)*x + xx;
145 if ( xx > (x - 1)/2 ) rx -= x;
148 if ( ix < 0 ) ix += x;
149 radius = rad_scale*sqrt(rx*rx + ry*ry + rz*rz);
150 iradius = (long) radius;
151 iradius2 = iradius + 1;
152 if ( iradius2 < maxrad ) {
153 fraction = radius - iradius;
154 fraction2 = 1.0 - fraction;
155 j = (iz*y + iy)*x + ix;
162 F1[iradius] += fraction2*I1;
163 F1[iradius2] += fraction*I1;
164 F2[iradius] += fraction2*I2;
165 F2[iradius2] += fraction*I2;
167 FSC[iradius] += fraction2*F1F2re;
168 FSC[iradius2] += fraction*F1F2re;
170 Fsum[iradius] += fraction2*Isum;
171 Fsum[iradius2] += fraction*Isum;
173 DPR[iradius] += fraction2*Id2;
174 DPR[iradius2] += fraction*Id2;
181 if ( flag ) ncol = 2;
183 Bstring title(
"Resolution"), txt;
224 for ( i=1; i<maxrad; i++ ) {
226 if ( F1[i]*F2[i] >
SMALLFLOAT ) FSC[i] /= sqrt(F1[i]*F2[i]);
228 if ( FSC[i] > 1 ) FSC[i] = 1;
229 if ( Fsum[i] >
SMALLFLOAT ) DPR[i] = sqrt(DPR[i]/Fsum[i]);
231 (*plot)[i] = i/rad_scale;
232 (*plot)[maxrad+i] = FSC[i];
233 if ( ncol > 2 ) (*plot)[2*maxrad+i] = DPR[i]*180.0/
M_PI;
248 cout <<
"Determining resolution for " << n <<
" images" << endl;
249 cout <<
"Resolution limit: " << hi_res << endl;
250 cout <<
"Sampling ratio: " << sampling_ratio << endl << endl;
253 long i, j, nn, ncol(n+1);
254 double r, fsccut[4] = {0.143, 0.3, 0.5, 0.9};
255 Bstring title(
"Resolution"), txt;
262 for ( i=0; i<4; i++ )
if ( fsccut[i] ) cout <<
tab <<
"FSC(" << fsccut[i] <<
")";
266 for ( nn=0; nn<n; nn++ ) {
269 plot1 = p1->
fsc_dpr(hi_res, sampling_ratio);
276 (*
plot)[i] = (*plot1)[i];
280 (*
plot)[j] = (*plot1)[i];
285 title =
label() +
" image " + to_string(nn+1);
310 for ( i=0; i<4; i++ )
if ( fsccut[i] ) {
311 r = 1/
plot->
cut(nn+1,fsccut[i],-1);
328 cout <<
"Determining resolution for " << n <<
" images" << endl;
329 cout <<
"Resolution limit: " << hi_res << endl;
330 cout <<
"Sampling ratio: " << sampling_ratio << endl << endl;
333 long i, j, nn, ncol(n+1);
335 double rad_scale(
real_size().max()/sampling_ratio);
337 Bstring title(
"Resolution"), txt;
344 for ( nn=0; nn<n; nn++ ) {
354 p2 = p1->
radial(0, maxrad, 1, 1);
356 for ( i=1; i<p2->
sizeX(); ++i ) p2->
set(i, (*p2)[i] / sqrt((*pr1)[i] * (*pr2)[i]));
365 (*
plot)[i] = i/rad_scale;
368 for ( i=0, j=(nn+1)*
plot->
rows(); i<plot->rows(); ++i, ++j )
369 if ( (*p2)[i] > 0 ) (*plot)[j] = (*p2)[i];
374 title =
"Image: " + to_string(nn+1);
416 int thickness,
int step,
int minrad,
int maxrad,
417 int pad,
int smooth,
double fill)
422 double sampling_ratio(1);
427 long ft_size(x), nmap(0), imgsize(
image_size());
435 if ( minrad < 0 ) minrad = 0;
436 if ( maxrad > x/2 ) maxrad = x/2;
437 if ( maxrad < 1 ) maxrad = x/2;
440 for ( i=0; i<4; ++i )
if ( cutoff[i] > 0 ) nmap++;
443 cout <<
"Calculating shell resolution:" << endl;
444 cout <<
"Shell thickness: " <<
thickness << endl;
445 cout <<
"Step size: " << step << endl;
446 cout <<
"Radii: " << minrad <<
" " << maxrad << endl;
447 cout <<
"Padding size: " <<
pad << endl;
448 cout <<
"Smoothing: " << smooth << endl;
449 cout <<
"Cutoff(s): ";
450 for ( i=0; i<nmap-1; ++i ) cout <<
"," << cutoff[i];
451 cout << cutoff[i] << endl;
463 pr->
size((maxrad - minrad)/step + 1, 1, 1);
476 cout <<
"Radius\tResolution" << endl;
478 for ( r=minrad, i=0; r<=maxrad; r+=step, i++ ) {
481 if ( r1 < 0 ) r1 = 0;
489 cp1->
pad(
pad, fill_type, avg);
497 for ( j=0; j<4; ++j ) {
499 res_est = 1/
plot->
cut(1, cutoff[j], -1);
500 if ( res_est < hi_res ) res_est = hi_res;
501 if ( res_est <
LORESLIM ) pr->
set(i + j*imgsize, res_est);
505 cout << r <<
tab << res_est << endl;
514 double* Bimage::fsc_local_voxel(
Bimage* p,
double hi_res,
int size,
515 int pad,
int taper,
double* cutoff,
fft_plan plan,
long i)
517 long nn(0), xx, yy, zz, cc;
519 double sampling_ratio(1);
521 double width =
size/4.0;
538 }
else if ( taper == 1 ) {
539 cp1->
edge(1, taper_size, taper_start, width, fill_type, cp1->
average());
540 cp2->
edge(1, taper_size, taper_start, width, fill_type, cp2->
average());
555 double* res_est =
new double[4];
556 for (
long j=0; j<4; ++j ) {
559 res_est[j] = 1/
plot->
cut(1, cutoff[j], -1);
560 if ( res_est[j] < hi_res ) res_est[j] = hi_res;
594 int step,
int taper,
double fill)
597 long xx, yy, zz, i, nmap(0), nvox(0);
608 for ( i=0; i<4; ++i )
if ( cutoff[i] > 0 ) nmap++;
611 cout <<
"Calculating local resolution:" << endl;
613 cout <<
"Mask: " << pmask->
file_name() <<
" (" << mask_level <<
")" << endl;
614 cout <<
"Kernel size: " <<
size << endl;
615 cout <<
"Step size: " << step << endl;
616 cout <<
"Edge size: " << vedge << endl;
617 cout <<
"Padding size: " <<
pad << endl;
618 cout <<
"Smoothing/tapering: " << taper << endl;
619 cout <<
"Cutoff(s): " << setprecision(3);
620 for ( i=0; i<nmap-1; ++i ) cout << cutoff[i] <<
",";
621 cout << cutoff[i] << endl;
646 for ( i=zz=0; zz<pmask->
sizeZ(); zz++ ) {
647 for ( yy=0; yy<pmask->
sizeY(); yy++ ) {
648 for ( xx=0; xx<pmask->
sizeX(); xx++, i++ ) {
650 if ( zz%step || yy%step || xx%step ) dovox = 0;
651 if ( (*pmask)[i] < 0.5 ) dovox = 0;
652 if ( zz < vedge[2] || zz > pmask->
sizeZ()-vedge[2] ) dovox = 0;
653 if ( yy < vedge[1] || yy > pmask->
sizeY()-vedge[1] ) dovox = 0;
654 if ( xx < vedge[0] || xx > pmask->
sizeX()-vedge[0] ) dovox = 0;
655 if ( mask_level > 0 && fabs((*pmask)[i] - mask_level) > 0.1 ) dovox = 0;
656 pmask->
set(i, dovox);
666 for ( i=nvox=0; i<datasize; i++ )
if ( (*pmask)[i] ) nvox++;
667 cout <<
"Boxes to calculate: " << nvox << endl << endl;
671 __block
long ndone(0);
672 dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
673 dispatch_apply(imgsize, dispatch_get_global_queue(0, 0), ^(
size_t k){
675 double* res_est = fsc_local_voxel(p, hi_res,
676 size,
pad, taper, cutoff, plan, k);
677 for (
long j=0; j<nmap; ++j ) pr->
set(k + j*imgsize, res_est[j]);
680 dispatch_sync(myq, ^{
682 cerr <<
"Complete: " << setprecision(3)
683 << ndone*100.0/nvox <<
" % \r" << flush;
689 #pragma omp parallel for
690 for (
long k=0; k<imgsize; k++ )
if ( (*pmask)[k] ) {
691 double* res_est = fsc_local_voxel(p, hi_res,
692 size,
pad, taper, cutoff, plan, k);
693 for (
long j=0; j<nmap; ++j ) pr->
set(k + j*imgsize, res_est[j]);
699 cerr <<
"Complete: " << setprecision(3)
700 << ndone*100.0/nvox <<
" % \r" << flush;
707 for ( i=nvox=0; i<imgsize; i++ )
if ( fabs((*pr)[i] -
fill) > 1e-10 ) nvox++;
711 cout <<
"Boxes calculated: " << nvox << endl << endl;
715 cout <<
"x\ty\tz\tResolution" << endl;
716 for ( zz=0; zz<pr->
sizeZ(); zz+=step ) {
717 for ( yy=0; yy<p->
sizeY(); yy+=step ) {
718 for ( xx=0; xx<p->
sizeX(); xx+=step ) {
721 cout << xx <<
tab << yy <<
tab << zz <<
tab << (*pr)[i] << endl;
737 double Bimage::local_filter_voxel(
Bimage* resmap,
long size,
740 long nn, xx, yy, zz, cc;
745 double bandpass_width(0.002);
750 long cenvox = (boxsize[2]/2*boxsize[1] + boxsize[1]/2)*boxsize[0] + boxsize[0]/2;
754 start =
Vector3<double>(xx - boxhalf[0], yy - boxhalf[1], zz - boxhalf[2]);
755 res_hi = (*resmap)[i];
756 cp =
extract(nn, start, boxsize);
760 double val = (*cp)[cenvox];
786 cerr <<
"Error: A local resolution map must be provided!" << endl;
794 long xx, yy, zz, nn, i, nvox(0);
798 cout <<
"Filtering by local resolution:" << endl;
799 cout <<
"Local resolution image: " << resmap->
file_name() << endl;
801 cout <<
"Mask: " << pmask->
file_name() <<
" (" << mask_level <<
")" << endl;
802 cout <<
"Kernel size: " <<
size << endl;
803 cout <<
"Edge size: " << vedge << endl;
820 for ( i=zz=0; zz<pmask->
sizeZ(); zz++ ) {
821 for ( yy=0; yy<pmask->
sizeY(); yy++ ) {
822 for ( xx=0; xx<pmask->
sizeX(); xx++, i++ ) {
824 if ( (*pmask)[i] < 0.99 ) dovox = 0;
825 if ( zz < vedge[2] || zz > pmask->
sizeZ()-vedge[2] ) dovox = 0;
826 if ( yy < vedge[1] || yy > pmask->
sizeY()-vedge[1] ) dovox = 0;
827 if ( xx < vedge[0] || xx > pmask->
sizeX()-vedge[0] ) dovox = 0;
828 if ( mask_level > 0 && fabs((*pmask)[i] - mask_level) > 0.1 ) dovox = 0;
829 pmask->
set(i, dovox);
834 for ( i=0; i<datasize; i++ )
if ( (*pmask)[i] ) nvox++;
842 __block
long ndone(0);
843 dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
844 dispatch_apply(datasize, dispatch_get_global_queue(0, 0), ^(
size_t k){
848 dispatch_sync(myq, ^{
850 cerr <<
"Complete: " << setprecision(3)
851 << ndone*100.0/nvox <<
" % \r" << flush;
857 #pragma omp parallel for
858 for (
long j=0; j<datasize; j++ )
if ( (*pmask)[j] ) {
864 cerr <<
"Complete: " << setprecision(3)
865 << ndone*100.0/nvox <<
" % \r" << flush;
874 cout <<
"ImgNum\tx\ty\tz\tResolution" << endl;
875 for ( i=nn=0; nn<p->
images(); nn++ ) {
876 for ( zz=0; zz<p->
sizeZ(); zz++ ) {
877 for ( yy=0; yy<p->
sizeY(); yy++ ) {
878 for ( xx=0; xx<p->
sizeX(); xx++, i++ ) {
880 cout << nn <<
tab << xx <<
tab << yy <<
tab << zz <<
tab << (*p)[i] << endl;