Bsoft  2.1.0
Bernard's software package
Bimage_reconstruct.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "linked_list.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
26  double part_weight, int interp_type)
27 {
28  if ( !next ) next = new Bimage(Float, TSimple, size(), n);
29  if ( !next->next ) next->next = new Bimage(Float, TSimple, size(), n);
30  if ( !next->next->next ) next->next->next = new Bimage(Float, TSimple, size(), n);
31 
32  long xx, yy, zz, j, jx, jy, jz;
33  double w(part_weight);
34  Vector3<long> coor;
35  Vector3<double> dist;
36 
37  float* power = (float *) next->data_pointer();
38  float* weight = (float *) next->next->data_pointer();
39  float* weight2 = (float *) next->next->next->data_pointer();
40 
41  if ( interp_type < 2 ) { // Nearest neighbour
42  coor = Vector3<long>((long) floor(m[0] + 0.5),
43  (long) floor(m[1] + 0.5),
44  (long) floor(m[2] + 0.5));
45  if ( interp_type == 1 ) { // Weighted nearest neighbour
46  dist = m - coor;
47  w *= 1 - dist.length();
48  }
49  if ( w > 0 ) {
50  j = index_wrap(coor);
51  add(j, cv * w);
52  power[j] += w*cv.power();
53  weight[j] += w;
54  weight2[j] += w*w;
55  }
56  } else if ( interp_type == 2 ) { // Trilinear interpolation
57  coor = Vector3<long>((long) floor(m[0]),
58  (long) floor(m[1]),
59  (long) floor(m[2]));
60  dist = m - coor;
61  for ( zz=0; zz<2; zz++ ) {
62  jz = coor[2] + zz;
63  if ( jz < 0 ) jz += z;
64  else if ( jz >= z ) jz -= z;
65  dist[2] = 1.0 - dist[2];
66  for ( yy=0; yy<2; yy++ ) {
67  jy = coor[1] + yy;
68  if ( jy < 0 ) jy += y;
69  else if ( jy >= y ) jy -= y;
70  dist[1] = 1.0 - dist[1];
71  for ( xx=0; xx<2; xx++ ) {
72  jx = coor[0] + xx;
73  if ( jx < 0 ) jx += x;
74  else if ( jx >= x ) jx -= x;
75  dist[0] = 1.0 - dist[0];
76  w = dist.volume() * part_weight;
77  j = index(jx, jy, jz);
78  add(j, cv * w);
79  power[j] += w*cv.power();
80  weight[j] += w;
81  weight2[j] += w*w;
82  }
83  }
84  }
85  }
86 
87  return 0;
88 }
89 
107 int Bimage::fspace_pack_2D(Bimage* p, Matrix3 mat, double hi_res,
108  double lo_res, Vector3<double> scale, double part_weight, int interp_type)
109 {
110  double min_rad_sq = (lo_res)? sampling(0)[0]/lo_res: 0;
111  double max_rad_sq = (hi_res)? sampling(0)[0]/hi_res: 0.5;
112  if ( max_rad_sq > 0.5 ) max_rad_sq = 0.5;
113  min_rad_sq *= x;
114  max_rad_sq *= x;
115  max_rad_sq += 1; // Add one pixel width additional interpolation to be removed later
116  min_rad_sq *= min_rad_sq;
117  max_rad_sq *= max_rad_sq;
118 
119  long i, xx, yy;
120  long hx = (p->x - 1)/2, hy = (p->y - 1)/2;
121  double d, w;
122  Vector3<double> m, iv;
123  Vector3<double> vscale(x/(scale[0]*p->x), y/(scale[1]*p->y), 1);
124 
125  mat[2] *= z*1.0/x;
126 
127  if ( verbose & VERB_FULL )
128  cout << "Packing an image into reciprocal space up to " << hi_res << " A resolution" << endl;
129 
130  if ( verbose & VERB_DEBUG )
131  cout << mat << endl;
132 
133  for ( yy=i=0; yy<p->y; yy++ ) {
134  iv[1] = yy;
135  if ( yy > hy ) iv[1] -= (double)p->y;
136  iv[1] *= vscale[1];
137  for ( xx=0; xx<p->x; xx++, i++ ) {
138  iv[0] = xx;
139  if ( xx > hx ) iv[0] -= (double)p->x;
140  iv[0] *= vscale[0];
141  d = iv.length2();
142  if ( d >= min_rad_sq && d <= max_rad_sq ) {
143  d = max_rad_sq - d;
144  m = mat * iv;
145  w = part_weight;
146  if ( d < 1 ) w *= sqrt(d);
148  w, interp_type);
149  }
150  }
151  }
152 
153  return 0;
154 }
155 
174 int Bimage::fspace_pack_2D(Bimage* p, View asu_view, Bsymmetry* sym, double hi_res,
175  double lo_res, Vector3<double> scale, double part_weight, int interp_type)
176 {
177  View* v;
178  View* view = symmetry_get_all_views(sym, asu_view);
179 
180  if ( verbose & VERB_DEBUG ) {
181  cout << "DEBUG Bimage::fspace_pack_2D: sym=" << sym->label << endl;
182  cout << "DEBUG Bimage::fspace_pack_2D: view=" << asu_view << endl;
183  cout << "DEBUG Bimage::fspace_pack_2D: part_weight=" << part_weight << endl;
184  }
185 
186  for ( v=view; v; v=v->next )
187  fspace_pack_2D(p, v->matrix(), hi_res, lo_res, scale, part_weight, interp_type);
188 
189  kill_list((char *) view, sizeof(View));
190 
191  return 0;
192 }
193 
213  long ft_size, double scale, double hi_res, double lo_res,
214  Matrix3 matr, Matrix3 mat)
215 {
216  if ( !p->data_pointer() ) return 0;
217 
218  check_resolution(hi_res);
219 
220  if ( verbose & VERB_FULL )
221  cout << "Packing an image into reciprocal space up to " << hi_res << " A resolution" << endl << endl;
222 
223  long i, j, xx, yy, kx, ky;
224  long ix, iy, ixx, iyy, nover(0);
225  double w, dx, dy, d2;
226  Vector3<double> m, d(0,0,1), iv;
227 
228  Vector3<double> vscale(x/(scale*ft_size), y/(scale*ft_size), 1/scale);
229 
230  float* fom = (float *) next->data_pointer();
231 
232  if ( verbose & VERB_FULL )
233  cout << mat << endl;
234 
235  double maxrad = p->image->sampling()[0]/hi_res;
236  double maxrad_sq = maxrad*maxrad; // Maximum radius squared on map scale
237  double ymin = floor(-maxrad*p->y);
238  double ymax = -ymin;
239  double xmin = floor(-maxrad*p->x);
240  double xmax = -xmin;
241 
242 // matr = matr.transpose();
243 
244  // Vector to calculate z-shift
246 // Vector3<double> vn(mat[2][2]/mat[2][0], mat[2][2]/mat[2][1], 0);
247 // cout << vn << tab << vscale << endl;
248 
250 // matr = vscale * matr;
251 
252  matr = mat * matr.transpose();
253 // matr = matr * mat.transpose();
254 // matr = matr.transpose() * mat;
255 // matr = mat.transpose() * matr;
256 
257  for ( iv[1]=ymin; iv[1]<=ymax; iv[1]+=1 ) {
258  if ( iv[1] >= 0 ) yy = (long)iv[1];
259  else yy = (long)(iv[1] + p->y);
260  dy = iv[1]/p->y;
261  for ( iv[0]=xmin; iv[0]<=xmax; iv[0]+=1 ) {
262  if ( iv[0] >= 0 ) xx = (long)iv[0];
263  else xx = (long)(iv[0] + p->x);
264  dx = iv[0]/p->x;
265  d2 = dx*dx + dy*dy;
266  if ( d2 <= maxrad_sq ) {
267  m = matr * iv;
271 // d[2] = fabs(m[2]);
272  if ( m[2] <= 1 ) {
273 // m = iv * 2 - m;
274  ix = (long) floor(m[0]);
275  iy = (long) floor(m[1]);
276  d[0] = m[0] - ix;
277  d[1] = m[1] - iy;
278  i = yy*p->x + xx;
279  for ( ky=0; ky<2; ky++ ) {
280  iyy = iy + ky;
281  if ( iyy < 0 ) iyy += y;
282  d[1] = 1 - d[1];
283  for ( kx=0; kx<2; kx++ ) {
284  ixx = ix + kx;
285  if ( ixx < 0 ) ixx += x;
286  d[0] = 1 - d[0];
287  j = iyy*x + ixx;
288  w = d[0]*d[1];
289  fom[j] += w;
290  add(j, p->complex(i) * w);
291  }
292  }
293  nover++;
294  }
295  }
296  }
297  }
298 
299  return nover;
300 }
301 
302 
314 int Bimage::fspace_pack_3D(Bimage* p, double hi_res, double threshold)
315 {
316  if ( !next ) next = new Bimage(Float, TSimple, size(), n);
317  if ( !next->next ) next->next = new Bimage(Float, TSimple, size(), n);
318  if ( !next->next->next ) next->next->next = new Bimage(Float, TSimple, size(), n);
319 
320  check_resolution(hi_res);
321 
322  long i, xx, yy, zz;
323  double rx, ry, rz, r2, maxrad, maxrad2, pwr, w;
324  Vector3<long> h((size()-1)/2);
325  Vector3<double> freq_scale(1.0/real_size());
326  Complex<float> cv;
327 
328  float* power = (float *) next->data_pointer();
329  float* weight = (float *) next->next->data_pointer();
330  float* weight2 = (float *) next->next->next->data_pointer();
331 
332  double rad_scale = real_size().max(), rad_scale2 = rad_scale*rad_scale;
333 
334  if ( hi_res > 0.1 )
335  maxrad = rad_scale/hi_res;
336  else
337  maxrad = size().max()/2.0;
338  maxrad2 = maxrad*maxrad;
339 
340  for ( i=zz=0; zz<z; zz++ ) {
341  rz = zz;
342  if ( zz > h[2] ) rz -= z;
343  rz *= freq_scale[2];
344  rz *= rz;
345  for ( yy=0; yy<y; yy++ ) {
346  ry = yy;
347  if ( yy > h[1] ) ry -= y;
348  ry *= freq_scale[1];
349  ry *= ry;
350  for ( xx=0; xx<x; xx++, i++ ) {
351  rx = xx;
352  if ( xx > h[0] ) rx -= x;
353  rx *= freq_scale[0];
354  rx *= rx;
355  cv = p->complex(i);
356  pwr = cv.power();
357  if ( pwr >= threshold ) {
358  r2 = rad_scale2*(rx + ry + rz);
359  if ( r2 <= maxrad2 ) {
360  w = 1;
361  add(i, cv);
362  power[i] += pwr;
363  weight[i] += w;
364  weight2[i] += w*w;
365  }
366  }
367  }
368  }
369  }
370 
371  return 0;
372 }
373 
385 {
386 // cout << "adding" << endl;
387 
388  add(p);
389 
390  if ( next && p->next ) {
391  next->add(p->next);
392 
393  if ( next->next && p->next->next ) {
394  next->next->add(p->next->next);
395 
396  if ( next->next->next && p->next->next->next ) next->next->next->add(p->next->next->next);
397  }
398  }
399 
400  return 0;
401 }
402 
413 {
414  long i, cov(0), ds(x*y*z);
415  Complex<double> cv;
416 
417  float* power = (float *) next->data_pointer();
418  float* weight = (float *) next->next->data_pointer();
419  float* weight2 = (float *) next->next->next->data_pointer();
420 
421  weight[0] = 0;
422 
423  for ( i=0; i<ds; i++ ) {
424  if ( weight[i] > SMALLFLOAT ) {
425  weight2[i] = weight[i] - weight2[i]/weight[i];
426  set(i, complex(i) / weight[i]);
427  if ( weight2[i] > 1 ) power[i] /= weight2[i];
428  else if ( weight[i] > 1 ) power[i] /= weight[i];
429  if ( power[i] < 0 ) power[i] = 0;
430  cov++;
431  } else {
432  set(i, cv);
433  power[i] = 0;
434  weight[i] = weight2[i] = SMALLFLOAT;
435  }
436  }
437 
438  power[0] = 1; // The zero frequency contains no information
439 
440  return cov;
441 }
442 
452 int Bimage::fspace_reconstruction_stats(double resolution, double sampling_ratio)
453 {
454 // cout << "Compound type: " << compoundtype << endl;
455 // cout << "Fourier type: " << fouriertype << endl;
456 
457  long maxrad = fspace_maximum_radius(resolution, sampling_ratio);
458 
459  Bimage* prad = fspace_radial_power(resolution, sampling_ratio);
460  Bimage* pfom = prad->next = next->radial(0, maxrad, sampling_ratio, 1);
461  Bimage* pw = prad->next->next = next->next->radial(0, maxrad, sampling_ratio, 1);
462  Bimage* pw2 = prad->next->next->next = next->next->next->radial(0, maxrad, sampling_ratio, 1);
463 
464  long i;
465  double rad_scale(real_size().max()/sampling_ratio);
466  double s, noise, signal, w, w2;
467  double res, fsc, fscp(1), snr, snrp(10), sc(0), res_fsc(0), res_snr(0);
468  double fsc_cut(0.3), snr_cut(0.5);
469 
470  if ( verbose ) {
471  cout << "Radius\ts(1/A)\tRes(A)\t"
472  << "Weight\tPower\tSignal\tNoise\tSNR\tFSC" << endl;
473  for ( i=1; i<maxrad; i++ ) {
474  s = i/rad_scale;
475  res = rad_scale/i;
476  fsc = snr = signal = noise = 0;
477  w = (*pw)[i];
478  if ( w > 1e-3 ) {
479  w2 = (*pw2)[i];
480 // w2 = w - (*pw2)[i]/w;
481 // if ( w < 1 ) w = 1;
482 // if ( w2 < 1 ) w2 = 1;
483  noise = ((*pfom)[i] - (*prad)[i])/w2;
484  signal = (w*(*prad)[i] - (*pfom)[i])/w2;
485 // if ( noise < 0 )
486 // noise = (*prad)[i] - signal;
487 // if ( noise < 0 )
488 // cout << i << tab << w2 << tab << (*prad)[i] << tab << (*pfom)[i] << tab << (*prad)[i]/(*pfom)[i] << endl;
489 // if ( signal > 0 )signal -= sqrt(noise*signal);
490 // else signal = 0;
491 // signal = ((w-2-2/w)*(*prad)[i] - (1+2/w)*(*pfom)[i])/w2;
492 // if ( signal < 0 ) signal = 0;
493  if ( noise > 0 ) snr = signal/noise;
494 // fsc = (w-(*pfom)[i]/(*prad)[i])/(w-1);
495  fsc = snr/(snr+1);
496  if ( fsc < 0 ) fsc = 0;
497  }
498  if ( fscp >= fsc_cut && fsc < fsc_cut ) {
499  sc = s - (fsc_cut - fsc)/((fscp - fsc)*rad_scale);
500  res_fsc = 1/sc;
501  }
502  if ( snrp >= snr_cut && snr < snr_cut ) {
503  sc = s - (snr_cut - snr)/((snrp - snr)*rad_scale);
504  res_snr = 1/sc;
505  }
506  fscp = fsc;
507  snrp = snr;
508  cout << i << tab << setprecision(4) << s << tab
509  << setprecision(2) << res << tab
510  << setprecision(2) << w << tab
511  << setprecision(6) << (*prad)[i] << tab
512  << setprecision(6) << signal << tab
513  << setprecision(6) << noise << tab
514  << setprecision(4) << snr << tab << fsc << endl;
515  }
516  cout << endl;
517  cout << "Resolution estimates:" << endl;
518  cout << "SNR(0.5): " << res_snr << " A" << endl;
519  cout << "FSC(0.3): " << res_fsc << " A" << endl << endl;
520  }
521 
522  delete prad;
523 
524  return 0;
525 }
526 
537 {
538  long i, cov(0), ds(x*y*z);
539  double signal, noise;
540 
541  float* power = (float *) next->data_pointer();
542  float* weight = (float *) next->next->data_pointer();
543 
544  for ( i=0; i<ds; i++ ) {
545  signal = complex(i).power();
546  noise = power[i] - signal;
547  if ( noise < signal ) noise = signal;
548 // if ( signal < SMALLFLOAT && noise < SMALLFLOAT )
549  // cout << i << tab << signal << tab << noise << endl;
550  if ( weight[i] > 1 && signal > SMALLFLOAT && noise > SMALLFLOAT ) {
551  power[i] = (weight[i]*signal - power[i])/noise;
552  cov++;
553  } else
554  power[i] = 0;
555  }
556 
557  power[0] = 1; // The zero frequency contains no information
558 
559  return cov;
560 }
View::next
View * next
Definition: View.h:28
Bimage::real_size
Vector3< double > real_size()
Definition: Bimage.h:500
Bimage::fspace_radial_power
Bimage * fspace_radial_power(double resolution, double sampling_ratio=1)
Calculates the radial power spectrum from a Fourier transform.
Definition: Bimage_fspace.cpp:610
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::fspace_reconstruction_weigh
long fspace_reconstruction_weigh()
Weighs a reconstruction.
Definition: Bimage_reconstruct.cpp:412
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
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::fspace_reconstruction_snr
long fspace_reconstruction_snr()
Calculates the SNR map.
Definition: Bimage_reconstruct.cpp:536
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
Bimage::size
Vector3< long > size() const
Definition: Bimage.h:386
View
Definition: View.h:26
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bsymmetry
Definition: symmetry.h:38
Bimage::fspace_2D_interpolate
int fspace_2D_interpolate(Complex< float > cv, Vector3< double > m, double part_weight, int interp_type)
Interpolates a 2D image for packing ito a 3D reciprocal space volume.
Definition: Bimage_reconstruct.cpp:25
Matrix3
Definition: Matrix3.h:18
TSimple
@ TSimple
Definition: Bimage.h:59
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:46
verbose
int verbose
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::power
void power(double v)
Calculates the power of an image.
Definition: Bimage.cpp:3291
Bimage::fspace_reconstruction_stats
int fspace_reconstruction_stats(double resolution, double sampling_ratio=1)
Calculates Fourier shell statistics.
Definition: Bimage_reconstruct.cpp:452
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
Bimage.h
Header file for image class.
Bimage::fspace_maximum_radius
long fspace_maximum_radius(double resolution, double sampling_ratio=1)
Calculates the maximum radius in frequency space from a given resolution.
Definition: Bimage_fspace.cpp:26
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Complex::power
double power()
Definition: Complex.h:103
Bimage::check_resolution
void check_resolution(double &resolution)
Checks that the resolution falls within reasonable limits.
Definition: Bimage.cpp:635
Bimage::fspace_reconstruction_add
long fspace_reconstruction_add(Bimage *p)
Adds all components to a reconstruction.
Definition: Bimage_reconstruct.cpp:384
Bimage::fsc
Bplot * fsc(double hi_res, double sampling_ratio=1)
Definition: Bimage_resolution.cpp:245
Bimage::fspace_pack_2D_into_central_section
long fspace_pack_2D_into_central_section(Bimage *p, long ft_size, double scale, double hi_res, double lo_res, Matrix3 matr, Matrix3 mat)
Packs a 2D Fourier transform into a central section of a 3D reciprocal space volume.
Definition: Bimage_reconstruct.cpp:212
Bimage::index
long index(long nx, long ny) const
Definition: Bimage.h:396
Vector3::volume
double volume()
Definition: Vector3.h:237
Bimage::fspace_pack_2D
int fspace_pack_2D(Bimage *p, Matrix3 mat, double hi_res, double lo_res, Vector3< double > scale, double part_weight=1, int interp_type=0)
Packs a 2D Fourier transform into a 3D reciprocal space volume.
Definition: Bimage_reconstruct.cpp:107
Bsub_image::sampling
void sampling(double x, double y, double z)
Definition: Bimage.h:127
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
Bimage::sampling
vector< Vector3< double > > sampling()
Definition: Bimage.h:529
View::matrix
Matrix3 matrix()
Definition: View.h:153
Bsymmetry::label
Bstring label
Definition: symmetry.h:41
Vector3::length
double length()
Definition: Vector3.h:222
Bimage::fspace_pack_3D
int fspace_pack_3D(Bimage *p, double hi_res=0, double threshold=0)
Packs a 3D Fourier transform into a 3D reciprocal space volume.
Definition: Bimage_reconstruct.cpp:314
Float
@ Float
Definition: utilities.h:161
Bimage::index_wrap
long index_wrap(long nx, long ny, long nz) const
Definition: Bimage.h:414
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:596
Vector3::length2
double length2()
Definition: Vector3.h:221
Vector3::max
Type max()
Definition: Vector3.h:175