Bsoft  2.1.0
Bernard's software package
Bimage_resolution.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "utilities.h"
11 
12 #define LORESLIM 200
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
18 {
19  if ( fouriertype != NoTransform ) {
20  cerr << "Error: File " << file_name() << " must be a real space map!" << endl;
21  return NULL;
22  }
23 
24  if ( p->fourier_type() != NoTransform ) {
25  cerr << "Error: File " << p->file_name() << " must be a real space map!" << endl;
26  return NULL;
27  }
28 
29  if ( verbose > VERB_RESULT )
30  cout << "Determining resolution from images " << file_name() << " and " << p->file_name() << endl;
31 
32  // Pack the two images into one complex block
33  Bimage* pc = pack_two_in_complex(p);
34  if ( pc == NULL ) return pc;
35 
36  pc->fft();
37 
38  pc->label(file_name() + " vs " + p->file_name());
39 
40  if ( verbose & VERB_DEBUG )
41  cout << "DEBUG Bimage::resolution_prepare: FFT done" << endl;
42 
43  return pc;
44 }
45 
47 {
48  if ( fouriertype != NoTransform ) {
49  cerr << "Error: File " << file_name() << " must be a real space map!" << endl;
50  return NULL;
51  }
52 
53  if ( p->fourier_type() != NoTransform ) {
54  cerr << "Error: File " << p->file_name() << " must be a real space map!" << endl;
55  return NULL;
56  }
57 
58  if ( verbose > VERB_RESULT )
59  cout << "Determining resolution from images " << file_name() << " and " << p->file_name() << endl;
60 
61  // Pack the two images into one complex block
62  Bimage* pc = pack_two_in_complex(p);
63  if ( pc == NULL ) return pc;
64 
65  pc->fft(plan, 0);
66 
67  pc->label(file_name() + " vs " + p->file_name());
68 
69  if ( verbose & VERB_DEBUG )
70  cout << "DEBUG Bimage::resolution_prepare: FFT done" << endl;
71 
72  return pc;
73 }
74 
75 
92 Bplot* Bimage::fsc_dpr(double hi_res, double sampling_ratio, int flag)
93 {
94  if ( sampling_ratio <= 0 ) sampling_ratio = 1;
95 
96  // The frequency scaling is linked to the different dimensions of the
97  // data set (important when the x, y and z dimensions are different)
98  // The radius scaling is set to a value consistent with one pixel width
99  // in reciprocal space for the longest dimension.
100  Vector3<double> freq_scale(1/real_size());
101  double rad_scale = real_size().max()/sampling_ratio;
102  if ( hi_res < 2*sampling(0).max() ) hi_res = 2*sampling(0).max();
103  if ( hi_res > real_size().max()/2.0 ) hi_res = real_size().max()/2.0;
104 
105  long maxrad = (long) (2 + rad_scale/hi_res);
106 
107  Complex<double> sf1, sf2;
108 
109  if ( verbose & VERB_DEBUG )
110  cout << "DEBUG Bimage::fsc_dpr: FFT done" << endl;
111 
112  long i, j, xx, yy, zz, ix, iy, iz, iradius, iradius2;
113  double radius, fraction, fraction2, I1, I2, Isum, F1F2re, dphi, Id2;
114  double rx, ry, rz;
115 
116  if ( verbose > VERB_RESULT ) {
117  cout << "Resolution limit: " << hi_res << " A (" << maxrad << ")" << endl;
118  cout << "Sampling ratio: " << sampling_ratio << endl << endl;
119  }
120 
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];
126 
127  for ( i=0; i<maxrad; i++ )
128  F1[i] = F2[i] = Fsum[i] = FSC[i] = DPR[i] = 0;
129 
130  for ( zz=0; zz<z; zz++ ) {
131  rz = zz;
132  if ( rz > (z - 1)/2 ) rz -= z;
133  rz *= freq_scale[2];
134  iz = -zz;
135  if ( iz < 0 ) iz += z;
136  for ( yy=0; yy<y; yy++ ) {
137  ry = yy;
138  if ( ry > (y - 1)/2 ) ry -= y;
139  ry *= freq_scale[1];
140  iy = -yy;
141  if ( iy < 0 ) iy += y;
142  for ( xx=0; xx<x/2; xx++ ) {
143  i = (zz*y + yy)*x + xx;
144  rx = xx;
145  if ( xx > (x - 1)/2 ) rx -= x;
146  rx *= freq_scale[0];
147  ix = -xx;
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;
156  sf1 = complex(i).unpack_first(complex(j));
157  sf2 = complex(i).unpack_second(complex(j));
158  I1 = sf1.power();
159  I2 = sf2.power();
160  dphi = angle_set_negPI_to_PI(sf1.phi() - sf2.phi());
161  dphi *= dphi;
162  F1[iradius] += fraction2*I1;
163  F1[iradius2] += fraction*I1;
164  F2[iradius] += fraction2*I2;
165  F2[iradius2] += fraction*I2;
166  F1F2re = sf1.real()*sf2.real() + sf1.imag()*sf2.imag();
167  FSC[iradius] += fraction2*F1F2re;
168  FSC[iradius2] += fraction*F1F2re;
169  Isum = I1 + I2;
170  Fsum[iradius] += fraction2*Isum;
171  Fsum[iradius2] += fraction*Isum;
172  Id2 = Isum*dphi;
173  DPR[iradius] += fraction2*Id2;
174  DPR[iradius2] += fraction*Id2;
175  }
176  }
177  }
178  }
179 
180  int ncol(3);
181  if ( flag ) ncol = 2;
182 
183  Bstring title("Resolution"), txt;
184  Bplot* plot = new Bplot(1, maxrad, ncol);
185 
186  plot->title(title);
187  plot->page(0).title(label());
188  plot->page(0).columns(ncol);
189  for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
190  plot->page(0).column(0).label("s");
191  plot->page(0).column(0).axis(1);
192 
193  if ( z > 1 ) plot->page(0).column(1).label("FSC");
194  else plot->page(0).column(1).label("FRC");
195  plot->page(0).column(1).type(2);
196  plot->page(0).column(1).axis(3);
197  plot->page(0).column(1).color(1,0,0);
198 
199  plot->page(0).axis(1).min(0);
200  plot->page(0).axis(1).max(1/hi_res);
201  plot->page(0).axis(1).inc(0.1/hi_res);
202  plot->page(0).axis(1).flags(1);
203  plot->page(0).axis(1).label("Resolution (A)");
204  plot->page(0).axis(3).min(0);
205  plot->page(0).axis(3).max(1);
206  plot->page(0).axis(3).inc(0.1);
207  if ( z > 1 ) plot->page(0).axis(3).label("FSC");
208  else plot->page(0).axis(3).label("FRC");
209 
210  if ( ncol > 2 ) {
211  plot->page(0).column(2).label("DPR");
212  plot->page(0).column(2).type(2);
213  plot->page(0).column(2).axis(4);
214  plot->page(0).column(2).color(0,0,1);
215 
216  plot->page(0).axis(4).min(0);
217  plot->page(0).axis(4).max(120);
218  plot->page(0).axis(4).inc(10);
219  plot->page(0).axis(4).label("DPR");
220  }
221 
222  (*plot)[0] = 0;
223  (*plot)[maxrad] = 1;
224  for ( i=1; i<maxrad; i++ ) {
225 // cout << i << tab << F1[i] << tab << F2[i] << tab << FSC[i] << tab << endl;
226  if ( F1[i]*F2[i] > SMALLFLOAT ) FSC[i] /= sqrt(F1[i]*F2[i]);
227  if ( FSC[i] < SMALLFLOAT ) FSC[i] = 0;
228  if ( FSC[i] > 1 ) FSC[i] = 1;
229  if ( Fsum[i] > SMALLFLOAT ) DPR[i] = sqrt(DPR[i]/Fsum[i]);
230  else DPR[i] = 0;
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;
234  }
235 
236  delete[] F1;
237  delete[] F2;
238  delete[] Fsum;
239  delete[] FSC;
240  delete[] DPR;
241 
242  return plot;
243 }
244 
245 Bplot* Bimage::fsc(double hi_res, double sampling_ratio)
246 {
247  if ( verbose ) {
248  cout << "Determining resolution for " << n << " images" << endl;
249  cout << "Resolution limit: " << hi_res << endl;
250  cout << "Sampling ratio: " << sampling_ratio << endl << endl;
251  }
252 
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;
256  Bimage* p1 = NULL;
257  Bplot* plot = NULL;
258  Bplot* plot1 = NULL;
259 
260  if ( verbose ) {
261  cout << "Image";
262  for ( i=0; i<4; i++ ) if ( fsccut[i] ) cout << tab << "FSC(" << fsccut[i] << ")";
263  cout << endl;
264  }
265 
266  for ( nn=0; nn<n; nn++ ) {
267 
268  p1 = extract(nn);
269  plot1 = p1->fsc_dpr(hi_res, sampling_ratio);
270  delete p1;
271 
272  if ( !plot ) {
273  plot = new Bplot(n, plot1->rows(), ncol);
274  plot->title(title);
275  for ( i=0; i<plot->rows(); i++ )
276  (*plot)[i] = (*plot1)[i];
277  }
278 
279  for ( i=plot->rows(), j=(nn+1)*plot->rows(); i<2*plot->rows(); i++, j++ )
280  (*plot)[j] = (*plot1)[i];
281 
282  delete plot1;
283 
284 // title = label() + Bstring(nn+1, " image %d");
285  title = label() + " image " + to_string(nn+1);
286  plot->page(nn).title(title);
287  plot->page(nn).columns(2);
288  plot->page(nn).column(0).number(0);
289  plot->page(nn).column(1).number(nn+1);
290  plot->page(nn).column(0).label("s");
291  plot->page(nn).column(0).axis(1);
292  if ( z > 1 ) plot->page(nn).column(1).label("FSC");
293  else plot->page(nn).column(1).label("FRC");
294  plot->page(nn).column(1).type(2);
295  plot->page(nn).column(1).axis(3);
296  plot->page(nn).column(1).color(0,0,0);
297  plot->page(nn).axis(1).min(0);
298  plot->page(nn).axis(1).max(1/hi_res);
299  plot->page(nn).axis(1).inc(0.1/hi_res);
300  plot->page(nn).axis(1).flags(1);
301  plot->page(nn).axis(1).label("Resolution (A)");
302  plot->page(nn).axis(3).min(0);
303  plot->page(nn).axis(3).max(1);
304  plot->page(nn).axis(3).inc(0.1);
305  if ( z > 1 ) plot->page(nn).axis(3).label("FSC");
306  else plot->page(nn).axis(3).label("FRC");
307 
308  if ( verbose )
309  cout << nn+1;
310  for ( i=0; i<4; i++ ) if ( fsccut[i] ) {
311  r = 1/plot->cut(nn+1,fsccut[i],-1);
312  txt = plot->page(nn).column(1).label() + Bstring(fsccut[i], "(%g): ")
313  + Bstring(r, "%g A");
314  plot->page(nn).add_text(txt);
315  if ( verbose )
316  cout << tab << r;
317  }
318  if ( verbose )
319  cout << endl;
320  }
321 
322  return plot;
323 }
324 
325 Bplot* Bimage::fsc(Bimage* p, double hi_res, double sampling_ratio)
326 {
327  if ( verbose ) {
328  cout << "Determining resolution for " << n << " images" << endl;
329  cout << "Resolution limit: " << hi_res << endl;
330  cout << "Sampling ratio: " << sampling_ratio << endl << endl;
331  }
332 
333  long i, j, nn, ncol(n+1);
334 // double r, fsccut[4] = {0.143, 0.3, 0.5, 0.9};
335  double rad_scale(real_size().max()/sampling_ratio);
336  long maxrad = fspace_maximum_radius(hi_res, sampling_ratio);
337  Bstring title("Resolution"), txt;
338  Bimage* p1 = NULL;
339  Bimage* p2 = NULL;
340  Bimage* pr1 = NULL;
341  Bimage* pr2 = NULL;
342  Bplot* plot = NULL;
343 
344  for ( nn=0; nn<n; nn++ ) {
345 
346  p1 = extract(nn);
347  p2 = p->extract(nn);
348  pr1 = p1->fspace_radial_power(hi_res);
349  pr2 = p2->fspace_radial_power(hi_res);
351  p1->complex_to_real();
352  delete p2;
353  p1->origin(0,0,0);
354  p2 = p1->radial(0, maxrad, 1, 1);
355  p2->set(0, 1);
356  for ( i=1; i<p2->sizeX(); ++i ) p2->set(i, (*p2)[i] / sqrt((*pr1)[i] * (*pr2)[i]));
357  delete p1;
358  delete pr1;
359  delete pr2;
360 
361  if ( !plot ) {
362  plot = new Bplot(n, p2->sizeX(), ncol);
363  plot->title(title);
364  for ( i=0; i<plot->rows(); ++i )
365  (*plot)[i] = i/rad_scale;
366  }
367 
368  for ( i=0, j=(nn+1)*plot->rows(); i<plot->rows(); ++i, ++j )
369  if ( (*p2)[i] > 0 ) (*plot)[j] = (*p2)[i];
370 
371  delete p2;
372 
373 // title = label() + Bstring(nn+1, " image %d");
374  title = "Image: " + to_string(nn+1);
375  plot->page(nn).title(title);
376  plot->page(nn).columns(2);
377  plot->page(nn).column(0).number(0);
378  plot->page(nn).column(1).number(nn+1);
379  plot->page(nn).column(0).label("s");
380  plot->page(nn).column(0).axis(1);
381  if ( z > 1 ) plot->page(nn).column(1).label("FSC");
382  else plot->page(nn).column(1).label("FRC");
383  plot->page(nn).column(1).type(2);
384  plot->page(nn).column(1).axis(3);
385  plot->page(nn).column(1).color(0,0,0);
386  plot->page(nn).axis(1).min(0);
387  plot->page(nn).axis(1).max(1/hi_res);
388  plot->page(nn).axis(1).inc(0.1/hi_res);
389  plot->page(nn).axis(1).flags(1);
390  plot->page(nn).axis(1).label("Resolution (A)");
391  plot->page(nn).axis(3).min(0);
392  plot->page(nn).axis(3).max(1);
393  plot->page(nn).axis(3).inc(0.1);
394  if ( z > 1 ) plot->page(nn).axis(3).label("FSC");
395  else plot->page(nn).axis(3).label("FRC");
396  }
397 
398  return plot;
399 }
400 
415 Bimage* Bimage::fsc_shell(Bimage* p, double hi_res, double* cutoff,
416  int thickness, int step, int minrad, int maxrad,
417  int pad, int smooth, double fill)
418 {
419  long i, j, r;
420  double r1, r2;
421  int fill_type(FILL_AVERAGE); // Fill type for resizing
422  double sampling_ratio(1); // Sampling ratio in reciprocal space
423 
424  Bimage* cp1 = NULL;
425  Bimage* cp2 = NULL;
426  Bplot* plot;
427  long ft_size(x), nmap(0), imgsize(image_size());
428  double res_est; // Resolution estimate
429 
430  if ( pad > 0 ) {
431  pad = x*(pad+1);
432  ft_size = pad;
433  }
434 
435  if ( minrad < 0 ) minrad = 0;
436  if ( maxrad > x/2 ) maxrad = x/2;
437  if ( maxrad < 1 ) maxrad = x/2;
438 
439  // Count the number of output maps
440  for ( i=0; i<4; ++i ) if ( cutoff[i] > 0 ) nmap++;
441 
442  if ( verbose ) {
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;
452  cout << endl;
453  }
454 
455  change_type(Float) ;
456  statistics();
457  p->change_type(Float) ;
458  p->statistics();
459  p->rescale_to_avg_std(avg, std);
460 
461  Bimage* pr = copy_header(nmap);
462  pr->data_type(Float);
463  pr->size((maxrad - minrad)/step + 1, 1, 1);
464  pr->sampling(image->sampling()[0]*step, 1, 1);
465  pr->image->origin(-minrad/step, 0, 0);
466 
467  pr->data_alloc();
468 
469  pr->fill(fill);
470 
471  fft_plan plan = fft_setup_plan(ft_size, ft_size, ft_size, FFTW_FORWARD, 1);
472 
473  Bimage* pc;
474 
475  if ( verbose )
476  cout << "Radius\tResolution" << endl;
477 
478  for ( r=minrad, i=0; r<=maxrad; r+=step, i++ ) {
479  r1 = r - thickness/2.0;
480  r2 = r + thickness/2.0;
481  if ( r1 < 0 ) r1 = 0;
482  cp1 = extract_shell(0, r1, r2);
483  cp2 = p->extract_shell(0, r1, r2);
484  if ( smooth ) {
485  cp1->filter_average(3);
486  cp2->filter_average(3);
487  }
488  if ( pad > 0 ) {
489  cp1->pad(pad, fill_type, avg);
490  cp2->pad(pad, fill_type, p->average());
491  }
492  pc = cp1->resolution_prepare(cp2, plan);
493  delete cp1;
494  delete cp2;
495  plot = pc->fsc_dpr(hi_res, sampling_ratio);
496  delete pc;
497  for ( j=0; j<4; ++j ) {
498  if ( cutoff[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);
502  }
503  }
504  if ( verbose )
505  cout << r << tab << res_est << endl;
506  delete plot;
507  }
508 
509  fft_destroy_plan(plan);
510 
511  return pr;
512 }
513 
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)
516 {
517  long nn(0), xx, yy, zz, cc;
518  int fill_type(FILL_AVERAGE); // Fill type for resizing
519  double sampling_ratio(1); // Sampling ratio in reciprocal space
520 
521  double width = size/4.0;
522  Vector3<long> boxsize(size,size,size); // Resolution box
523  Vector3<long> boxhalf(boxsize/2); // Half resolution box
524 
525  Vector3<long> taper_size(boxhalf);
526  Vector3<double> taper_start(boxsize/4);
527 
528  coordinates(i, cc, xx, yy, zz, nn);
529 
530  Vector3<double> start = Vector3<double>(xx - boxhalf[0], yy - boxhalf[1], zz - boxhalf[2]);
531 
532  Bimage* cp1 = extract(nn, start, boxsize);
533  Bimage* cp2 = p->extract(nn, start, boxsize);
534 
535  if ( taper == 2 ) {
536  cp1->hanning_taper(cp1->average());
537  cp2->hanning_taper(cp2->average());
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());
541  }
542 
543  if ( pad > 0 ) {
544  cp1->pad(pad, fill_type, cp1->average());
545  cp2->pad(pad, fill_type, cp2->average());
546  }
547 
548  Bimage* pc = cp1->resolution_prepare(cp2, plan);
549  delete cp1;
550  delete cp2;
551 
552  Bplot* plot = pc->fsc_dpr(hi_res, sampling_ratio);
553  delete pc;
554 
555  double* res_est = new double[4];
556  for ( long j=0; j<4; ++j ) {
557  res_est[j] = 0;
558  if ( cutoff[j] ) {
559  res_est[j] = 1/plot->cut(1, cutoff[j], -1);
560  if ( res_est[j] < hi_res ) res_est[j] = hi_res;
561  if ( res_est[j] > LORESLIM ) res_est[j] = LORESLIM;
562  }
563  }
564 
565  delete plot;
566 
567  return res_est;
568 }
569 
592 Bimage* Bimage::fsc_local(Bimage* p, Bimage* pmask, double hi_res, double* cutoff,
593  int mask_level, int size, int pad, Vector3<long> vedge,
594  int step, int taper, double fill)
595 {
596  int dovox;
597  long xx, yy, zz, i, nmap(0), nvox(0);
598  long ft_size(size), imgsize(image_size());
599 
600  if ( vedge[0] < 0 ) vedge = Vector3<long>(size/2, size/2 ,size/2);
601 
602  if ( pad > 0 ) {
603  pad = size*(pad+1);
604  ft_size = pad;
605  }
606 
607  // Count the number of output maps
608  for ( i=0; i<4; ++i ) if ( cutoff[i] > 0 ) nmap++;
609 
610  if ( verbose ) {
611  cout << "Calculating local resolution:" << endl;
612  if ( pmask )
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;
622  cout << endl ;
623  }
624 
625  change_type(Float) ;
626  statistics();
627  p->change_type(Float) ;
628  p->statistics();
629  p->rescale_to_avg_std(avg, std);
630 
631  Bimage* pr = copy_header(nmap);
632  pr->data_alloc();
633  pr->fill(fill);
634  pr->background(fill);
635 
636  int nomask(0);
637  if ( !pmask ) {
638  nomask = 1;
639  pmask = copy_header();
640  pmask->data_type(UCharacter);
641  pmask->data_alloc();
642  pmask->fill(1);
643  }
644 
645  // Set up the mask for which voxels to calculate
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++ ) {
649  dovox = 1;
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);
657  }
658  }
659  }
660 
661 // write_img("locres_mask.map", pmask);
662 
663  fft_plan plan = fft_setup_plan(ft_size, ft_size, ft_size, FFTW_FORWARD, 1);
664 
665  if ( verbose ) {
666  for ( i=nvox=0; i<datasize; i++ ) if ( (*pmask)[i] ) nvox++;
667  cout << "Boxes to calculate: " << nvox << endl << endl;
668  }
669 
670 #ifdef HAVE_GCD
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){
674  if ( (*pmask)[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]);
678  delete[] res_est;
679  if ( verbose )
680  dispatch_sync(myq, ^{
681  ndone++;
682  cerr << "Complete: " << setprecision(3)
683  << ndone*100.0/nvox << " % \r" << flush;
684  });
685  }
686  });
687 #else
688  long ndone(0);
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]);
694  delete[] res_est;
695  #pragma omp critical
696  {
697  ndone++;
698  if ( verbose )
699  cerr << "Complete: " << setprecision(3)
700  << ndone*100.0/nvox << " % \r" << flush;
701  }
702  }
703 #endif
704 
705  fft_destroy_plan(plan);
706 
707  for ( i=nvox=0; i<imgsize; i++ ) if ( fabs((*pr)[i] - fill) > 1e-10 ) nvox++;
708 
709  if ( verbose ) {
710  cerr << endl;
711  cout << "Boxes calculated: " << nvox << endl << endl;
712  }
713 
714  if ( verbose & VERB_FULL ) {
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 ) {
719  pr->index(xx,yy,zz);
720  if ( (*pmask)[i] )
721  cout << xx << tab << yy << tab << zz << tab << (*pr)[i] << endl;
722  }
723  }
724  }
725  }
726 
727  if ( nomask ) {
728  delete pmask;
729  pmask = NULL;
730  }
731 
732  pr->statistics();
733 
734  return pr;
735 }
736 
737 double Bimage::local_filter_voxel(Bimage* resmap, long size,
738  fft_plan planf, fft_plan planb, long i)
739 {
740  long nn, xx, yy, zz, cc;
741  Bimage* cp = NULL;
742 
743  double res_hi(0); // High resolution limit
744  double res_lo(1e37); // Low resolution limit
745  double bandpass_width(0.002); // Edge width for the bandpass filter
746 
747  Vector3<long> boxsize(size,size,size); // Resolution box
748  Vector3<long> boxhalf(boxsize/2); // Half resolution box
749  Vector3<double> start; // Origin of kernel to extract
750  long cenvox = (boxsize[2]/2*boxsize[1] + boxsize[1]/2)*boxsize[0] + boxsize[0]/2;
751 
752  coordinates(i, cc, xx, yy, zz, nn);
753 
754  start = Vector3<double>(xx - boxhalf[0], yy - boxhalf[1], zz - boxhalf[2]);
755  res_hi = (*resmap)[i];
756  cp = extract(nn, start, boxsize);
757  cp->fspace_bandpass(res_hi, res_lo, bandpass_width, planf, planb);
758 
759  // read value of cp1 at the center of the box and assign to output
760  double val = (*cp)[cenvox];
761 
762  delete cp;
763 
764  return val;
765 }
766 
782 Bimage* Bimage::local_filter(Bimage* pmask, int mask_level, Bimage* resmap,
783  int size, Vector3<long> vedge)
784 {
785  if ( !resmap ) {
786  cerr << "Error: A local resolution map must be provided!" << endl;
787  return NULL;
788  }
789 
791 
792  if ( vedge[0] < 0 ) vedge = Vector3<long>(size/2, size/2 ,size/2);
793 
794  long xx, yy, zz, nn, i, nvox(0);
795  int dovox;
796 
797  if ( verbose ) {
798  cout << "Filtering by local resolution:" << endl;
799  cout << "Local resolution image: " << resmap->file_name() << endl;
800  if ( pmask )
801  cout << "Mask: " << pmask->file_name() << " (" << mask_level << ")" << endl;
802  cout << "Kernel size: " << size << endl;
803  cout << "Edge size: " << vedge << endl;
804  cout << endl;
805  }
806 
807  Bimage* p = copy_header();
808  p->data_alloc();
809 
810  int nomask(0);
811  if ( !pmask ) {
812  nomask = 1;
813  pmask = copy_header();
814  pmask->data_type(UCharacter);
815  pmask->data_alloc();
816  pmask->fill(1);
817  }
818 
819  // Set up the mask for which voxels to calculate
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++ ) {
823  dovox = 1;
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);
830  }
831  }
832  }
833 
834  for ( i=0; i<datasize; i++ ) if ( (*pmask)[i] ) nvox++;
835 
836  fft_plan planf = fft_setup_plan(size, size, size, FFTW_FORWARD, 1);
837  fft_plan planb = fft_setup_plan(size, size, size, FFTW_BACKWARD, 1);
838 
839 // write_img("locres_mask.map", pmask);
840 
841 #ifdef HAVE_GCD
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){
845  if ( (*pmask)[k] ) {
846  p->set(k, local_filter_voxel(resmap, size, planf, planb, k));
847  if ( verbose )
848  dispatch_sync(myq, ^{
849  ndone++;
850  cerr << "Complete: " << setprecision(3)
851  << ndone*100.0/nvox << " % \r" << flush;
852  });
853  }
854  });
855 #else
856  long ndone(0);
857 #pragma omp parallel for
858  for ( long j=0; j<datasize; j++ ) if ( (*pmask)[j] ) {
859  p->set(j, local_filter_voxel(resmap, size, planf, planb, j));
860  #pragma omp critical
861  {
862  ndone++;
863  if ( verbose )
864  cerr << "Complete: " << setprecision(3)
865  << ndone*100.0/nvox << " % \r" << flush;
866  }
867  }
868 #endif
869 
872 
873  if ( verbose & VERB_FULL ) {
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++ ) {
879  if ( (*pmask)[i] )
880  cout << nn << tab << xx << tab << yy << tab << zz << tab << (*p)[i] << endl;
881  }
882  }
883  }
884  }
885  }
886 
887  if ( nomask ) {
888  delete pmask;
889  pmask = NULL;
890  }
891 
892  p->statistics();
893 
894  return p;
895 }
Bimage::coordinates
void coordinates(long i, long &nx, long &ny, long &nz)
Definition: Bimage.h:429
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bimage::fspace_radial_power
Bimage * fspace_radial_power(double resolution, double sampling_ratio=1)
Calculates the radial power spectrum from a Fourier transform.
Definition: Bimage_fspace.cpp:610
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Baxis::flags
long flags()
Definition: Bplot.h:38
Bimage::label
string & label()
Definition: Bimage.h:251
Bimage::fsc_dpr
Bplot * fsc_dpr(double hi_res, double sampling_ratio=1, int flag=0)
Calculates an FSC and DPR curves from two images.
Definition: Bimage_resolution.cpp:92
UCharacter
@ UCharacter
Definition: utilities.h:153
Complex
Definition: Complex.h:23
Complex::real
Type real()
Definition: Complex.h:101
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bstring
Definition: Bstring.h:29
Bplot::rows
void rows(long n)
Definition: Bplot.h:212
Bcolumn::color
double * color()
Definition: Bplot.h:76
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::pack_two_in_complex
Bimage * pack_two_in_complex(Bimage *p)
Packs two real images into one complex image.
Definition: Bimage_complex.cpp:679
Bimage::radial
Bimage * radial(long minrad, long maxrad, double rad_step=1, int wrap=0)
Calculates the radial average of an image.
Definition: Bimage_polar.cpp:33
Bimage::background
double background(long nn)
Definition: Bimage.h:549
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bplot
Definition: Bplot.h:146
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
Bcolumn::type
long type()
Definition: Bplot.h:66
Bimage::thickness
Bimage * thickness(double reference, double lambda)
Calculates a thickness based on intensities with respect to a reference.
Definition: Bimage_rescale.cpp:920
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bcolumn::axis
long axis()
Definition: Bplot.h:68
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
planf
fft_plan planf
Definition: bico.cpp:24
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Bpage::title
Bstring & title()
Definition: Bplot.h:100
Bimage::local_filter
Bimage * local_filter(Bimage *pmask, int mask_level, Bimage *resmap, int size, Vector3< long > vedge)
Applies a local resolution filter to a map.
Definition: Bimage_resolution.cpp:782
Bimage::images
long images()
Definition: Bimage.h:504
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Bimage::file_name
string & file_name()
Definition: Bimage.h:246
Baxis::inc
double inc()
Definition: Bplot.h:34
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:46
Bimage::fft
int fft()
Definition: Bimage.h:830
Bimage::image_size
long image_size()
Definition: Bimage.h:290
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:380
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
Bimage::hanning_taper
int hanning_taper(double fill=0)
Apply Hanning taper window to the image.
Definition: Bimage_edit.cpp:388
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:271
Bplot::cut
double cut(long icol, double threshold, int dir=1)
Definition: Bplot.h:251
Bimage::resolution_prepare
Bimage * resolution_prepare(Bimage *p)
Definition: Bimage_resolution.cpp:17
Bimage::fsc_local
Bimage * fsc_local(Bimage *p, Bimage *pmask, double resolution, double *cutoff, int mask_level, int size, int pad, Vector3< long > vedge, int step=1, int taper=1, double fill=0)
Determine the local resolution at each masked voxel in a map.
Definition: Bimage_resolution.cpp:592
Bplot::title
Bstring & title()
Definition: Bplot.h:194
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
Complex::imag
Type imag()
Definition: Complex.h:102
Baxis::min
double min()
Definition: Bplot.h:30
Bimage.h
Header file for image class.
Bimage::plot
Bplot * plot()
Converts a one-dimensional image into a plot.
Definition: Bimage.cpp:3839
Complex::phi
double phi()
Definition: Complex.h:105
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Baxis::label
Bstring & label()
Definition: Bplot.h:28
verbose
int verbose
Bimage::fspace_bandpass
int fspace_bandpass(double res_hi, double res_lo=0, double width=0)
Applies a bandpass filter to an image.
Definition: Bimage_fspace.cpp:496
Bimage::fspace_maximum_radius
long fspace_maximum_radius(double resolution, double sampling_ratio=1)
Calculates the maximum radius in frequency space from a given resolution.
Definition: Bimage_fspace.cpp:26
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Complex::power
double power()
Definition: Complex.h:103
fft_setup_plan
fft_plan fft_setup_plan(long x, long y, long z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
Definition: fft.cpp:31
Bimage::extract_shell
Bimage * extract_shell(long nn, double minrad, double maxrad)
Extracts a shell from an image into a new image.
Definition: Bimage_extract.cpp:193
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::fsc
Bplot * fsc(double hi_res, double sampling_ratio=1)
Definition: Bimage_resolution.cpp:245
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
LORESLIM
#define LORESLIM
Definition: Bimage_resolution.cpp:12
Bimage::complex_conjugate_product
int complex_conjugate_product(Bimage *p)
Calculates the product of a complex image with the conjugate of a second.
Definition: Bimage_complex.cpp:460
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:658
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:207
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
Bimage::fill
void fill(double v)
Definition: Bimage.h:706
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::sampling
vector< Vector3< double > > sampling()
Definition: Bimage.h:529
Bimage::pad
int pad(long sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
Definition: Bimage_resize.cpp:347
planb
fft_plan planb
Definition: bico.cpp:24
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:216
Bimage::edge
int edge(int type, Vector3< long > rect, Vector3< double > start, double width, int fill_type=0, double fill=0)
Smooths the image edge with a soft rectangular function.
Definition: Bimage_edit.cpp:321
Bcolumn::number
long number()
Definition: Bplot.h:61
Float
@ Float
Definition: utilities.h:161
Baxis::max
double max()
Definition: Bplot.h:32
NoTransform
@ NoTransform
Definition: Bimage.h:41
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
Bimage::complex_to_real
void complex_to_real()
The real part of a complex image is written to a simple image.
Definition: Bimage_complex.cpp:50
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::fsc_shell
Bimage * fsc_shell(Bimage *p, double hi_res, double *cutoff, int thickness, int step, int minrad, int maxrad, int pad=1, int smooth=0, double fill=0)
Determine the resolution for each concentric shell in a map.
Definition: Bimage_resolution.cpp:415
Bimage::label
void label(string s)
Definition: Bimage.h:250
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163
Vector3::max
Type max()
Definition: Vector3.h:175