Bsoft  2.1.0
Bernard's software package
mg_refine.cpp
Go to the documentation of this file.
1 
9 #include "mg_processing.h"
10 #include "mg_refine.h"
11 #include "rwmg.h"
12 #include "mg_ctf.h"
13 #include "rwimg.h"
14 #include "Complex.h"
15 #include "Matrix3.h"
16 #include "linked_list.h"
17 #include "random_numbers.h"
18 #include "utilities.h"
19 
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 
23 // Declaration of global variables
24 extern int verbose; // Level of output to the screen
25 
26 // Local function prototypes
27 long part_refine_orientation(Bparticle* part, Bstring& partfile,
28  Bimage* pref, Bimage* pmask, Bsymmetry* sym, int max_iter, double alpha_step, double accuracy,
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,
34 double img_refine_monte(Bimage* p, Bimage* pref, double hi_res, double lo_res,
35  int max_iter, int fom_type, vector<double>& weight, CTFparam* em_ctf,
36  double def_std, double shift_std, double view_std,
37  double max_angle, double max_mag, FSI_Kernel* kernel, long &number);
38 double img_refine_grid(Bimage* p, Bimage* pref, double hi_res, double lo_res,
39  double alpha_step, double accuracy, double shift_step,
40  double shift_accuracy, double max_mag,
41  int fom_type, vector<double>& weight, CTFparam* em_ctf, double def_std, FSI_Kernel* kernel, long &number);
42 
78  Bstring& sym_string, int part_select, int max_iter,
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)
84 {
85  if ( reffile.length() < 1 ) {
86  cerr << "Error: A reference map must be provided!" << endl << endl;
87  return -1;
88  }
89 
90  if ( max_iter < 1 ) {
91  if ( alpha_step < SMALLFLOAT || accuracy < SMALLFLOAT ) {
92  if ( alpha_step < SMALLFLOAT )
93  cerr << "Error: The angular step size is too small! (" << alpha_step*180.0/M_PI << ")" << endl;
94  if ( accuracy < SMALLFLOAT )
95  cerr << "Error: The accuracy is too small! (" << accuracy*180.0/M_PI << ")" << endl;
96  return -1;
97  }
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;
100  return -1;
101  }
102  }
103 
104  random_seed();
105 
106  if ( sym_string.length() < 1 ) sym_string = "C1";
107 
108  Bsymmetry* sym = init_point_group_symmetry(sym_string);
109 
110  long npart(0), npix, number(0);
111  Bfield* field = project->field;
112  Bmicrograph* mg;
113  Bparticle* part;
114 
115  Bimage* pref = read_img(reffile, 1, -1);
116 
117  if ( !pref ) {
118  cerr << "Error: The reference was not read!" << endl << endl;
119  return -1;
120  }
121 
122  pref->check_resolution(hi_res);
123 
124  if ( !weight.size() )
125  weight = C_curve(pref->sizeX(), 1/pref->real_size()[0]);
126 
127  // Fourier transform size for the original map size
128  long ft_size = pref->sizeX();
129 
130  if ( pref->compound_type() == TSimple ) {
131  if ( verbose )
132  cout << "Fourier transforming reference map " << pref->file_name() << endl;
133  pref->fft();
134  }
135 
136  // Reduce the reference map's transform size to save memory
137  int diam = (int) (2.2*pref->real_size()[0]/hi_res + 0.5);
138  if ( diam%2 ) diam += 1;
139  Vector3<long> size(diam, diam, diam);
140  Vector3<double> scale(1,1,1);
141  if ( diam < pref->sizeX() )
142  scale = pref->change_transform_size(size);
143 
144  pref->set(0, 0);
145  pref->set(1, 0);
146 
147  npix = pref->sizeX()*pref->sizeY();
148 
149  // Mask to apply to particles
150  Bimage* pmask = NULL;
151  if ( maskfile.length() ) pmask = read_img(maskfile, 1, 0);
152 
153  pref->check_resolution(hi_res);
154 
155  // Interpolation kernel
156  FSI_Kernel* kernel = new FSI_Kernel(kernel_width, kernel_power);
157 
158  // FFT plan for the particle images
159  fft_plan planf = fft_setup_plan(ft_size, ft_size, 1, FFTW_FORWARD, 1);
160 
161  if ( verbose ) {
162  cout << "Refining orientations:" << endl;
163  if ( max_iter ) {
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;
170  } else {
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;
176  }
177  cout << "Reference: " << pref->file_name() << endl;
178 // cout << "Reference origin: " << pref->image->origin()/scale << 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;
189  }
190 
191  pref->phase_shift_to_origin();
192 
193  if ( verbose & VERB_DEBUG )
194  cout << "DEBUG mg_refine_orientations: origin after phase shift: " << pref->image->origin() << endl;
195 
196  // Directory for individual particle parameter files
197  if ( flags & WRITE_PPX ) {
198  mkdir("ppx", O_CREAT );
199  chmod("ppx", 0755);
200  }
201 
202  time_t t = time(NULL);
203 
204  for ( field = project->field; field; field = field->next ) {
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 ) ) {
208  number += part_refine_orientation(part, mg->fpart, pref, pmask, sym,
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,
212  flags, kernel, planf);
213  npart++;
214  if ( verbose & VERB_RESULT )
215  cout << setw(15) << mg->id << tab << part->id << tab << part->mag << tab <<
216  setprecision(2) << part->ori[0] << tab << part->ori[1] << tab <<
217  setprecision(4) << part->view[0] << tab <<
218  part->view[1] << tab << part->view[2] << tab <<
219  setprecision(2) << part->view.angle()*180.0/M_PI << tab <<
220  setprecision(4) << part->fom[0] << tab << part->fom[1] << tab <<
221  (long)(time(NULL) - t) << endl;
222  } else {
223  part->fom[0] = part->fom[1] = part->sel = 0;
224  }
225  }
226  }
227  }
228 
229  t = (time(NULL) - t);
230 
231  long ts = (long) t;
232  long hr = ts/3600, min = (ts - 3600*hr)/60, sec = ts - 3600*hr - 60*min;
233 
234  if ( verbose & VERB_RESULT ) {
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;
240  }
241 
242  kill_symmetry(sym);
243 
244  delete kernel;
245 
247 
248  return number;
249 }
250 
252  Bstring& sym_string, int part_select, int max_iter,
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)
258 {
259  if ( reffile.length() < 1 ) {
260  cerr << "Error: A reference map must be provided!" << endl << endl;
261  return -1;
262  }
263 
264  if ( max_iter < 1 ) {
265  if ( alpha_step < SMALLFLOAT || accuracy < SMALLFLOAT ) {
266  if ( alpha_step < SMALLFLOAT )
267  cerr << "Error: The angular step size is too small! (" << alpha_step*180.0/M_PI << ")" << endl;
268  if ( accuracy < SMALLFLOAT )
269  cerr << "Error: The accuracy is too small! (" << accuracy*180.0/M_PI << ")" << endl;
270  return -1;
271  }
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;
274  return -1;
275  }
276  }
277 
278  random_seed();
279 
280  if ( sym_string.length() < 1 ) sym_string = "C1";
281 
282  Bsymmetry* sym = init_point_group_symmetry(sym_string);
283 
284  long npix;
285  Bimage* pref = read_img(reffile, 1, -1);
286 
287  if ( !pref ) {
288  cerr << "Error: The reference was not read!" << endl << endl;
289  return -1;
290  }
291 
292  pref->check_resolution(hi_res);
293 // pref->information();
294 
295  // Fourier transform size for the original map size
296  long ft_size = pref->sizeX();
297 
298  if ( pref->compound_type() == TSimple ) {
299  if ( verbose )
300  cout << "Fourier transforming reference map " << pref->file_name() << endl;
301  pref->fft();
302  }
303 
304  // Reduce the reference map's transform size to save memory
305  int diam = (int) (2.2*pref->real_size()[0]/hi_res + 0.5);
306  if ( diam%2 ) diam += 1;
307  Vector3<long> size(diam, diam, diam);
308  Vector3<double> scale(1,1,1);
309  if ( diam < pref->sizeX() )
310  scale = pref->change_transform_size(size);
311 
312  pref->set(0, 0);
313  pref->set(1, 0);
314 
315  npix = pref->sizeX()*pref->sizeY();
316 
317  // Mask to apply to particles
318  Bimage* pmask = NULL;
319  if ( maskfile.length() ) {
320  pmask = read_img(maskfile, 1, 0);
321  pmask->origin(pmask->size()/2);
322  }
323 
324  pref->check_resolution(hi_res);
325 
326  // Interpolation kernel
327  FSI_Kernel* kernel = new FSI_Kernel(kernel_width, kernel_power);
328 
329  // FFT plan for the particle images
330  fft_plan planf = fft_setup_plan(ft_size, ft_size, 1, FFTW_FORWARD, 1);
331 
332  long npart(0);
334 
335  if ( verbose ) {
336  cout << "Refining orientations:" << endl;
337  if ( max_iter ) {
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;
343  } else {
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;
347  }
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;
362  else
363  cout << "Input curve" << endl;
364  cout << "Micrograph\tPID\tDef\tSamX\tSamY\tOriX\tOriY\tViewX\tViewY\tViewZ\tAngle\tCC\tCV\tTime" << endl;
365  }
366 
367  pref->phase_shift_to_origin();
368 
369  if ( !weight.size() )
370  weight = C_curve(pref->sizeX(), 1/pref->real_size()[0]);
371 
372  if ( verbose & VERB_DEBUG )
373  cout << "DEBUG project_refine_orientations: origin after phase shift: " << pref->image->origin() << endl;
374 
375  // Directory for individual particle parameter files
376  if ( flags & WRITE_PPX ) {
377  mkdir("ppx", O_CREAT );
378  chmod("ppx", 0755);
379  }
380 
381  time_t t = time(NULL);
382  long number(0);
383  int* nar = new int[npart];
384 
385 #ifdef HAVE_GCD
386  dispatch_queue_t myq = dispatch_queue_create(NULL, NULL);
387  dispatch_apply(npart, dispatch_get_global_queue(0, 0), ^(size_t i){
388  Bparticle* part = partarr[i];
389  Bmicrograph* mg = part->mg;
390  nar[i] = part_refine_orientation(part,
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,
395  flags, kernel, planf);
396  dispatch_sync(myq, ^{
397  if ( verbose & VERB_RESULT )
398  cout << setw(15) << mg->id << tab << part->id << tab <<
399  setprecision(0) << part->def << tab <<
400  setprecision(2) << part->pixel_size[0] << tab << part->pixel_size[1] << tab <<
401  setprecision(2) << part->ori[0] << tab << part->ori[1] << tab <<
402  setprecision(4) << part->view[0] << tab <<
403  part->view[1] << tab << part->view[2] << tab <<
404  setprecision(2) << part->view.angle()*180.0/M_PI << tab <<
405  setprecision(4) << part->fom[0] << tab << part->fom[1] << tab <<
406  (long)(time(NULL) - t) << endl;
407  });
408  });
409 #else
410 #pragma omp parallel for
411  for ( long i=0; i<npart; i++ ) {
412  Bparticle* part = partarr[i];
413  Bmicrograph* mg = part->mg;
414  nar[i] = part_refine_orientation(part,
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,
419  flags, kernel, planf);
420  #pragma omp critical
421  {
422  if ( verbose & VERB_RESULT )
423  cout << setw(15) << mg->id << tab << part->id << tab <<
424  setprecision(0) << part->def << tab <<
425  setprecision(2) << part->pixel_size[0] << tab << part->pixel_size[1] << tab <<
426  setprecision(2) << part->ori[0] << tab << part->ori[1] << tab <<
427  setprecision(4) << part->view[0] << tab <<
428  part->view[1] << tab << part->view[2] << tab <<
429  setprecision(2) << part->view.angle()*180.0/M_PI << tab <<
430  setprecision(4) << part->fom[0] << tab << part->fom[1] << tab <<
431  (long)(time(NULL) - t) << endl;
432  }
433  }
434 #endif
435 
436  for ( long i=0; i<npart; i++ ) number += nar[i];
437 
438  t = (time(NULL) - t);
439 
440  long ts = (long) t;
441  long hr = ts/3600, min = (ts - 3600*hr)/60, sec = ts - 3600*hr - 60*min;
442 
443  if ( verbose & VERB_RESULT ) {
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;
449  }
450 
451  delete[] partarr;
452  delete[] nar;
453 
454  kill_symmetry(sym);
455 
456  delete kernel;
457 
459 
460  return number;
461 }
462 
496  Bimage* pref, Bimage* pmask, Bsymmetry* sym, int max_iter, double alpha_step, double accuracy,
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,
502 {
503  FOMType fom_tag[NFOM] = {FOM, FOM_CV};
504 
505  if ( flags & CHECK_PPX )
506  if ( ppx_check(part, fom_tag) ) return 0;
507 
508  if ( verbose & VERB_DEBUG )
509  cout << "DEBUG part_refine_orientation: file=" << partfile << " image=" << part->id << endl;
510 
511  Bimage* p = read_img(partfile, 1, part->id - 1);
512  if ( !p )
513  return error_show("Error in part_refine_orientation", __FILE__, __LINE__);
514 
515  if ( part->pixel_size[0] > 0 ) p->sampling(part->pixel_size);
516  else if ( part->mg->pixel_size[0] > 0 ) p->sampling(part->mg->pixel_size);
517  part->pixel_size = p->image->sampling();
518 
519  if ( part->ori[0] <= 0 ) {
520  if ( p->image->origin()[0] > 0 ) part->ori[0] = p->image->origin()[0];
521  else part->ori[0] = p->sizeX()/2;
522  }
523  if ( part->ori[1] <= 0 ) {
524  if ( p->image->origin()[1] > 0 ) part->ori[1] = p->image->origin()[1];
525  else part->ori[1] = p->sizeY()/2;
526  }
527  part->ori[2] = 0;
528  p->origin(part->ori);
529 
530  if ( part->mag < 0.5 ) {
531  if ( p->image->magnification() > 0.5 )
532  part->mag = p->image->magnification();
533  else part->mag = 1;
534  }
535 
536  part->fom[0] = part->fom[1] = 0;
537 
538  p->change_type(Float);
539  p->rescale_to_avg_std(0, 1);
541 
542  int edge_size = (int) (2*edge_radius);
543  Vector3<long> size(edge_size, edge_size, 1);
544  Vector3<double> start(part->ori[0]-edge_radius, part->ori[1]-edge_radius, 0);
545  if ( edge_size )
546  p->edge(1, size, start, 1, FILL_BACKGROUND, 0);
547 
548  Vector3<double> shift;
549  Bimage* pm;
550 
551  if ( pmask ) {
552  pm = pmask->copy();
553  shift = p->image->origin() - pm->image->origin();
554  pm->shift(shift);
555  p->multiply(pm);
556  delete pm;
557  }
558 
559  p->fft(planf, 0);
560 
561  p->set(0, 0);
562  p->set(1, 0);
563 
564  // Reduce the particle transform size to match that of the reference
565  Vector3<double> scale(1,1,1);
566  if ( pref->sizeX() < p->sizeX() ) {
567  size = Vector3<int>(pref->sizeX(), pref->sizeY(), 1);
568  scale = p->change_transform_size(size);
569  }
570 
571  int apply_ctf(0);
572  double bestdef(0);
573  if ( em_ctf ) {
574  bestdef = em_ctf->defocus_average();
575  if ( def_std ) {
576  if ( part->def < 1 ) part->def = em_ctf->defocus_average();
577  bestdef = part->def;
578  apply_ctf = 1;
579  }
580  }
581 
582  long i, number(0);
583  double fom, bestmag = part->mag;
584  Vector3<double> sam(p->image->sampling()), bestsam(sam);
585  Vector3<double> ori(p->image->origin()), bestorigin(ori);
586  View bestview = part->view;
587  View* view = symmetry_get_all_views(sym, part->view);
588  View* v;
589  CTFparam* ctf_copy = new CTFparam;
590 
591  for ( i=1, v = view; v; v = v->next, i++ ) {
592  p->sampling(sam);
593  p->origin(ori);
594  p->view(*v);
595  p->image->magnification(part->mag);
596  if ( verbose & VERB_DEBUG )
597  cout << "DEBUG part_refine_orientation: view=" << *v << endl;
598 
599  if ( em_ctf ) {
600  ctf_copy->update(em_ctf);
601  if ( apply_ctf && part->def > 1 ) ctf_copy->defocus_average(part->def);
602  }
603 
604  if ( max_iter )
605  fom = img_refine_monte(p, pref, hi_res, lo_res, max_iter, fom_type,
606  weight, ctf_copy, def_std,
607  shift_std, view_std, max_angle, max_mag, kernel, number);
608  else
609  fom = img_refine_grid(p, pref, hi_res, lo_res, alpha_step, accuracy,
610  shift_step, shift_accuracy, max_mag,
611  fom_type, weight, ctf_copy, def_std, kernel, number);
612 
613  if ( part->fom[0] < fom ) {
614  part->fom[0] = fom;
615  part->fom[1] = p->image->FOM();
616  part->sel = i;
617  if ( ctf_copy && apply_ctf ) bestdef = ctf_copy->defocus_average();
618  bestsam = p->image->sampling();
619  bestorigin = p->image->origin();
620  bestview = p->image->view();
621  bestmag = p->image->magnification();
622  if ( bestdef < 1000 ) {
623  cerr << "Bad defocus! pid = " << part->id << tab << bestdef << endl;
624  }
625  }
626 
627  }
628 
629  if ( ctf_copy ) delete ctf_copy;
630 
631  if ( apply_ctf ) {
632  if ( bestdef > 1 ) part->def = bestdef;
633  part->dev = em_ctf->defocus_deviation();
634  part->ast = em_ctf->astigmatism_angle();
635  }
636  part->pixel_size = bestsam*scale;
637  part->ori = bestorigin/scale;
638  part->view = bestview;
639  part->mag = bestmag;
640 
641  kill_list((char *) view, sizeof(View));
642  delete p;
643 
644  if ( flags & WRITE_PPX ) {
645  Bstring ppx_name = ppx_filename(part->mg->id, part->id);
646  write_particle(ppx_name, part, 0, 0, fom_tag);
647  ppx_name = 0;
648  }
649 
650  return number;
651 }
652 
653 /*
654  FRC:
655  sum((FoFc*)re)
656  -------------------------
657  sqrt(sum(Fc^2)*sum(Fo^2))
658  DPR:
659  (sum((|Fo|+|Fc|)*dPhi^2))
660  sqrt|-----------------------|
661  ( sum(|Fo|+|Fc|) )
662 */
663 double img_recip_space_fom(Bimage* p, Bimage* pref, double hi_res, double lo_res,
664  int fom_type, vector<double>& weight, int apply_ctf, CTFparam* em_ctf)
665 {
666  if ( pref->sampling(0)[0] < 0.01 ) pref->sampling(1,1,1);
667  if ( lo_res > 0 && hi_res > lo_res ) swap(hi_res, lo_res);
668  pref->check_resolution(hi_res);
669 
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;
673 
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);
677  if ( lo_res > 0 ) {
678  min_rad_sq = pref->real_size()[0]/lo_res;
679  min_rad_sq *= min_rad_sq;
680  }
681 
682  if ( verbose & VERB_DEBUG )
683  cout << "DEBUG img_recip_space_fom: max_rad=" << max_rad << " (" << max_rad_sq << ")" << endl;
684 
685  double dphi, ctf_fac;
686  if ( !em_ctf ) apply_ctf = 0;
687  else if ( em_ctf->defocus_average() < 1 ) apply_ctf = 0;
688 
689  long i, j, xx, yy;
690  long hx = (p->sizeX() - 1)/2, hy = (p->sizeY() - 1)/2;
691  double sx2, sy2, s2, d2, amp, denom, f, cv, wsum;
692  Vector3<double> m, d, iv;
693  Vector3<double> freq_scale(1.0/p->real_size()[0], 1.0/p->real_size()[1], 1.0);
694 // cout << "size = " << p->size() << endl;
695 // cout << "sampling = " << p->image->sampling() << endl;
696 // cout << "real size = " << p->real_size() << endl;
697 // bexit(-1);
698 
699  double* w = new double[p->sizeX()];
700  for ( j=0; j<p->sizeX(); j++ ) w[j] = 1;
701  if ( weight.size() )
702  for ( size_t j=0; j<weight.size(); j++ ) if ( weight[j] ) w[j] = weight[j];
703 
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;
708 
709  Complex<float>* data = (Complex<float> *) p->data_pointer();
710  Complex<float>* refdata = (Complex<float> *) pref->data_pointer();
711  Complex<float> dtemp;
712 
713  for ( yy=i=0; yy<p->sizeY(); yy++ ) {
714  iv[1] = yy;
715  if ( iv[1] > hy ) iv[1] -= p->sizeY();
716  sy2 = freq_scale[1]*iv[1];
717  sy2 *= sy2;
718  for ( xx=0; xx<p->sizeX(); xx++, i++ ) {
719  iv[0] = xx;
720  if ( iv[0] > hx ) iv[0] -= p->sizeX();
721  sx2 = freq_scale[0]*iv[0];
722  sx2 *= sx2;
723  d2 = iv.length2();
724  if ( d2 >= min_rad_sq && d2 <= max_rad_cv_sq ) {
725  s2 = sx2 + sy2;
726  j = (long) sqrt(d2);
727  dtemp = refdata[i];
728  if ( apply_ctf ) {
729  ctf_fac = em_ctf->calculate(s2, atan2(iv[1],iv[0]));
730  dtemp *= -ctf_fac;
731  }
732  if ( fom_type < 1 ) {
733  fom[j] += data[i].real()*dtemp.real() + data[i].imag()*dtemp.imag();
734  msum[j] += dtemp.power();
735  dsum[j] += data[i].power();
736  } else {
737  amp = data[i].amp() + dtemp.amp();
738  dphi = angle_set_negPI_to_PI(data[i].phi() - dtemp.phi());
739  fom[j] += amp*dphi*dphi;
740  msum[j] += amp;
741  }
742  }
743  }
744  }
745 
746  if ( fom_type < 1 ) {
747  for ( f=wsum=0, j=0; j<max_rad; j++ ) {
748  wsum += w[j];
749  denom = dsum[j]*msum[j];
750  if ( denom > 0 ) f += w[j]*fom[j]/sqrt(denom);
751  }
752  f /= wsum;
753  for ( cv=wsum=0, j=(long)max_rad; j<max_rad_cv; j++ ) {
754  wsum += w[j];
755  denom = dsum[j]*msum[j];
756  if ( denom > 0 ) cv += w[j]*fom[j]/sqrt(denom);
757  }
758  cv /= wsum;
759  } else {
760  for ( f=wsum=0, j=0; j<max_rad; j++ ) {
761  wsum += w[j];
762  if ( msum[j] ) f += w[j]*cos(sqrt(fom[j]/msum[j]));
763  }
764  f /= wsum;
765  for ( cv=wsum=0, j=(long)max_rad; j<max_rad_cv; j++ ) {
766  wsum += w[j];
767  if ( msum[j] ) cv += w[j]*cos(sqrt(fom[j]/msum[j]));
768  }
769  cv /= wsum;
770  }
771 
772  delete[] fom;
773  delete[] dsum;
774  delete[] msum;
775  delete[] w;
776 
777  p->image->FOM(cv);
778 
779  return f;
780 }
781 
782 double img_fit_in_recip_space_monte(Bimage* p, Bimage* pref, double hi_res, double lo_res,
783  int max_iter, int fom_type, vector<double>& weight, CTFparam* em_ctf, double def_std, double shift_std,
784  FSI_Kernel* kernel, long &number)
785 {
786  // Defocus may be changed in this function
787  int apply_ctf(0);
788  double predef(0), bestdef(0);
789  if ( def_std && em_ctf && em_ctf->defocus_average() ) {
790  predef = bestdef = em_ctf->defocus_average();
791  apply_ctf = 1;
792  }
793 
794  Vector3<double> scale = pref->sampling(0)/p->sampling(0);
795  View view(p->image->view());
796  Matrix3 mat = view.matrix();
797 // mat = mat * p->image->magnification();
798  mat[0] *= scale;
799  mat[1] *= scale;
800 // cout << mat << endl;
801 
802  if ( verbose & VERB_DEBUG )
803  cout << "DEBUG img_fit_in_recip_space_monte: view=" << view << endl;
804 
805  Bimage* pcs = pref->central_section(mat, hi_res/1.11, kernel);
806 
807  long iter;
808  double fom, prefom(-1), bestfom(-1), bestcv(0);
809  double irm = 1.0/get_rand_max();
810  Vector3<double> origin(p->image->origin());
811  Vector3<double> r, preorigin = origin, bestorigin = origin;
812 
813  Bimage* pcopy;
814 
815  for ( iter = 0; iter<max_iter; iter++ ) {
816  if ( verbose & VERB_DEBUG )
817  cout << "DEBUG img_fit_in_recip_space_monte: iter=" << iter << endl;
818  if ( iter > 0 ) {
819  if ( apply_ctf ) em_ctf->defocus_average(em_ctf->defocus_average() + random_gaussian(0, def_std));
820  r = vector3_xy_random_gaussian(0, shift_std);
821  origin += r;
822  }
823 
824  pcopy = p->copy();
825  pcopy->origin(origin);
826  pcopy->phase_shift_to_origin();
827 
828  fom = img_recip_space_fom(pcopy, pcs, hi_res, lo_res, fom_type, weight, apply_ctf, em_ctf);
829  number++;
830 
831  if ( bestfom < fom ) {
832  bestfom = fom;
833  if ( apply_ctf ) bestdef = em_ctf->defocus_average();
834  bestorigin = origin;
835  bestcv = pcopy->image->FOM();
836  if ( verbose & VERB_FULL )
837  cout << iter << tab << fom << tab << bestcv << endl;
838  }
839 
840  if ( fom/prefom > random()*irm ) {
841  if ( apply_ctf ) predef = em_ctf->defocus_average();
842  preorigin = origin;
843  prefom = fom;
844  } else {
845  if ( apply_ctf ) em_ctf->defocus_average(predef);
846  origin = preorigin;
847  fom = prefom;
848  }
849  delete pcopy;
850  }
851 
852  p->origin(bestorigin);
853  p->image->FOM(bestcv);
854  if ( apply_ctf ) em_ctf->defocus_average(bestdef);
855 
856  delete pcs;
857 
858  return bestfom;
859 }
860 
861 double img_fit_in_recip_space_grid(Bimage* p, Bimage* pref, double hi_res, double lo_res,
862  double step, double accuracy, int fom_type, vector<double>& weight, CTFparam* em_ctf, double def_std, FSI_Kernel* kernel, long &number)
863 {
864  // Defocus may be changed in this function
865  int apply_ctf(0);
866  double bestdef(em_ctf->defocus_average());
867  if ( def_std && em_ctf && bestdef ) {
868  bestdef = em_ctf->defocus_average();
869  apply_ctf = 1;
870  }
871 
872  Vector3<double> scale = pref->sampling(0)/p->sampling(0);
873 // Vector3<double> scale = p->sampling(0)/pref->sampling(0);
874  scale[2] = 1;
875 
876  View view(p->image->view());
877  Matrix3 mat = view.matrix();
878 // mat = mat * p->image->magnification();
879 // mat = scale * mat;
880  mat[0] *= scale;
881  mat[1] *= scale;
882 // cout << mat << endl;
883 
884  if ( verbose & VERB_DEBUG )
885  cout << "DEBUG img_fit_in_recip_space_grid: view=" << view << endl;
886 
887  Bimage* pcs = pref->central_section(mat, hi_res/1.11, kernel);
888 
889  int i, ibest;
890  double fom, prevfom, bestfom(-1);
891  double def, def_start(bestdef - 5*def_std), def_end(bestdef + 5*def_std), bestcv(0);
892  Vector3<double> ori, oldori(p->image->origin()), bestorigin(oldori);
893  Bimage* pcopy;
894 
895  if ( apply_ctf ) {
896  pcopy = p->copy();
897  pcopy->phase_shift_to_origin();
898  for ( def = def_start; def <= def_end; def += def_std ) {
899  em_ctf->defocus_average(def);
900  fom = img_recip_space_fom(pcopy, pcs, hi_res, lo_res, fom_type, weight, apply_ctf, em_ctf);
901  number++;
902  if ( bestfom < fom ) {
903  bestfom = fom;
904  bestdef = def;
905  bestcv = pcopy->image->FOM();
906  if ( verbose & VERB_FULL )
907  cout << def << tab << fom << tab << bestcv << endl;
908  }
909  }
910  delete pcopy;
911  em_ctf->defocus_average(bestdef);
912  }
913 
914  while ( step >= accuracy ) {
915  prevfom = bestfom;
916  bestfom = -1;
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 ) {
919  pcopy = p->copy();
920  pcopy->image->origin(ori);
921  pcopy->phase_shift_to_origin();
922 
923  fom = img_recip_space_fom(pcopy, pcs, hi_res, lo_res, fom_type, weight, apply_ctf, em_ctf);
924 
925  number++;
926 
927  if ( bestfom < fom ) {
928  bestfom = fom;
929  ibest = i;
930  bestorigin = ori;
931  bestcv = pcopy->image->FOM();
932  if ( verbose & VERB_FULL )
933  cout << ori[0] << tab << ori[1] << tab << fom << tab << bestcv << endl;
934  }
935 
936  delete pcopy;
937  }
938  }
939  if ( ( ibest == i/2 ) || fabs(prevfom - bestfom) < 1e-6 )
940  step /= 2; // Contract around best case
941  }
942 
943  p->origin(bestorigin);
944  p->image->FOM(bestcv);
945 
946  delete pcs;
947 
948  return bestfom;
949 }
950 
977 double img_refine_monte(Bimage* p, Bimage* pref, double hi_res, double lo_res,
978  int max_iter, int fom_type, vector<double>& weight, CTFparam* em_ctf,
979  double def_std, double shift_std, double view_std,
980  double max_angle, double max_mag, FSI_Kernel* kernel, long &number)
981 {
982  int apply_ctf(0);
983  double def(0), predef(0), bestdef(0);
984  if ( def_std && em_ctf && em_ctf->defocus_average() ) {
985  def = predef = bestdef = em_ctf->defocus_average();
986  apply_ctf = 1;
987  }
988 
989  long iter;
990  double fom, prefom(-1), bestfom(-1), bestcv(0);
991  double a, irm(1.0/get_rand_max());
992 // double mag = p->image->magnification();
993  double magx(1), magy(1);
994 // double bestmag(mag), premag(mag);
995  Vector3<double> sam(p->image->sampling()), bestsam(sam), presam(sam);
996  Vector3<double> ori(p->image->origin()), r, bestorigin(ori);
997  View view(p->image->view());
998  View bestview = view, preview = view;
999 
1000  for ( iter = 0; iter<max_iter; iter++ ) {
1001  if ( verbose & VERB_DEBUG )
1002  cout << "DEBUG img_refine_monte: iter=" << iter << endl;
1003  if ( iter > 0 ) {
1004  r = vector3_random_gaussian(0, view_std);
1005  a = max_angle*(random()*irm*2 - 1);
1006  view[0] += r[0];
1007  view[1] += r[1];
1008  view[2] += r[2];
1009  view[3] += a;
1010  view.normalize();
1011  if ( max_mag > 0 ) {
1012  magx = 1 + max_mag*(random()*irm*2 - 1);
1013  magy = 1 + max_mag*(random()*irm*2 - 1);
1014  }
1015  }
1016  p->view(view);
1017 // p->image->magnification(mag);
1018  p->image->sampling(magx * sam[0], magy * sam[1], 1);
1019  if ( apply_ctf ) em_ctf->defocus_average(def); // Resets defocus to original
1020  p->origin(ori); // Resets origin to original
1021  fom = img_fit_in_recip_space_monte(p, pref, hi_res, lo_res, max_iter, fom_type, weight, em_ctf,
1022  def_std, shift_std, kernel, number);
1023  if ( bestfom < fom ) {
1024  bestfom = fom;
1025  bestview = view;
1026 // bestmag = mag;
1027  bestsam = p->image->sampling();
1028  bestorigin = p->image->origin();
1029  if ( apply_ctf ) bestdef = em_ctf->defocus_average();
1030  bestcv = p->image->FOM();
1031  if ( verbose & VERB_FULL )
1032  cout << iter << tab << fom << endl;
1033  }
1034  if ( fom/prefom > random()*irm ) {
1035  preview = view;
1036 // premag = mag;
1037  presam = sam;
1038  prefom = fom;
1039  } else {
1040  view = preview;
1041 // mag = premag;
1042  sam = presam;
1043  fom = prefom;
1044  }
1045  }
1046 
1047  p->view(bestview);
1048  p->image->origin(bestorigin);
1049 // p->image->magnification(bestmag);
1050  p->image->sampling(bestsam);
1051  p->image->FOM(bestcv);
1052  if ( apply_ctf ) em_ctf->defocus_average(bestdef);
1053 
1054  return bestfom;
1055 }
1056 
1083 double img_refine_grid(Bimage* p, Bimage* pref, double hi_res, double lo_res,
1084  double alpha_step, double accuracy, double shift_step,
1085  double shift_accuracy, double max_mag, int fom_type, vector<double>& weight,
1086  CTFparam* em_ctf, double def_std, FSI_Kernel* kernel, long &number)
1087 {
1088  if ( alpha_step < SMALLFLOAT || accuracy < SMALLFLOAT ) {
1089  if ( alpha_step < SMALLFLOAT )
1090  cerr << "Error: The angular step size is too small! (" << alpha_step*180.0/M_PI << ")" << endl;
1091  if ( accuracy < SMALLFLOAT )
1092  cerr << "Error: The accuracy is too small! (" << accuracy*180.0/M_PI << ")" << endl;
1093  return -1;
1094  }
1095 
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;
1098  return -1;
1099  }
1100 
1101  double def(0), bestdef(0);
1102  if ( def_std && em_ctf && em_ctf->defocus_average() )
1103  def = bestdef = em_ctf->defocus_average();
1104 
1105  int i, ibest;
1106  double fom, prefom, bestfom(-1), bestcv(0);
1107 // double mag = p->image->magnification(), bestmag = mag;
1108  double magx, magy, mmin(1.0-max_mag), mmax(1.0+max_mag), mstep(max_mag/10);
1109  Vector3<double> sam(p->image->sampling()), bestsam(sam);
1110  Vector3<double> ori(p->image->origin()), bestorigin(ori);
1111  View view(p->image->view()), bestview(view);
1112  View *views, *v;
1113 
1114  while ( alpha_step >= accuracy ) {
1115  prefom = bestfom;
1116  bestfom = -1;
1117  views = views_for_refinement(bestview, alpha_step);
1118  for ( i=ibest=0, v = views; v; v = v->next, i++ ) {
1119  p->view(*v);
1120  if ( def ) em_ctf->defocus_average(def); // Resets defocus to original
1121  p->origin(ori); // Resets origin to original
1122 // p->image->magnification(mag); // Resets magnification to original
1123  if ( verbose & VERB_DEBUG )
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 ) {
1127  bestfom = fom;
1128  ibest = i;
1129  bestview = *v;
1130  bestorigin = p->image->origin();
1131  if ( def ) bestdef = em_ctf->defocus_average();
1132  bestcv = p->image->FOM();
1133  }
1134  }
1135  kill_list((char *) views, sizeof(View));
1136  if ( verbose & VERB_FULL )
1137  cout << alpha_step << tab << bestfom << tab << bestcv << endl;
1138  if ( ibest == i/2 || fabs(prefom - bestfom) < 1e-6 ) alpha_step /= 2; // Contract around best case
1139  }
1140 
1141  p->view(bestview);
1142  ori = bestorigin;
1143 // if ( def ) {
1144 // em_ctf->defocus_average(bestdef);
1145 // def = bestdef;
1146 // }
1147 
1148  if ( max_mag > 0 ) {
1149  prefom = bestfom;
1150  bestfom = -1;
1151  for ( magy = mmin; magy <= mmax; magy += mstep ) {
1152  for ( magx = mmin; magx <= mmax; magx += mstep ) {
1153  p->image->sampling(sam[0] * magx, sam[1] * magy, 1);
1154 // cerr << p->sampling(0) << endl;
1155  p->origin(ori);
1156  if ( def ) em_ctf->defocus_average(def); // Resets defocus to original
1157 // for ( mag = 1-max_mag; mag <= 1+max_mag; mag += 0.001 ) {
1158 // p->image->magnification(mag);
1159 // if ( verbose & VERB_DEBUG )
1160 // cout << "DEBUG img_refine_grid: mag=" << mag << endl;
1161  if ( verbose & VERB_DEBUG )
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 ) {
1165  bestfom = fom;
1166 // bestmag = mag;
1167  bestsam = p->image->sampling();
1168  bestorigin = p->image->origin();
1169  if ( def ) bestdef = em_ctf->defocus_average();
1170  bestcv = p->image->FOM();
1171 // if ( verbose & VERB_FULL )
1172 // cout << mag << tab << fom << tab << bestcv << endl;
1173  if ( verbose & VERB_FULL )
1174  cout << magx << tab << magy << tab << fom << tab << bestcv << endl;
1175  }
1176  }
1177  }
1178  }
1179 
1180  p->sampling(bestsam);
1181  p->origin(bestorigin);
1182 // p->image->magnification(bestmag);
1183  p->image->FOM(bestcv);
1184  if ( def ) { // Make sure the defocus is reasonable
1185  if ( bestdef > def - 5*def_std && bestdef < def + 5*def_std )
1186  em_ctf->defocus_average(bestdef);
1187  else
1188  em_ctf->defocus_average(def);
1189  }
1190 
1191  return bestfom;
1192 }
CHECK_PPX
#define CHECK_PPX
Definition: mg_processing.h:27
views_for_refinement
View * views_for_refinement(View theview, double alpha_step)
Generates a 3x3 grid of views around an input view.
Definition: View.cpp:285
Bmicrograph::id
Bstring id
Definition: mg_processing.h:203
Bmicrograph
Definition: mg_processing.h:201
View::next
View * next
Definition: View.h:28
WRITE_PPX
#define WRITE_PPX
Definition: mg_processing.h:26
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
symmetry_get_all_views
View * symmetry_get_all_views(Bsymmetry *sym, View asu_view)
Get all symmetry-related views of one given view.
Definition: symmetry.cpp:442
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bimage::shift
int shift(Vector3< double > vec, int fill_type=0, double fill=0)
Shifts an image.
Definition: Bimage_transform.cpp:589
part_refine_orientation
long part_refine_orientation(Bparticle *part, Bstring &partfile, Bimage *pref, Bimage *pmask, Bsymmetry *sym, int max_iter, double alpha_step, double accuracy, double shift_step, double shift_accuracy, double hi_res, double lo_res, int fom_type, vector< double > weight, double edge_radius, CTFparam *em_ctf, double def_std, double shift_std, double view_std, double max_angle, double max_mag, int flags, FSI_Kernel *kernel, fft_plan planf)
Refine the orientation and origin of one particle with respect to a reference map.
Definition: mg_refine.cpp:495
kill_list
long kill_list(char *list, unsigned long size)
Frees all the items in a linked list.
Definition: linked_list.cpp:214
Complex
Definition: Complex.h:23
Bparticle::view
View view
Definition: mg_processing.h:102
NFOM
#define NFOM
Definition: mg_processing.h:16
Complex::real
Type real()
Definition: Complex.h:101
vector3_xy_random_gaussian
Vector3< double > vector3_xy_random_gaussian(double avg, double std)
Generates a random vector within a random gaussian-distributed length in the xy plane.
Definition: random_numbers.cpp:522
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
CTFparam::update
int update(CTFparam *ctf)
Definition: ctf.h:81
vector3_random_gaussian
Vector3< double > vector3_random_gaussian(double avg, double std)
Generates a random vector within a random gaussian-distributed length.
Definition: random_numbers.cpp:502
Bparticle
Definition: mg_processing.h:90
img_fit_in_recip_space_grid
double img_fit_in_recip_space_grid(Bimage *p, Bimage *pref, double hi_res, double lo_res, double step, double accuracy, int fom_type, vector< double > &weight, CTFparam *em_ctf, double def_std, FSI_Kernel *kernel, long &number)
Definition: mg_refine.cpp:861
Bproject::field
Bfield * field
Definition: mg_processing.h:346
Bimage::central_section
Bimage * central_section(Matrix3 mat, double resolution, FSI_Kernel *kernel)
Calculates a central section of a 3D fourier transform.
Definition: Bimage_project.cpp:287
Bimage::copy
Bimage * copy()
Copies the header information and data of an image into a new image structure.
Definition: Bimage.cpp:2702
img_recip_space_fom
double img_recip_space_fom(Bimage *p, Bimage *pref, double hi_res, double lo_res, int fom_type, vector< double > &weight, int apply_ctf, CTFparam *em_ctf)
Definition: mg_refine.cpp:663
FOMType
FOMType
Definition: mg_processing.h:38
CTFparam::astigmatism_angle
double astigmatism_angle()
Definition: ctf.h:110
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
random_numbers.h
Header file for functions for creating random images.
Bparticle::ast
double ast
Definition: mg_processing.h:98
Matrix3.h
3x3 Matrix manipulation functions
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
Complex.h
Class for complex numbers.
Bparticle::fom
double fom[NFOM]
Definition: mg_processing.h:103
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
Bparticle::mg
Bmicrograph * mg
Definition: mg_processing.h:105
Bmicrograph::part
Bparticle * part
Definition: mg_processing.h:242
random_gaussian
double random_gaussian(double avg, double std)
Generates a value with a gaussian random distribution.
Definition: random_numbers.cpp:123
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
View
Definition: View.h:26
Bsub_image::view
void view(double x, double y, double z, double a)
Definition: Bimage.h:157
ppx_filename
Bstring ppx_filename(Bstring &id, int part_id)
Definition: rwmg.cpp:415
Bmicrograph::fpart
Bstring fpart
Definition: mg_processing.h:208
Bfield
Definition: mg_processing.h:260
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
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
CTFparam::calculate
double calculate(double s2, double angle)
Definition: ctf.h:159
project
Bproject * project
Definition: bshow.cpp:33
Matrix3
Definition: Matrix3.h:18
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
rwmg.h
Header file for reading and writing micrograph parameters.
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::change_transform_size
Vector3< double > change_transform_size(Vector3< long > nusize)
Resizes a "standard" transform.
Definition: Bimage_fft.cpp:239
Vector3::normalize
double normalize()
Definition: Vector3.h:238
project_refine_orientations
long project_refine_orientations(Bproject *project, Bstring &reffile, Bstring &maskfile, Bstring &sym_string, int part_select, int max_iter, double alpha_step, double accuracy, double shift_step, double shift_accuracy, int fom_type, vector< double > weight, double hi_res, double lo_res, int kernel_width, int kernel_power, double edge_radius, double def_std, double shift_std, double view_std, double max_angle, double max_mag, int flags)
Definition: mg_refine.cpp:251
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
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:46
Bimage::fft
int fft()
Definition: Bimage.h:830
verbose
int verbose
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
mg_ctf.h
Functions for CTF (contrast transfer function) processing.
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::compound_type
CompoundType compound_type()
Definition: Bimage.h:364
Bproject
Definition: mg_processing.h:336
part_select
Tcl_Obj * part_select(Bparticle *part, int objc, Tcl_Obj *CONST objv[])
Definition: tcltk_bmg.cpp:563
Bparticle::sel
long sel
Definition: mg_processing.h:104
Bparticle::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:99
C_curve
vector< double > C_curve(long n, double freq_step)
Calculates the carbon scattering curve.
Definition: ctf.cpp:515
CTFparam::defocus_deviation
double defocus_deviation()
Definition: ctf.h:108
Complex::imag
Type imag()
Definition: Complex.h:102
Complex::phi
double phi()
Definition: Complex.h:105
img_refine_monte
double img_refine_monte(Bimage *p, Bimage *pref, double hi_res, double lo_res, int max_iter, int fom_type, vector< double > &weight, CTFparam *em_ctf, double def_std, double shift_std, double view_std, double max_angle, double max_mag, FSI_Kernel *kernel, long &number)
Refine the orientation and origin of one particle with respect to a reference map.
Definition: mg_refine.cpp:977
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
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
Bsub_image::magnification
double magnification()
Definition: Bimage.h:163
Bimage::check_resolution
void check_resolution(double &resolution)
Checks that the resolution falls within reasonable limits.
Definition: Bimage.cpp:635
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
Bstring::length
long length() const
Definition: Bstring.cpp:263
Bparticle::def
double def
Definition: mg_processing.h:96
init_point_group_symmetry
Bsymmetry * init_point_group_symmetry(Bstring symmetry_string)
Sets up a symmetry structure from a string identifier.
Definition: symmetry.cpp:39
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:41
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
CTFparam
Definition: ctf.h:28
rwimg.h
Header file for 2D and 3D image I/O.
Bmicrograph::ctf
CTFparam * ctf
Definition: mg_processing.h:241
Bparticle::dev
double dev
Definition: mg_processing.h:97
Bfield::next
Bfield * next
Definition: mg_processing.h:261
Bparticle::ori
Vector3< double > ori
Definition: mg_processing.h:101
mg_processing.h
Header file for micrograph processing.
mg_refine.h
Reciprocal space refinement of orientation parameters of particle images.
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
project_mg_particle_array
Bparticle ** project_mg_particle_array(Bproject *project, int part_select, long &npart)
Sets up an array of selected particle pointers.
Definition: mg_processing.cpp:1282
CTFparam::defocus_average
double defocus_average()
Definition: ctf.h:106
ppx_check
int ppx_check(Bparticle *part, FOMType fom_tag[NFOM])
Definition: rwmg.cpp:441
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
Bparticle::next
Bparticle * next
Definition: mg_processing.h:91
kill_symmetry
int kill_symmetry(Bsymmetry *sym)
Deallocates a symmetry structure.
Definition: symmetry.cpp:207
linked_list.h
Header file for generalized linked list functions.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:284
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bmicrograph::pixel_size
Vector3< double > pixel_size
Definition: mg_processing.h:215
irm
double irm(1.0L/get_rand_max())
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
View::matrix
Matrix3 matrix()
Definition: View.h:153
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
Bimage::phase_shift_to_origin
int phase_shift_to_origin()
Phase shifts a set of reflections to the image origin.
Definition: Bimage_complex.cpp:327
img_refine_grid
double img_refine_grid(Bimage *p, Bimage *pref, double hi_res, double lo_res, double alpha_step, double accuracy, double shift_step, double shift_accuracy, double max_mag, int fom_type, vector< double > &weight, CTFparam *em_ctf, double def_std, FSI_Kernel *kernel, long &number)
Refine the orientation and origin of one particle with respect to a reference map.
Definition: mg_refine.cpp:1083
write_particle
int write_particle(Bstring &filename, Bparticle *part, int euler_flag, int omega_flag, FOMType fom_tag[NFOM])
Definition: rwmg.cpp:406
FOM_CV
@ FOM_CV
Definition: mg_processing.h:42
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
Bparticle::mag
double mag
Definition: mg_processing.h:95
img_fit_in_recip_space_monte
double img_fit_in_recip_space_monte(Bimage *p, Bimage *pref, double hi_res, double lo_res, int max_iter, int fom_type, vector< double > &weight, CTFparam *em_ctf, double def_std, double shift_std, FSI_Kernel *kernel, long &number)
Definition: mg_refine.cpp:782
Complex::amp
double amp()
Definition: Complex.h:104
Float
@ Float
Definition: utilities.h:161
View::angle
double angle()
Definition: View.h:98
Bmicrograph::next
Bmicrograph * next
Definition: mg_processing.h:202
Bfield::mg
Bmicrograph * mg
Definition: mg_processing.h:267
Bparticle::id
int id
Definition: mg_processing.h:93
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:596
Vector3::length2
double length2()
Definition: Vector3.h:221
kernel
Definition: kernlut.h:14
FOM
@ FOM
Definition: mg_processing.h:40
FSI_Kernel
Definition: FSI_Kernel.h:14
mg_refine_orientations
long mg_refine_orientations(Bproject *project, Bstring &reffile, Bstring &maskfile, Bstring &sym_string, int part_select, int max_iter, double alpha_step, double accuracy, double shift_step, double shift_accuracy, int fom_type, vector< double > weight, double hi_res, double lo_res, int kernel_width, int kernel_power, double edge_radius, double def_std, double shift_std, double view_std, double max_angle, double max_mag, int flags)
Refine the orientation and origin with respect to a reference map.
Definition: mg_refine.cpp:77
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