29 double shift_step,
double shift_accuracy,
double hi_res,
double lo_res,
30 int fom_type, vector<double> weight,
double edge_radius,
31 CTFparam* em_ctf,
double def_std,
double shift_std,
32 double view_std,
double max_angle,
double max_mag,
int flags,
35 int max_iter,
int fom_type, vector<double>& weight,
CTFparam* em_ctf,
36 double def_std,
double shift_std,
double view_std,
39 double alpha_step,
double accuracy,
double shift_step,
40 double shift_accuracy,
double max_mag,
79 double alpha_step,
double accuracy,
double shift_step,
80 double shift_accuracy,
int fom_type, vector<double> weight,
81 double hi_res,
double lo_res,
int kernel_width,
int kernel_power,
82 double edge_radius,
double def_std,
double shift_std,
83 double view_std,
double max_angle,
double max_mag,
int flags)
85 if ( reffile.
length() < 1 ) {
86 cerr <<
"Error: A reference map must be provided!" << endl << endl;
93 cerr <<
"Error: The angular step size is too small! (" << alpha_step*180.0/
M_PI <<
")" << endl;
95 cerr <<
"Error: The accuracy is too small! (" << accuracy*180.0/
M_PI <<
")" << endl;
98 if ( alpha_step < accuracy ) {
99 cerr <<
"Error: The angular step size is smaller than the accuracy! (" << alpha_step*180.0/
M_PI <<
" < " << accuracy*180.0/
M_PI <<
")" << endl;
106 if ( sym_string.
length() < 1 ) sym_string =
"C1";
110 long npart(0), npix, number(0);
118 cerr <<
"Error: The reference was not read!" << endl << endl;
124 if ( !weight.size() )
128 long ft_size = pref->
sizeX();
132 cout <<
"Fourier transforming reference map " << pref->
file_name() << endl;
137 int diam = (int) (2.2*pref->
real_size()[0]/hi_res + 0.5);
138 if ( diam%2 ) diam += 1;
141 if ( diam < pref->sizeX() )
162 cout <<
"Refining orientations:" << endl;
164 cout <<
"Maximum iterations: " << max_iter << endl;
165 cout <<
"Defocus standard deviation: " << def_std <<
" A" << endl;
166 cout <<
"Shift standard deviation: " << shift_std <<
" pixels" << endl;
167 cout <<
"View standard deviation: " << view_std << endl;
168 cout <<
"Maximum angle variation: " << max_angle*180.0/
M_PI <<
" degrees" << endl;
169 cout <<
"Maximum magnification variation: " << max_mag << endl;
171 cout <<
"Grid angular step and accuracy: " << alpha_step*180.0/
M_PI
172 <<
" " << accuracy*180.0/
M_PI <<
" degrees" << endl;
173 cout <<
"Grid shift step and accuracy: " << shift_step
174 <<
" " << shift_accuracy <<
" pixels" << endl;
175 cout <<
"Maximum magnification change: " << max_mag << endl;
177 cout <<
"Reference: " << pref->
file_name() << endl;
179 cout <<
"Reference origin: " << pref->
image->
origin()/scale << endl;
180 if ( maskfile.
length() ) cout <<
"Real space mask file: " << maskfile << endl;
181 cout <<
"Symmetry: " << sym_string << endl;
182 cout <<
"Resolution measurement type: ";
183 if ( fom_type < 1 ) cout <<
"FRC" << endl;
184 else cout <<
"DPR" << endl;
185 cout <<
"Resolution limits: " << hi_res <<
" - ";
186 if ( lo_res > 0 ) cout << lo_res <<
" A" << endl;
187 else cout <<
" inf A" << endl;
188 cout <<
"Micrograph\tPID\tMag\tOriX\tOriY\tViewX\tViewY\tViewZ\tAngle\tCC\tCV\tTime" << endl;
194 cout <<
"DEBUG mg_refine_orientations: origin after phase shift: " << pref->
image->
origin() << endl;
198 mkdir(
"ppx", O_CREAT );
202 time_t t = time(NULL);
205 for ( mg = field->
mg; mg; mg = mg->
next )
if ( mg->
part ) {
206 for ( part = mg->
part; part; part = part->
next ) {
207 if ( ( part_select<0 && part->sel ) || ( part->
sel==
part_select ) ) {
209 max_iter, alpha_step, accuracy, shift_step, shift_accuracy,
210 hi_res, lo_res, fom_type, weight, edge_radius,
211 mg->
ctf, def_std, shift_std, view_std, max_angle, max_mag,
216 setprecision(2) << part->
ori[0] <<
tab << part->
ori[1] <<
tab <<
217 setprecision(4) << part->
view[0] <<
tab <<
220 setprecision(4) << part->
fom[0] <<
tab << part->
fom[1] <<
tab <<
221 (long)(time(NULL) - t) << endl;
223 part->
fom[0] = part->
fom[1] = part->
sel = 0;
229 t = (time(NULL) - t);
232 long hr = ts/3600, min = (ts - 3600*hr)/60, sec = ts - 3600*hr - 60*min;
235 cout <<
"Number of comparisons: " << number << endl;
236 cout <<
"Comparisons per particle: " << number*1.0/npart << endl;
237 cout <<
"Time: " << ts <<
" s (" << hr <<
":" << min <<
":" << sec <<
")" << endl;
238 cout <<
"Time per particle: " << ts*1.0/npart <<
" s/particle" << endl;
239 cout <<
"Algorithm time: " << ts*1.0e6/(number*npix) <<
" us/pixel" << endl << endl;
253 double alpha_step,
double accuracy,
double shift_step,
254 double shift_accuracy,
int fom_type, vector<double> weight,
255 double hi_res,
double lo_res,
int kernel_width,
int kernel_power,
256 double edge_radius,
double def_std,
double shift_std,
257 double view_std,
double max_angle,
double max_mag,
int flags)
259 if ( reffile.
length() < 1 ) {
260 cerr <<
"Error: A reference map must be provided!" << endl << endl;
264 if ( max_iter < 1 ) {
267 cerr <<
"Error: The angular step size is too small! (" << alpha_step*180.0/
M_PI <<
")" << endl;
269 cerr <<
"Error: The accuracy is too small! (" << accuracy*180.0/
M_PI <<
")" << endl;
272 if ( alpha_step < accuracy ) {
273 cerr <<
"Error: The angular step size is smaller than the accuracy! (" << alpha_step*180.0/
M_PI <<
" < " << accuracy*180.0/
M_PI <<
")" << endl;
280 if ( sym_string.
length() < 1 ) sym_string =
"C1";
288 cerr <<
"Error: The reference was not read!" << endl << endl;
296 long ft_size = pref->
sizeX();
300 cout <<
"Fourier transforming reference map " << pref->
file_name() << endl;
305 int diam = (int) (2.2*pref->
real_size()[0]/hi_res + 0.5);
306 if ( diam%2 ) diam += 1;
309 if ( diam < pref->sizeX() )
319 if ( maskfile.
length() ) {
336 cout <<
"Refining orientations:" << endl;
338 cout <<
"Maximum iterations: " << max_iter << endl;
339 cout <<
"Shift standard deviation: " << shift_std <<
" pixels" << endl;
340 cout <<
"View standard deviation: " << view_std << endl;
341 cout <<
"Maximum angle variation: " << max_angle*180.0/
M_PI <<
" degrees" << endl;
342 cout <<
"Maximum magnification variation: " << max_mag << endl;
344 cout <<
"Grid step and accuracy: " << alpha_step*180.0/
M_PI
345 <<
" " << accuracy*180.0/
M_PI <<
" degrees" << endl;
346 cout <<
"Maximum magnification change: " << max_mag << endl;
348 cout <<
"Defocus standard deviation: " << def_std <<
" A" << endl;
349 cout <<
"Reference: " << pref->
file_name() << endl;
350 cout <<
"Reference origin: " << pref->
image->
origin()/scale << endl;
351 if ( maskfile.
length() ) cout <<
"Real space mask file: " << maskfile << endl;
352 cout <<
"Symmetry: " << sym_string << endl;
353 cout <<
"Resolution measurement type: ";
354 if ( fom_type < 1 ) cout <<
"FRC" << endl;
355 else cout <<
"DPR" << endl;
356 cout <<
"Resolution limits: " << hi_res <<
" - ";
357 if ( lo_res > 0 ) cout << lo_res <<
" A" << endl;
358 else cout <<
" inf A" << endl;
359 cout <<
"Weighting scheme: ";
360 if ( !weight.size() )
361 cout <<
"C curve" << endl;
363 cout <<
"Input curve" << endl;
364 cout <<
"Micrograph\tPID\tDef\tSamX\tSamY\tOriX\tOriY\tViewX\tViewY\tViewZ\tAngle\tCC\tCV\tTime" << endl;
369 if ( !weight.size() )
373 cout <<
"DEBUG project_refine_orientations: origin after phase shift: " << pref->
image->
origin() << endl;
377 mkdir(
"ppx", O_CREAT );
381 time_t t = time(NULL);
383 int* nar =
new int[npart];
386 dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
387 dispatch_apply(npart, dispatch_get_global_queue(0, 0), ^(
size_t i){
391 mg->
fpart, pref, pmask, sym,
392 max_iter, alpha_step, accuracy, shift_step, shift_accuracy,
393 hi_res, lo_res, fom_type, weight, edge_radius,
394 mg->
ctf, def_std, shift_std, view_std, max_angle, max_mag,
396 dispatch_sync(myq, ^{
398 cout << setw(15) << mg->
id <<
tab << part->
id <<
tab <<
399 setprecision(0) << part->
def <<
tab <<
401 setprecision(2) << part->
ori[0] <<
tab << part->
ori[1] <<
tab <<
402 setprecision(4) << part->
view[0] <<
tab <<
405 setprecision(4) << part->
fom[0] <<
tab << part->
fom[1] <<
tab <<
406 (
long)(time(NULL) - t) << endl;
410 #pragma omp parallel for
411 for (
long i=0; i<npart; i++ ) {
415 mg->
fpart, pref, pmask, sym,
416 max_iter, alpha_step, accuracy, shift_step, shift_accuracy,
417 hi_res, lo_res, fom_type, weight, edge_radius,
418 mg->
ctf, def_std, shift_std, view_std, max_angle, max_mag,
423 cout << setw(15) << mg->
id <<
tab << part->
id <<
tab <<
424 setprecision(0) << part->
def <<
tab <<
426 setprecision(2) << part->
ori[0] <<
tab << part->
ori[1] <<
tab <<
427 setprecision(4) << part->
view[0] <<
tab <<
430 setprecision(4) << part->
fom[0] <<
tab << part->
fom[1] <<
tab <<
431 (long)(time(NULL) - t) << endl;
436 for (
long i=0; i<npart; i++ ) number += nar[i];
438 t = (time(NULL) - t);
441 long hr = ts/3600, min = (ts - 3600*hr)/60, sec = ts - 3600*hr - 60*min;
444 cout <<
"Number of comparisons: " << number << endl;
445 cout <<
"Comparisons per particle: " << number*1.0/npart << endl;
446 cout <<
"Time: " << ts <<
" s (" << hr <<
":" << min <<
":" << sec <<
")" << endl;
447 cout <<
"Time per particle: " << ts*1.0/npart <<
" s/particle" << endl;
448 cout <<
"Algorithm time: " << ts*1.0e6/(number*npix) <<
" us/pixel" << endl << endl;
497 double shift_step,
double shift_accuracy,
double hi_res,
double lo_res,
498 int fom_type, vector<double> weight,
double edge_radius,
499 CTFparam* em_ctf,
double def_std,
double shift_std,
500 double view_std,
double max_angle,
double max_mag,
int flags,
506 if (
ppx_check(part, fom_tag) )
return 0;
509 cout <<
"DEBUG part_refine_orientation: file=" << partfile <<
" image=" << part->
id << endl;
513 return error_show(
"Error in part_refine_orientation", __FILE__, __LINE__);
519 if ( part->
ori[0] <= 0 ) {
523 if ( part->
ori[1] <= 0 ) {
530 if ( part->
mag < 0.5 ) {
536 part->
fom[0] = part->
fom[1] = 0;
542 int edge_size = (int) (2*edge_radius);
583 double fom, bestmag = part->
mag;
591 for ( i=1, v = view; v; v = v->
next, i++ ) {
597 cout <<
"DEBUG part_refine_orientation: view=" << *v << endl;
606 weight, ctf_copy, def_std,
607 shift_std, view_std, max_angle, max_mag,
kernel, number);
610 shift_step, shift_accuracy, max_mag,
611 fom_type, weight, ctf_copy, def_std,
kernel, number);
613 if ( part->
fom[0] < fom ) {
622 if ( bestdef < 1000 ) {
623 cerr <<
"Bad defocus! pid = " << part->
id <<
tab << bestdef << endl;
629 if ( ctf_copy )
delete ctf_copy;
632 if ( bestdef > 1 ) part->
def = bestdef;
637 part->
ori = bestorigin/scale;
638 part->
view = bestview;
664 int fom_type, vector<double>& weight,
int apply_ctf,
CTFparam* em_ctf)
667 if ( lo_res > 0 && hi_res > lo_res ) swap(hi_res, lo_res);
670 double max_rad = pref->
real_size()[0]/hi_res;
671 double max_rad_cv = 1.1*max_rad;
672 if ( max_rad_cv <= max_rad ) max_rad_cv = max_rad + 1;
674 double max_rad_sq = max_rad*max_rad;
675 double max_rad_cv_sq = max_rad_cv*max_rad_cv;
676 double min_rad_sq(0);
678 min_rad_sq = pref->
real_size()[0]/lo_res;
679 min_rad_sq *= min_rad_sq;
683 cout <<
"DEBUG img_recip_space_fom: max_rad=" << max_rad <<
" (" << max_rad_sq <<
")" << endl;
685 double dphi, ctf_fac;
686 if ( !em_ctf ) apply_ctf = 0;
690 long hx = (p->
sizeX() - 1)/2, hy = (p->
sizeY() - 1)/2;
691 double sx2, sy2, s2, d2, amp, denom, f, cv, wsum;
699 double* w =
new double[p->
sizeX()];
700 for ( j=0; j<p->
sizeX(); j++ ) w[j] = 1;
702 for (
size_t j=0; j<weight.size(); j++ )
if ( weight[j] ) w[j] = weight[j];
704 double* fom =
new double[p->
sizeX()];
705 double* dsum =
new double[p->
sizeX()];
706 double* msum =
new double[p->
sizeX()];
707 for ( i=0; i<p->
sizeX(); i++ ) fom[i] = dsum[i] = msum[i] = 0;
713 for ( yy=i=0; yy<p->
sizeY(); yy++ ) {
715 if ( iv[1] > hy ) iv[1] -= p->
sizeY();
716 sy2 = freq_scale[1]*iv[1];
718 for ( xx=0; xx<p->
sizeX(); xx++, i++ ) {
720 if ( iv[0] > hx ) iv[0] -= p->
sizeX();
721 sx2 = freq_scale[0]*iv[0];
724 if ( d2 >= min_rad_sq && d2 <= max_rad_cv_sq ) {
729 ctf_fac = em_ctf->
calculate(s2, atan2(iv[1],iv[0]));
732 if ( fom_type < 1 ) {
734 msum[j] += dtemp.
power();
735 dsum[j] += data[i].
power();
737 amp = data[i].
amp() + dtemp.
amp();
739 fom[j] += amp*dphi*dphi;
746 if ( fom_type < 1 ) {
747 for ( f=wsum=0, j=0; j<max_rad; j++ ) {
749 denom = dsum[j]*msum[j];
750 if ( denom > 0 ) f += w[j]*fom[j]/sqrt(denom);
753 for ( cv=wsum=0, j=(
long)max_rad; j<max_rad_cv; j++ ) {
755 denom = dsum[j]*msum[j];
756 if ( denom > 0 ) cv += w[j]*fom[j]/sqrt(denom);
760 for ( f=wsum=0, j=0; j<max_rad; j++ ) {
762 if ( msum[j] ) f += w[j]*cos(sqrt(fom[j]/msum[j]));
765 for ( cv=wsum=0, j=(
long)max_rad; j<max_rad_cv; j++ ) {
767 if ( msum[j] ) cv += w[j]*cos(sqrt(fom[j]/msum[j]));
783 int max_iter,
int fom_type, vector<double>& weight,
CTFparam* em_ctf,
double def_std,
double shift_std,
788 double predef(0), bestdef(0);
803 cout <<
"DEBUG img_fit_in_recip_space_monte: view=" << view << endl;
808 double fom, prefom(-1), bestfom(-1), bestcv(0);
815 for ( iter = 0; iter<max_iter; iter++ ) {
817 cout <<
"DEBUG img_fit_in_recip_space_monte: iter=" << iter << endl;
828 fom =
img_recip_space_fom(pcopy, pcs, hi_res, lo_res, fom_type, weight, apply_ctf, em_ctf);
831 if ( bestfom < fom ) {
837 cout << iter <<
tab << fom <<
tab << bestcv << endl;
840 if ( fom/prefom > random()*
irm ) {
862 double step,
double accuracy,
int fom_type, vector<double>& weight,
CTFparam* em_ctf,
double def_std,
FSI_Kernel*
kernel,
long &number)
867 if ( def_std && em_ctf && bestdef ) {
885 cout <<
"DEBUG img_fit_in_recip_space_grid: view=" << view << endl;
890 double fom, prevfom, bestfom(-1);
891 double def, def_start(bestdef - 5*def_std), def_end(bestdef + 5*def_std), bestcv(0);
898 for ( def = def_start; def <= def_end; def += def_std ) {
900 fom =
img_recip_space_fom(pcopy, pcs, hi_res, lo_res, fom_type, weight, apply_ctf, em_ctf);
902 if ( bestfom < fom ) {
907 cout << def <<
tab << fom <<
tab << bestcv << endl;
914 while ( step >= accuracy ) {
917 for ( i=ibest=0, ori[1] = oldori[1] - step; ori[1] <= oldori[1] + step; ori[1] += step ) {
918 for ( ori[0] = oldori[0] - step; ori[0] <= oldori[0] + step; ori[0] += step, ++i ) {
923 fom =
img_recip_space_fom(pcopy, pcs, hi_res, lo_res, fom_type, weight, apply_ctf, em_ctf);
927 if ( bestfom < fom ) {
933 cout << ori[0] <<
tab << ori[1] <<
tab << fom <<
tab << bestcv << endl;
939 if ( ( ibest == i/2 ) || fabs(prevfom - bestfom) < 1e-6 )
978 int max_iter,
int fom_type, vector<double>& weight,
CTFparam* em_ctf,
979 double def_std,
double shift_std,
double view_std,
983 double def(0), predef(0), bestdef(0);
990 double fom, prefom(-1), bestfom(-1), bestcv(0);
993 double magx(1), magy(1);
998 View bestview = view, preview = view;
1000 for ( iter = 0; iter<max_iter; iter++ ) {
1002 cout <<
"DEBUG img_refine_monte: iter=" << iter << endl;
1005 a = max_angle*(random()*
irm*2 - 1);
1011 if ( max_mag > 0 ) {
1012 magx = 1 + max_mag*(random()*
irm*2 - 1);
1013 magy = 1 + max_mag*(random()*
irm*2 - 1);
1022 def_std, shift_std,
kernel, number);
1023 if ( bestfom < fom ) {
1032 cout << iter <<
tab << fom << endl;
1034 if ( fom/prefom > random()*
irm ) {
1084 double alpha_step,
double accuracy,
double shift_step,
1085 double shift_accuracy,
double max_mag,
int fom_type, vector<double>& weight,
1090 cerr <<
"Error: The angular step size is too small! (" << alpha_step*180.0/
M_PI <<
")" << endl;
1092 cerr <<
"Error: The accuracy is too small! (" << accuracy*180.0/
M_PI <<
")" << endl;
1096 if ( alpha_step < accuracy ) {
1097 cerr <<
"Error: The angular step size is smaller than the accuracy! (" << alpha_step*180.0/
M_PI <<
" < " << accuracy*180.0/
M_PI <<
")" << endl;
1101 double def(0), bestdef(0);
1106 double fom, prefom, bestfom(-1), bestcv(0);
1108 double magx, magy, mmin(1.0-max_mag), mmax(1.0+max_mag), mstep(max_mag/10);
1114 while ( alpha_step >= accuracy ) {
1118 for ( i=ibest=0, v = views; v; v = v->
next, i++ ) {
1124 cout <<
"DEBUG img_refine_grid: view=" << *v << endl;
1125 fom =
img_fit_in_recip_space_grid(p, pref, hi_res, lo_res, shift_step, shift_accuracy, fom_type, weight, em_ctf, def_std,
kernel, number);
1126 if ( bestfom < fom ) {
1137 cout << alpha_step <<
tab << bestfom <<
tab << bestcv << endl;
1138 if ( ibest == i/2 || fabs(prefom - bestfom) < 1e-6 ) alpha_step /= 2;
1148 if ( max_mag > 0 ) {
1151 for ( magy = mmin; magy <= mmax; magy += mstep ) {
1152 for ( magx = mmin; magx <= mmax; magx += mstep ) {
1162 cout <<
"DEBUG img_refine_grid: sam=" << p->
image->
sampling() << endl;
1163 fom =
img_fit_in_recip_space_grid(p, pref, hi_res, lo_res, 0.4, 0.1, fom_type, weight, em_ctf, def_std,
kernel, number);
1164 if ( bestfom < fom ) {
1174 cout << magx <<
tab << magy <<
tab << fom <<
tab << bestcv << endl;
1185 if ( bestdef > def - 5*def_std && bestdef < def + 5*def_std )