Xmipp  v3.23.11-Nereus
ctf_estimate_from_micrograph.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Javier Angel Velazquez Muriel (javi@cnb.csic.es)
4  * Carlos Oscar Sanchez Sorzano
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
28 #include "ctf_enhance_psd.h"
29 #include "core/xmipp_fftw.h"
31 #include "core/transformations.h"
32 #include "core/xmipp_threads.h"
34 #include "data/basic_pca.h"
35 #include "data/normalize.h"
36 #include "data/numerical_tools.h"
37 
38 /* Read parameters ========================================================= */
40 {
43 }
44 
46 {
47  fn_micrograph = getParam("--micrograph");
48  fn_root = getParam("--oroot");
49  if (fn_root == "")
51  pieceDim = getIntParam("--pieceDim");
52  skipBorders = getIntParam("--skipBorders");
53  overlap = getDoubleParam("--overlap");
54  String aux = getParam("--psd_estimator");
55  if (aux == "periodogram")
57  else
58  {
60  ARMA_prm.readParams(this);
61  }
62  Nsubpiece = getIntParam("--Nsubpiece");
63 
64  String mode = getParam("--mode");
65  if (mode == "micrograph")
67  else if (mode == "regions")
68  {
70  fn_pos = getParam("--mode", 1);
71  }
72  else if (mode == "particles")
73  {
75  fn_pos = getParam("--mode", 1);
76  }
77  estimate_ctf = !checkParam("--dont_estimate_ctf");
78  acceleration1D = checkParam("--acceleration1D");
79 
80  if (estimate_ctf)
81  {
82  if (!acceleration1D)
84  else
86  }
87 
88  bootstrapN = getIntParam("--bootstrapFit");
89 }
90 
92 {
93  addUsageLine("Estimate the CTF from a micrograph.");
94  addUsageLine("The PSD of the micrograph is first estimated using periodogram averaging or ");
95  addUsageLine("ARMA models ([[http://www.ncbi.nlm.nih.gov/pubmed/12623169][See article]]). ");
96  addUsageLine("Then, the PSD is enhanced ([[http://www.ncbi.nlm.nih.gov/pubmed/16987671][See article]]). ");
97  addUsageLine("And finally, the CTF is fitted to the PSD, being guided by the enhanced PSD ");
98  addUsageLine("([[http://www.ncbi.nlm.nih.gov/pubmed/17911028][See article]]).");
99  addParamsLine(" --micrograph <file> : File with the micrograph");
100  addParamsLine(" [--oroot <rootname=\"\">] : Rootname for output");
101  addParamsLine(" : If not given, the micrograph without extensions is taken");
102  addParamsLine(" :++ rootname.psd or .psdstk contains the PSD or PSDs");
103  addParamsLine("==+ PSD estimation");
104  addParamsLine(" [--psd_estimator <method=periodogram>] : Method for estimating the PSD");
105  addParamsLine(" where <method>");
106  addParamsLine(" periodogram");
107  addParamsLine(" ARMA");
108  addParamsLine(" [--pieceDim <d=512>] : Size of the piece");
109  addParamsLine(" [--overlap <o=0.5>] : Overlap (0=no overlap, 1=full overlap)");
110  addParamsLine(" [--skipBorders <s=2>] : Number of pieces around the border to skip");
111  addParamsLine(" [--Nsubpiece <N=1>] : Each piece is further subdivided into NxN subpieces.");
112  addParamsLine(" : This option is useful for small micrographs in which ");
113  addParamsLine(" : not many pieces of size pieceDim x pieceDim can be defined. ");
114  addParamsLine(" :++ Note that this is not the same as defining a smaller pieceDim. ");
115  addParamsLine(" :++ Defining a smaller pieceDim, would result in a small PSD, while ");
116  addParamsLine(" :++ subdividing the piece results in a large PSD, although smoother.");
117  addParamsLine(" [--mode <mode=micrograph>] : How many PSDs are to be estimated");
118  addParamsLine(" where <mode>");
119  addParamsLine(" micrograph : Single PSD for the whole micrograph");
120  addParamsLine(" regions <file=\"\"> : The micrograph is divided into a region grid ");
121  addParamsLine(" : and a PSD is computed for each one.");
122  addParamsLine(" : The file is metadata with the position of each particle within the micrograph");
123  addParamsLine(" particles <file> : One PSD per particle.");
124  addParamsLine(" : The file is metadata with the position of each particle within the micrograph");
125  addParamsLine("==+ CTF fit");
126  addParamsLine(" [--dont_estimate_ctf] : Do not fit a CTF to PSDs");
127  addParamsLine(" [--acceleration1D] : Accelerate PSD estimation");
131  addExampleLine("Estimate PSD", false);
132  addExampleLine("xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --dont_estimate_ctf");
133  addExampleLine("Estimate a single CTF for the whole micrograph", false);
134  addExampleLine("xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --sampling_rate 1.4 --voltage 200 --spherical_aberration 2.5");
135  addExampleLine("Estimate a single CTF for the whole micrograph providing a starting point for the defocus",false);
136  addExampleLine("xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --sampling_rate 1.4 --voltage 200 --spherical_aberration 2.5 --defocusU -15000");
137  addExampleLine("Estimate a CTF per region", false);
138  addExampleLine("xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --mode regions micrograph.pos --sampling_rate 1.4 --voltage 200 --spherical_aberration 2.5 --defocusU -15000");
139  addExampleLine("Estimate a CTF per particle", false);
140  addExampleLine("xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --mode particles micrograph.pos --sampling_rate 1.4 --voltage 200 --spherical_aberration 2.5 --defocusU -15000");
141 }
142 
143 /* Construct piece smoother =============================================== */
144 template<typename T>
146  const MultidimArray<T> &piece,
147  MultidimArray<T> &pieceSmoother)
148 {
149  // Attenuate borders to avoid discontinuities
150  pieceSmoother.resizeNoCopy(piece);
151  pieceSmoother.initConstant(1);
152  pieceSmoother.setXmippOrigin();
153  T iHalfsize = (T)2 / YSIZE(pieceSmoother);
154  const T alpha = 0.025;
155  const T alpha1 = 1 - alpha;
156  const T ialpha = 1.0 / alpha;
157 
158  auto computeMaskValue = [&] (T fraction) {
159  return 0.5 * (1 + cos(PI * ((fraction - 1) * ialpha + 1)));
160  };
161 
162  for (int i = STARTINGY(pieceSmoother);
163  i <= FINISHINGY(pieceSmoother);
164  i++) {
165  T iFraction = fabs(i * iHalfsize);
166  if (iFraction > alpha1)
167  {
168  T maskValue = computeMaskValue(iFraction);
169  for (int j = STARTINGX(pieceSmoother);
170  j <= FINISHINGX(pieceSmoother); j++) {
171  A2D_ELEM(pieceSmoother,i,j) *= maskValue;
172  }
173  }
174  }
175 
176  for (int j = STARTINGX(pieceSmoother);
177  j <= FINISHINGX(pieceSmoother);
178  j++) {
179  T jFraction = fabs(j * iHalfsize);
180  if (jFraction > alpha1)
181  {
182  T maskValue = computeMaskValue(jFraction);
183  for (int i = STARTINGY(pieceSmoother);
184  i <= FINISHINGY(pieceSmoother); i++) {
185  A2D_ELEM(pieceSmoother,i,j) *= maskValue;
186  }
187  }
188  }
189 }
190 
191 /* Compute PSD by piece averaging ========================================== */
192 //#define DEBUG
195 {
196  int small_Ydim = 2 * YSIZE(piece) / Nsubpiece;
197  int small_Xdim = 2 * XSIZE(piece) / Nsubpiece;
198  MultidimArray<double> small_piece(small_Ydim, small_Xdim);
199 
200  int Xstep = (XSIZE(piece) - small_Xdim) / (Nsubpiece - 1);
201  int Ystep = (YSIZE(piece) - small_Ydim) / (Nsubpiece - 1);
202  psd.initZeros(small_piece);
203 #ifdef DEBUG
204 
205  Image<double> save;
206  save()=piece;
207  save.write("PPPpiece.xmp");
208 #endif
209 
211  MultidimArray<double> small_psd;
212 
213  // Attenuate borders to avoid discontinuities
214  MultidimArray<double> pieceSmoother;
215  constructPieceSmoother(piece, pieceSmoother);
216 
217  for (int ii = 0; ii < Nsubpiece; ii++)
218  for (int jj = 0; jj < Nsubpiece; jj++)
219  {
220  // Take the corresponding small piece from the piece
221  int i0 = ii * Xstep;
222  int j0 = jj * Ystep;
223 
224  int i, j, ib, jb;
225  for (i = 0, ib = i0; i < small_Ydim; i++, ib++)
226  for (j = 0, jb = j0; j < small_Xdim; j++, jb++)
227  DIRECT_A2D_ELEM(small_piece, i, j)=
228  DIRECT_A2D_ELEM(piece, ib, jb);
229  normalize_ramp(piece);
230  piece *= pieceSmoother;
231 
232 #ifdef DEBUG
233 
234  save()=small_piece;
235  save.write("PPPsmall_piece.xmp");
236 #endif
237 
238  // Compute the PSD of the small piece
239  small_psd.initZeros(small_piece);
240  if (PSDEstimator_mode == ARMA)
241  {
242  CausalARMA(small_piece, ARMA_prm);
243  ARMAFilter(small_piece, small_psd, ARMA_prm);
244  }
245  else
246  {
247  FourierTransform(small_piece, Periodogram);
248  FFT_magnitude(Periodogram, small_psd);
249  small_psd *= small_psd;
250  small_psd *= small_Ydim * small_Xdim;
251  }
252 
253 #ifdef DEBUG
254  save()=small_psd;
255  save.write("PPPsmall_psd.xmp");
256 #endif
257 
258  // Add to the average
259  psd += small_psd;
260  }
261 
262  // Compute the average of all the small pieces and enlarge
263  psd *= 1.0 / (Nsubpiece * Nsubpiece);
264 
265 #ifdef DEBUG
266 
267  save()=psd;
268  save.write("PPPpsd1.xmp");
269 #endif
270 
271  CenterFFT(psd, true);
273  CenterFFT(psd, false);
274  psd.threshold("below", 0, 0);
275 
276 #ifdef DEBUG
277 
278  save()=psd;
279  save.write("PPPpsd2.xmp");
280  std::cout << "Press any key\n";
281  char c;
282  std::cin >> c;
283 #endif
284 }
285 #undef DEBUG
286 
287 /* Main ==================================================================== */
288 //#define DEBUG
290 {
291  // Open input files -----------------------------------------------------
292  // Open coordinates
293  MetaDataVec posFile;
294  if (fn_pos != "")
295  posFile.read(fn_pos);
296  auto iterPosFile = posFile.ids().begin();
297 
298  // Open the micrograph --------------------------------------------------
299  Image<double> M_in;
300  size_t Ndim, Zdim, Ydim , Xdim; // Micrograph dimensions
301 
302  //ImageInfo imgInfo;
303  //getImageInfo(fn_micrograph, imgInfo);
304  //imgInfo.adim.ndim
305 
306  M_in.read(fn_micrograph,HEADER);
307  M_in.getDimensions(Xdim, Ydim, Zdim, Ndim);
308 
309  // Compute the number of divisions --------------------------------------
310  int div_Number = 0;
311  int div_NumberX=1, div_NumberY=1;
312  if (psd_mode == OnePerParticle)
313  div_Number = posFile.size();
314  else if (psd_mode == OnePerMicrograph)
315  {
316  div_NumberX = CEIL((double)Xdim / (pieceDim *(1-overlap))) - 1;
317  div_NumberY = CEIL((double)Ydim / (pieceDim *(1-overlap))) - 1;
318  div_Number = div_NumberX * div_NumberY;
319  }
320  else if (psd_mode == OnePerRegion)
321  {
322  div_NumberX = CEIL((double)Xdim / pieceDim);
323  div_NumberY = CEIL((double)Ydim / pieceDim);
324  if (div_NumberX<=2*skipBorders || div_NumberY<=2*skipBorders)
325  REPORT_ERROR(ERR_ARG_INCORRECT,formatString("The micrograph is not big enough to skip %d pieces on each side",skipBorders));
326  div_Number = div_NumberX * div_NumberY;
327  }
328 
329  // Process each piece ---------------------------------------------------
330  Image<double> psd_avg, psd_std, psd, psd2;
333  psd().resizeNoCopy(piece);
334  MultidimArray<double> &mpsd = psd();
335  MultidimArray<double> &mpsd2 = psd2();
336  PCAMahalanobisAnalyzer pcaAnalyzer;
337  MultidimArray<int> PCAmask;
339  double pieceDim2 = pieceDim * pieceDim;
340 
341  //Multidimensional data variables to store the defocus obtained locally for plane fitting
342  MultidimArray<double> defocusPlanefittingU(div_NumberX-2*skipBorders, div_NumberY-2*skipBorders);
343  MultidimArray<double> defocusPlanefittingV(defocusPlanefittingU);
344  MultidimArray<double> Xm(defocusPlanefittingU);
345  MultidimArray<double> Ym(defocusPlanefittingU);
346 
347  // Attenuate borders to avoid discontinuities
348  MultidimArray<double> pieceSmoother;
349  constructPieceSmoother(piece, pieceSmoother);
350  if (verbose)
351  std::cerr << "Computing models of each piece ...\n";
352 
353  // Prepare these filenames in case they are needed
354  FileName fn_psd;
355  if (psd_mode == OnePerMicrograph)
356  fn_psd = fn_root + ".psd";
357  else
358  fn_psd = fn_root + ".psdstk";
359  if (fileExists(fn_psd))
360  fn_psd.deleteFile();
361  if (fileExists(fn_root+".ctfparam"))
362  FileName(fn_root+".ctfparam").deleteFile();
363 
364  if (verbose)
365  init_progress_bar(div_Number);
366  int N = 1; // Index of current piece
367  size_t piecei = 0, piecej = 0; // top-left corner of the current piece
368  FourierTransformer transformer;
369  int actualDiv_Number = 0;
370 
371  for (size_t nIm = 1; nIm <= Ndim; nIm++)
372  {
373  M_in.read(fn_micrograph,DATA,nIm);
374  std::cout << "Micrograph number: " << nIm << std::endl;
375 
376  while (N <= div_Number)
377  {
378  bool skip = false;
379  int blocki=0, blockj=0;
380  // Compute the top-left corner of the piece ..........................
381  if (psd_mode == OnePerParticle)
382  {
383  // Read position of the particle
384  posFile.getValue(MDL_X, piecej, *iterPosFile);
385  posFile.getValue(MDL_Y, piecei, *iterPosFile);
386 
387  // j,i are the selfWindow center, we need the top-left corner
388  piecej -= (int) (pieceDim / 2);
389  piecei -= (int) (pieceDim / 2);
390  if (piecei < 0)
391  piecei = 0;
392  if (piecej < 0)
393  piecej = 0;
394  }
395  else
396  {
397  int step = pieceDim;
398  if (psd_mode == OnePerMicrograph)
399  step = (int) ((1 - overlap) * step);
400  blocki = (N - 1) / div_NumberX;
401  blockj = (N - 1) % div_NumberX;
402  if (blocki < skipBorders || blockj < skipBorders
403  || blocki > (div_NumberY - skipBorders - 1)
404  || blockj > (div_NumberX - skipBorders - 1))
405  skip = true;
406  piecei = blocki * step;
407  piecej = blockj * step;
408  }
409 
410  // test if the full piece is inside the micrograph
411  if (piecei + pieceDim > Ydim)
412  piecei = Ydim - pieceDim;
413  if (piecej + pieceDim > Xdim)
414  piecej = Xdim - pieceDim;
415 
416  if (!skip)
417  {
418  // Extract micrograph piece ..........................................
419 // M_in().window(piece, 0, 0, piecei, piecej, 0, 0, piecei + YSIZE(piece) - 1,
420 // piecej + XSIZE(piece) - 1);
421  window2D( M_in(), piece, piecei, piecej, piecei + YSIZE(piece) - 1, piecej + XSIZE(piece) - 1);
422  piece.statisticsAdjust(0., 1.);
423  normalize_ramp(piece);
424  piece *= pieceSmoother;
425 
426  // Estimate the power spectrum .......................................
427  if (Nsubpiece == 1)
428  if (PSDEstimator_mode == ARMA)
429  {
430  CausalARMA(piece, ARMA_prm);
431  ARMAFilter(piece, mpsd, ARMA_prm);
432  }
433  else
434  {
435  transformer.completeFourierTransform(piece, Periodogram);
436  FFT_magnitude(Periodogram, mpsd);
438  DIRECT_MULTIDIM_ELEM(mpsd,n)*=DIRECT_MULTIDIM_ELEM(mpsd,n)*pieceDim2;
439  }
440  else
441  PSD_piece_by_averaging(piece, mpsd);
442  mpsd2.resizeNoCopy(mpsd);
444  {
445  double psdval = DIRECT_MULTIDIM_ELEM(mpsd,n);
446  DIRECT_MULTIDIM_ELEM(mpsd2,n)=psdval*psdval;
447  }
448 
449  // Perform averaging if applicable ...................................
450  if (psd_mode == OnePerMicrograph)
451  {
452  actualDiv_Number += 1;
453  // Compute average and standard deviation
454  if (XSIZE(psd_avg()) != XSIZE(mpsd))
455  {
456  psd_avg() = mpsd;
457  psd_std() = psd2();
458  }
459  else
460  {
461  psd_avg() += mpsd;
462  psd_std() += psd2();
463  }
464 
465  // Keep psd for the PCA
466  if (estimate_ctf)
467  {
468  if (XSIZE(PCAmask) == 0)
469  {
470  PCAmask.initZeros(mpsd);
471  Matrix1D<int> idx(2); // Indexes for Fourier plane
472  Matrix1D<double> freq(2); // Frequencies for Fourier plane
473  size_t PCAdim = 0;
475  {
476  VECTOR_R2(idx, j, i);
477  FFT_idx2digfreq(mpsd, idx, freq);
478  double w = freq.module();
479  if (w > 0.05 && w < 0.4)
480  {
481  A2D_ELEM(PCAmask,i,j)=1;
482  ++PCAdim;
483  }
484  }
485  PCAv.initZeros(PCAdim);
486  }
487 
488  size_t ii = -1;
490  if (DIRECT_MULTIDIM_ELEM(PCAmask,n))
491  A1D_ELEM(PCAv,++ii)=(float)DIRECT_MULTIDIM_ELEM(mpsd,n);
492  pcaAnalyzer.addVector(PCAv);
493  }
494  }
495 
496  // Compute the theoretical model if not averaging ....................
497  if (psd_mode != OnePerMicrograph)
498  {
499 
500  if (bootstrapN != -1)
502  "Bootstrapping is only available for micrograph averages");
503 
504  FileName fn_psd_piece;
505  fn_psd_piece.compose(N, fn_psd);
506  psd.write(fn_psd_piece);
507  if (psd_mode == OnePerParticle)
508  posFile.setValue(MDL_PSD, fn_psd_piece, *iterPosFile);
509  if (estimate_ctf)
510  {
511  // Estimate the CTF parameters of this piece
512  prmEstimateCTFFromPSD.fn_psd = fn_psd_piece;
513  CTFDescription ctfmodel;
514 
515  ctfmodel.isLocalCTF = true;
516  ctfmodel.x0 = piecej;
517  ctfmodel.xF = (piecej + pieceDim-1);
518  ctfmodel.y0 = piecei;
519  ctfmodel.yF = (piecei + pieceDim-1);
520  ROUT_Adjust_CTF(prmEstimateCTFFromPSD, ctfmodel, false);
521 
522  int idxi=blocki-skipBorders;
523  int idxj=blockj-skipBorders;
524  A2D_ELEM(defocusPlanefittingU,idxi,idxj)=ctfmodel.DeltafU;
525  A2D_ELEM(defocusPlanefittingV,idxi,idxj)=ctfmodel.DeltafV;
526 
527  A2D_ELEM(Xm,idxi,idxj)=(piecei+pieceDim/2)*ctfmodel.Tm;
528  A2D_ELEM(Ym,idxi,idxj)=(piecej+pieceDim/2)*ctfmodel.Tm;
529 
530  //1D//
531  prmEstimateCTFFromPSDFast.fn_psd = fn_psd_piece; //Nuevo
532  CTFDescription1D ctf1Dmodel; //Nuevo
533  ctf1Dmodel.isLocalCTF = true;
534  ctf1Dmodel.x0 = piecej;
535  ctf1Dmodel.xF = (piecej + pieceDim-1);
536  ctf1Dmodel.y0 = piecei;
537  ctf1Dmodel.yF = (piecei + pieceDim-1);
538  ROUT_Adjust_CTFFast(prmEstimateCTFFromPSDFast, ctf1Dmodel, false); //Nuevo
539 
540  A2D_ELEM(defocusPlanefittingU,idxi,idxj)=ctf1Dmodel.Defocus;
541 
542  A2D_ELEM(Xm,idxi,idxj)=(piecei+pieceDim/2)*ctf1Dmodel.Tm;
543  A2D_ELEM(Ym,idxi,idxj)=(piecej+pieceDim/2)*ctf1Dmodel.Tm;
544 
546  posFile.setValue(MDL_CTF_MODEL,
547  fn_psd_piece.withoutExtension() + ".ctfparam",
548  *iterPosFile);
549  }
550  }
551  }
552  // Increment the division counter
553  ++N;
554  if (verbose)
555  progress_bar(N);
556  if (psd_mode == OnePerParticle)
557  ++iterPosFile;
558  }
559 
560  init_progress_bar(div_Number);
561  N = 1;
562  }
563  if (verbose)
564  progress_bar(div_Number);
565 
566  // If averaging, compute the CTF model ---------------------------------- //Program execution
567  if (psd_mode == OnePerMicrograph)
568  {
569  // Compute the avg and stddev of the local PSDs
570  const MultidimArray<double> &mpsd_std = psd_std();
571  const MultidimArray<double> &mpsd_avg = psd_avg();
572  double idiv_Number = 1.0 / actualDiv_Number;
574  {
575  DIRECT_MULTIDIM_ELEM(mpsd_avg,n)*=idiv_Number;
576  DIRECT_MULTIDIM_ELEM(mpsd_std,n)*=idiv_Number;
577  DIRECT_MULTIDIM_ELEM(mpsd_std,n)-=DIRECT_MULTIDIM_ELEM(mpsd_avg,n)*
578  DIRECT_MULTIDIM_ELEM(mpsd_avg,n);
579  if (DIRECT_MULTIDIM_ELEM(mpsd_std,n)<0)
580  DIRECT_MULTIDIM_ELEM(mpsd_std,n)=0;
581  else
582  DIRECT_MULTIDIM_ELEM(mpsd_std,n)=sqrt(DIRECT_MULTIDIM_ELEM(mpsd_std,n));
583  }
584  psd_avg.write(fn_psd);
585 
586  if (estimate_ctf)
587  {
588  // Estimate the CTF parameters
589  std::cerr << "Adjusting CTF model to the PSD ...\n";
590  prmEstimateCTFFromPSD.fn_psd = fn_psd;
592  CTFDescription ctfmodel;
593  CTFDescription1D ctf1Dmodel;
594 
595  if (bootstrapN == -1)
596  {
597  try {
598  // No bootstrapping
599  // Compute the PCA of the local PSDs
600  pcaAnalyzer.standardarizeVariables();
601  // pcaAnalyzer.subtractAvg();
602  pcaAnalyzer.learnPCABasis(1, 10);
603  } catch (XmippError &xe)
604  {
605  if (xe.__errno==ERR_NUMERICAL)
606  REPORT_ERROR(ERR_NUMERICAL,"There is no variance in the PSD, check that the micrograph is not constant");
607  else
608  throw xe;
609  }
610 
611 #ifdef DEBUG
612 
613  Image<double> save;
614  save().initZeros(psd());
615  int ii=-1;
617  if (PCAmask(i,j))
618  save(i,j)=pcaAnalyzer.PCAbasis[0](++ii);
619  save.write("PPPbasis.xmp");
620 #endif
621 
622  Matrix2D<double> CtY;
623  pcaAnalyzer.projectOnPCABasis(CtY);
625  CtY.toVector(p);
626  double pavg = p.sum(true);
627  double pstd = p.sum2() / VEC_XSIZE(p) - pavg * pavg;
628  pstd = (pstd < 0) ? 0 : sqrt(pstd);
629 
630  std::string psign;
632  if (p(i) < 0)
633  psign += "-";
634  else
635  psign += "+";
636  double zrandomness = checkRandomness(psign);
637 
638  if (!acceleration1D)
639  {
640  ctfmodel.isLocalCTF = false;
641  ctfmodel.x0 = 0;
642  ctfmodel.xF = (Xdim-1);
643  ctfmodel.y0 = 0;
644  ctfmodel.yF = (Ydim-1);
645  ROUT_Adjust_CTF(prmEstimateCTFFromPSD,ctfmodel, false);
646  }
647  else
648  {
649  ctf1Dmodel.isLocalCTF = false;
650  ctf1Dmodel.x0 = 0;
651  ctf1Dmodel.xF = (Xdim-1);
652  ctf1Dmodel.y0 = 0;
653  ctf1Dmodel.yF = (Ydim-1);
655  }
656  // Evaluate PSD variance and write into the CTF
657  double stdQ = 0;
659  stdQ += A2D_ELEM(mpsd_std,i,j)/A2D_ELEM(mpsd_avg,i,j);
660  stdQ /= MULTIDIM_SIZE(psd_std());
661 
662  MetaDataVec MD;
663  MD.read(fn_psd.withoutExtension() + ".ctfparam");
664  size_t id = MD.firstRowId();
665  MD.setValue(MDL_CTF_CRIT_PSDVARIANCE, stdQ, id);
667  MD.setValue(MDL_CTF_CRIT_PSDPCARUNSTEST, zrandomness, id);
668  MD.write((String)"fullMicrograph@"+fn_psd.withoutExtension() + ".ctfparam");
669  }
670  else
671  {
672  // If bootstrapping
675  if (!acceleration1D)
676  {
678  FileName fnBase = fn_psd.withoutExtension();
679  std::cerr << "Computing bootstrap ...\n";
681  for (int n = 0; n < bootstrapN; n++)
682  {
684  ctfmodel, false);
685 
686  CTFs(n, 0) = ctfmodel.Tm;
687  CTFs(n, 1) = ctfmodel.kV;
688  CTFs(n, 2) = ctfmodel.DeltafU;
689  CTFs(n, 3) = ctfmodel.DeltafV;
690  CTFs(n, 4) = ctfmodel.azimuthal_angle;
691  CTFs(n, 5) = ctfmodel.Cs;
692  CTFs(n, 6) = ctfmodel.Ca;
693  CTFs(n, 7) = ctfmodel.espr;
694  CTFs(n, 8) = ctfmodel.ispr;
695  CTFs(n, 9) = ctfmodel.alpha;
696  CTFs(n, 10) = ctfmodel.DeltaF;
697  CTFs(n, 11) = ctfmodel.DeltaR;
698  CTFs(n, 12) = ctfmodel.Q0;
699  CTFs(n, 13) = ctfmodel.K;
700  CTFs(n, 14) = ctfmodel.gaussian_K;
701  CTFs(n, 15) = ctfmodel.sigmaU;
702  CTFs(n, 16) = ctfmodel.sigmaV;
703  CTFs(n, 17) = ctfmodel.cU;
704  CTFs(n, 18) = ctfmodel.cV;
705  CTFs(n, 19) = ctfmodel.gaussian_angle;
706  CTFs(n, 20) = ctfmodel.sqrt_K;
707  CTFs(n, 21) = ctfmodel.sqU;
708  CTFs(n, 22) = ctfmodel.sqV;
709  CTFs(n, 23) = ctfmodel.sqrt_angle;
710  CTFs(n, 24) = ctfmodel.base_line;
711  CTFs(n, 25) = ctfmodel.gaussian_K2;
712  CTFs(n, 26) = ctfmodel.sigmaU2;
713  CTFs(n, 27) = ctfmodel.sigmaV2;
714  CTFs(n, 28) = ctfmodel.cU2;
715  CTFs(n, 29) = ctfmodel.cV2;
716  CTFs(n, 30) = ctfmodel.gaussian_angle2;
717 
718 
719  std::string command = (std::string) "mv -i " + fnBase
720  + ".ctfparam " + fnBase + "_bootstrap_"
721  + integerToString(n, 4) + ".ctfparam";
722  if (system(command.c_str())==-1)
723  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
724  command = (std::string) "mv -i " + fnBase
725  + ".ctfmodel_quadrant " + fnBase + "_bootstrap_"
726  + integerToString(n, 4) + ".ctfmodel_quadrant";
727  if (system(command.c_str())==-1)
728  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
729  command = (std::string) "mv -i " + fnBase
730  + ".ctfmodel_halfplane " + fnBase + "_bootstrap_"
731  + integerToString(n, 4) + ".ctfmodel_halfplane";
732  if (system(command.c_str())==-1)
733  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
734 
735  progress_bar(n);
736  }
737  progress_bar(bootstrapN);
738 
739  }
741  else
742  {
746  FileName fnBase = fn_psd.withoutExtension();
747  std::cerr << "Computing bootstrap ...\n";
749  for (int n = 0; n < bootstrapN; n++)
750  {
751  CTFs(n, 21) = ROUT_Adjust_CTFFast(prmEstimateCTFFromPSDFast,ctf1Dmodel, false);
752 
753  CTFs(n, 0) = ctf1Dmodel.Tm;
754  CTFs(n, 1) = ctf1Dmodel.kV;
755  CTFs(n, 2) = ctf1Dmodel.Defocus;
756  CTFs(n, 3) = ctf1Dmodel.Cs;
757  CTFs(n, 4) = ctf1Dmodel.Ca;
758  CTFs(n, 5) = ctf1Dmodel.espr;
759  CTFs(n, 6) = ctf1Dmodel.ispr;
760  CTFs(n, 7) = ctf1Dmodel.alpha;
761  CTFs(n, 8) = ctf1Dmodel.DeltaF;
762  CTFs(n, 9) = ctf1Dmodel.DeltaR;
763  CTFs(n, 10) = ctf1Dmodel.Q0;
764  CTFs(n, 11) = ctf1Dmodel.K;
765  CTFs(n, 12) = ctf1Dmodel.gaussian_K;
766  CTFs(n, 13) = ctf1Dmodel.sigma1;
767  CTFs(n, 14) = ctf1Dmodel.Gc1;
768  CTFs(n, 15) = ctf1Dmodel.sqrt_K;
769  CTFs(n, 16) = ctf1Dmodel.sq;
770  CTFs(n, 17) = ctf1Dmodel.base_line;
771  CTFs(n, 18) = ctf1Dmodel.gaussian_K2;
772  CTFs(n, 19) = ctf1Dmodel.sigma2;
773  CTFs(n, 20) = ctf1Dmodel.Gc2;
774 
775 
776  std::string command = (std::string) "mv -i " + fnBase
777  + ".ctfparam " + fnBase + "_bootstrap_"
778  + integerToString(n, 4) + ".ctfparam";
779  if (system(command.c_str())==-1)
780  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
781  command = (std::string) "mv -i " + fnBase
782  + ".ctf1Dmodel_quadrant " + fnBase + "_bootstrap_"
783  + integerToString(n, 4) + ".ctf1Dmodel_quadrant";
784  if (system(command.c_str())==-1)
785  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
786  command = (std::string) "mv -i " + fnBase
787  + ".ctf1Dmodel_halfplane " + fnBase + "_bootstrap_"
788  + integerToString(n, 4) + ".ctf1Dmodel_halfplane";
789  if (system(command.c_str())==-1)
790  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
791 
792  progress_bar(n);
793  }
794  progress_bar(bootstrapN);
795  }
796  }
797 
798 
799  }
800  }
801 
802  // Assign a CTF to each particle ----------------------------------------
804  {
805  double pU0 = 0, pU1 = 0, pU2 = 0;
806  planeFit(defocusPlanefittingU, Xm, Ym, pU0, pU1, pU2);
807  double pV0 = 0, pV1 = 0, pV2 = 0;
808  planeFit(defocusPlanefittingV, Xm, Ym, pV0, pV1, pV2);
809 
810  MetaDataVec MDctf;
811  MDctf.read(fn_root+".ctfparam");
812  double Tm, downsampling;
813  size_t id=MDctf.firstRowId();
814  MDctf.getValue(MDL_CTF_SAMPLING_RATE,Tm,id);
815  MDctf.getValue(MDL_CTF_DOWNSAMPLE_PERFORMED,downsampling,id);
816 
817  MetaDataVec MD;
818  MD.setColumnFormat(false);
819  id = MD.addObject();
820  MD.setValue(MDL_CTF_DEFOCUS_PLANEUA, pU1, id);
821  MD.setValue(MDL_CTF_DEFOCUS_PLANEUB, pU2, id);
822  MD.setValue(MDL_CTF_DEFOCUS_PLANEUC, pU0, id);
823  MD.setValue(MDL_CTF_DEFOCUS_PLANEVA, pV1, id);
824  MD.setValue(MDL_CTF_DEFOCUS_PLANEVB, pV2, id);
825  MD.setValue(MDL_CTF_DEFOCUS_PLANEVC, pV0, id);
826  MD.setValue(MDL_CTF_X0, 0., id);
827  MD.setValue(MDL_CTF_Y0, 0., id);
828  MD.setValue(MDL_CTF_XF, (Xdim-1)*Tm*downsampling, id);
829  MD.setValue(MDL_CTF_YF, (Ydim-1)*Tm*downsampling, id);
830  MD.write((String)"fullMicrograph@"+fn_root+".ctfparam", MD_APPEND);
831 
832  if (fn_pos != "")
833  {
834  FileName fn_img, fn_psd_piece, fn_ctfparam_piece;
835  int Y, X;
836  for (size_t objId : posFile.ids())
837  {
838  posFile.getValue(MDL_IMAGE, fn_img, objId);
839  posFile.getValue(MDL_X, X, objId);
840  posFile.getValue(MDL_Y, Y, objId);
841  auto idx_X = (int)floor((double) X / pieceDim);
842  auto idx_Y = (int)floor((double) Y / pieceDim);
843  int N = idx_Y * div_NumberX + idx_X + 1;
844 
845  fn_psd_piece.compose(N, fn_psd);
846  fn_ctfparam_piece = fn_psd_piece.withoutExtension()
847  + ".ctfparam";
848  posFile.setValue(MDL_PSD, fn_psd_piece, objId);
849  posFile.setValue(MDL_CTF_MODEL, fn_ctfparam_piece, objId);
850  }
851  }
852  }
853  posFile.write(fn_pos);
854 }
855 
856 /* Fast estimate of PSD --------------------------------------------------- */
858 {
859 public:
861  MultidimArray<double> *PSD, *pieceSmoother;
865 };
866 
868 {
869  auto *args = (ThreadFastEstimateEnhancedPSDParams*) thArg.workClass;
870  int Nthreads = thArg.getNumberOfThreads();
871  int id = thArg.thread_id;
872  ImageGeneric &I = *(args->I);
873  const MultidimArrayGeneric& mI = I();
874  size_t IXdim, IYdim, IZdim;
875  I.getDimensions(IXdim, IYdim, IZdim);
876  MultidimArray<double> &pieceSmoother = *(args->pieceSmoother);
877  MultidimArray<int> &pieceMask = *(args->pieceMask);
878  MultidimArray<double> localPSD, piece;
880  piece.initZeros(pieceMask);
881  localPSD.initZeros(*(args->PSD));
882 
883  FourierTransformer transformer;
884  transformer.setReal(piece);
885 
886  int pieceNumber = 0;
887  int Nprocessed = 0;
888  double pieceDim2 = XSIZE(piece) * XSIZE(piece);
889  for (size_t i = 0; i < (IYdim - YSIZE(piece)); i+=YSIZE(piece))
890  for (size_t j = 0; j < (IXdim - XSIZE(piece)); j+=XSIZE(piece), pieceNumber++)
891  {
892  if ((pieceNumber + 1) % Nthreads != id)
893  continue;
894  Nprocessed++;
895 
896  // Extract micrograph piece ..........................................
897  for (size_t k = 0; k < YSIZE(piece); k++)
898  for (size_t l = 0; l < XSIZE(piece); l++)
899  DIRECT_A2D_ELEM(piece, k, l)= mI(i+k, j+l);
900  piece.statisticsAdjust(0., 1.);
901  normalize_ramp(piece, &pieceMask);
902  piece *= pieceSmoother;
903 
904  // Estimate the power spectrum .......................................
905  transformer.FourierTransform();
906  transformer.getCompleteFourier(Periodogram);
908  {
909  const auto *ptr = (double*) &DIRECT_MULTIDIM_ELEM(Periodogram, n);
910  double re=*ptr;
911  double im=*(ptr+1);
912  double magnitude2=re*re+im*im;
913  DIRECT_MULTIDIM_ELEM(localPSD,n)+=magnitude2*pieceDim2;
914  }
915  }
916 
917  // Gather results
918  args->mutex->lock();
919  args->Nprocessed += Nprocessed;
920  *(args->PSD) += localPSD;
921  args->mutex->unlock();
922 }
923 
924 void fastEstimateEnhancedPSD(const FileName &fnMicrograph, double downsampling,
925  MultidimArray<double> &enhancedPSD, int numberOfThreads)
926 {
927  size_t Xdim, Ydim, Zdim, Ndim;
928  getImageSizeFromFilename(fnMicrograph, Xdim, Ydim, Zdim, Ndim);
929  int minSize = 2 * (std::max(Xdim, Ydim) / 10);
930  minSize = (int)std::min((double) std::min(Xdim, Ydim), NEXT_POWER_OF_2(minSize));
931  minSize = std::min(1024, minSize);
932 
933  /*
934  ProgCTFEstimateFromMicrograph prog1;
935  prog1.fn_micrograph=fnMicrograph;
936  prog1.fn_root=fnMicrograph.withoutExtension()+"_tmp";
937  prog1.pieceDim=(int)(minSize*downsampling);
938  prog1.PSDEstimator_mode=ProgCTFEstimateFromMicrograph::Periodogram;
939  prog1.Nsubpiece=1;
940  prog1.psd_mode=ProgCTFEstimateFromMicrograph::OnePerMicrograph;
941  prog1.estimate_ctf=false;
942  prog1.bootstrapN=-1;
943  prog1.verbose=1;
944  prog1.overlap=0;
945  prog1.run();
946  */
947  // Prepare auxiliary variables
948  ImageGeneric I;
949  I.read(fnMicrograph);
950 
952  PSD.initZeros(minSize, minSize);
953 
954  MultidimArray<int> pieceMask;
955  pieceMask.resizeNoCopy(PSD);
956  pieceMask.initConstant(1);
957 
958  MultidimArray<double> pieceSmoother;
960 
961  // Prepare thread arguments
962  Mutex mutex;
964  args.I = &I;
965  args.PSD = &PSD;
966  args.pieceMask = &pieceMask;
967  args.pieceSmoother = &pieceSmoother;
968  args.Nprocessed = 0;
969  args.mutex = &mutex;
970  auto thMgr = std::unique_ptr<ThreadManager>(std::make_unique<ThreadManager>(numberOfThreads, &args));
972  if (args.Nprocessed != 0)
973  *(args.PSD) /= args.Nprocessed;
974 
975  ProgCTFEnhancePSD prog2;
976  prog2.filter_w1 = 0.02;
977  prog2.filter_w2 = 0.2;
978  prog2.decay_width = 0.02;
979  prog2.mask_w1 = 0.005;
980  prog2.mask_w2 = 0.5;
981 
982  prog2.applyFilter(*(args.PSD));
983  enhancedPSD = *(args.PSD);
984 
985  auto downXdim = (int) ((double)XSIZE(enhancedPSD) / downsampling);
986  int firstIndex = FIRST_XMIPP_INDEX(downXdim);
987  int lastIndex = LAST_XMIPP_INDEX(downXdim);
988  enhancedPSD.setXmippOrigin();
989  enhancedPSD.selfWindow(firstIndex, firstIndex, lastIndex, lastIndex);
990 }
991 
992 // explicit instantiation
993 template void ProgCTFEstimateFromMicrograph::constructPieceSmoother<float>(MultidimArray<float> const&, MultidimArray<float>&);
994 template void ProgCTFEstimateFromMicrograph::constructPieceSmoother<double>(MultidimArray<double> const&, MultidimArray<double>&);
double sigmaU
Spherical aberration (in milimeters). Typical value 5.6.
Definition: ctf.h:871
void run(ThreadFunction function, void *data=NULL)
Just to locate unclassified errors.
Definition: xmipp_error.h:192
#define VECTOR_R2(v, x, y)
Definition: matrix1d.h:112
void planeFit(const MultidimArray< double > &z, const MultidimArray< double > &x, const MultidimArray< double > &y, double &p0, double &p1, double &p2)
void init_progress_bar(long total)
Mutex mutex
void selfWindow(int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T init_value=0)
void getImageSizeFromFilename(const FileName &filename, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim, MDLabel image_label)
The CTF is valid within (x0,y0) to (xF,yF) in the micrograph coordinates.
#define YSIZE(v)
void min(Image< double > &op1, const Image< double > &op2)
#define A2D_ELEM(v, i, j)
void addVector(const MultidimArray< float > &_v)
Add vector.
Definition: basic_pca.h:100
double module() const
Definition: matrix1d.h:983
double getDoubleParam(const char *param, int arg=0)
void threshold(const String &type, T a, T b=0, MultidimArray< int > *mask=NULL)
__host__ __device__ float2 floor(const float2 v)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
#define FINISHINGX(v)
double gaussian_angle2
Second Gaussian angle.
Definition: ctf.h:897
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
doublereal * c
#define MULTIDIM_SIZE(v)
ThreadManager * thMgr
void projectOnPCABasis(Matrix2D< double > &CtY)
Project on basis.
Definition: basic_pca.cpp:119
void resizeNoCopy(const MultidimArray< T1 > &v)
void threadFastEstimateEnhancedPSD(ThreadArgument &thArg)
Defocus = A*x+B*y+C.
Defocus = A*x+B*y+C.
int getNumberOfThreads()
void sqrt(Image< double > &op)
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
static void constructPieceSmoother(const MultidimArray< T > &piece, MultidimArray< T > &pieceSmoother)
#define DIRECT_A2D_ELEM(v, i, j)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
#define A1D_ELEM(v, i)
void readBasicParams(XmippProgram *program)
Read parameters.
void toVector(Matrix1D< T > &op1) const
Definition: matrix2d.cpp:832
void readBasicParams(XmippProgram *program)
Read parameters.
double espr
Definition: ctf.h:251
double cU2
Second Gaussian center for U.
Definition: ctf.h:893
void compose(const String &str, const size_t no, const String &ext="")
double cU
Gaussian center for U.
Definition: ctf.h:875
doublereal * w
void initConstant(T val)
Name for the CTF Model (std::string)
void PSD_piece_by_averaging(MultidimArray< double > &piece, MultidimArray< double > &psd)
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
bool estimate_ctf
Estimate a CTF for each PSD.
String integerToString(int I, int _width, char fill_with)
Defocus = A*x+B*y+C.
Defocus = A*x+B*y+C.
The CTF is valid within (x0,y0) to (xF,yF) in the micrograph coordinates.
virtual IdIteratorProxy< false > ids()
double gaussian_K
Gain for the gaussian term.
Definition: ctf.h:279
void FFT_idx2digfreq(T &v, const Matrix1D< int > &idx, Matrix1D< double > &freq)
Definition: xmipp_fft.h:80
A Power Spectrum Density file name (std::string)
double sq
Sqrt width.
Definition: ctf.h:287
FileName fn_psd
CTF filename.
#define STARTINGX(v)
double DeltaF
Longitudinal mechanical displacement (ansgtrom). Typical value 100.
Definition: ctf.h:257
double sqrt_angle
Sqrt angle.
Definition: ctf.h:887
size_t size() const override
#define i
void selfScaleToSize(int SplineDegree, MultidimArrayGeneric &V1, int Xdim, int Ydim, int Zdim)
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
Defocus = A*x+B*y+C.
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
#define STARTINGY(v)
void CenterFFT(MultidimArray< T > &v, bool forward)
Definition: xmipp_fft.h:291
void standardarizeVariables()
Standardarize variables.
Definition: basic_pca.cpp:61
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
Definition: ctf.h:832
double Gc2
Second Gaussian center.
Definition: ctf.h:293
double sigma2
Second Gaussian width.
Definition: ctf.h:291
bool bootstrap
Bootstrap estimation.
double sum2() const
Definition: matrix1d.cpp:673
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
void run()
Process the whole thing.
double base_line
Global base_line.
Definition: ctf.h:277
double sqU
Gain for the square root term.
Definition: ctf.h:883
double Gc1
Gaussian center.
Definition: ctf.h:283
const char * getParam(const char *param, int arg=0)
void setReal(MultidimArray< double > &img)
Definition: xmipp_fftw.cpp:129
if(fabs(c[*nmax+ *nmax *c_dim1])==0.e0)
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
bool acceleration1D
Accelerate PSD estimation.
void CausalARMA(MultidimArray< double > &Img, ARMA_parameters &prm)
double gaussian_K2
Gain for the second Gaussian term.
Definition: ctf.h:289
bool setValue(const MDObject &mdValueIn, size_t id)
#define CEIL(x)
Definition: xmipp_macros.h:225
size_t addObject() override
void getCompleteFourier(T &V)
Definition: xmipp_fftw.h:234
double ROUT_Adjust_CTFFast(ProgCTFEstimateFromPSDFast &prm, CTFDescription1D &output_ctfmodel, bool standalone)
void normalize_ramp(MultidimArray< double > &I, MultidimArray< int > *bg_mask)
Definition: normalize.cpp:333
Incorrect argument received.
Definition: xmipp_error.h:113
size_t firstRowId() const override
#define XSIZE(v)
void progress_bar(long rlen)
ProgCTFEstimateFromPSDFast prmEstimateCTFFromPSDFast
Parameters for adjust_CTF program.
double ispr
Objective lens stability (deltaI/I) (ppm). Typical value 1.
Definition: ctf.h:253
void max(Image< double > &op1, const Image< double > &op2)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
double sqV
Sqrt width V.
Definition: ctf.h:885
double Tm
Sampling rate (A/pixel)
Definition: ctf.h:240
Error related to numerical calculation.
Definition: xmipp_error.h:179
#define DIRECT_MULTIDIM_ELEM(v, n)
void addExampleLine(const char *example, bool verbatim=true)
double sum(bool average=false) const
Definition: matrix1d.cpp:652
std::vector< MultidimArray< double > > PCAbasis
Definition: basic_pca.h:69
void window2D(const MultidimArray< T > &Ibig, MultidimArray< T > &Ismall, size_t y0, size_t x0, size_t yF, size_t xF)
int verbose
Verbosity level.
void mode
static void defineBasicParams(XmippProgram *program)
Define basic parameters.
double Cs
Spherical aberration (in milimeters). Typical value 5.6.
Definition: ctf.h:246
int pieceDim
Dimension of micrograph pieces.
void FourierTransform(const MultidimArray< double > &in, MultidimArray< std::complex< double > > &out)
Definition: xmipp_fft.cpp:124
ProgCTFEstimateFromPSD prmEstimateCTFFromPSD
Parameters for adjust_CTF program.
Defocus = A*x+B*y+C.
void * workClass
The class in which threads will be working.
double DeltaR
Transversal mechanical displacement (ansgtrom). Typical value 3.
Definition: ctf.h:259
double sigmaV
Gaussian width V.
Definition: ctf.h:873
bool show_optimization
Show convergence values.
void FourierTransform(T &v, T1 &V, bool getCopy=true)
Definition: xmipp_fftw.h:166
Runs test on the projection of the PSD on the first principal component.
#define NEXT_POWER_OF_2(x)
Definition: xmipp_macros.h:374
#define j
double Defocus
Defocus (in Angstroms). Negative values are underfocused.
Definition: ctf.h:244
void deleteFile() const
double checkRandomness(const std::string &sequence)
Downsampling performed to estimate the CTF.
bool getValue(MDObject &mdValueOut, size_t id) const override
double K
Global gain. By default, 1.
Definition: ctf.h:238
void ARMAFilter(MultidimArray< double > &Img, MultidimArray< double > &Filter, ARMA_parameters &prm)
double gaussian_angle
Gaussian angle.
Definition: ctf.h:879
Variance in the first principal component of the PSDs.
virtual void setColumnFormat(bool column)
#define FINISHINGY(v)
bool isLocalCTF
Local CTF determination.
Definition: ctf.h:271
ARMA_parameters ARMA_prm
Parameters for ARMA.
FileName withoutExtension() const
Y component (double)
double filter_w1
Bandpass filter low frequency (in Fourier space, max 0.5)
double Ca
Chromatic aberration (in milimeters). Typical value 2.
Definition: ctf.h:248
#define FIRST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:439
std::string String
Definition: xmipp_strings.h:34
double alpha
Convergence cone semiangle (in mrad). Typical value 0.5.
Definition: ctf.h:255
bool fileExists(const char *filename)
String formatString(const char *format,...)
void completeFourierTransform(T &v, T1 &V)
Definition: xmipp_fftw.h:315
X component (double)
int thread_id
The thread id.
void readParams(XmippProgram *program)
Read parameters from command line.
static void defineParams(XmippProgram *program)
Define parameters.
double y0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
Definition: ctf.h:267
void learnPCABasis(size_t NPCA, size_t Niter)
Learn basis.
Definition: basic_pca.cpp:170
bool checkParam(const char *param)
The CTF is valid within (x0,y0) to (xF,yF) in the micrograph coordinates.
double Q0
Factor for the importance of the Amplitude contrast.
Definition: ctf.h:261
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
double x0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
Definition: ctf.h:263
ErrorType __errno
Definition: xmipp_error.h:227
double kV
Accelerating Voltage (in KiloVolts)
Definition: ctf.h:242
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
double cV2
Second Gaussian center for V.
Definition: ctf.h:895
FileName fn_micrograph
Micrograph filename.
#define LAST_XMIPP_INDEX(size)
Definition: xmipp_macros.h:448
double xF
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
Definition: ctf.h:265
#define PI
Definition: tools.h:43
int getIntParam(const char *param, int arg=0)
double sigma1
Gaussian width.
Definition: ctf.h:281
double sqrt_K
Gain for the square root term.
Definition: ctf.h:285
static void defineBasicParams(XmippProgram *program)
Define basic parameters.
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false)
void FFT_magnitude(const MultidimArray< std::complex< double > > &v, MultidimArray< double > &mag)
Definition: xmipp_fftw.cpp:393
Incorrect value received.
Definition: xmipp_error.h:195
double ROUT_Adjust_CTF(ProgCTFEstimateFromPSD &prm, CTFDescription &output_ctfmodel, bool standalone)
double yF
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
Definition: ctf.h:269
int * n
Name of an image (std::string)
void fastEstimateEnhancedPSD(const FileName &fnMicrograph, double downsampling, MultidimArray< double > &enhancedPSD, int numberOfThreads)
double sigmaU2
Second Gaussian width U.
Definition: ctf.h:889
void addParamsLine(const String &line)
void statisticsAdjust(U avgF, U stddevF)
The CTF is valid within (x0,y0) to (xF,yF) in the micrograph coordinates.
double sigmaV2
Second Gaussian width V.
Definition: ctf.h:891
double cV
Gaussian center for V.
Definition: ctf.h:877
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const