22 float* weight,
float* weight2,
double part_weight,
26 long xx, yy, zz, jx, jy, jz;
27 double w(part_weight);
31 float* fom = pmap->
FOM();
33 if ( interp_type < 2 ) {
35 (long) floor(m.
y() + 0.5),
36 (long) floor(m.
z() + 0.5));
37 if ( interp_type == 1 ) {
45 fom[j] += w*cv.
power();
49 }
else if ( interp_type == 2 ) {
54 for ( zz=0; zz<2; zz++ ) {
56 if ( jz < 0 ) jz += pmap->
sizeZ();
57 else if ( jz >= pmap->
sizeZ() ) jz -= pmap->
sizeZ();
59 for ( yy=0; yy<2; yy++ ) {
61 if ( jy < 0 ) jy += pmap->
sizeY();
62 else if ( jy >= pmap->
sizeY() ) jy -= pmap->
sizeY();
64 for ( xx=0; xx<2; xx++ ) {
66 if ( jx < 0 ) jx += pmap->
sizeX();
67 else if ( jx >= pmap->
sizeX() ) jx -= pmap->
sizeX();
69 w = d.
volume() * part_weight;
70 j = pmap->
index(jx, jy, jz);
72 fom[j] += w*cv.
power();
109 float* weight,
float* weight2,
Bsymmetry* sym,
double hi_res,
double scale,
110 int ft_size,
fft_plan plan,
double part_weight,
111 int interp_type,
CTFparam* em_ctf,
int ctf_action)
115 if ( hi_res < 2*pmap->sampling().x() ) hi_res = 2*pmap->
sampling().x();
125 double pad_ratio(ft_size*1.0L/p->
sizeX());
127 if ( pad_ratio > 1 ) {
128 pad_ratio *= pad_ratio;
129 part_weight /= pad_ratio;
136 cout <<
"DEBUG img_pack_2D_in_recip_space: Doing FFT " << ft_size <<
"x" << ft_size <<
" now" << endl;
142 cout <<
"DEBUG img_pack_2D_in_recip_space: FFT done" << endl;
146 if ( ctf_action )
img_ctf_apply(p, em_ctf, ctf_action, 0.2, 0, hi_res);
148 double max_rad_sq = pmap->
sampling().x()/hi_res;
149 if ( max_rad_sq > 0.5 ) max_rad_sq = 0.5;
150 max_rad_sq *= max_rad_sq;
153 long xx, yy, zz, jx, jy, jz;
155 size_t hx = (p->
sizeX() - 1)/2, hy = (p->
sizeY() - 1)/2;
161 vscale /= scale*ft_size;
162 if ( pmap->
sizeZ() < 2 ) vscale[2] = 1;
165 cout <<
"DEBUG img_pack_2D_in_recip_space: vscale = " << vscale << endl;
167 float* fom = pmap->
FOM();
170 cout <<
"Packing an image into reciprocal space up to " << hi_res <<
" A resolution" << endl << endl;
177 cout <<
"DEBUG img_pack_2D_in_recip_space: view = " << asu_view << endl;
179 for ( v=view; v; v=v->
next ) {
183 cout << mat << shift << endl;
190 for ( y=i=0; y<p->
sizeY(); y++ ) {
192 if ( y > hy ) iv[1] -= p->
sizeY();
193 for ( x=0; x<p->
sizeX(); x++, i++ ) {
195 if ( x > hx ) iv[0] -= p->
sizeX();
197 d2 = (m * invsize).length2();
198 if ( d2 < max_rad_sq ) {
200 weight, weight2, part_weight, interp_type, vscale);
224 error_show(
"Error in part_ft_size", __FILE__, __LINE__);
225 cerr <<
"Size not set!" << endl << endl;
229 int ft_size = (int) (xsize*scale);
231 if ( pad_factor < 1 )
return ft_size;
233 if ( pad_factor > 8 ) pad_factor = 8;
237 if ( ft_size < xsize )
263 float* fom = prec->
FOM();
265 float* weight2 = prec->
next->
FOM();
270 for ( i=0; i<datasize; i++ ) {
274 w = weight[i]*weight[i] - weight2[i];
276 data2 = (prec->
complex(i)).power();
277 fom[i] = (fom[i] - weight[i]*data2)/w;
278 fom[i] = data2/fom[i] - 1;
317 int maps_per_class = nthreads;
318 int iclass = imap/nmaps;
327 prec->
next = pweight;
329 size_t i, j, k, n(0);
330 size_t datasize = (size_t) prec->
size().volume();
337 size_t st(0), inc(1);
342 if ( nmaps == 3 && imap%3 ) {
347 for ( i=iclass*maps_per_class+st, j=st; j<maps_per_class; i+=inc, j+=inc ) {
349 fom_acc = pacc[i]->
FOM();
352 for ( k=0; k<datasize; k++ ) {
353 prec->
add(k, pacc[i]->complex(k));
354 fom[k] += fom_acc[k];
355 weight[k] += w_acc[k];
356 weight2[k] += w2_acc[k];
362 cout <<
"DEBUG img_reconstruction_weigh: volumes accumulated: " << n << endl;
364 weight[0] = fom[0] = 0;
366 size_t hist[11], cov(0);
367 for ( j=0; j<11; j++ ) hist[j] = 0;
368 for ( i=0; i<datasize; i++ ) {
372 data2 = (prec->
complex(i)).power();
375 if ( fom[i] < 0 ) fom[i] = 0;
376 cc = data2/(fom[i] + data2);
377 if ( cc > 0 ) cc = sqrt(cc);
384 j = (size_t) (10*cc + 0.5);
385 if ( j > 10 ) j = 10;
396 size_t vol = datasize;
400 cout <<
"Full reconstruction:" << endl;
402 cout <<
"Half reconstruction " << st+1 <<
":" << endl;
403 cout <<
"Friedel symmetry residual: " << Rf << endl;
404 cout <<
"Coverage:" << endl;
405 cout <<
"FOM\tNumber\t%" << endl;
406 for ( j=0; j<11; j++ ) cout <<
407 j/10.0 <<
tab << hist[j] <<
tab << hist[j]*100.0/vol << endl;
408 cout <<
"Total number of voxels: " << vol << endl;
409 cout <<
"Total coverage: " << cov << endl;
410 cout <<
"Percentage coverage: " << cov*100.0/vol <<
" %" << endl << endl;
415 if ( imap%nmaps == 0 )
443 if ( !p->
FOM() )
return -1;
446 long i, n, x, y, z, xx, yy, zz;
447 long maxrad, iradius, iradius2;
448 double radius, rad_scale, rx, ry, rz;
449 double fraction, fraction2, pwr, noise, uw;
453 rad_scale = realsize.x();
454 if ( rad_scale < realsize.y() ) rad_scale = realsize.y();
455 if ( rad_scale < realsize.z() ) rad_scale = realsize.z();
457 if ( maxrad < p->sizeY() ) maxrad = p->
sizeY();
458 if ( maxrad < p->sizeZ() ) maxrad = p->
sizeZ();
459 if ( resolution > 0.1 )
460 maxrad = (long) (rad_scale/resolution);
462 maxrad = (long) (maxrad/2.0);
465 cout <<
"Reconstruction statistics:" << endl;
466 cout <<
"Resolution limit: " << resolution <<
" A" << endl;
467 cout <<
"Maximum reciprocal radius: " << maxrad << endl;
468 cout <<
"Maximum FOM: " << p->
FOM_maximum() << endl;
472 float* fom = p->
FOM();
474 float* weight2 = (
float *) p->
next->
FOM();
476 float* s =
new float[maxrad];
477 float* num =
new float[maxrad];
478 float* w =
new float[maxrad];
479 float* f =
new float[maxrad];
480 float* a =
new float[maxrad];
481 float* np =
new float[maxrad];
482 float* cov =
new float[maxrad];
483 for ( i=0; i<maxrad; i++ ) s[i] = num[i] = w[i] = f[i] = a[i] = np[i] = cov[i] = 0;
485 for ( i=n=0; n<p->
images(); n++ ) {
486 for ( z=0; z<p->
sizeZ(); z++ ) {
488 if ( z > (p->
sizeZ() - 1)/2 ) zz -= p->
sizeZ();
489 rz = zz*freq_scale.
z();
490 for ( y=0; y<p->
sizeY(); y++ ) {
492 if ( y > (p->
sizeY() - 1)/2 ) yy -= p->
sizeY();
493 ry = yy*freq_scale.
y();
494 for ( x=0; x<p->
sizeX(); x++, i++ ) {
496 if ( x > (p->
sizeX() - 1)/2 ) xx -= p->
sizeX();
497 rx = xx*freq_scale.
x();
498 radius = rad_scale*sqrt(rx*rx + ry*ry + rz*rz);
499 iradius = (long) radius;
500 iradius2 = iradius + 1;
501 if ( iradius2 < maxrad ) {
502 fraction = radius - iradius;
503 fraction2 = 1.0 - fraction;
504 num[iradius] += fraction2;
505 num[iradius2] += fraction;
507 a[iradius] += fraction2 * pwr;
508 a[iradius2] += fraction * pwr;
509 f[iradius] += fraction2*fom[i];
510 f[iradius2] += fraction*fom[i];
511 w[iradius] += fraction2*weight[i];
512 w[iradius2] += fraction*weight[i];
515 noise = fom[i] - pwr;
522 uw = weight[i]*weight[i] - weight2[i];
524 noise = (fom[i] - pwr)*weight[i]/uw;
526 if ( noise < 0 ) noise = 0;
527 np[iradius] += fraction2*noise;
528 np[iradius2] += fraction*noise;
529 cov[iradius] += fraction2;
530 cov[iradius2] += fraction;
538 double res, fsc, fscp(1), snr, snrp(10), sc(0), res_fsc(0), res_snr(0);
539 double fsc_cut(0.3), snr_cut(0.5);
541 cout <<
"k\tn\tP\tS\tN\tR" << endl;
542 for ( i=1; i<maxrad; i++ )
if ( num[i] ) {
553 cout << i <<
tab << 2*i*w[i] <<
tab << f[i] <<
tab << a[i] <<
tab << np[i] <<
tab << np[i]/a[i] << endl;
559 cout <<
"Radius\ts(1/A)\tRes(A)\tnRefl\tCov\tWeight\tNoise\tFSC\tSNR" << endl;
560 for ( i=1; i<maxrad; i++ ) {
563 fsc = snr = noise = 0;
565 noise = 2*sqrt(a[i]*np[i]);
573 fsc = 1 - np[i]/a[i];
576 if ( fsc < 1 ) snr = fsc/(1 - fsc);
586 if ( fscp >= fsc_cut && fsc < fsc_cut ) {
587 sc = s[i] - (fsc_cut - fsc)/((fscp - fsc)*rad_scale);
590 if ( snrp >= snr_cut && snr < snr_cut ) {
591 sc = s[i] - (snr_cut - snr)/((snrp - snr)*rad_scale);
596 cout << i <<
tab << setprecision(4) << s[i] <<
tab
597 << setprecision(2) << res <<
tab << num[i] <<
tab
598 << setprecision(4) << cov[i] <<
tab
599 << setprecision(2) << w[i] <<
tab
600 << setprecision(6) << np[i] <<
tab
601 << setprecision(4) << fsc <<
tab << snr << endl;
604 cout <<
"Resolution estimates:" << endl;
605 cout <<
"FSC(0.3): " << res_fsc <<
" A" << endl;
606 cout <<
"SNR(0.5): " << res_snr <<
" A" << endl << endl;
630 int nmaps,
int nthreads)
633 cout <<
"DEBUG img_backprojection_accumulate:" << endl;
635 int maps_per_class = nthreads;
636 int iclass = imap/nmaps;
642 size_t datasize = (size_t) prec->
size().volume();
644 size_t st(0), inc(1);
649 if ( nmaps == 3 && imap%3 ) {
655 cout <<
"DEBUG img_backprojection_weigh: nmaps=" << nmaps <<
" imap=" << imap << endl;
656 cout <<
"DEBUG img_backprojection_weigh: maps_per_class=" << maps_per_class <<
" iclass=" << iclass << endl;
657 cout <<
"DEBUG img_backprojection_weigh: st=" << st <<
" inc=" << inc << endl;
660 for ( i=iclass*maps_per_class+st, j=st; j<maps_per_class; i+=inc, j+=inc ) {
662 for ( k=0; k<datasize; k++ )
670 cout <<
"DEBUG img_backprojection_weigh: Accumulation done" << endl;
void change_type(char letter)
Get the data type indicated by a single letter code.
void view(double x, double y, double z, double a)
size_t img_reconstruction_weigh(Bimage *prec)
Weighs a reconstruction.
int img_pack_2D_in_recip_space(Bimage *p, Bimage *pmap, float *weight, float *weight2, Bsymmetry *sym, double hi_res, double scale, int ft_size, fft_plan plan, double part_weight, int interp_type, CTFparam *em_ctf, int ctf_action)
Packs a 2D Fourier transform into a 3D reciprocal space volume.
int calculate_background(size_t nn)
Calculates the background for one sub-image.
Header file for general utilities.
CompoundType compound_type()
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Class for complex numbers.
int error_show(const char *message, const char *file, int line)
Displays the error with file and line reference.
size_t index(size_t nx, size_t ny) const
Vector3< size_t > size() const
size_t statistics()
Calculates the statistics for an image.
3x3 Matrix manipulation functions
General image parameter class.
Vector3< double > real_size()
unsigned char * data_alloc()
Header file for generalized linked list functions.
float * FOM_initialize()
Allocates and initializes the FOM block.
int phase_shift_to_center()
Phase shifts a set of reflections to the nominal center of the image origin.
Complex< double > complex(size_t j)
Returns a complex value at the given index.
int img_reconstruction_stats(Bimage *p, double resolution)
Calculates Fourier shell statistics.
int phase_shift_to_origin()
Phase shifts a set of reflections to the image origin.
void origin(double x, double y, double z)
void add(size_t j, double v)
void set(size_t j, double v)
Sets a single value at the given index.
size_t index_wrap(long nx, long ny, long nz) const
int write_img(const char *filename, Bimage *p)
General driver function to write multiple image formats This is the only image writing function that ...
Header file for CTF (contrast transfer function) functions.
unsigned char * data_pointer()
Header file for 2D and 3D reconstruction.
Bimage * img_reconstruction_sum_weigh(Bimage **pacc, int imap, int nmaps, int nthreads, double hi_res)
Combines and weighs a map from several partial maps and weight sets.
int pad(size_t sz, int fill_type=0, double fill=0)
Pads an image to a new size with a given fill value.
Bimage * img_backprojection_accumulate(Bimage **pacc, int imap, int nmaps, int nthreads)
Accumulates multiple backprojections.
double friedel_check()
Checks Friedel symmetry.
Header file for micrograph processing.
Header file for 2D and 3D image I/O.
int findNextPowerOf(int startNumber, int powerOf)
Finds the next greatest number that is a power of a given number.
int img_ctf_apply(Bimage *p, CTFparam *em_ctf, int action, double wiener, double lores, double hires)
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
ostream & tab(ostream &out)
int img_reconstruction_interpolation(Complex< float > cv, Bimage *pmap, Vector3< double > m, float *weight, float *weight2, double part_weight, int interp_type, Vector3< double > vscale)
void origin(double ox, double oy, double oz)
int part_ft_size(int xsize, double scale, int pad_factor)
Sets the Fourier transform size for 2D particle images for reconstruction.