Bsoft  2.1.0
Bernard's software package
Bimage_search.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 //#include "rwimg.h"
11 #include "options.h"
12 #include "utilities.h"
13 #include "timer.h"
14 
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17 
34 double Bimage::search_views(Bimage* ptemp, View* views,
35  double hires, double lores, double search_radius,
36  Bimage* pmask, View& currview, Vector3<double>& currshift)
37 {
38  long i, nviews, ibest(0);
39  double best(-1e37);
40  View bestview;
41  View* v;
42 
43  for ( nviews=0, v=views; v; v=v->next ) nviews++;
44 
45  View* view_arr = new View[nviews];
46  double* cc = new double[nviews];
47  Vector3<double>* translate = new Vector3<double>[nviews];
48 
49  for ( i=0, v=views; v; v=v->next, i++ ) view_arr[i] = *v;
50 
51  if ( search_radius < 0 ) search_radius = ptemp->sizeX()/4;
52 
54  ptemp->change_type(Float);
55  rescale_to_avg_std(0, 1);
56  ptemp->rescale_to_avg_std(0, 1);
58  ptemp->calculate_background();
59 
60  Vector3<double> ori(ptemp->image->origin());
61 
62  if ( ori.length() == 0 ) {
63  ori = ptemp->size()/2;
64  ptemp->origin(ori);
65  }
66 
67  if ( verbose ) {
68  cout << "Finding a template in a map:" << endl;
69  cout << "Template: " << ptemp->file_name() << endl;
70  cout << "Map: " << file_name() << endl;
71  cout << "Template origin: " << ptemp->image->origin() << endl;
72  cout << "Map origin: " << image->origin() << endl;
73  cout << "Number of views: " << nviews << endl;
74  cout << "Resolution range: ";
75  if ( lores <= 0 ) cout << "inf";
76  else cout << lores;
77  cout << " - " << hires << " A" << endl;
78  cout << "Shift search radius: " << search_radius << " pixels" << endl;
79  if ( pmask )
80  cout << "Frequency space mask: " << pmask->file_name() << endl;
81  }
82 
83  fft_plan planf = fft_setup_plan(ptemp->size(), FFTW_FORWARD, 1);
84  fft_plan planb = fft_setup_plan(ptemp->size(), FFTW_BACKWARD, 1);
85 
86 #ifdef HAVE_GCD
87  dispatch_apply(nviews, dispatch_get_global_queue(0, 0), ^(size_t k){
88  translate[k] = rotate_cross_correlate(ptemp, view_arr[k], hires, lores,
89  search_radius, pmask, cc[k], planf, planb);
90  });
91 #else
92 #pragma omp parallel for
93  for ( i=0; i<nviews; i++ ) {
94  translate[i] = rotate_cross_correlate(ptemp, view_arr[i], hires, lores,
95  search_radius, pmask, cc[i], planf, planb);
96  }
97 #endif
98 
99  if ( verbose & VERB_RESULT )
100  cout << "View\ttx\tty\ttz\tvx\tvy\tvz\tva\tCC" << endl;
101  for ( i=0; i<nviews; i++ ) {
102  if ( best < cc[i] ) {
103  best = cc[i];
104  ibest = i;
105  }
106  if ( verbose & VERB_RESULT )
107  cout << i << tab << setprecision(2) << translate[i] << tab
108  << setprecision(4) << view_arr[i] << tab << cc[i] << endl;
109  }
110 
111  bestview = view_arr[ibest];
112 
115 
116  image->origin(ptemp->image->origin() + translate[ibest]);
117  view(bestview);
118  image->FOM(best);
119 
120  if ( verbose )
121  cout << "Best view:\t" << ibest << tab << setprecision(2) << translate[ibest] << tab
122  << setprecision(4) << bestview << tab << best << endl;
123 
124  delete[] cc;
125  delete[] view_arr;
126 
127  currview = bestview;
128  currshift = translate[ibest];
129 
130  delete[] translate;
131 
132  return best;
133 }
134 
152  double hires, double lores, Bimage* pmask,
153  double threshold, Bimage* pfit)
154 {
155  Vector3<double> translate;
156  Matrix3 mat = view.matrix();
157  mat = mat.transpose();
158 
159  Bimage* prot = NULL;
160 
161  if ( z > 1 ) prot = ptemp->rotate(size(), mat);
162  else prot = ptemp->rotate_project(mat, translate, 0);
163 
164 // if ( z == 1 ) prot->project('z');
165 
166 // write_img("t.mrc", ptemp, 0);
167 // write_img("t.mrc", prot, 0);
168 
169 // bexit(-1);
170 
171  check_if_same_size(prot);
172 
173  Bimage* pcc = cross_correlate(prot, hires, lores, pmask);
174 
175  long i;
176  double fommax(0);
177 
178  for ( i=0; i<datasize; i++ ) {
179  if ( (*pfit->next)[i] < (*pcc)[i] ) {
180  pfit->next->set(i, (*pcc)[i]);
181  pfit->set(i, view);
182  }
183  if ( fommax < (*pcc)[i] ) fommax = (*pcc)[i];
184  }
185 
186  delete prot;
187  delete pcc;
188 
189  return fommax;
190 }
191 
208 Bimage* Bimage::search_volume(Bimage* ptemp, View* view, double alpha,
209  double alpha_step, double hires, double lores,
210  Bimage* pmask, double threshold)
211 {
212  int mode(0); // Mode: 0=global, 1=refine, 2=symmetry
213  long i, nviews;
214  double alpha_min(0);
215  double alpha_max(TWOPI - alpha_step/2);
216  double a;
217  double fommin(1), fommax(0), fomavg(0), fomstd(0);
218  View* v;
219 
220  if ( alpha >= 0 ) { // Case of refinement
221  mode = 1;
222  alpha_min = alpha - alpha_step;
223  alpha_max = alpha + alpha_step;
224  } else if ( alpha_step >= 1.999*M_PI ) mode = 2; // Symmetry
225 
227  ptemp->change_type(Float);
228  rescale_to_avg_std(0, 1);
229  ptemp->rescale_to_avg_std(0, 1);
231  ptemp->calculate_background();
232 
233  Vector3<double> ori(image->origin());
234 
235  if ( ori.sum() == 0 ) {
236  ori = size()/2;
237  origin(ori);
238  }
239 
240  for ( nviews=0, v=view; v; v=v->next )
241  for ( a=alpha_min; a<=alpha_max; a+=alpha_step ) nviews++;
242 
243  View* view_arr = new View[nviews];
244 
245  for ( i=0, v=view; v; v=v->next )
246  for ( a=alpha_min; a<=alpha_max; a+=alpha_step, i++ ) {
247  view_arr[i] = *v;
248  if ( mode < 2 ) view_arr[i][3] = a;
249  }
250 
251  if ( verbose ) {
252  cout << "Finding a template in a map:" << endl;
253  cout << "Number of views: " << nviews << endl;
254  cout << "Alpha step size: " << alpha_step*180.0/M_PI << endl;
255  cout << "Origin: " << ori << endl;
256  }
257 
258  Bimage* pfit = new Bimage(Float, TView, size(), 1);
259  pfit->next = new Bimage(Float, TSimple, size(), 1);
260  pfit->sampling(sampling(0));
261 
262  if ( verbose & VERB_RESULT )
263  cout << "View\tCCmax" << endl;
264 
265  for ( i=0; i<nviews; i++ ) {
266  fommax = search_volume_view(ptemp, view_arr[i], hires, lores, pmask, threshold, pfit);
267  if ( verbose )
268  cout << view_arr[i] << tab << fommax << endl;
269  }
270 
271  for ( i=0, fommax=-1, fommin=1, fomavg=fomstd=0; i<datasize; i++ ) {
272  a = (*pfit->next)[i];
273  if ( fommin > a ) fommin = a;
274  if ( fommax < a ) fommax = a;
275  fomavg += a;
276  fomstd += a*a;
277  }
278 
279  delete[] view_arr;
280 
281  if ( verbose ) {
282  fomavg /= datasize;
283  fomstd = fomstd/datasize - fomavg*fomavg;
284  if ( fomstd > 0 ) fomstd = sqrt(fomstd);
285  else fomstd = 0;
286  cout << "FOM minimum and maximum: " << fommin << " " << fommax << endl;
287  cout << "FOM average: " << fomavg << endl;
288  cout << "FOM standard deviation: " << fomstd << endl << endl;
289  }
290 
291  return pfit;
292 }
293 
294 
TView
@ TView
Definition: Bimage.h:62
View::next
View * next
Definition: View.h:28
Bimage::cross_correlate
Bimage * cross_correlate(Bimage *p, double hires, double lores, fft_plan planf, fft_plan planb)
Calculates a cross-correlation map by Fast Fourier transformation.
Definition: Bimage_correlate.cpp:363
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
Bimage::next
Bimage * next
Definition: Bimage.h:179
Matrix3::transpose
Matrix3 transpose() const
Definition: Matrix3.h:268
Bimage::Bimage
Bimage()
Initializes an image.
Definition: Bimage.cpp:43
Vector3< double >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
options.h
Header file for option handlers.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bimage::rotate_project
Bimage * rotate_project(Matrix3 mat, Vector3< double > translate, double radial_cutoff, int norm_flag=1)
Rotates a 3D map and projects it along the z-axis.
Definition: Bimage_project.cpp:118
View
Definition: View.h:26
Bsub_image::FOM
double FOM()
Definition: Bimage.h:165
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
planf
fft_plan planf
Definition: bico.cpp:24
Matrix3
Definition: Matrix3.h:18
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
TSimple
@ TSimple
Definition: Bimage.h:59
fft_destroy_plan
int fft_destroy_plan(fft_plan plan)
Deallocates a plan for fast Fourier transforms.
Definition: fft.cpp:87
Bimage::file_name
string & file_name()
Definition: Bimage.h:246
Bimage::rotate
int rotate()
Rotates an image using parameters defined in the image in place.
Definition: Bimage_transform.cpp:255
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
Bimage::rotate_cross_correlate
Vector3< double > rotate_cross_correlate(Bimage *pref, View view, double hires, double lores, double search_radius, Bimage *pmask, double &cc, fft_plan planf, fft_plan planb)
Definition: Bimage_correlate.cpp:801
Bimage
General image parameter class.
Definition: Bimage.h:177
verbose
int verbose
timer.h
Utilities for timing functions.
Bimage.h
Header file for image class.
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
Vector3::sum
double sum()
Definition: Vector3.h:236
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
fft_setup_plan
fft_plan fft_setup_plan(long x, long y, long z, fft_direction dir, int opt)
Sets up a plan for fast Fourier transforms.
Definition: fft.cpp:31
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Bimage::search_volume_view
double search_volume_view(Bimage *ptemp, View view, double hires, double lores, Bimage *pmask, double threshold, Bimage *pfit)
Searches a 2D/3D density map for a template using a specific view.
Definition: Bimage_search.cpp:151
fft_plan
fftwf_plan fft_plan
Definition: fft.h:20
Bimage::sampling
vector< Vector3< double > > sampling()
Definition: Bimage.h:529
Bimage::search_views
double search_views(Bimage *ptemp, View *view, double hires, double lores, double search_radius, Bimage *pmask, View &currview, Vector3< double > &currshift)
Searches a 2D/3D density map for a template.
Definition: Bimage_search.cpp:34
planb
fft_plan planb
Definition: bico.cpp:24
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
Bimage::search_volume
Bimage * search_volume(Bimage *ptemp, View *view, double alpha, double alpha_step, double hires, double lores, Bimage *pmask, double threshold)
Searches a 2D/3D density map for a template.
Definition: Bimage_search.cpp:208
Vector3::length
double length()
Definition: Vector3.h:222
Float
@ Float
Definition: utilities.h:161
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:596
Bimage::check_if_same_size
bool check_if_same_size(Bimage *p)
Check if two images are the same size.
Definition: Bimage.cpp:566
TWOPI
#define TWOPI
Definition: utilities.h:72
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