Xmipp  v3.23.11-Nereus
ctf_estimate_from_psd_base.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Javier Mota Garcia (jmota@cnb.csic.es)
4  * Carlos Oscar Sanchez Sorzano (coss@cnb.csic.es)
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 "data/fourier_filter.h"
30 
31 
32 /* Read parameters --------------------------------------------------------- */
34 {
35  downsampleFactor = program->getDoubleParam("--downSamplingPerformed");
36  show_optimization = program->checkParam("--show_optimization");
37  min_freq = program->getDoubleParam("--min_freq");
38  max_freq = program->getDoubleParam("--max_freq");
39  defocus_range = program->getDoubleParam("--defocus_range");
40  modelSimplification = program->getIntParam("--model_simplification");
41  bootstrap = program->checkParam("--bootstrapFit");
42  refineAmplitudeContrast = program->checkParam("--refine_amplitude_contrast");
43  fastDefocusEstimate = program->checkParam("--fastDefocus");
44  noDefocusEstimate = program->checkParam("--noDefocus");
45  selfEstimation = program->checkParam("--selfEstimation");
47  {
48  lambdaPhase=program->getDoubleParam("--fastDefocus",0);
49  sizeWindowPhase=program->getIntParam("--fastDefocus",1);
50  }
51  ctfmodelSize = program->getIntParam("--ctfmodelSize");
52  enhanced_weight = program->getDoubleParam("--enhance_weight");
53  if (!program->checkParam("--enhance_min_freq"))
54  f1 = (max_freq > 0.35) ? 0.01 : 0.02;
55  else
56  f1 = program->getDoubleParam("--enhance_min_freq");
57  if (!program->checkParam("--enhance_max_freq"))
58  f2 = (max_freq > 0.35) ? 0.08 : 0.15;
59  else
60  f2 = program->getDoubleParam("--enhance_max_freq");
61 }
62 
64 {
65  fn_psd = getParam("--psd");
66  readBasicParams(this);
67 }
68 
69 /* Show -------------------------------------------------------------------- */
71 {
72  if (verbose==0)
73  return;
74  std::cout
75  << "PSD file: " << fn_psd << std::endl
76  << "Downsampling: " << downsampleFactor << std::endl
77  << "Min Freq.: " << min_freq << std::endl
78  << "Max Freq.: " << max_freq << std::endl
79  << "Sampling: " << Tm << std::endl
80  << "Defocus range: " << defocus_range << std::endl
81  << "ctfmodelSize: " << ctfmodelSize << std::endl
82  << "Enhance min freq: " << f1 << std::endl
83  << "Enhance max freq: " << f2 << std::endl
84  << "Enhance weight: " << enhanced_weight << std::endl
85  << "Model simplification: " << modelSimplification << std::endl
86  << "Bootstrap: " << bootstrap << std::endl
87  << "Fast defocus: " << fastDefocusEstimate << std::endl
88  << "No defocus: " << noDefocusEstimate << std::endl
89  << "Refine amplitude contrast: " << refineAmplitudeContrast << std::endl
90  ;
92  std::cout
93  << "Regularization factor: " << lambdaPhase << std::endl
94  << "Window size: " << sizeWindowPhase << std::endl;
95 
96 }
97 
98 /* Usage ------------------------------------------------------------------- */
100 {
101  program->addSeeAlsoLine("ctf_enhance_psd");
102  program->addParamsLine("== Downsampling");
103  program->addParamsLine(
104  " [--downSamplingPerformed <F=1>] : Downsampling performed to produce this PSD");
105  program->addParamsLine(
106  " : Note that the output CTF model is referred to the original sampling rate");
107  program->addParamsLine(
108  " : not the one of the downsampled image.");
109  program->addParamsLine("== CTF fit: Optimization constraints");
110  program->addParamsLine(
111  " [--min_freq <fmin=0.03>] : Minimum digital frequency (<0.5) to use in adjust. Its value");
112  program->addParamsLine(
113  " : should be a little lower than the dig. freq. of the first ");
114  program->addParamsLine(" : CTF zero.");
115  program->addParamsLine(
116  " [--max_freq <fmax=0.35>] : Maximum digital frequency (<0.5) to use in adjust.");
117  program->addParamsLine(
118  " : It should be higher than the last zero of the CTF.");
119  program->addParamsLine(
120  " [--fastDefocus <lambda=2> <size=10>] : Estimate first defocus with Zernike polynomials");
121  program->addParamsLine(
122  " :+Lambda is a regularization factor used during the estimation of the CTF phase");
123  program->addParamsLine(
124  " :+During the estimation, the phase values are averaged within a window of this size");
125  program->addParamsLine(
126  " [--noDefocus] : No defocus estimation");
127  program->addParamsLine(
128  " [--defocus_range <D=8000>] : Defocus range in Angstroms");
129  program->addParamsLine(
130  " [--refine_amplitude_contrast] : Refine amplitude contrast with respect to the input one");
131  program->addParamsLine(
132  " [--show_optimization+] : Show optimization process");
133  program->addParamsLine(
134  " [--radial_noise++] : By default, noise is astigmatic");
135  program->addParamsLine(
136  " [--enhance_weight++ <w=1>] : Weight of the enhanced term");
137  program->addParamsLine(
138  " [--model_simplification++ <s=0>]: 0 (no simplification)");
139  program->addParamsLine(
140  " : 1 (simplified envelope)");
141  program->addParamsLine(
142  " : 2 (last Gaussian removal)");
143  program->addParamsLine(
144  " : 3 (symmetric intermediate Gaussian)");
145  program->addParamsLine(
146  " [--bootstrapFit++ <N=-1>] : Perform bootstrap fit (Fourier pixels are randomly chosen)");
147  program->addParamsLine(
148  " : This is used to test the variability of the fit");
149  program->addParamsLine(
150  " : N defines the number of times the fit is repeated");
151  program->addParamsLine("==+ CTF fit: Output CTF models");
152  program->addParamsLine(
153  " [--ctfmodelSize <size=256>] : Size for the ctfmodel thumbnails");
154  program->addParamsLine("==+ PSD enhancement");
155  program->addParamsLine(
156  " [--enhance_min_freq <f1>] : Bandpass cutoff. Normalized to 0.5");
157  program->addParamsLine(
158  " : If fmax>0.35, f1 default=0.01");
159  program->addParamsLine(
160  " : If fmax<0.35, f1 default=0.02");
161  program->addParamsLine(
162  " [--enhance_max_freq <f2>] : Bandpass cutoff. Normalized to 0.5.");
163  program->addParamsLine(
164  " [--selfEstimation] : Estimate defocus without previous estimation");
165  program->addParamsLine(
166  " : If fmax>0.35, f2 default=0.08");
167  program->addParamsLine(
168  " : If fmax<0.35, f2 default=0.15");
169 }
170 
172 {
173  addUsageLine("Adjust a parametric model to a PSD file.");
174  addUsageLine(
175  "The PSD is enhanced ([[http://www.ncbi.nlm.nih.gov/pubmed/16987671][See article]]). ");
176  addUsageLine(
177  "And finally, the CTF is fitted to the PSD, being guided by the enhanced PSD ");
178  addUsageLine(
179  "([[http://www.ncbi.nlm.nih.gov/pubmed/17911028][See article]]).");
180  addParamsLine(" --psd <PSDfile> : PSD file");
181  addSeeAlsoLine("ctf_estimate_from_micrograph");
182  defineBasicParams(this);
183 }
184 
185 /* Produce side information ------------------------------------------------ */
187 {
188  // Resize the frequency
189  x_digfreq.initZeros(YSIZE(*f), XSIZE(*f) / 2);
190  y_digfreq.initZeros(YSIZE(*f), XSIZE(*f) / 2);
191  w_digfreq.initZeros(YSIZE(*f), XSIZE(*f) / 2);
192  w_digfreq_r.initZeros(YSIZE(*f), XSIZE(*f) / 2);
193  x_contfreq.initZeros(YSIZE(*f), XSIZE(*f) / 2);
194  y_contfreq.initZeros(YSIZE(*f), XSIZE(*f) / 2);
195  w_contfreq.initZeros(YSIZE(*f), XSIZE(*f) / 2);
196 
197  Matrix1D<int> idx(2); // Indexes for Fourier plane
198  Matrix1D<double> freq(2); // Frequencies for Fourier plane
199 
201  {
202  XX(idx) = j;
203  YY(idx) = i;
204 
205  // Digital frequency
206  FFT_idx2digfreq(*f, idx, freq);
207  x_digfreq(i, j) = XX(freq);
208  y_digfreq(i, j) = YY(freq);
209  w_digfreq(i, j) = freq.module();
210  w_digfreq_r(i, j) = (int)(w_digfreq(i,j) * (double)YSIZE(w_digfreq));
211 
212  // Continuous frequency
213  digfreq2contfreq(freq, freq, Tm);
214  x_contfreq(i, j) = XX(freq);
215  y_contfreq(i, j) = YY(freq);
216  w_contfreq(i, j) = freq.module();
217  }
218 
219  // Build frequency mask
223  {
224  if (w_digfreq(i, j) >= max_freq
225  || w_digfreq(i, j) <= min_freq)
226  continue;
227  mask(i, j) = 1;
228  w_count(w_digfreq_r(i, j))++;
229  }
230 
231  // Enhance PSD for ctfmodels
233  prm.filter_w1 = 0.02;
234  prm.filter_w2 = 0.2;
235  prm.decay_width = 0.02;
236  prm.mask_w1 = 0.01;
237  prm.mask_w2 = 0.5;
240  if (fn_psd.find('@')==std::string::npos)
241  enhanced_ctftomodel.write(fn_psd.withoutExtension() + "_enhanced_psd.xmp");
242  else
243  enhanced_ctftomodel.write(fn_psd.withoutExtension() + "_enhanced_psd.stk");
244  CenterFFT(enhanced_ctftomodel(), false);
246 
247  // Enhance PSD for optimization
248  prm.filter_w1 = f1;
249  prm.filter_w2 = f2;
252  CenterFFT(enhanced_ctftomodel(), false);
253  enhanced_ctftomodel().resize(w_digfreq);
254 
255  // Divide by the number of count at each frequency
256  // and mask between min_freq and max_freq
257  double min_val = enhanced_ctftomodel().computeMin();
259  if (mask(i, j) <= 0)
260  enhanced_ctftomodel(i, j) = min_val;
261 
264 
265  enhanced_ctftomodel() = aux;
266  enhanced_ctftomodel().rangeAdjust(0, 1);
267 
268  FourierFilter Filter;
269  Filter.FilterShape = RAISED_COSINE;
270  Filter.FilterBand = HIGHPASS;
271  Filter.w1 = 0.01;
272  Filter.raised_w = 0.005;
273  enhanced_ctftomodel().setXmippOrigin();
277 
278  // Compute now radial average of the enhanced_ctftomodel
286  {
288  {
289  int r = w_digfreq_r(i, j);
290  w_digfreq_r_iN(r)+=1;
292  psd_exp_radial(r) += ctftomodel(i, j);
293  }
294  }
296  if (w_digfreq_r_iN(i)>0)
297  {
301  }
302 
303  // Compute its derivative
304  int state=0;
305  double maxDiff=0;
307  {
308  switch (state)
309  {
310  case 0:
311  if (w_digfreq(i,0)>min_freq)
312  state=1;
313  break;
314  case 1:
315  state=2; // Skip first sample
316  break;
317  case 2:
318  if (w_digfreq(i,0)>max_freq)
319  state=3;
320  else
321  {
324  maxDiff=std::max(maxDiff,fabs(diff));
325  }
326  break;
327  }
328  }
330  if (show_optimization)
331  {
332  psd_exp_enhanced_radial.write("PPPexpEnhanced_fast.txt");
333  psd_exp_radial.write("PPPexp_fast.txt");
334  }
335 
336 }
337 
339 {
340  if (Tm>1.8)
341  return 0.0; // We cannot measure at 3.6A, if the Tm>1.8A
342  double R4_4=floor(XSIZE(psd)*Tm/4.4);
343  double R4_0=floor(XSIZE(psd)*Tm/4.0);
344  double R3_6=floor(XSIZE(psd)*Tm/3.6);
345  R4_4*=R4_4;
346  R4_0*=R4_0;
347  R3_6*=R3_6;
348  double N1=0, N2=0, avg1=0, avg2=0;
350  {
351  double R2=i*i+j*j;
352  if (R2>R4_4)
353  {
354  if (R2<R4_0)
355  {
356  avg1+=A2D_ELEM(psd,i,j);
357  N1+=1;
358  }
359  else if (R2<R3_6)
360  {
361  avg2+=A2D_ELEM(psd,i,j);
362  N2+=1;
363  }
364  }
365  }
366  if (N1>0 && N2>0)
367  {
368  avg1/=N1;
369  avg2/=N2;
370  return avg2/avg1;
371  }
372  return -1;
373 }
double defocus_range
Defocus range.
#define YSIZE(v)
#define A2D_ELEM(v, i, j)
double enhanced_weight
Weight of the enhanced image.
MultidimArray< double > * f
double module() const
Definition: matrix1d.h:983
double getDoubleParam(const char *param, int arg=0)
Image< double > ctftomodel
CTF amplitude to model.
double filter_w2
Bandpass filter high frequency (in Fourier space, max 0.5)
__host__ __device__ float2 floor(const float2 v)
bool fastDefocusEstimate
Fast defocus estimate.
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY2D(m)
int sizeWindowPhase
Size of the average window used during phase direction and unwrapping estimates (used in Zernike esti...
MultidimArray< double > w_digfreq
double mask_w1
Lower frequency for the mask (in Fourier space, max 0.5)
void readBasicParams(XmippProgram *program)
Read parameters.
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)
MultidimArray< double > y_digfreq
double f1
Enhancement filter low freq.
double Tm
Sampling rate.
MultidimArray< double > w_digfreq_r_iN
double downsampleFactor
Downsample performed.
void show()
Show parameters.
MultidimArray< double > psd_exp_enhanced_radial_derivative
void FFT_idx2digfreq(T &v, const Matrix1D< int > &idx, Matrix1D< double > &freq)
Definition: xmipp_fft.h:80
FileName fn_psd
CTF filename.
#define STARTINGX(v)
MultidimArray< double > w_digfreq_r
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
void addSeeAlsoLine(const char *seeAlso)
double max_freq
Maximum frequency to adjust.
double mask_w2
Higher frequency for the mask (in Fourier space, max 0.5)
#define STARTINGY(v)
void CenterFFT(MultidimArray< T > &v, bool forward)
Definition: xmipp_fft.h:291
Image< double > enhanced_ctftomodel_fullsize
CTF amplitude to model.
int ctfmodelSize
X dimension of particle projections (-1=the same as the psd)
bool bootstrap
Bootstrap estimation.
MultidimArray< double > w_contfreq
MultidimArray< double > psd_theo_radial
MultidimArray< double > psd_exp_radial
PSD data.
const char * getParam(const char *param, int arg=0)
#define XX(v)
Definition: matrix1d.h:85
bool noDefocusEstimate
No defocus estimate.
#define XSIZE(v)
void write(const FileName &fn) const
#define HIGHPASS
#define RAISED_COSINE
void max(Image< double > &op1, const Image< double > &op2)
bool refineAmplitudeContrast
Refine amplitude contrast.
double decay_width
Decay width (raised cosine)
int verbose
Verbosity level.
static void defineBasicParams(XmippProgram *program)
Define basic parameters.
double f2
Enhancement filter high freq.
bool show_optimization
Show convergence values.
MultidimArray< double > psd_theo_radial_derivative
#define j
void defineParams()
Define Parameters.
#define YY(v)
Definition: matrix1d.h:93
MultidimArray< double > x_digfreq
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
double evaluateIceness(MultidimArray< double > &psd, double Tm)
void produceSideInfo()
Produce side information.
int modelSimplification
Model simplification.
FileName withoutExtension() const
double filter_w1
Bandpass filter low frequency (in Fourier space, max 0.5)
void medianFilter3x3(MultidimArray< T > &m, MultidimArray< T > &out)
Definition: filters.h:1088
void applyFilter(MultidimArray< double > &PSD)
double min_freq
Minimum frequency to adjust.
Image< double > enhanced_ctftomodel
CTF amplitude to model.
void readParams()
Read parameters.
MultidimArray< double > x_contfreq
Frequencies in axes.
bool checkParam(const char *param)
MultidimArray< double > psd_exp_enhanced_radial
ProgClassifyCL2D * prm
double lambdaPhase
Regularization factor for the phase direction and unwrapping estimates (used in Zernike estimate) ...
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.
int getIntParam(const char *param, int arg=0)
void generateMask(MultidimArray< double > &v)
MultidimArray< double > w_count
void addParamsLine(const String &line)
MultidimArray< double > y_contfreq
void applyMaskSpace(MultidimArray< double > &v)
void digfreq2contfreq(const Matrix1D< double > &digfreq, Matrix1D< double > &contfreq, double pixel_size)
Definition: xmipp_fft.h:125