| Bsoft 2.0.5 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
17 Bimage* Bimage::resolution_prepare(Bimage* p)
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
46 Bimage* Bimage::resolution_prepare(Bimage* p, fft_plan plan)
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> u(image->sampling());
101 Vector3<double> freq_scale(1/real_size());
102 double rad_scale = real_size()[0]/sampling_ratio;
103 if ( rad_scale < real_size()[1]/sampling_ratio ) rad_scale = real_size()[1]/sampling_ratio;
104 if ( rad_scale < real_size()[2]/sampling_ratio ) rad_scale = real_size()[2]/sampling_ratio;
105 if ( x > 1 && hi_res < 2*u[0] ) hi_res = 2*u[0];
106 if ( y > 1 && hi_res < 2*u[1] ) hi_res = 2*u[1];
107 if ( z > 1 && hi_res < 2*u[2] ) hi_res = 2*u[2];
108 if ( hi_res > real_size()[0]/2.0 ) hi_res = real_size()[0]/2.0;
109
110 long maxrad = (long) (2 + rad_scale/hi_res);
111
112 Complex<double> sf1, sf2;
113
114 if ( verbose & VERB_DEBUG )
115 cout << "DEBUG Bimage::fsc_dpr: FFT done" << endl;
116
117 long i, j, xx, yy, zz, ix, iy, iz, iradius, iradius2;
118 double radius, fraction, fraction2, I1, I2, Isum, F1F2re, dphi, Id2;
119 double rx, ry, rz;
120
121 if ( verbose > VERB_RESULT ) {
122 cout << "Resolution limit: " << hi_res << " A (" << maxrad << ")" << endl;
123 cout << "Sampling ratio: " << sampling_ratio << endl << endl;
124 }
125
126 double* F1 = new double[maxrad];
127 double* F2 = new double[maxrad];
128 double* Fsum = new double[maxrad];
129 double* FSC = new double[maxrad];
130 double* DPR = new double[maxrad];
131
132 for ( i=0; i<maxrad; i++ )
133 F1[i] = F2[i] = Fsum[i] = FSC[i] = DPR[i] = 0;
134
135 for ( zz=0; zz<z; zz++ ) {
136 rz = zz;
137 if ( rz > (z - 1)/2 ) rz -= z;
138 rz *= freq_scale[2];
139 iz = -zz;
140 if ( iz < 0 ) iz += z;
141 for ( yy=0; yy<y; yy++ ) {
142 ry = yy;
143 if ( ry > (y - 1)/2 ) ry -= y;
144 ry *= freq_scale[1];
145 iy = -yy;
146 if ( iy < 0 ) iy += y;
147 for ( xx=0; xx<x/2; xx++ ) {
148 i = (zz*y + yy)*x + xx;
149 rx = xx;
150 if ( xx > (x - 1)/2 ) rx -= x;
151 rx *= freq_scale[0];
152 ix = -xx;
153 if ( ix < 0 ) ix += x;
154 radius = rad_scale*sqrt(rx*rx + ry*ry + rz*rz);
155 iradius = (long) radius;
156 iradius2 = iradius + 1;
157 if ( iradius2 < maxrad ) {
158 fraction = radius - iradius;
159 fraction2 = 1.0 - fraction;
160 j = (iz*y + iy)*x + ix;
161 sf1 = complex(i).unpack_first(complex(j));
162 sf2 = complex(i).unpack_second(complex(j));
163 I1 = sf1.power();
164 I2 = sf2.power();
165 dphi = angle_set_negPI_to_PI(sf1.phi() - sf2.phi());
166 dphi *= dphi;
167 F1[iradius] += fraction2*I1;
168 F1[iradius2] += fraction*I1;
169 F2[iradius] += fraction2*I2;
170 F2[iradius2] += fraction*I2;
171 F1F2re = sf1.real()*sf2.real() + sf1.imag()*sf2.imag();
172 FSC[iradius] += fraction2*F1F2re;
173 FSC[iradius2] += fraction*F1F2re;
174 Isum = I1 + I2;
175 Fsum[iradius] += fraction2*Isum;
176 Fsum[iradius2] += fraction*Isum;
177 Id2 = Isum*dphi;
178 DPR[iradius] += fraction2*Id2;
179 DPR[iradius2] += fraction*Id2;
180 }
181 }
182 }
183 }
184
185 int ncol(3);
186 if ( flag ) ncol = 2;
187
188 Bstring title("Resolution"), txt;
189 Bplot* plot = new Bplot(1, maxrad, ncol);
190
191 plot->title(title);
192 plot->page(0).title(label());
193 plot->page(0).columns(ncol);
194 for ( i=0; i<ncol; i++ ) plot->page(0).column(i).number(i);
195 plot->page(0).column(0).label("s");
196 plot->page(0).column(0).axis(1);
197
198 if ( z > 1 ) plot->page(0).column(1).label("FSC");
199 else plot->page(0).column(1).label("FRC");
200 plot->page(0).column(1).type(2);
201 plot->page(0).column(1).axis(3);
202 plot->page(0).column(1).color(1,0,0);
203
204 plot->page(0).axis(1).min(0);
205 plot->page(0).axis(1).max(1/hi_res);
206 plot->page(0).axis(1).inc(0.1/hi_res);
207 plot->page(0).axis(1).flags(1);
208 plot->page(0).axis(1).label("Resolution (A)");
209 plot->page(0).axis(3).min(0);
210 plot->page(0).axis(3).max(1);
211 plot->page(0).axis(3).inc(0.1);
212 if ( z > 1 ) plot->page(0).axis(3).label("FSC");
213 else plot->page(0).axis(3).label("FRC");
214
215 if ( ncol > 2 ) {
216 plot->page(0).column(2).label("DPR");
217 plot->page(0).column(2).type(2);
218 plot->page(0).column(2).axis(4);
219 plot->page(0).column(2).color(0,0,1);
220
221 plot->page(0).axis(4).min(0);
222 plot->page(0).axis(4).max(120);
223 plot->page(0).axis(4).inc(10);
224 plot->page(0).axis(4).label("DPR");
225 }
226
227 (*plot)[0] = 0;
228 (*plot)[maxrad] = 1;
229 for ( i=1; i<maxrad; i++ ) {
230 // cout << i << tab << F1[i] << tab << F2[i] << tab << FSC[i] << tab << endl;
231 if ( F1[i]*F2[i] > SMALLFLOAT ) FSC[i] /= sqrt(F1[i]*F2[i]);
232 if ( FSC[i] < SMALLFLOAT ) FSC[i] = 0;
233 if ( FSC[i] > 1 ) FSC[i] = 1;
234 if ( Fsum[i] > SMALLFLOAT ) DPR[i] = sqrt(DPR[i]/Fsum[i]);
235 else DPR[i] = 0;
236 (*plot)[i] = i/rad_scale;
237 (*plot)[maxrad+i] = FSC[i];
238 if ( ncol > 2 ) (*plot)[2*maxrad+i] = DPR[i]*180.0/M_PI;
239 }
240
241 delete[] F1;
242 delete[] F2;
243 delete[] Fsum;
244 delete[] FSC;
245 delete[] DPR;
246
247 return plot;
248 }
249
250 Bplot* Bimage::fsc(double hi_res, double sampling_ratio)
251 {
252 if ( verbose ) {
253 cout << "Determining resolution for " << n << " images" << endl;
254 cout << "Resolution limit: " << hi_res << endl;
255 cout << "Sampling ratio: " << sampling_ratio << endl << endl;
256 }
257
258 long i, j, nn, ncol(n+1);
259 double r, fsccut[4] = {0.143, 0.3, 0.5, 0.9};
260 Bstring title("Resolution"), txt;
261 Bimage* p1 = NULL;
262 Bplot* plot = NULL;
263 Bplot* plot1 = NULL;
264
265 if ( verbose ) {
266 cout << "Image";
267 for ( i=0; i<4; i++ ) if ( fsccut[i] ) cout << tab << "FSC(" << fsccut[i] << ")";
268 cout << endl;
269 }
270
271 for ( nn=0; nn<n; nn++ ) {
272
273 p1 = extract(nn);
274 plot1 = p1->fsc_dpr(hi_res, sampling_ratio);
275 delete p1;
276
277 if ( !plot ) {
278 plot = new Bplot(n, plot1->rows(), ncol);
279 plot->title(title);
280 for ( i=0; i<plot->rows(); i++ )
281 (*plot)[i] = (*plot1)[i];
282 }
283
284 for ( i=plot->rows(), j=(nn+1)*plot->rows(); i<2*plot->rows(); i++, j++ )
285 (*plot)[j] = (*plot1)[i];
286
287 delete plot1;
288
289 // title = label() + Bstring(nn+1, " image %d");
290 title = label() + " image " + to_string(nn+1);
291 plot->page(nn).title(title);
292 plot->page(nn).columns(2);
293 plot->page(nn).column(0).number(0);
294 plot->page(nn).column(1).number(nn+1);
295 plot->page(nn).column(0).label("s");
296 plot->page(nn).column(0).axis(1);
297 if ( z > 1 ) plot->page(nn).column(1).label("FSC");
298 else plot->page(nn).column(1).label("FRC");
299 plot->page(nn).column(1).type(2);
300 plot->page(nn).column(1).axis(3);
301 plot->page(nn).column(1).color(0,0,0);
302 plot->page(nn).axis(1).min(0);
303 plot->page(nn).axis(1).max(1/hi_res);
304 plot->page(nn).axis(1).inc(0.1/hi_res);
305 plot->page(nn).axis(1).flags(1);
306 plot->page(nn).axis(1).label("Resolution (A)");
307 plot->page(nn).axis(3).min(0);
308 plot->page(nn).axis(3).max(1);
309 plot->page(nn).axis(3).inc(0.1);
310 if ( z > 1 ) plot->page(nn).axis(3).label("FSC");
311 else plot->page(nn).axis(3).label("FRC");
312
313 if ( verbose )
314 cout << nn+1;
315 for ( i=0; i<4; i++ ) if ( fsccut[i] ) {
316 r = 1/plot->cut(nn+1,fsccut[i],-1);
317 txt = plot->page(nn).column(1).label() + Bstring(fsccut[i], "(%g): ")
318 + Bstring(r, "%g A");
319 plot->page(nn).add_text(txt);
320 if ( verbose )
321 cout << tab << r;
322 }
323 if ( verbose )
324 cout << endl;
325 }
326
327 return plot;
328 }
329
330 Bplot* Bimage::fsc(Bimage* p, double hi_res, double sampling_ratio)
331 {
332 if ( verbose ) {
333 cout << "Determining resolution for " << n << " images" << endl;
334 cout << "Resolution limit: " << hi_res << endl;
335 cout << "Sampling ratio: " << sampling_ratio << endl << endl;
336 }
337
338 long i, j, nn, ncol(n+1);
339 // double r, fsccut[4] = {0.143, 0.3, 0.5, 0.9};
340 double rad_scale = real_size()[0]/sampling_ratio;
341 long maxrad = fspace_maximum_radius(hi_res);
342 Bstring title("Resolution"), txt;
343 Bimage* p1 = NULL;
344 Bimage* p2 = NULL;
345 Bimage* pr1 = NULL;
346 Bimage* pr2 = NULL;
347 Bplot* plot = NULL;
348
349 for ( nn=0; nn<n; nn++ ) {
350
351 p1 = extract(nn);
352 p2 = p->extract(nn);
353 pr1 = p1->fspace_radial_power(hi_res);
354 pr2 = p2->fspace_radial_power(hi_res);
355 p1->complex_conjugate_product(p2);
356 p1->complex_to_real();
357 delete p2;
358 p1->origin(0,0,0);
359 p2 = p1->radial(0, maxrad, 1, 1);
360 p2->set(0, 1);
361 for ( i=1; i<p2->sizeX(); ++i ) p2->set(i, (*p2)[i] / sqrt((*pr1)[i] * (*pr2)[i]));
362 delete p1;
363 delete pr1;
364 delete pr2;
365
366 if ( !plot ) {
367 plot = new Bplot(n, p2->sizeX(), ncol);
368 plot->title(title);
369 for ( i=0; i<plot->rows(); ++i )
370 (*plot)[i] = i/rad_scale;
371 }
372
373 for ( i=0, j=(nn+1)*plot->rows(); i<plot->rows(); ++i, ++j )
374 if ( (*p2)[i] > 0 ) (*plot)[j] = (*p2)[i];
375
376 delete p2;
377
378 // title = label() + Bstring(nn+1, " image %d");
379 title = "Image: " + to_string(nn+1);
380 plot->page(nn).title(title);
381 plot->page(nn).columns(2);
382 plot->page(nn).column(0).number(0);
383 plot->page(nn).column(1).number(nn+1);
384 plot->page(nn).column(0).label("s");
385 plot->page(nn).column(0).axis(1);
386 if ( z > 1 ) plot->page(nn).column(1).label("FSC");
387 else plot->page(nn).column(1).label("FRC");
388 plot->page(nn).column(1).type(2);
389 plot->page(nn).column(1).axis(3);
390 plot->page(nn).column(1).color(0,0,0);
391 plot->page(nn).axis(1).min(0);
392 plot->page(nn).axis(1).max(1/hi_res);
393 plot->page(nn).axis(1).inc(0.1/hi_res);
394 plot->page(nn).axis(1).flags(1);
395 plot->page(nn).axis(1).label("Resolution (A)");
396 plot->page(nn).axis(3).min(0);
397 plot->page(nn).axis(3).max(1);
398 plot->page(nn).axis(3).inc(0.1);
399 if ( z > 1 ) plot->page(nn).axis(3).label("FSC");
400 else plot->page(nn).axis(3).label("FRC");
401 }
402
403 return plot;
404 }
405
420 Bimage* Bimage::fsc_shell(Bimage* p, double hi_res, double* cutoff,
421 int thickness, int step, int minrad, int maxrad,
422 int pad, int smooth, double fill)
423 {
424 long i, j, r;
425 double r1, r2;
426 int fill_type(FILL_AVERAGE); // Fill type for resizing
427 double sampling_ratio(1); // Sampling ratio in reciprocal space
428
429 Bimage* cp1 = NULL;
430 Bimage* cp2 = NULL;
431 Bplot* plot;
432 long ft_size(x), nmap(0), imgsize(image_size());
433 double res_est; // Resolution estimate
434
435 if ( pad > 0 ) {
436 pad = x*(pad+1);
437 ft_size = pad;
438 }
439
440 if ( minrad < 0 ) minrad = 0;
441 if ( maxrad > x/2 ) maxrad = x/2;
442 if ( maxrad < 1 ) maxrad = x/2;
443
444 // Count the number of output maps
445 for ( i=0; i<4; ++i ) if ( cutoff[i] > 0 ) nmap++;
446
447 if ( verbose ) {
448 cout << "Calculating shell resolution:" << endl;
449 cout << "Shell thickness: " << thickness << endl;
450 cout << "Step size: " << step << endl;
451 cout << "Radii: " << minrad << " " << maxrad << endl;
452 cout << "Padding size: " << pad << endl;
453 cout << "Smoothing: " << smooth << endl;
454 cout << "Cutoff(s): ";
455 for ( i=0; i<nmap-1; ++i ) cout << "," << cutoff[i];
456 cout << cutoff[i] << endl;
457 cout << endl;
458 }
459
460 change_type(Float) ;
461 statistics();
462 p->change_type(Float) ;
463 p->statistics();
464 p->rescale_to_avg_std(avg, std);
465
466 Bimage* pr = copy_header(nmap);
467 pr->data_type(Float);
468 pr->size((maxrad - minrad)/step + 1, 1, 1);
469 pr->sampling(image->sampling()[0]*step, 1, 1);
470 pr->image->origin(-minrad/step, 0, 0);
471
472 pr->data_alloc();
473
474 pr->fill(fill);
475
476 fft_plan plan = fft_setup_plan(ft_size, ft_size, ft_size, FFTW_FORWARD, 1);
477
478 Bimage* pc;
479
480 if ( verbose )
481 cout << "Radius\tResolution" << endl;
482
483 for ( r=minrad, i=0; r<=maxrad; r+=step, i++ ) {
484 r1 = r - thickness/2.0;
485 r2 = r + thickness/2.0;
486 if ( r1 < 0 ) r1 = 0;
487 cp1 = extract_shell(0, r1, r2);
488 cp2 = p->extract_shell(0, r1, r2);
489 if ( smooth ) {
490 cp1->filter_average(3);
491 cp2->filter_average(3);
492 }
493 if ( pad > 0 ) {
494 cp1->pad(pad, fill_type, avg);
495 cp2->pad(pad, fill_type, p->average());
496 }
497 pc = cp1->resolution_prepare(cp2, plan);
498 delete cp1;
499 delete cp2;
500 plot = pc->fsc_dpr(hi_res, sampling_ratio);
501 delete pc;
502 for ( j=0; j<4; ++j ) {
503 if ( cutoff[j] ) {
504 res_est = 1/plot->cut(1, cutoff[j], -1);
505 if ( res_est < hi_res ) res_est = hi_res;
506 if ( res_est < LORESLIM ) pr->set(i + j*imgsize, res_est);
507 }
508 }
509 if ( verbose )
510 cout << r << tab << res_est << endl;
511 delete plot;
512 }
513
514 fft_destroy_plan(plan);
515
516 return pr;
517 }
518
519 double* Bimage::fsc_local_voxel(Bimage* p, double hi_res, int size,
520 int pad, int taper, double* cutoff, fft_plan plan, long i)
521 {
522 long nn(0), xx, yy, zz, cc;
523 int fill_type(FILL_AVERAGE); // Fill type for resizing
524 double sampling_ratio(1); // Sampling ratio in reciprocal space
525
526 double width = size/4.0;
527 Vector3<long> boxsize(size,size,size); // Resolution box
528 Vector3<long> boxhalf(boxsize/2); // Half resolution box
529
530 Vector3<long> taper_size(boxhalf);
531 Vector3<double> taper_start(boxsize/4);
532
533 coordinates(i, cc, xx, yy, zz, nn);
534
535 Vector3<double> start = Vector3<double>(xx - boxhalf[0], yy - boxhalf[1], zz - boxhalf[2]);
536
537 Bimage* cp1 = extract(nn, start, boxsize);
538 Bimage* cp2 = p->extract(nn, start, boxsize);
539
540 if ( taper == 2 ) {
541 cp1->hanning_taper(cp1->average());
542 cp2->hanning_taper(cp2->average());
543 } else if ( taper == 1 ) {
544 cp1->edge(1, taper_size, taper_start, width, fill_type, cp1->average());
545 cp2->edge(1, taper_size, taper_start, width, fill_type, cp2->average());
546 }
547
548 if ( pad > 0 ) {
549 cp1->pad(pad, fill_type, cp1->average());
550 cp2->pad(pad, fill_type, cp2->average());
551 }
552
553 Bimage* pc = cp1->resolution_prepare(cp2, plan);
554 delete cp1;
555 delete cp2;
556
557 Bplot* plot = pc->fsc_dpr(hi_res, sampling_ratio);
558 delete pc;
559
560 double* res_est = new double[4];
561 for ( long j=0; j<4; ++j ) {
562 res_est[j] = 0;
563 if ( cutoff[j] ) {
564 res_est[j] = 1/plot->cut(1, cutoff[j], -1);
565 if ( res_est[j] < hi_res ) res_est[j] = hi_res;
566 if ( res_est[j] > LORESLIM ) res_est[j] = LORESLIM;
567 }
568 }
569
570 delete plot;
571
572 return res_est;
573 }
574
597 Bimage* Bimage::fsc_local(Bimage* p, Bimage* pmask, double hi_res, double* cutoff,
598 int mask_level, int size, int pad, Vector3<long> vedge,
599 int step, int taper, double fill)
600 {
601 int dovox;
602 long xx, yy, zz, i, nmap(0), nvox(0);
603 long ft_size(size), imgsize(image_size());
604
605 if ( vedge[0] < 0 ) vedge = Vector3<long>(size/2, size/2 ,size/2);
606
607 if ( pad > 0 ) {
608 pad = size*(pad+1);
609 ft_size = pad;
610 }
611
612 // Count the number of output maps
613 for ( i=0; i<4; ++i ) if ( cutoff[i] > 0 ) nmap++;
614
615 if ( verbose ) {
616 cout << "Calculating local resolution:" << endl;
617 if ( pmask )
618 cout << "Mask: " << pmask->file_name() << " (" << mask_level << ")" << endl;
619 cout << "Kernel size: " << size << endl;
620 cout << "Step size: " << step << endl;
621 cout << "Edge size: " << vedge << endl;
622 cout << "Padding size: " << pad << endl;
623 cout << "Smoothing/tapering: " << taper << endl;
624 cout << "Cutoff(s): " << setprecision(3);
625 for ( i=0; i<nmap-1; ++i ) cout << cutoff[i] << ",";
626 cout << cutoff[i] << endl;
627 cout << endl ;
628 }
629
630 change_type(Float) ;
631 statistics();
632 p->change_type(Float) ;
633 p->statistics();
634 p->rescale_to_avg_std(avg, std);
635
636 Bimage* pr = copy_header(nmap);
637 pr->data_alloc();
638 pr->fill(fill);
639 pr->background(fill);
640
641 int nomask(0);
642 if ( !pmask ) {
643 nomask = 1;
644 pmask = copy_header();
645 pmask->data_type(UCharacter);
646 pmask->data_alloc();
647 pmask->fill(1);
648 }
649
650 // Set up the mask for which voxels to calculate
651 for ( i=zz=0; zz<pmask->sizeZ(); zz++ ) {
652 for ( yy=0; yy<pmask->sizeY(); yy++ ) {
653 for ( xx=0; xx<pmask->sizeX(); xx++, i++ ) {
654 dovox = 1;
655 if ( zz%step || yy%step || xx%step ) dovox = 0;
656 if ( (*pmask)[i] < 0.5 ) dovox = 0;
657 if ( zz < vedge[2] || zz > pmask->sizeZ()-vedge[2] ) dovox = 0;
658 if ( yy < vedge[1] || yy > pmask->sizeY()-vedge[1] ) dovox = 0;
659 if ( xx < vedge[0] || xx > pmask->sizeX()-vedge[0] ) dovox = 0;
660 if ( mask_level > 0 && fabs((*pmask)[i] - mask_level) > 0.1 ) dovox = 0;
661 pmask->set(i, dovox);
662 }
663 }
664 }
665
666 // write_img("locres_mask.map", pmask);
667
668 fft_plan plan = fft_setup_plan(ft_size, ft_size, ft_size, FFTW_FORWARD, 1);
669
670 if ( verbose ) {
671 for ( i=nvox=0; i<datasize; i++ ) if ( (*pmask)[i] ) nvox++;
672 cout << "Boxes to calculate: " << nvox << endl << endl;
673 }
674
675 #ifdef HAVE_GCD
676 __block long ndone(0);
677 dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
678 dispatch_apply(imgsize, dispatch_get_global_queue(0, 0), ^(size_t k){
679 if ( (*pmask)[k] ) {
680 double* res_est = fsc_local_voxel(p, hi_res,
681 size, pad, taper, cutoff, plan, k);
682 for ( long j=0; j<nmap; ++j ) pr->set(k + j*imgsize, res_est[j]);
683 delete[] res_est;
684 if ( verbose )
685 dispatch_sync(myq, ^{
686 ndone++;
687 cerr << "Complete: " << setprecision(3)
688 << ndone*100.0/nvox << " % \r" << flush;
689 });
690 }
691 });
692 #else
693 #pragma omp parallel for
694 long ndone(0);
695 for ( long k=0; k<imgsize; k++ ) if ( (*pmask)[k] ) {
696 double* res_est = fsc_local_voxel(p, hi_res,
697 size, pad, taper, cutoff, plan, k);
698 for ( long j=0; j<nmap; ++j ) pr->set(k + j*imgsize, res_est[j]);
699 delete[] res_est;
700 #pragma omp critical
701 {
702 ndone++;
703 if ( verbose )
704 cerr << "Complete: " << setprecision(3)
705 << ndone*100.0/nvox << " % \r" << flush;
706 }
707 }
708 #endif
709
710 fft_destroy_plan(plan);
711
712 for ( i=nvox=0; i<imgsize; i++ ) if ( fabs((*pr)[i] - fill) > 1e-10 ) nvox++;
713
714 if ( verbose ) {
715 cerr << endl;
716 cout << "Boxes calculated: " << nvox << endl << endl;
717 }
718
719 if ( verbose & VERB_FULL ) {
720 cout << "x\ty\tz\tResolution" << endl;
721 for ( zz=0; zz<pr->sizeZ(); zz+=step ) {
722 for ( yy=0; yy<p->sizeY(); yy+=step ) {
723 for ( xx=0; xx<p->sizeX(); xx+=step ) {
724 pr->index(xx,yy,zz);
725 if ( (*pmask)[i] )
726 cout << xx << tab << yy << tab << zz << tab << (*pr)[i] << endl;
727 }
728 }
729 }
730 }
731
732 if ( nomask ) {
733 delete pmask;
734 pmask = NULL;
735 }
736
737 pr->statistics();
738
739 return pr;
740 }
741
742 double Bimage::local_filter_voxel(Bimage* resmap, long size,
743 fft_plan planf, fft_plan planb, long i)
744 {
745 long nn, xx, yy, zz, cc;
746 Bimage* cp = NULL;
747
748 double res_hi(0); // High resolution limit
749 double res_lo(1e37); // Low resolution limit
750 double bandpass_width(0.002); // Edge width for the bandpass filter
751
752 Vector3<long> boxsize(size,size,size); // Resolution box
753 Vector3<long> boxhalf(boxsize/2); // Half resolution box
754 Vector3<double> start; // Origin of kernel to extract
755 long cenvox = (boxsize[2]/2*boxsize[1] + boxsize[1]/2)*boxsize[0] + boxsize[0]/2;
756
757 coordinates(i, cc, xx, yy, zz, nn);
758
759 start = Vector3<double>(xx - boxhalf[0], yy - boxhalf[1], zz - boxhalf[2]);
760 res_hi = (*resmap)[i];
761 cp = extract(nn, start, boxsize);
762 cp->fspace_bandpass(res_hi, res_lo, bandpass_width, planf, planb);
763
764 // read value of cp1 at the center of the box and assign to output
765 double val = (*cp)[cenvox];
766
767 delete cp;
768
769 return val;
770 }
771
787 Bimage* Bimage::local_filter(Bimage* pmask, int mask_level, Bimage* resmap,
788 int size, Vector3<long> vedge)
789 {
790 if ( !resmap ) {
791 cerr << "Error: A local resolution map must be provided!" << endl;
792 return NULL;
793 }
794
795 change_type(Float);
796
797 if ( vedge[0] < 0 ) vedge = Vector3<long>(size/2, size/2 ,size/2);
798
799 long xx, yy, zz, nn, i, nvox(0);
800 int dovox;
801
802 if ( verbose ) {
803 cout << "Filtering by local resolution:" << endl;
804 cout << "Local resolution image: " << resmap->file_name() << endl;
805 if ( pmask )
806 cout << "Mask: " << pmask->file_name() << " (" << mask_level << ")" << endl;
807 cout << "Kernel size: " << size << endl;
808 cout << "Edge size: " << vedge << endl;
809 cout << endl;
810 }
811
812 Bimage* p = copy_header();
813 p->data_alloc();
814
815 int nomask(0);
816 if ( !pmask ) {
817 nomask = 1;
818 pmask = copy_header();
819 pmask->data_type(UCharacter);
820 pmask->data_alloc();
821 pmask->fill(1);
822 }
823
824 // Set up the mask for which voxels to calculate
825 for ( i=zz=0; zz<pmask->sizeZ(); zz++ ) {
826 for ( yy=0; yy<pmask->sizeY(); yy++ ) {
827 for ( xx=0; xx<pmask->sizeX(); xx++, i++ ) {
828 dovox = 1;
829 if ( (*pmask)[i] < 0.99 ) dovox = 0;
830 if ( zz < vedge[2] || zz > pmask->sizeZ()-vedge[2] ) dovox = 0;
831 if ( yy < vedge[1] || yy > pmask->sizeY()-vedge[1] ) dovox = 0;
832 if ( xx < vedge[0] || xx > pmask->sizeX()-vedge[0] ) dovox = 0;
833 if ( mask_level > 0 && fabs((*pmask)[i] - mask_level) > 0.1 ) dovox = 0;
834 pmask->set(i, dovox);
835 }
836 }
837 }
838
839 fft_plan planf = fft_setup_plan(size, size, size, FFTW_FORWARD, 1);
840 fft_plan planb = fft_setup_plan(size, size, size, FFTW_BACKWARD, 1);
841
842 // write_img("locres_mask.map", pmask);
843
844 #ifdef HAVE_GCD
845 dispatch_apply(datasize, dispatch_get_global_queue(0, 0), ^(size_t k){
846 if ( (*pmask)[k] )
847 p->set(k, local_filter_voxel(resmap, size, planf, planb, k));
848 });
849 #else
850 #pragma omp parallel for
851 for ( long j=0; j<datasize; j++ ) if ( (*pmask)[j] )
852 p->set(j, local_filter_voxel(resmap, size, planf, planb, j));
853 #endif
854
855 fft_destroy_plan(planf);
856 fft_destroy_plan(planb);
857
858 for ( i=0; i<datasize; i++ ) if ( (*pmask)[i] ) nvox++;
859
860 if ( verbose & VERB_FULL ) {
861 cout << "ImgNum\tx\ty\tz\tResolution" << endl;
862 for ( i=nn=0; nn<p->images(); nn++ ) {
863 for ( zz=0; zz<p->sizeZ(); zz++ ) {
864 for ( yy=0; yy<p->sizeY(); yy++ ) {
865 for ( xx=0; xx<p->sizeX(); xx++, i++ ) {
866 if ( (*pmask)[i] )
867 cout << nn << tab << xx << tab << yy << tab << zz << tab << (*p)[i] << endl;
868 }
869 }
870 }
871 }
872 }
873
874 if ( nomask ) {
875 delete pmask;
876 pmask = NULL;
877 }
878
879 p->statistics();
880
881 return p;
882 }
Baxis::inc
double inc()
Definition: Bplot.h:34
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
LORESLIM
#define LORESLIM
Definition: Bimage_resolution.cpp:12
Baxis::label
Bstring & label()
Definition: Bplot.h:28
Bplot::cut
double cut(long icol, double threshold, int dir=1)
Definition: Bplot.h:246
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:650
Bimage.h
Header file for image class.
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
Baxis::min
double min()
Definition: Bplot.h:30
planb
fft_plan planb
Definition: bico.cpp:24
Bimage::file_name
string & file_name()
Definition: Bimage.h:231
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:499
Bplot::title
Bstring & title()
Definition: Bplot.h:189
Bimage::image_size
long image_size()
Definition: Bimage.h:267
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:45
Bimage::fft
int fft()
Definition: Bimage.h:822
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Bcolumn::label
Bstring & label()
Definition: Bplot.h:63
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:594
planf
fft_plan planf
Definition: bico.cpp:24
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:319
Bimage::resolution_prepare
Bimage * resolution_prepare(Bimage *p)
Definition: Bimage_resolution.cpp:17
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:383
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:173
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1005
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:230
Bcolumn::color
double * color()
Definition: Bplot.h:76
verbose
int verbose
Bpage::column
Bcolumn & column(long i)
Definition: Bplot.h:110
Baxis::max
double max()
Definition: Bplot.h:32
Float
Definition: utilities.h:157
Bimage::fsc
Bplot * fsc(double hi_res, double sampling_ratio=1)
Definition: Bimage_resolution.cpp:250
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:487
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:256
Bimage::fspace_maximum_radius
long fspace_maximum_radius(double resolution)
Calculates the maximum radius in frequency space from a given resolution.
Definition: Bimage_fspace.cpp:25
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
M_PI
#define M_PI
Definition: utilities.h:59
Bimage::extract
Bimage * extract(long nn)
Extracts one sub-image into new image structure.
Definition: Bimage_extract.cpp:21
Bimage::fspace_radial_power
Bimage * fspace_radial_power(double resolution)
Calculates the radial power spectrum from a Fourier transform.
Definition: Bimage_fspace.cpp:612
Bimage::filter_average
int filter_average(long kernel_size)
Applies an averaging filter to an image.
Definition: Bimage_filter.cpp:208
Bcolumn::number
long number()
Definition: Bplot.h:61
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:420
Bimage::label
void label(string s)
Definition: Bimage.h:235
Bcolumn::type
long type()
Definition: Bplot.h:66
fft_plan
fftwf_plan fft_plan
Definition: fft.h:16
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:787
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
Bpage::axis
Baxis & axis(long id)
Definition: Bplot.h:117
Bpage::title
Bstring & title()
Definition: Bplot.h:100
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:373
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:127
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:123
Bimage::hanning_taper
int hanning_taper(double fill=0)
Apply Hanning taper window to the image.
Definition: Bimage_edit.cpp:386
Bimage::label
string & label()
Definition: Bimage.h:236
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
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1096
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:105
Bpage::add_text
void add_text(Bstring &s)
Definition: Bplot.h:122
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
Complex
Definition: Complex.h:23
Baxis::flags
long flags()
Definition: Bplot.h:38
UCharacter
Definition: utilities.h:149
Bstring
Definition: Bstring.h:29
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:351
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
Bplot::rows
void rows(long n)
Definition: Bplot.h:207
Bplot::page
Bpage & page(long i)
Definition: Bplot.h:211
Complex::phi
double phi()
Definition: Complex.h:105
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
Vector3< double >
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Complex::imag
Type imag()
Definition: Complex.h:102
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
Bpage::columns
void columns(long ncol)
Definition: Bplot.h:105
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:1349
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:597
Complex::power
double power()
Definition: Complex.h:103
Complex::real
Type real()
Definition: Complex.h:101
Bimage::images
long images()
Definition: Bimage.h:491
Bimage::coordinates
void coordinates(long i, long &nx, long &ny, long &nz)
Definition: Bimage.h:416
Bimage::plot
Bplot * plot()
Converts a one-dimensional image into a plot.
Definition: Bimage.cpp:3836
NoTransform
Definition: Bimage.h:40
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::fill
void fill(double v)
Definition: Bimage.h:698
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
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
Bimage::background
double background(long nn)
Definition: Bimage.h:542
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
Bimage::fourier_type
FourierType fourier_type()
Definition: Bimage.h:367
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
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:197
Bimage::image
Bsub_image * image
Definition: Bimage.h:188
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
Bcolumn::axis
long axis()
Definition: Bplot.h:68
Bplot
Definition: Bplot.h:146