Xmipp  v3.23.11-Nereus
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members

#include <ctf_estimate_from_psd.h>

Inheritance diagram for ProgCTFEstimateFromPSD:
Inheritance graph
[legend]
Collaboration diagram for ProgCTFEstimateFromPSD:
Collaboration graph
[legend]

Public Member Functions

 ProgCTFEstimateFromPSD ()
 
 ProgCTFEstimateFromPSD (const ProgCTFEstimateFromPSDFast *copy)
 
void readBasicParams (XmippProgram *program)
 Read parameters. More...
 
void readParams ()
 
void defineParams ()
 Define Parameters. More...
 
void produceSideInfo ()
 Produce side information. More...
 
void generate_model_halfplane (int Ydim, int Xdim, MultidimArray< double > &model)
 
void generate_model_quadrant (int Ydim, int Xdim, MultidimArray< double > &model)
 
void run ()
 
void assignCTFfromParameters (double *p, CTFDescription &ctfmodel, int ia, int l, int modelSimplification)
 
void assignParametersFromCTF (const CTFDescription &ctfmodel, double *p, int ia, int l, int modelSimplification)
 
void center_optimization_focus (bool adjust_freq, bool adjust_th, double margin)
 
void generateModelSoFar (Image< double > &I, bool apply_log)
 
void compute_central_region (double &w1, double &w2, double ang)
 
void saveIntermediateResults (const FileName &fn_root, bool generate_profiles)
 
double CTF_fitness_object (double *p)
 
void estimate_background_sqrt_parameters ()
 
void estimate_background_gauss_parameters ()
 
void estimate_background_gauss_parameters2 ()
 
void estimate_envelope_parameters ()
 
void showFirstDefoci ()
 
void estimate_defoci ()
 
void estimate_defoci_Zernike (const MultidimArray< double > &psdToModelFullSize, double min_freq, double max_freq, double Tm, double kV, double lambdaPhase, int sizeWindowPhase, double &defocusU, double &defocusV, double &ellipseAngle, int verbose)
 
void estimate_defoci_Zernike ()
 
- Public Member Functions inherited from ProgCTFBasicParams
 ProgCTFBasicParams ()
 
void readParams ()
 Read parameters. More...
 
void readBasicParams (XmippProgram *program)
 Read parameters. More...
 
void show ()
 Show parameters. More...
 
void defineParams ()
 Define Parameters. More...
 
void produceSideInfo ()
 Produce side information. More...
 
void generate_model_halfplane (int Ydim, int Xdim, MultidimArray< double > &model)
 
void generate_model_quadrant (int Ydim, int Xdim, MultidimArray< double > &model)
 
- Public Member Functions inherited from XmippProgram
const char * getParam (const char *param, int arg=0)
 
const char * getParam (const char *param, const char *subparam, int arg=0)
 
int getIntParam (const char *param, int arg=0)
 
int getIntParam (const char *param, const char *subparam, int arg=0)
 
double getDoubleParam (const char *param, int arg=0)
 
double getDoubleParam (const char *param, const char *subparam, int arg=0)
 
float getFloatParam (const char *param, int arg=0)
 
float getFloatParam (const char *param, const char *subparam, int arg=0)
 
void getListParam (const char *param, StringVector &list)
 
int getCountParam (const char *param)
 
bool checkParam (const char *param)
 
bool existsParam (const char *param)
 
void addParamsLine (const String &line)
 
void addParamsLine (const char *line)
 
ParamDefgetParamDef (const char *param) const
 
virtual void quit (int exit_code=0) const
 
virtual int tryRun ()
 
void initProgress (size_t total, size_t stepBin=60)
 
void setProgress (size_t value=0)
 
void endProgress ()
 
void processDefaultComment (const char *param, const char *left)
 
void setDefaultComment (const char *param, const char *comment)
 
virtual void initComments ()
 
void setProgramName (const char *name)
 
void addUsageLine (const char *line, bool verbatim=false)
 
void clearUsage ()
 
void addExampleLine (const char *example, bool verbatim=true)
 
void addSeeAlsoLine (const char *seeAlso)
 
void addKeywords (const char *keywords)
 
const char * name () const
 
virtual void usage (int verb=0) const
 
virtual void usage (const String &param, int verb=2)
 
int version () const
 
virtual void show () const
 
virtual void read (int argc, const char **argv, bool reportErrors=true)
 
virtual void read (int argc, char **argv, bool reportErrors=true)
 
void read (const String &argumentsLine)
 
 XmippProgram ()
 
 XmippProgram (int argc, const char **argv)
 
virtual ~XmippProgram ()
 

Static Public Member Functions

static void defineBasicParams (XmippProgram *program)
 Define basic parameters. More...
 
- Static Public Member Functions inherited from ProgCTFBasicParams
static void defineBasicParams (XmippProgram *program)
 Define basic parameters. More...
 

Public Attributes

CTFDescription initial_ctfmodel
 CTF model. More...
 
CTFDescription current_ctfmodel
 
CTFDescription ctfmodel_defoci
 
- Public Attributes inherited from ProgCTFBasicParams
FileName fn_psd
 CTF filename. More...
 
double downsampleFactor
 Downsample performed. More...
 
Image< double > ctftomodel
 CTF amplitude to model. More...
 
Image< double > enhanced_ctftomodel
 CTF amplitude to model. More...
 
Image< double > enhanced_ctftomodel_fullsize
 CTF amplitude to model. More...
 
bool show_optimization
 Show convergence values. More...
 
bool selfEstimation
 
int ctfmodelSize
 X dimension of particle projections (-1=the same as the psd) More...
 
bool bootstrap
 Bootstrap estimation. More...
 
bool refineAmplitudeContrast
 Refine amplitude contrast. More...
 
bool fastDefocusEstimate
 Fast defocus estimate. More...
 
bool noDefocusEstimate
 No defocus estimate. More...
 
double lambdaPhase
 Regularization factor for the phase direction and unwrapping estimates (used in Zernike estimate) More...
 
int sizeWindowPhase
 Size of the average window used during phase direction and unwrapping estimates (used in Zernike estimate) More...
 
double min_freq
 Minimum frequency to adjust. More...
 
double max_freq
 Maximum frequency to adjust. More...
 
double Tm
 Sampling rate. More...
 
double defocus_range
 Defocus range. More...
 
double f1
 Enhancement filter low freq. More...
 
double f2
 Enhancement filter high freq. More...
 
double enhanced_weight
 Weight of the enhanced image. More...
 
Matrix1D< double > adjust
 Set of parameters for the complete adjustment of the CTF. More...
 
int modelSimplification
 Model simplification. More...
 
MultidimArray< double > x_contfreq
 Frequencies in axes. More...
 
MultidimArray< double > y_contfreq
 
MultidimArray< double > w_contfreq
 
MultidimArray< double > x_digfreq
 
MultidimArray< double > y_digfreq
 
MultidimArray< double > w_digfreq
 
MultidimArray< double > psd_exp_radial
 PSD data. More...
 
MultidimArray< double > psd_exp_enhanced_radial
 
MultidimArray< double > psd_exp_enhanced_radial_2
 
MultidimArray< double > psd_exp_enhanced_radial_derivative
 
MultidimArray< double > psd_theo_radial_derivative
 
MultidimArray< double > psd_exp_radial_derivative
 
MultidimArray< double > psd_theo_radial
 
MultidimArray< double > w_digfreq_r_iN
 
MultidimArray< double > w_digfreq_r
 
MultidimArray< std::complex< double > > psd_fft
 
std::vector< double > amplitud
 
MultidimArray< double > mask
 Masks. More...
 
MultidimArray< double > mask_between_zeroes
 
MultidimArray< double > w_count
 
double value_th
 
double min_freq_psd
 
double max_freq_psd
 
double max_gauss_freq
 
int show_inf
 
int action
 
double corr13
 
bool penalize
 
int evaluation_reduction
 
double heavy_penalization
 
double current_penalty
 
MultidimArray< double > * f
 
Matrix1D< double > * adjust_params
 
- Public Attributes inherited from XmippProgram
bool doRun
 
bool runWithoutArgs
 
int verbose
 Verbosity level. More...
 
int debug
 

Additional Inherited Members

- Static Public Attributes inherited from ProgCTFBasicParams
static constexpr double penalty = 32.0
 
- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 
- Protected Attributes inherited from XmippProgram
int errorCode
 
ProgramDefprogDef
 Program definition and arguments parser. More...
 
std::map< String, CommentListdefaultComments
 
int argc
 Original command line arguments. More...
 
const char ** argv
 

Detailed Description

Adjust CTF parameters.

Definition at line 39 of file ctf_estimate_from_psd.h.

Constructor & Destructor Documentation

◆ ProgCTFEstimateFromPSD() [1/2]

ProgCTFEstimateFromPSD::ProgCTFEstimateFromPSD ( )
inline

Definition at line 46 of file ctf_estimate_from_psd.h.

46 {};

◆ ProgCTFEstimateFromPSD() [2/2]

ProgCTFEstimateFromPSD::ProgCTFEstimateFromPSD ( const ProgCTFEstimateFromPSDFast copy)

PSD data

Masks

Definition at line 2484 of file ctf_estimate_from_psd.cpp.

2485 {
2486  action = copy->action;
2487  x_contfreq = copy->x_contfreq;
2488  y_contfreq = copy->y_contfreq;
2489  w_contfreq = copy->w_contfreq;
2490  x_digfreq = copy->x_digfreq;
2491  y_digfreq = copy->y_digfreq;
2492  w_digfreq = copy->w_digfreq;
2501  w_digfreq_r = copy->w_digfreq_r;
2503  mask = copy->mask;
2505  max_freq = copy->max_freq;
2506  min_freq = copy->min_freq;
2507  min_freq_psd = copy->min_freq_psd;
2508  max_freq_psd = copy->max_freq_psd;
2509  corr13 = copy->corr13;
2510 
2511  show_inf = copy->show_inf;
2513  penalize = copy->penalize;
2516  defocus_range = copy->defocus_range;
2518 
2522 
2527 
2528  Tm = copy->Tm;
2529  f = copy->f;
2530  ctfmodelSize = copy->ctfmodelSize;
2532  fn_psd = copy->fn_psd;
2533 
2534 }
double defocus_range
Defocus range.
double enhanced_weight
Weight of the enhanced image.
MultidimArray< double > * f
MultidimArray< double > w_digfreq
MultidimArray< double > y_digfreq
double Tm
Sampling rate.
MultidimArray< double > w_digfreq_r_iN
double downsampleFactor
Downsample performed.
MultidimArray< double > psd_exp_enhanced_radial_derivative
FileName fn_psd
CTF filename.
MultidimArray< double > w_digfreq_r
double max_freq
Maximum frequency to adjust.
Image< double > enhanced_ctftomodel_fullsize
CTF amplitude to model.
int ctfmodelSize
X dimension of particle projections (-1=the same as the psd)
MultidimArray< double > w_contfreq
MultidimArray< double > psd_theo_radial
MultidimArray< double > psd_exp_radial
PSD data.
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
bool show_optimization
Show convergence values.
MultidimArray< double > psd_theo_radial_derivative
MultidimArray< double > x_digfreq
int modelSimplification
Model simplification.
MultidimArray< double > mask_between_zeroes
double min_freq
Minimum frequency to adjust.
Image< double > enhanced_ctftomodel
CTF amplitude to model.
MultidimArray< double > psd_exp_radial_derivative
MultidimArray< double > x_contfreq
Frequencies in axes.
MultidimArray< double > psd_exp_enhanced_radial
MultidimArray< double > mask
Masks.
CTFDescription initial_ctfmodel
CTF model.
MultidimArray< double > y_contfreq

Member Function Documentation

◆ assignCTFfromParameters()

void ProgCTFEstimateFromPSD::assignCTFfromParameters ( double *  p,
CTFDescription ctfmodel,
int  ia,
int  l,
int  modelSimplification 
)

Definition at line 81 of file ctf_estimate_from_psd.cpp.

83 {
84 
85  ctfmodel.Tm = Tm;
86 
87  /*
88  BE CAREFULL in add new parameters between the existing ones !!
89  below in the core of the program we set step(i) = 0 or 1
90  if you change here the value of certain param, change it everywhere!
91  */
92  ASSIGN_CTF_PARAM(0, DeltafU);
93  ASSIGN_CTF_PARAM(1, DeltafV);
94  ASSIGN_CTF_PARAM(2, azimuthal_angle);
95  ASSIGN_CTF_PARAM(3, kV);
96  ASSIGN_CTF_PARAM(4, K);
97  ASSIGN_CTF_PARAM(5, Cs);
98  ASSIGN_CTF_PARAM(6, Ca);
99  ASSIGN_CTF_PARAM(7, espr);
100  ASSIGN_CTF_PARAM(8, ispr);
101  ASSIGN_CTF_PARAM(9, alpha);
102  ASSIGN_CTF_PARAM(10, DeltaF);
103  ASSIGN_CTF_PARAM(11, DeltaR);
104  ASSIGN_CTF_PARAM(12, envR0);
105  ASSIGN_CTF_PARAM(13, envR1);
106  ASSIGN_CTF_PARAM(14, envR2);
107  ASSIGN_CTF_PARAM(15, Q0);
108  ASSIGN_CTF_PARAM(16, base_line);
109  ASSIGN_CTF_PARAM(17, sqrt_K);
110  ASSIGN_CTF_PARAM(18, sqU);
111  ASSIGN_CTF_PARAM(19, sqV);
112  ASSIGN_CTF_PARAM(20, sqrt_angle);
113  ASSIGN_CTF_PARAM(21, bgR1);
114  ASSIGN_CTF_PARAM(22, bgR2);
115  ASSIGN_CTF_PARAM(23, bgR3);
116  ASSIGN_CTF_PARAM(24, gaussian_K);
117  ASSIGN_CTF_PARAM(25, sigmaU);
118 
119  if (ia <= 26 && l > 0)
120  {
121  if (modelSimplification < 3)
122  {
123  ctfmodel.sigmaV = p[26];
124  l--;
125  } // 7 *
126  else
127  {
128  ctfmodel.sigmaV = p[25];
129  l--;
130  }
131  }
132  if (ia <= 27 && l > 0)
133  {
134  if (modelSimplification < 3)
135  {
136  ctfmodel.gaussian_angle = p[27];
137  l--;
138  } // 8 *
139  else
140  {
141  ctfmodel.gaussian_angle = 0;
142  l--;
143  }
144  }
145 
146  ASSIGN_CTF_PARAM(28, cU);
147 
148  if (ia <= 29 && l > 0)
149  {
150  if (modelSimplification < 3)
151  {
152  ctfmodel.cV = p[29];
153  l--;
154  } // 10 *
155  else
156  {
157  ctfmodel.cV = p[28];
158  l--;
159  }
160  }
161 
162  ASSIGN_CTF_PARAM(30, gaussian_K2);
163  ASSIGN_CTF_PARAM(31, sigmaU2);
164  ASSIGN_CTF_PARAM(32, sigmaV2);
165  ASSIGN_CTF_PARAM(33, gaussian_angle2);
166  ASSIGN_CTF_PARAM(34, cU2);
167  ASSIGN_CTF_PARAM(35, cV2);
168  ASSIGN_CTF_PARAM(36, phase_shift);
169  ASSIGN_CTF_PARAM(37, VPP_radius);
170 
171 }//function assignCTFfromParameters
double Tm
Sampling rate.
double Tm
Sampling rate (A/pixel)
Definition: ctf.h:240
double sigmaV
Gaussian width V.
Definition: ctf.h:873
double gaussian_angle
Gaussian angle.
Definition: ctf.h:879
int modelSimplification
Model simplification.
#define ASSIGN_CTF_PARAM(index, paramName)
constexpr int K
double cV
Gaussian center for V.
Definition: ctf.h:877

◆ assignParametersFromCTF()

void ProgCTFEstimateFromPSD::assignParametersFromCTF ( const CTFDescription ctfmodel,
double *  p,
int  ia,
int  l,
int  modelSimplification 
)

Definition at line 176 of file ctf_estimate_from_psd.cpp.

178 {
179  /*
180  BE CAREFUL in add new parameters between the existing ones !!
181  below in the core of the program we set step(i) to 0 or 1
182  if you change here the value of certain param, change it everywhere!
183  */
184  ASSIGN_PARAM_CTF(0, DeltafU);
185  ASSIGN_PARAM_CTF(1, DeltafV);
186  ASSIGN_PARAM_CTF(2, azimuthal_angle);
187  ASSIGN_PARAM_CTF(3, kV);
188  ASSIGN_PARAM_CTF(4, K);
189  ASSIGN_PARAM_CTF(5, Cs);
190  ASSIGN_PARAM_CTF(6, Ca);
191  ASSIGN_PARAM_CTF(7, espr);
192  ASSIGN_PARAM_CTF(8, ispr);
193  ASSIGN_PARAM_CTF(9, alpha);
194  ASSIGN_PARAM_CTF(10, DeltaF);
195  ASSIGN_PARAM_CTF(11, DeltaR);
196  ASSIGN_PARAM_CTF(12, envR0);
197  ASSIGN_PARAM_CTF(13, envR1);
198  ASSIGN_PARAM_CTF(14, envR2);
199  ASSIGN_PARAM_CTF(15, Q0);
200  ASSIGN_PARAM_CTF(16, base_line);
201  ASSIGN_PARAM_CTF(17, sqrt_K);
202  ASSIGN_PARAM_CTF(18, sqU);
203  ASSIGN_PARAM_CTF(19, sqV);
204  ASSIGN_PARAM_CTF(20, sqrt_angle);
205  ASSIGN_PARAM_CTF(21, bgR1);
206  ASSIGN_PARAM_CTF(22, bgR2);
207  ASSIGN_PARAM_CTF(23, bgR3);
208  ASSIGN_PARAM_CTF(24, gaussian_K);
209  ASSIGN_PARAM_CTF(25, sigmaU);
210 
211  if (ia <= 26 && l > 0)
212  {
213  if (modelSimplification < 3)
214  {
215  p[26] = ctfmodel.sigmaV;
216  l--;
217  }
218  else
219  {
220  p[26] = ctfmodel.sigmaU;
221  l--;
222  }
223  }
224  if (ia <= 27 && l > 0)
225  {
226  if (modelSimplification < 3)
227  {
228  p[27] = ctfmodel.gaussian_angle;
229  l--;
230  }
231  else
232  {
233  p[27] = 0;
234  l--;
235  }
236  }
237 
238  ASSIGN_PARAM_CTF(28, cU);
239 
240  if (ia <= 29 && l > 0)
241  {
242  if (modelSimplification < 3)
243  {
244  p[29] = ctfmodel.cV;
245  l--;
246  }
247  else
248  {
249  p[29] = ctfmodel.cU;
250  l--;
251  }
252  }
253 
254  ASSIGN_PARAM_CTF(30, gaussian_K2);
255  ASSIGN_PARAM_CTF(31, sigmaU2);
256  ASSIGN_PARAM_CTF(32, sigmaV2);
257  ASSIGN_PARAM_CTF(33, gaussian_angle2);
258  ASSIGN_PARAM_CTF(34, cU2);
259  ASSIGN_PARAM_CTF(35, cV2);
260  ASSIGN_PARAM_CTF(36, phase_shift);
261  ASSIGN_PARAM_CTF(37, VPP_radius);
262 }
double sigmaU
Spherical aberration (in milimeters). Typical value 5.6.
Definition: ctf.h:871
#define ASSIGN_PARAM_CTF(index, paramName)
double cU
Gaussian center for U.
Definition: ctf.h:875
double sigmaV
Gaussian width V.
Definition: ctf.h:873
double gaussian_angle
Gaussian angle.
Definition: ctf.h:879
int modelSimplification
Model simplification.
constexpr int K
double cV
Gaussian center for V.
Definition: ctf.h:877

◆ center_optimization_focus()

void ProgCTFEstimateFromPSD::center_optimization_focus ( bool  adjust_freq,
bool  adjust_th,
double  margin = 1 
)

Definition at line 1027 of file ctf_estimate_from_psd.cpp.

1028 {
1029  if (show_optimization)
1030  std::cout << "Freq frame before focusing=" << min_freq_psd << ","
1031  << max_freq_psd << std::endl << "Value_th before focusing="
1032  << value_th << std::endl;
1033 
1034  double w1 = min_freq_psd, w2 = max_freq_psd;
1035  if (adjust_freq)
1036  {
1037  double w1U, w2U, w1V, w2V;
1040  w1 = XMIPP_MIN(w1U, w1V);
1041  w2 = XMIPP_MAX(w2U, w2V);
1042  min_freq_psd = XMIPP_MAX(min_freq_psd, w1 - 0.05);
1043  max_freq_psd = XMIPP_MIN(max_freq_psd, w2 + 0.01);
1044  }
1045 
1046  // Compute maximum value within central region
1047  if (adjust_th)
1048  {
1049  Image<double> save;
1050  generateModelSoFar(save);
1051  double max_val = 0;
1053  {
1054  double w = w_digfreq(i, j);
1055  if (w >= w1 && w <= w2)
1056  max_val = XMIPP_MAX(max_val, save()(i, j));
1057  }
1058  if (value_th != -1)
1059  value_th = XMIPP_MIN(value_th, max_val * margin);
1060  else
1061  value_th = max_val * margin;
1062  }
1063 
1064  if (show_optimization)
1065  std::cout << "Freq frame after focusing=" << min_freq_psd << ","
1066  << max_freq_psd << std::endl << "Value_th after focusing="
1067  << value_th << std::endl;
1068 }
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
MultidimArray< double > w_digfreq
doublereal * w
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
Definition: ctf.h:832
void generateModelSoFar(Image< double > &I, bool apply_log)
void compute_central_region(double &w1, double &w2, double ang)
bool show_optimization
Show convergence values.
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
#define j

◆ compute_central_region()

void ProgCTFEstimateFromPSD::compute_central_region ( double &  w1,
double &  w2,
double  ang 
)

Definition at line 986 of file ctf_estimate_from_psd.cpp.

987 {
988  w1 = max_freq_psd;
989  w2 = min_freq_psd;
990  Matrix1D<double> freq(2), dir(2);
991 
992  // Compute first and third zero in the given direction
993  VECTOR_R2(dir, COSD(ang), SIND(ang));
994 
995  // Detect first zero
996  current_ctfmodel.lookFor(1, dir, freq, 0);
997  if (XX(freq) == -1 && YY(freq) == -1)
998  w1 = min_freq_psd;
999  else
1000  {
1001  contfreq2digfreq(freq, freq, Tm);
1002  double w;
1003  if (XX(dir) > 0.1)
1004  w = XX(freq) / XX(dir);
1005  else
1006  w = YY(freq) / YY(dir);
1007  w1 = XMIPP_MAX(min_freq_psd, XMIPP_MIN(w1, w));
1008  }
1009 
1010  // Detect fifth zero
1011  current_ctfmodel.lookFor(5, dir, freq, 0);
1012  if (XX(freq) == -1 && YY(freq) == -1)
1013  w2 = max_freq_psd;
1014  else
1015  {
1016  double w;
1017  contfreq2digfreq(freq, freq, Tm);
1018  if (XX(dir) > 0.1)
1019  w = XX(freq) / XX(dir);
1020  else
1021  w = YY(freq) / YY(dir);
1022  w2 = XMIPP_MIN(max_freq_psd, XMIPP_MAX(w2, w));
1023  }
1024 }
#define VECTOR_R2(v, x, y)
Definition: matrix1d.h:112
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
double Tm
Sampling rate.
doublereal * w
#define SIND(x)
Definition: xmipp_macros.h:347
#define XX(v)
Definition: matrix1d.h:85
void contfreq2digfreq(const Matrix1D< double > &contfreq, Matrix1D< double > &digfreq, double pixel_size)
Definition: xmipp_fft.h:139
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
#define YY(v)
Definition: matrix1d.h:93
void lookFor(int n, const Matrix1D< double > &u, Matrix1D< double > &freq, int iwhat=0)
Definition: ctf.cpp:1428
#define COSD(x)
Definition: xmipp_macros.h:329

◆ CTF_fitness_object()

double ProgCTFEstimateFromPSD::CTF_fitness_object ( double *  p)

CTF fitness

Definition at line 601 of file ctf_estimate_from_psd.cpp.

602 {
603  double retval;
604  // Generate CTF model
605  switch (action)
606  {
607  // Remind that p is a vector whose first element is at index 1
608  case 0:
612  if (show_inf >= 2)
613  {
614  std::cout << "Input vector:";
615  for (int i = 1; i <= SQRT_CTF_PARAMETERS; i++)
616  std::cout << p[i] << " ";
617  std::cout << std::endl;
618  }
619  break;
620  case 1:
624  if (show_inf >= 2)
625  {
626  std::cout << "Input vector:";
627  for (int i = 1; i <= BACKGROUND_CTF_PARAMETERS; i++)
628  std::cout << p[i] << " ";
629  std::cout << std::endl;
630  }
631  break;
632  case 2:
636  if (show_inf >= 2)
637  {
638  std::cout << "Input vector:";
639  for (int i = 1; i <= ENVELOPE_PARAMETERS; i++)
640  std::cout << p[i] << " ";
641  std::cout << std::endl;
642  }
643  break;
644  case 3:
649  if (show_inf >= 2)
650  {
651  std::cout << "Input vector:";
652  for (int i = 1; i <= DEFOCUS_PARAMETERS; i++)
653  std::cout << p[i] << " ";
654  std::cout << std::endl;
655  }
656  break;
657  case 4:
661  if (show_inf >= 2)
662  {
663  std::cout << "Input vector:";
664  for (int i = 1; i <= CTF_PARAMETERS; i++)
665  std::cout << p[i] << " ";
666  std::cout << std::endl;
667  }
668  break;
669  case 5:
670  case 6:
671  case 7:
675  if (show_inf >= 2)
676  {
677  std::cout << "Input vector:";
678  for (int i = 1; i <= ALL_CTF_PARAMETERS; i++)
679  std::cout << p[i] << " ";
680  std::cout << std::endl;
681  }
682 
683  break;
684  }
685 
687 
688  if (show_inf >= 2)
689  std::cout << "Model:\n" << current_ctfmodel << std::endl;
691  {
692  if (show_inf >= 2)
693  std::cout << "Does not have physical meaning\n";
694  return heavy_penalization;
695  }
696 
697  if (action > 3 && !noDefocusEstimate
698  && (fabs(
700  / ctfmodel_defoci.DeltafU) > 0.2
701  || fabs(
704  / ctfmodel_defoci.DeltafU) > 0.2))
705  {
706  if (show_inf >= 2)
707  std::cout << "Too large defocus\n";
708  return heavy_penalization;
709  }
710  /*if (initial_ctfmodel.DeltafU != 0 && action >= 3)
711  {
712  // If there is an initial model, the true solution
713  // cannot be too far
714 
715  if (fabs(initial_ctfmodel.DeltafU - current_ctfmodel.DeltafU) > defocus_range ||
716  fabs(initial_ctfmodel.DeltafV - current_ctfmodel.DeltafV) > defocus_range)
717  {std::cout << "Entra2" << std::endl;
718  if (show_inf >= 2)
719  {
720  std::cout << "Too far from hint: Initial (" << initial_ctfmodel.DeltafU << "," << initial_ctfmodel.DeltafV << ")"
721  << " current guess (" << current_ctfmodel.DeltafU << "," << current_ctfmodel.DeltafV << ") max allowed difference: "
722  << defocus_range << std::endl;
723  }
724  return heavy_penalization;
725  }
726  }*/
727 
728  /*if ((initial_ctfmodel.phase_shift != 0.0 || initial_ctfmodel.phase_shift != 1.57079) && action >= 5)
729  {
730  if (fabs(initial_ctfmodel.phase_shift - current_ctfmodel.phase_shift) > 0.05)
731  {
732  return heavy_penalization;
733  }
734  }*/
735 
736  // Now the 2D error
737  double distsum = 0;
738  int N = 0, Ncorr = 0;
739  double enhanced_avg = 0;
740  double model_avg = 0;
741  double enhanced_model = 0;
742  double enhanced2 = 0;
743  double model2 = 0;
744  double lowerLimit = 1.1 * min_freq_psd;
745  double upperLimit = 0.9 * max_freq_psd;
746  const MultidimArray<double>& local_enhanced_ctf = enhanced_ctftomodel();
747  int XdimW=XSIZE(w_digfreq);
748  int YdimW=YSIZE(w_digfreq);
749  corr13=0;
750 
751  for (int i = 0; i < YdimW; i += evaluation_reduction)
752  for (int j = 0; j < XdimW; j += evaluation_reduction)
753  {
754  if (DIRECT_A2D_ELEM(mask, i, j) <= 0)
755  continue;
756 
757  // Compute each component
759  double bg = current_ctfmodel.getValueNoiseAt();
760  double envelope=0, ctf_without_damping, ctf_with_damping=0;
761  double ctf2_th=0;
762  double ctf2 = DIRECT_A2D_ELEM(*f, i, j);
763  double dist = 0;
764  double ctf_with_damping2;
765  switch (action)
766  {
767  case 0:
768  case 1:
769  ctf2_th = bg;
770  dist = fabs(ctf2 - bg);
771  if (penalize && bg > ctf2
773  > max_gauss_freq)
774  dist *= current_penalty;
775  break;
776  case 2:
777  envelope = current_ctfmodel.getValueDampingAt();
778  ctf2_th = bg + envelope * envelope;
779  dist = fabs(ctf2 - ctf2_th);
780  if (penalize && ctf2_th < ctf2
782  > max_gauss_freq)
783  dist *= current_penalty;
784  break;
785  case 3:
786  case 4:
787  case 5:
788  case 6:
789  case 7:
790  envelope = current_ctfmodel.getValueDampingAt();
791  ctf_without_damping =
793  ctf_with_damping = envelope * ctf_without_damping;
794  ctf2_th = bg + ctf_with_damping * ctf_with_damping;
795 
796  if (DIRECT_A2D_ELEM(w_digfreq,i, j) < upperLimit
797  && DIRECT_A2D_ELEM(w_digfreq,i, j) > lowerLimit)
798  {
799  if (action == 3 ||
800  (action == 4 && DIRECT_A2D_ELEM(mask_between_zeroes,i,j) == 1) ||
801  (action == 7 && DIRECT_A2D_ELEM(mask_between_zeroes,i,j) == 1))
802  {
803  double enhanced_ctf =
804  DIRECT_A2D_ELEM(local_enhanced_ctf, i, j);
805  ctf_with_damping2 = ctf_with_damping * ctf_with_damping;
806  enhanced_model += enhanced_ctf * ctf_with_damping2;
807  enhanced2 += enhanced_ctf * enhanced_ctf;
808  model2 += ctf_with_damping2 * ctf_with_damping2;
809  enhanced_avg += enhanced_ctf;
810  model_avg += ctf_with_damping2;
811  Ncorr++;
812 
813  if (action==3)
814  {
815  int r = A2D_ELEM(w_digfreq_r,i, j);
816  A1D_ELEM(psd_theo_radial,r) += ctf2_th;
817  }
818  }
819  }
820  if (envelope > 1e-2)
821  dist = fabs(ctf2 - ctf2_th) / (envelope * envelope);
822  else
823  dist = fabs(ctf2 - ctf2_th);
824  // This expression comes from mapping any value so that
825  // bg becomes 0, and bg+envelope^2 becomes 1
826  // This is the transformation
827  // (x-bg) x-bg
828  // -------------=-------
829  // (bg+env^2-bg) env^2
830  // If we subtract two of this scaled values
831  // x-bg y-bg x-y
832  // ------- - ------- = -------
833  // env^2 env^2 env^2
834  break;
835  }
836 
837  distsum += dist * DIRECT_A2D_ELEM(mask,i,j);
838  N++;
839  }
840 
841  if (N > 0)
842  retval = distsum / N;
843  else
844  retval = heavy_penalization;
845 
846  if (show_inf >=2)
847  std::cout << "Fitness1=" << retval << std::endl;
848 
849  if ( (((action >= 3) && (action <= 4)) || (action == 7))
850  && (Ncorr > 0) && (enhanced_weight != 0) )
851  {
852  model_avg /= Ncorr;
853  enhanced_avg /= Ncorr;
854  double correlation_coeff = enhanced_model / Ncorr
855  - model_avg * enhanced_avg;
856  double sigma1 = sqrt(
857  fabs(enhanced2 / Ncorr - enhanced_avg * enhanced_avg));
858  double sigma2 = sqrt(fabs(model2 / Ncorr - model_avg * model_avg));
859  double maxSigma = std::max(sigma1, sigma2);
860 
861  if (sigma1 < XMIPP_EQUAL_ACCURACY || sigma2 < XMIPP_EQUAL_ACCURACY
862  || (fabs(sigma1 - sigma2) / maxSigma > 0.9 && action>=5))
863  {
864  retval = heavy_penalization;
865  if (show_inf>=2)
866  std::cout << "Fitness2=" << heavy_penalization << " sigma1=" << sigma1 << " sigma2=" << sigma2 << std::endl;
867  }
868  else
869  {
870  correlation_coeff /= sigma1 * sigma2;
871  if (action == 7)
872  corr13 = correlation_coeff;
873  else
874  retval -= enhanced_weight * correlation_coeff;
875  if (show_inf >= 2)
876  {
877  std::cout << "model_avg=" << model_avg << std::endl;
878  std::cout << "enhanced_avg=" << enhanced_avg << std::endl;
879  std::cout << "enhanced_model=" << enhanced_model / Ncorr
880  << std::endl;
881  std::cout << "sigma1=" << sigma1 << std::endl;
882  std::cout << "sigma2=" << sigma2 << std::endl;
883  std::cout << "Fitness2="
884  << -(enhanced_weight * correlation_coeff)
885  << " (" << correlation_coeff << ")" << std::endl;
886  }
887  }
888  // Correlation of the derivative of the radial profile
889  if (action==3 || evaluation_reduction==1)
890  {
891  int state=0;
892  double maxDiff=0;
894  double lowerlimt=1.1*min_freq;
895  double upperlimit=0.9*max_freq;
897  if (A1D_ELEM(w_digfreq_r_iN,i)>0)
898  {
900  double freq=A2D_ELEM(w_digfreq,i,0);
901  switch (state)
902  {
903  case 0:
904  if (freq>lowerlimt)
905  state=1;
906  break;
907  case 1:
908  if (freq>upperlimit)
909  state=2;
910  else
911  {
914  maxDiff=std::max(maxDiff,fabs(diff));
915  }
916  break;
917  }
918  }
919 
920  double corrRadialDerivative=0,mux=0, muy=0, Ncorr=0, sigmax=0, sigmay=0;
921  double iMaxDiff=1.0/maxDiff;
923  {
927  corrRadialDerivative+=x*y;
928  mux+=x;
929  muy+=y;
930  sigmax+=x*x;
931  sigmay+=y*y;
932  Ncorr++;
933  }
934  if (Ncorr>0)
935  {
936  double iNcorr=1.0/Ncorr;
937  corrRadialDerivative*=iNcorr;
938  mux*=iNcorr;
939  muy*=iNcorr;
940  sigmax=sqrt(fabs(sigmax*iNcorr-mux*mux));
941  sigmay=sqrt(fabs(sigmay*iNcorr-muy*muy));
942  corrRadialDerivative=(corrRadialDerivative-mux*muy)/(sigmax*sigmay);
943  }
944 
945  retval-=corrRadialDerivative;
946 
947  if (show_inf>=2)
948  {
949  std::cout << "Fitness3=" << -corrRadialDerivative << std::endl;
950  if (show_inf==3)
951  {
952  psd_exp_radial.write("PPPexpRadial.txt");
953  psd_theo_radial.write("PPPtheoRadial.txt");
954  psd_exp_radial_derivative.write("PPPexpRadialDerivative.txt");
955  psd_theo_radial_derivative.write("PPPtheoRadialDerivative.txt");
956  }
957  }
958 
959  }
960 
961  }
962 
963  // Show some debugging information
964  if (show_inf >= 2)
965  {
966  std::cout << "Fitness=" << retval << std::endl;
967  if (show_inf == 3)
968  {
970  std::cout << "Press any key\n";
971  char c;
972  std::cin >> c;
973  }
974  }
975 
976  return retval;
977 }
constexpr int ENVELOPE_PARAMETERS
#define YSIZE(v)
#define A2D_ELEM(v, i, j)
double enhanced_weight
Weight of the enhanced image.
MultidimArray< double > * f
void saveIntermediateResults(const FileName &fn_root, bool generate_profiles)
doublereal * c
void sqrt(Image< double > &op)
constexpr int FIRST_SQRT_PARAMETER
static double * y
MultidimArray< double > w_digfreq
void assignCTFfromParameters(double *p, CTFDescription &ctfmodel, int ia, int l, int modelSimplification)
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
#define DIRECT_A2D_ELEM(v, i, j)
#define A1D_ELEM(v, i)
MultidimArray< double > w_digfreq_r_iN
MultidimArray< double > psd_exp_enhanced_radial_derivative
constexpr int DEFOCUS_PARAMETERS
MultidimArray< double > w_digfreq_r
doublereal * x
#define i
double max_freq
Maximum frequency to adjust.
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
MultidimArray< double > psd_theo_radial
constexpr int ALL_CTF_PARAMETERS
MultidimArray< double > psd_exp_radial
PSD data.
constexpr int FIRST_ENVELOPE_PARAMETER
double getValueNoiseAt(bool show=false) const
Compute noise at (X,Y). Continuous frequencies, notice it is squared.
Definition: ctf.h:1121
bool noDefocusEstimate
No defocus estimate.
#define XMIPP_EQUAL_ACCURACY
Definition: xmipp_macros.h:119
constexpr int CTF_PARAMETERS
#define XSIZE(v)
void write(const FileName &fn) const
void max(Image< double > &op1, const Image< double > &op2)
constexpr int FIRST_DEFOCUS_PARAMETER
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
double getValueDampingAt(bool show=false) const
Compute CTF damping at (U,V). Continuous frequencies.
Definition: ctf.h:424
double getValuePureWithoutDampingAt(bool show=false) const
Compute pure CTF without damping at (U,V). Continuous frequencies.
Definition: ctf.h:542
MultidimArray< double > psd_theo_radial_derivative
#define j
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
int modelSimplification
Model simplification.
constexpr int BACKGROUND_CTF_PARAMETERS
MultidimArray< double > mask_between_zeroes
double min_freq
Minimum frequency to adjust.
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
Image< double > enhanced_ctftomodel
CTF amplitude to model.
constexpr int SQRT_CTF_PARAMETERS
MultidimArray< double > psd_exp_radial_derivative
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.
bool hasPhysicalMeaning()
Definition: ctf.cpp:1653

◆ defineBasicParams()

void ProgCTFEstimateFromPSD::defineBasicParams ( XmippProgram program)
static

Define basic parameters.

Definition at line 289 of file ctf_estimate_from_psd.cpp.

290 {
291 // ProgCTFBasicParams::defineBasicParams(program);
293 
294 }
static void defineParams(XmippProgram *program)
Define parameters in the command line.
Definition: ctf.cpp:1287

◆ defineParams()

void ProgCTFEstimateFromPSD::defineParams ( )
virtual

Define Parameters.

Reimplemented from XmippProgram.

Definition at line 296 of file ctf_estimate_from_psd.cpp.

297 {
298  defineBasicParams(this);
300 }
void defineParams()
Define Parameters.
static void defineBasicParams(XmippProgram *program)
Define basic parameters.

◆ estimate_background_gauss_parameters()

void ProgCTFEstimateFromPSD::estimate_background_gauss_parameters ( )

Definition at line 1184 of file ctf_estimate_from_psd.cpp.

1185 {
1186 
1187  if (show_optimization)
1188  std::cout << "Computing first background Gaussian parameters ...\n";
1189 
1190  // Compute radial averages
1191  MultidimArray<double> radial_CTFmodel_avg(YSIZE(*f) / 2);
1192  MultidimArray<double> radial_CTFampl_avg(YSIZE(*f) / 2);
1193  MultidimArray<int> radial_N(YSIZE(*f) / 2);
1194  double w_max_gauss = 0.25;
1196  {
1197  if (mask(i, j) <= 0)
1198  continue;
1199  double w = w_digfreq(i, j);
1200  if (w > w_max_gauss)
1201  continue;
1202 
1203  int r = FLOOR(w * (double)YSIZE(*f));
1205  radial_CTFmodel_avg(r) += current_ctfmodel.getValueNoiseAt();
1206  radial_CTFampl_avg(r) += (*f)(i, j);
1207  radial_N(r)++;
1208  }
1209 
1210  // Compute the average radial error
1211  double error2_avg = 0;
1212  int N_avg = 0;
1214  error.initZeros(radial_CTFmodel_avg);
1215  FOR_ALL_ELEMENTS_IN_ARRAY1D(radial_CTFmodel_avg)
1216  {
1217  if (radial_N(i) == 0)
1218  continue;
1219  error(i) = (radial_CTFampl_avg(i) - radial_CTFmodel_avg(i))
1220  / radial_N(i);
1221  error2_avg += error(i) * error(i);
1222  N_avg++;
1223  }
1224  if (N_avg != 0)
1225  error2_avg /= N_avg;
1226 
1227 #ifdef DEBUG
1228 
1229  std::cout << "Error2 avg=" << error2_avg << std::endl;
1230 #endif
1231 
1232  // Compute the minimum radial error
1233  bool first = true, OK_to_proceed = false;
1234  double error2_min = 0, wmin=0;
1235  FOR_ALL_ELEMENTS_IN_ARRAY1D(radial_CTFmodel_avg)
1236  {
1237  if (radial_N(i) == 0)
1238  continue;
1239  double w = w_digfreq(i, 0);
1240 
1241  if (error(i) < 0 && first)
1242  continue;
1243  else if (error(i) < 0)
1244  break;
1245  double error2 = error(i) * error(i);
1246  // If the two lines cross, do not consider any error until
1247  // the cross is "old" enough
1248  if (first && error2 > 0.15 * error2_avg)
1249  OK_to_proceed = true;
1250  if (first && i > 0)
1251  OK_to_proceed &= (error(i) < error(i - 1));
1252 
1253  // If the error now is bigger than a 30% (1.69=1.3*1.3) of the error min
1254  // this must be a rebound. Stop here
1255  if (!first && error2 > 1.69 * error2_min)
1256  break;
1257  if (first && OK_to_proceed)
1258  {
1259  wmin = w;
1260  error2_min = error2;
1261  first = false;
1262  }
1263  if (!first && error2 < error2_min)
1264  {
1265  wmin = w;
1266  error2_min = error2;
1267  }
1268 #ifdef DEBUG
1269  std::cout << w << " " << error2 << " " << wmin << " " << std::endl;
1270 #endif
1271 
1272  }
1273 
1274  // Compute the frequency of the minimum error
1275  max_gauss_freq = wmin;
1276 #ifdef DEBUG
1277 
1278  std::cout << "Freq of the minimum error: " << wmin << " " << fmin << std::endl;
1279 #endif
1280 
1281  // Compute the maximum radial error
1282  first = true;
1283  double error2_max = 0, wmax=0, fmax;
1284  FOR_ALL_ELEMENTS_IN_ARRAY1D(radial_CTFmodel_avg)
1285  {
1286  if (radial_N(i) == 0)
1287  continue;
1288  double w = w_digfreq(i, 0);
1289  if (w > wmin)
1290  continue;
1291 
1292  if (error(i) < 0 && first)
1293  continue;
1294  else if (error(i) < 0)
1295  break;
1296  double error2 = error(i) * error(i);
1297  if (first)
1298  {
1299  wmax = w;
1300  error2_max = error2;
1301  first = false;
1302  }
1303  if (error2 > error2_max)
1304  {
1305  wmax = w;
1306  error2_max = error2;
1307  }
1308 #ifdef DEBUG
1309  std::cout << w << " " << error2 << " " << wmax << std::endl;
1310 #endif
1311 
1312  }
1314 #ifdef DEBUG
1315 
1316  std::cout << "Freq of the maximum error: " << wmax << " " << fmax << std::endl;
1317 #endif
1318 
1319  // Find the linear least squares solution for the gauss part
1320  Matrix2D<double> A(2, 2);
1321  A.initZeros();
1322  Matrix1D<double> b(2);
1323  b.initZeros();
1324 
1326  {
1327  if (mask(i, j) <= 0)
1328  continue;
1329  if (w_digfreq(i, j) > wmin)
1330  continue;
1331  double fmod = w_contfreq(i, j);
1332 
1333  // Compute weight for this point
1334  double weight = 1 + max_freq_psd - w_digfreq(i, j);
1335  // Compute error
1337  double explained = current_ctfmodel.getValueNoiseAt();
1338 
1339  double unexplained = (*f)(i, j) - explained;
1340  if (unexplained <= 0)
1341  continue;
1342  unexplained = log(unexplained);
1343  double F = -(fmod - fmax) * (fmod - fmax);
1344 
1345  A(0, 0) += weight * 1;
1346  A(0, 1) += weight * F;
1347  A(1, 1) += weight * F * F;
1348  b(0) += weight * unexplained;
1349  b(1) += F * weight * unexplained;
1350  }
1351  A(1, 0) = A(0, 1);
1352  if ( (A(0, 0)== 0) && (A(1, 0)== 0) && (A(1, 1)== 0))
1353  {
1354  std::cout << "A matrix es zero" << std::endl;
1355  }
1356  else
1357  {
1358  b = A.inv() * b;
1359  current_ctfmodel.sigmaU = XMIPP_MIN(fabs(b(1)), 95e3); // This value should be
1360  current_ctfmodel.sigmaV = XMIPP_MIN(fabs(b(1)), 95e3); // conformant with the physical
1361  // meaning routine in CTF.cc
1362  current_ctfmodel.gaussian_K = exp(b(0));
1363  // Store the CTF values in global_prm->adjust
1366 
1367  if (show_optimization)
1368  {
1369  std::cout << "First Background Fit:\n" << current_ctfmodel << std::endl;
1370  saveIntermediateResults("step01c_first_background_fit");
1371  }
1372  center_optimization_focus(false, true, 1.5);
1373  }
1374 }
double sigmaU
Spherical aberration (in milimeters). Typical value 5.6.
Definition: ctf.h:871
#define YSIZE(v)
#define COPY_ctfmodel_TO_CURRENT_GUESS
MultidimArray< double > * f
void saveIntermediateResults(const FileName &fn_root, bool generate_profiles)
MultidimArray< double > w_digfreq
double Tm
Sampling rate.
double cU
Gaussian center for U.
Definition: ctf.h:875
doublereal * w
double gaussian_K
Gain for the gaussian term.
Definition: ctf.h:279
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
MultidimArray< double > w_contfreq
glob_log first
doublereal * b
void center_optimization_focus(bool adjust_freq, bool adjust_th, double margin)
#define FLOOR(x)
Definition: xmipp_macros.h:240
void log(Image< double > &op)
double getValueNoiseAt(bool show=false) const
Compute noise at (X,Y). Continuous frequencies, notice it is squared.
Definition: ctf.h:1121
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
double sigmaV
Gaussian width V.
Definition: ctf.h:873
bool show_optimization
Show convergence values.
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
#define j
void error(char *s)
Definition: tools.cpp:107
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
void forcePhysicalMeaning()
Definition: ctf.cpp:1781
MultidimArray< double > x_contfreq
Frequencies in axes.
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.
double fmax
MultidimArray< double > y_contfreq
double cV
Gaussian center for V.
Definition: ctf.h:877

◆ estimate_background_gauss_parameters2()

void ProgCTFEstimateFromPSD::estimate_background_gauss_parameters2 ( )

Definition at line 1379 of file ctf_estimate_from_psd.cpp.

1380 {
1381  if (show_optimization)
1382  std::cout << "Computing first background Gaussian2 parameters ...\n";
1383 
1384  // Compute radial averages
1385  MultidimArray<double> radial_CTFmodel_avg(YSIZE(*f) / 2);
1386  MultidimArray<double> radial_CTFampl_avg(YSIZE(*f) / 2);
1387  MultidimArray<int> radial_N(YSIZE(*f) / 2);
1388  double w_max_gauss = 0.25;
1390  {
1391  if (mask(i, j) <= 0)
1392  continue;
1393  double w = w_digfreq(i, j);
1394  if (w > w_max_gauss)
1395  continue;
1396 
1397  int r = FLOOR(w * (double)YSIZE(*f));
1398  double f_x = DIRECT_A2D_ELEM(x_contfreq, i, j);
1399  double f_y = DIRECT_A2D_ELEM(y_contfreq, i, j);
1401  double bg = current_ctfmodel.getValueNoiseAt();
1402  double envelope = current_ctfmodel.getValueDampingAt();
1403  double ctf_without_damping =
1405  double ctf_with_damping = envelope * ctf_without_damping;
1406  double ctf2_th = bg + ctf_with_damping * ctf_with_damping;
1407  radial_CTFmodel_avg(r) += ctf2_th;
1408  radial_CTFampl_avg(r) += (*f)(i, j);
1409  radial_N(r)++;
1410  }
1411 
1412  // Compute the average radial error
1414  error.initZeros(radial_CTFmodel_avg);
1415  FOR_ALL_ELEMENTS_IN_ARRAY1D(radial_CTFmodel_avg)
1416  {
1417  if (radial_N(i) == 0)
1418  continue;
1419  error(i) = (radial_CTFampl_avg(i) - radial_CTFmodel_avg(i))
1420  / radial_N(i);
1421  }
1422 #ifdef DEBUG
1423  std::cout << "Error:\n" << error << std::endl;
1424 #endif
1425 
1426  // Compute the frequency of the minimum error
1427  double wmin = 0.15;
1428 
1429  // Compute the maximum (negative) radial error
1430  double error_max = 0, wmax=0, fmax;
1431  FOR_ALL_ELEMENTS_IN_ARRAY1D(radial_CTFmodel_avg)
1432  {
1433  if (radial_N(i) == 0)
1434  continue;
1435  double w = w_digfreq(i, 0);
1436  if (w > wmin)
1437  break;
1438  if (error(i) < error_max)
1439  {
1440  wmax = w;
1441  error_max = error(i);
1442  }
1443  }
1445 #ifdef DEBUG
1446 
1447  std::cout << "Freq of the maximum error: " << wmax << " " << fmax << std::endl;
1448 #endif
1449 
1450  // Find the linear least squares solution for the gauss part
1451  Matrix2D<double> A(2, 2);
1452  A.initZeros();
1453  Matrix1D<double> b(2);
1454  b.initZeros();
1455  int N = 0;
1457  {
1458  if (mask(i, j) <= 0)
1459  continue;
1460  if (w_digfreq(i, j) > wmin)
1461  continue;
1462  double fmod = w_contfreq(i, j);
1463 
1464  // Compute the zero on the direction of this point
1465  Matrix1D<double> u(2), fzero(2);
1466  XX(u) = x_contfreq(i, j) / fmod;
1467  YY(u) = y_contfreq(i, j) / fmod;
1468  current_ctfmodel.lookFor(1, u, fzero, 0);
1469  if (fmod > fzero.module())
1470  continue;
1471 
1472  // Compute weight for this point
1473  double weight = 1 + max_freq_psd - w_digfreq(i, j);
1474 
1475  // Compute error
1476  double f_x = DIRECT_A2D_ELEM(x_contfreq, i, j);
1477  double f_y = DIRECT_A2D_ELEM(y_contfreq, i, j);
1479  double bg = current_ctfmodel.getValueNoiseAt();
1480  double envelope = current_ctfmodel.getValueDampingAt();
1481  double ctf_without_damping =
1483  double ctf_with_damping = envelope * ctf_without_damping;
1484  double ctf2_th = bg + ctf_with_damping * ctf_with_damping;
1485  double explained = ctf2_th;
1486  double unexplained = explained - (*f)(i, j);
1487 
1488  if (unexplained <= 0)
1489  continue;
1490  unexplained = log(unexplained);
1491  double F = -(fmod - fmax) * (fmod - fmax);
1492  A(0, 0) += weight * 1;
1493  A(0, 1) += weight * F;
1494  A(1, 1) += weight * F * F;
1495  b(0) += weight * unexplained;
1496  b(1) += F * weight * unexplained;
1497  N++;
1498  }
1499  if (N != 0)
1500  {
1501  A(1, 0) = A(0, 1);
1502 
1503  double det=A.det();
1504  if (fabs(det)>1e-9)
1505  {
1506  b = A.inv() * b;
1507  current_ctfmodel.sigmaU2 = XMIPP_MIN(fabs(b(1)), 95e3); // This value should be
1508  current_ctfmodel.sigmaV2 = XMIPP_MIN(fabs(b(1)), 95e3); // conformant with the physical
1509  // meaning routine in CTF.cc
1510  current_ctfmodel.gaussian_K2 = exp(b(0));
1511  }
1512  else
1513  {
1516  }
1517  }
1518  else
1519  {
1522  }
1523 
1524  // Store the CTF values in global_prm->adjust
1527 
1528 #ifdef DEBUG
1529  // Check
1531  {
1532  if (mask(i, j)<=0)
1533  continue;
1534  if (w_digfreq(i, j) > wmin)
1535  continue;
1536  double fmod = w_contfreq(i, j);
1537 
1538  // Compute the zero on the direction of this point
1539  Matrix1D<double> u(2), fzero(2);
1540  XX(u) = x_contfreq(i, j) / fmod;
1541  YY(u) = y_contfreq(i, j) / fmod;
1542  global_ctfmodel.zero(1, u, fzero);
1543  if (fmod > fzero.module())
1544  continue;
1545 
1546  // Compute error
1547  double f_x = DIRECT_A2D_ELEM(x_contfreq, i, j);
1548  double f_y = DIRECT_A2D_ELEM(y_contfreq, i, j);
1549  double bg = current_ctfmodel.getValueNoiseAt(f_x, f_y);
1550  double envelope = current_ctfmodel.getValueDampingAt(f_x, f_y);
1551  double ctf_without_damping = current_ctfmodel.getValuePureWithoutDampingAt(f_x, f_y);
1552  double ctf_with_damping = envelope * ctf_without_damping;
1553  double ctf2_th = bg + ctf_with_damping * ctf_with_damping;
1554  double explained = ctf2_th;
1555  double unexplained = explained - (*f)(i, j);
1556 
1557  if (unexplained <= 0)
1558  continue;
1559  std::cout << fmod << " " << unexplained << " "
1561  (fmod - fmax)*(fmod - fmax)) << std::endl;
1562  }
1563 #endif
1564 
1565  if (show_optimization)
1566  {
1567  std::cout << "First Background Gaussian 2 Fit:\n" << current_ctfmodel
1568  << std::endl;
1569  saveIntermediateResults("step04a_first_background2_fit");
1570  }
1571 }
#define YSIZE(v)
#define COPY_ctfmodel_TO_CURRENT_GUESS
MultidimArray< double > * f
void saveIntermediateResults(const FileName &fn_root, bool generate_profiles)
MultidimArray< double > w_digfreq
#define DIRECT_A2D_ELEM(v, i, j)
double Tm
Sampling rate.
double cU2
Second Gaussian center for U.
Definition: ctf.h:893
doublereal * w
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
MultidimArray< double > w_contfreq
doublereal * b
#define FLOOR(x)
Definition: xmipp_macros.h:240
void log(Image< double > &op)
#define XX(v)
Definition: matrix1d.h:85
double gaussian_K2
Gain for the second Gaussian term.
Definition: ctf.h:289
double getValueNoiseAt(bool show=false) const
Compute noise at (X,Y). Continuous frequencies, notice it is squared.
Definition: ctf.h:1121
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
double getValueDampingAt(bool show=false) const
Compute CTF damping at (U,V). Continuous frequencies.
Definition: ctf.h:424
double getValuePureWithoutDampingAt(bool show=false) const
Compute pure CTF without damping at (U,V). Continuous frequencies.
Definition: ctf.h:542
bool show_optimization
Show convergence values.
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
#define j
#define YY(v)
Definition: matrix1d.h:93
void error(char *s)
Definition: tools.cpp:107
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
void lookFor(int n, const Matrix1D< double > &u, Matrix1D< double > &freq, int iwhat=0)
Definition: ctf.cpp:1428
void forcePhysicalMeaning()
Definition: ctf.cpp:1781
doublereal * u
MultidimArray< double > x_contfreq
Frequencies in axes.
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.
double cV2
Second Gaussian center for V.
Definition: ctf.h:895
double fmax
double sigmaU2
Second Gaussian width U.
Definition: ctf.h:889
MultidimArray< double > y_contfreq
double sigmaV2
Second Gaussian width V.
Definition: ctf.h:891

◆ estimate_background_sqrt_parameters()

void ProgCTFEstimateFromPSD::estimate_background_sqrt_parameters ( )

Definition at line 1072 of file ctf_estimate_from_psd.cpp.

1073 {
1074  if (show_optimization)
1075  std::cout << "Computing first sqrt background ...\n";
1076 
1077  // Estimate the base line taking the value of the CTF
1078  // for the maximum X and Y frequencies
1079  double base_line = 0;
1080  int N = 0;
1081 
1083  if (w_digfreq(i, j) > 0.4)
1084  {
1085  N++;
1086  base_line += (*f)(i, j);
1087 
1088  }
1089  if (0 == N) {
1090  REPORT_ERROR(ERR_NUMERICAL, "N is zero (0), which would lead to division by zero");
1091  }
1092  current_ctfmodel.base_line = base_line / N;
1093  // Find the linear least squares solution for the sqrt part
1094  Matrix2D<double> A(2, 2);
1095  A.initZeros();
1096  Matrix1D<double> b(2);
1097  b.initZeros();
1099  {
1100  if (mask(i, j) <= 0)
1101  continue;
1102 
1103  // Compute weight for this point
1104  double weight = 1 + max_freq_psd - w_digfreq(i, j);
1105 
1106  // Compute error
1108  double explained = current_ctfmodel.getValueNoiseAt();
1109  double unexplained = (*f)(i, j) - explained;
1110  if (unexplained <= 0)
1111  continue;
1112  unexplained = log(unexplained);
1113 
1114  double X = -sqrt(w_contfreq(i, j));
1115  A(0, 0) += weight * X * X;
1116  A(0, 1) += weight * X;
1117  A(1, 1) += weight * 1;
1118  b(0) += X * weight * unexplained;
1119  b(1) += weight * unexplained;
1120  }
1121  A(1, 0) = A(0, 1);
1122 
1123  b = A.inv() * b;
1124 
1126  current_ctfmodel.sqrt_K = exp(b(1));
1128 
1130 
1131  if (show_optimization)
1132  {
1133  std::cout << "First SQRT Fit:\n" << current_ctfmodel << std::endl;
1134  saveIntermediateResults("step01a_first_sqrt_fit");
1135  }
1136 
1137  // Now optimize .........................................................
1138  double fitness;
1140  steps.resize(SQRT_CTF_PARAMETERS);
1141  steps.initConstant(1);
1142  // Optimize without penalization
1143  if (show_optimization)
1144  std::cout << "Looking for best fitting sqrt ...\n";
1145  penalize = false;
1146  int iter;
1148  SQRT_CTF_PARAMETERS, CTF_fitness, global_prm, 0.05, fitness, iter, steps,
1150  // Optimize with penalization
1151  if (show_optimization)
1152  std::cout << "Penalizing best fitting sqrt ...\n";
1153  penalize = true;
1154  current_penalty = 2;
1155  int imax = CEIL(log(penalty) / log(2.0));
1156  for (int i = 1; i <= imax; i++)
1157  {
1158  if (show_optimization)
1159  std::cout << " Iteration " << i << " penalty="
1160  << current_penalty << std::endl;
1162  SQRT_CTF_PARAMETERS, CTF_fitness, global_prm, 0.05, fitness, iter,
1163  steps, show_optimization);
1164  current_penalty *= 2;
1165  current_penalty =
1167  }
1168  // Keep the result in global_prm->adjust
1171 
1172  if (show_optimization)
1173  {
1174  std::cout << "Best penalized SQRT Fit:\n" << current_ctfmodel
1175  << std::endl;
1176  saveIntermediateResults("step01b_best_penalized_sqrt_fit");
1177  }
1178 
1179  center_optimization_focus(false, true, 1.5);
1180 }
#define COPY_ctfmodel_TO_CURRENT_GUESS
void saveIntermediateResults(const FileName &fn_root, bool generate_profiles)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void sqrt(Image< double > &op)
constexpr int FIRST_SQRT_PARAMETER
MultidimArray< double > w_digfreq
Matrix1D< double > * adjust_params
void powellOptimizer(Matrix1D< double > &p, int i0, int n, double(*f)(double *x, void *), void *prm, double ftol, double &fret, int &iter, const Matrix1D< double > &steps, bool show)
glob_prnt iter
double sqrt_angle
Sqrt angle.
Definition: ctf.h:887
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
static constexpr double penalty
MultidimArray< double > w_contfreq
double base_line
Global base_line.
Definition: ctf.h:277
doublereal * b
void center_optimization_focus(bool adjust_freq, bool adjust_th, double margin)
double sqU
Gain for the square root term.
Definition: ctf.h:883
void log(Image< double > &op)
#define CEIL(x)
Definition: xmipp_macros.h:225
double getValueNoiseAt(bool show=false) const
Compute noise at (X,Y). Continuous frequencies, notice it is squared.
Definition: ctf.h:1121
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
double sqV
Sqrt width V.
Definition: ctf.h:885
Error related to numerical calculation.
Definition: xmipp_error.h:179
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
bool show_optimization
Show convergence values.
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
#define j
double steps
void forcePhysicalMeaning()
Definition: ctf.cpp:1781
constexpr int SQRT_CTF_PARAMETERS
MultidimArray< double > x_contfreq
Frequencies in axes.
MultidimArray< double > mask
Masks.
double sqrt_K
Gain for the square root term.
Definition: ctf.h:285
ProgCTFEstimateFromPSD * global_prm
void initConstant(T val)
Definition: matrix1d.cpp:83
double fitness(double *p)
MultidimArray< double > y_contfreq
double CTF_fitness(double *, void *)

◆ estimate_defoci()

void ProgCTFEstimateFromPSD::estimate_defoci ( )

Definition at line 1681 of file ctf_estimate_from_psd.cpp.

1682 {
1683  if (show_optimization)
1684  std::cout << "Looking for first defoci ...\n";
1685  double best_defocusU=0, best_defocusV=0, best_angle=0, best_K=1;
1686  double best_error = heavy_penalization * 1.1;
1687  bool first = true;
1688  int i, j;
1689  double defocusV, defocusU;
1690 
1691  double defocusV0 = 1e3, defocusU0 = 1e3;
1692  double defocusVF = 100e3, defocusUF = 100e3;
1693  double initial_defocusStep = 8e3;
1695 
1696  // Check if there is no initial guess
1697  double min_allowed_defocusU = 1e3, max_allowed_defocusU = 100e3;
1698  double min_allowed_defocusV = 1e3, max_allowed_defocusV = 100e3;
1699  if (initial_ctfmodel.DeltafU != 0)
1700  {
1701  initial_defocusStep = std::min(defocus_range,20000.0);
1702  defocusU0 = std::max(
1703  1e3,
1705  - defocus_range);
1706  double maxDeviation = std::max(defocus_range,
1707  0.25 * initial_ctfmodel.DeltafU);
1708  max_allowed_defocusU = std::min(100e3,
1709  initial_ctfmodel.DeltafU + maxDeviation);
1710  defocusUF = std::min(
1711  150e3,
1713  + defocus_range);
1714  min_allowed_defocusU = std::max(1e3,
1715  initial_ctfmodel.DeltafU - maxDeviation);
1716  if (initial_ctfmodel.DeltafV == 0)
1717  {
1718  defocusV0 = defocusU0;
1719  min_allowed_defocusV = min_allowed_defocusU;
1720  defocusVF = defocusUF;
1721  max_allowed_defocusV = max_allowed_defocusU;
1722  }
1723  else
1724  {
1725  defocusV0 = std::max(
1726  1e3,
1728  - defocus_range);
1729  max_allowed_defocusV = std::max(100e3,
1730  initial_ctfmodel.DeltafV + maxDeviation);
1731  defocusVF = std::min(
1732  150e3,
1734  + defocus_range);
1735  min_allowed_defocusV = std::max(1e3,
1736  initial_ctfmodel.DeltafV - maxDeviation);
1737  }
1738  }
1739 
1740  double K_so_far = current_ctfmodel.K;
1742  steps.initConstant(1);
1743  steps(3) = 0; // Do not optimize kV
1744  steps(4) = 0; // Do not optimize K
1745  for (double defocusStep = initial_defocusStep;
1746  defocusStep >= std::min(5000., defocus_range / 2);
1747  defocusStep /= 2)
1748  {
1749  error.resize(CEIL((defocusVF - defocusV0) / defocusStep + 1),
1750  CEIL((defocusUF - defocusU0) / defocusStep + 1));
1752  if (show_optimization)
1753  std::cout << "V=[" << defocusV0 << "," << defocusVF << "]\n"
1754  << "U=[" << defocusU0 << "," << defocusUF << "]\n"
1755  << "Defocus step=" << defocusStep << std::endl;
1756  for (defocusV = defocusV0, i = 0; defocusV <= defocusVF; defocusV +=
1757  defocusStep, i++)
1758  {
1759  for (defocusU = defocusU0, j = 0; defocusU <= defocusUF; defocusU +=
1760  defocusStep, j++)
1761  {
1762  bool first_angle = true;
1763  if (fabs(defocusU - defocusV) > 30e3)
1764  {
1765  error(i, j) = heavy_penalization;
1766  continue;
1767  }
1768  for (double angle = 0; angle < 180; angle += 45)
1769  {
1770  int iter;
1771  double fitness;
1772 
1773  (*adjust_params)(0) = defocusU;
1774  (*adjust_params)(1) = defocusV;
1775  (*adjust_params)(2) = angle;
1776  (*adjust_params)(4) = K_so_far;
1777 
1780  fitness, iter, steps, false);
1781 
1782  if ((first_angle || fitness < error(i, j))
1783  && (current_ctfmodel.DeltafU >= min_allowed_defocusU
1785  <= max_allowed_defocusU
1787  >= min_allowed_defocusV
1789  <= max_allowed_defocusV))
1790  {
1791  error(i, j) = fitness;
1792  first_angle = false;
1793  if (error(i, j) < best_error || first)
1794  {
1795  best_error = error(i, j);
1796  best_defocusU = current_ctfmodel.DeltafU;
1797  best_defocusV = current_ctfmodel.DeltafV;
1798  best_angle = current_ctfmodel.azimuthal_angle;
1799  best_K = current_ctfmodel.K;
1800  first = false;
1801  if (show_optimization)
1802  {
1803  std::cout << " (DefocusU,DefocusV)=("
1804  << defocusU << "," << defocusV
1805  << "), ang=" << angle << " --> ("
1806  << current_ctfmodel.DeltafU << ","
1807  << current_ctfmodel.DeltafV << "),"
1809  << " K=" << current_ctfmodel.K
1810  << " error=" << error(i, j)
1811  << std::endl;
1812 #ifdef DEBUG
1813 
1814  show_inf=3;
1815  CTF_fitness(adjust_params->vdata-1,NULL);
1816  show_inf=0;
1817 
1818  Image<double> save;
1819  save() = enhanced_ctftomodel();
1820  save.write("PPPenhanced.xmp");
1821  for (int i = 0; i < YSIZE(w_digfreq); i += 1)
1822  for (int j = 0; j < XSIZE(w_digfreq); j += 1)
1823  {
1824  if (DIRECT_A2D_ELEM(mask, i, j)<=0)
1825  continue;
1826  double f_x = DIRECT_A2D_ELEM(x_contfreq, i, j);
1827  double f_y = DIRECT_A2D_ELEM(y_contfreq, i, j);
1828  double envelope = current_ctfmodel.getValueDampingAt(f_x, f_y);
1829  double ctf_without_damping = current_ctfmodel.getValuePureWithoutDampingAt(f_x, f_y);
1830  double ctf_with_damping = envelope * ctf_without_damping;
1831  double ctf_with_damping2 = ctf_with_damping * ctf_with_damping;
1832  save(i, j) = ctf_with_damping2;
1833  }
1834  save.write("PPPctf2_with_damping2.xmp");
1835  std::cout << "Press any key\n";
1836  char c;
1837  std::cin >> c;
1838 #endif
1839 
1840  }
1841  }
1842  }
1843  }
1844  }
1845  }
1846 
1847  // Compute the range of the errors
1848  double errmin = error(0, 0), errmax = error(0, 0);
1849  bool aValidErrorHasBeenFound=false;
1850  for (int ii = STARTINGY(error); ii <= FINISHINGY(error); ii++)
1851  for (int jj = STARTINGX(error); jj <= FINISHINGX(error); jj++)
1852  {
1853  if (error(ii, jj) != heavy_penalization)
1854  {
1855  aValidErrorHasBeenFound=true;
1856  if (error(ii, jj) < errmin)
1857  errmin = error(ii, jj);
1858  else if (errmax == heavy_penalization)
1859  errmax = error(ii, jj);
1860  else if (error(ii, jj) > errmax)
1861  errmax = error(ii, jj);
1862  }
1863  }
1864  if (show_optimization)
1865  std::cout << "Error matrix\n" << error << std::endl;
1866  if (!aValidErrorHasBeenFound)
1867  REPORT_ERROR(ERR_NUMERICAL,"Cannot find any good defocus within the given range");
1868 
1869  // Find those defoci which are within a 10% of the maximum
1870  if (show_inf >= 2)
1871  std::cout << "Range=" << errmax - errmin << std::endl;
1872  double best_defocusVmin = best_defocusV, best_defocusVmax =
1873  best_defocusV;
1874  double best_defocusUmin = best_defocusU, best_defocusUmax =
1875  best_defocusU;
1876  for (defocusV = defocusV0, i = 0; defocusV <= defocusVF; defocusV +=
1877  defocusStep, i++)
1878  {
1879  for (defocusU = defocusU0, j = 0; defocusU <= defocusUF; defocusU +=
1880  defocusStep, j++)
1881  {
1882  if (show_inf >= 2)
1883  std::cout << i << "," << j << " " << error(i, j) << " "
1884  << defocusU << " " << defocusV << std::endl
1885  << best_defocusUmin << " " << best_defocusUmax
1886  << std::endl << best_defocusVmin << " "
1887  << best_defocusVmax << std::endl;
1888  if (fabs((error(i, j) - errmin) / (errmax - errmin)) <= 0.1)
1889  {
1890  if (defocusV < best_defocusVmin)
1891  best_defocusVmin = defocusV;
1892  if (defocusU < best_defocusUmin)
1893  best_defocusUmin = defocusU;
1894  if (defocusV > best_defocusVmax)
1895  best_defocusVmax = defocusV;
1896  if (defocusU > best_defocusUmax)
1897  best_defocusUmax = defocusU;
1898  }
1899  }
1900  }
1901 
1902  defocusVF = std::min(max_allowed_defocusV,
1903  best_defocusVmax + defocusStep);
1904  defocusV0 = std::max(min_allowed_defocusV,
1905  best_defocusVmin - defocusStep);
1906  defocusUF = std::min(max_allowed_defocusU,
1907  best_defocusUmax + defocusStep);
1908  defocusU0 = std::max(min_allowed_defocusU,
1909  best_defocusUmin - defocusStep);
1910  i = j = 0;
1911  if (show_inf >= 2)
1912  {
1913  Image<double> save;
1914  save() = error;
1915  save.write("error.xmp");
1916  std::cout << "Press any key: Error saved\n";
1917  char c;
1918  std::cin >> c;
1919  }
1920  }
1921 
1922  current_ctfmodel.DeltafU = best_defocusU;
1923  current_ctfmodel.DeltafV = best_defocusV;
1924  current_ctfmodel.azimuthal_angle = best_angle;
1925  current_ctfmodel.K = best_K;
1926 
1927  // Keep the result in global_prm->adjust
1931 
1932  showFirstDefoci();
1933 }
double defocus_range
Defocus range.
#define YSIZE(v)
void min(Image< double > &op1, const Image< double > &op2)
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
#define COPY_ctfmodel_TO_CURRENT_GUESS
#define FINISHINGX(v)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
doublereal * c
MultidimArray< double > w_digfreq
Matrix1D< double > * adjust_params
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
#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)
void initConstant(T val)
void powellOptimizer(Matrix1D< double > &p, int i0, int n, double(*f)(double *x, void *), void *prm, double ftol, double &fret, int &iter, const Matrix1D< double > &steps, bool show)
constexpr int DEFOCUS_PARAMETERS
glob_prnt iter
#define STARTINGX(v)
#define i
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
#define STARTINGY(v)
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
Definition: ctf.h:832
glob_log first
#define CEIL(x)
Definition: xmipp_macros.h:225
#define XSIZE(v)
void max(Image< double > &op1, const Image< double > &op2)
Error related to numerical calculation.
Definition: xmipp_error.h:179
constexpr int FIRST_DEFOCUS_PARAMETER
double getValueDampingAt(bool show=false) const
Compute CTF damping at (U,V). Continuous frequencies.
Definition: ctf.h:424
double getValuePureWithoutDampingAt(bool show=false) const
Compute pure CTF without damping at (U,V). Continuous frequencies.
Definition: ctf.h:542
bool show_optimization
Show convergence values.
#define j
double steps
double K
Global gain. By default, 1.
Definition: ctf.h:238
void error(char *s)
Definition: tools.cpp:107
#define FINISHINGY(v)
void forcePhysicalMeaning()
Definition: ctf.cpp:1781
Image< double > enhanced_ctftomodel
CTF amplitude to model.
MultidimArray< double > x_contfreq
Frequencies in axes.
T * vdata
The array itself.
Definition: matrix1d.h:258
MultidimArray< double > mask
Masks.
CTFDescription initial_ctfmodel
CTF model.
ProgCTFEstimateFromPSD * global_prm
double fitness(double *p)
MultidimArray< double > y_contfreq
double CTF_fitness(double *, void *)

◆ estimate_defoci_Zernike() [1/2]

void ProgCTFEstimateFromPSD::estimate_defoci_Zernike ( const MultidimArray< double > &  psdToModelFullSize,
double  min_freq,
double  max_freq,
double  Tm,
double  kV,
double  lambdaPhase,
int  sizeWindowPhase,
double &  defocusU,
double &  defocusV,
double &  ellipseAngle,
int  verbose 
)

Definition at line 1937 of file ctf_estimate_from_psd.cpp.

1940 {
1941  // Center enhanced PSD
1942  MultidimArray<double> centeredEnhancedPSD=psdToModelFullSize;
1943  CenterFFT(centeredEnhancedPSD,true);
1944 
1945  // Estimate phase, modulation and Zernikes
1946  MultidimArray<double> mod, phase;
1947  Matrix1D<double> coefs(13);
1948  coefs.initConstant(0);
1949  VEC_ELEM(coefs,0) = 1;
1950  VEC_ELEM(coefs,3) = 1;
1951  VEC_ELEM(coefs,4) = 1;
1952  VEC_ELEM(coefs,5) = 1;
1953  VEC_ELEM(coefs,12) =1;
1954 
1955  auto x=(int)((0.3*max_freq+0.7*min_freq)*std::cos(PI/4)*XSIZE(centeredEnhancedPSD)+XSIZE(centeredEnhancedPSD)/2);
1956  DEBUG_TEXTFILE(formatString("Zernike1 %d",x));
1957  DEBUG_TEXTFILE(formatString("centeredEnhancedPSD80x80 %f",centeredEnhancedPSD(80,80)));
1958  DEBUG_TEXTFILE(formatString("centeredEnhancedPSD120x120 %f",centeredEnhancedPSD(120,120)));
1959  DEBUG_TEXTFILE(formatString("centeredEnhancedPSD160x160 %f",centeredEnhancedPSD(160,160)));
1960 
1961  int numElem = 10;
1962  kV = kV*1000;
1963  double K_so_far = current_ctfmodel.K;
1964  double lambda=12.2643247/std::sqrt(kV*(1.+0.978466e-6*kV));
1965  double Z8;
1966  double Z3;
1967  double Z4;
1968  double Z5;
1969  double eAngle=0.;
1970  double deFocusAvg;
1971  double deFocusDiff;
1972 
1973  max_freq = max_freq*0.8;
1974  double fmax = max_freq;
1975 
1976  Matrix1D<double> arrayDefocusDiff(numElem);
1977  arrayDefocusDiff.initZeros();
1978 
1979  Matrix1D<double> arrayDefocusAvg(numElem);
1980  arrayDefocusAvg.initZeros();
1981 
1982  Matrix1D<double> arrayError(numElem);
1983  arrayError.initConstant(-1);
1984 
1985  int iter;
1986  double fitness;
1988  steps.initConstant(1);
1989  steps(3) = 0; // Do not optimize kV
1990  steps(4) = 0; // Do not optimize K
1991  double fmaxStep = (max_freq-min_freq)/numElem;
1992 
1993  lambdaPhase = 0.8;
1994  sizeWindowPhase = 10;
1995 
1996  Matrix1D<double> initialGlobalAdjust = *adjust_params;
1997 
1998  for (int i = 1; i < numElem; i++)
1999  {
2000  if (((fmax - min_freq)/min_freq) > 0.5)
2001  {
2002  demodulate(centeredEnhancedPSD,lambdaPhase,sizeWindowPhase,
2003  x,x,
2004  (int)(min_freq*XSIZE(centeredEnhancedPSD)),
2005  (int)(fmax*XSIZE(centeredEnhancedPSD)),
2006  phase, mod, coefs, 0);
2007 
2008  Z8=VEC_ELEM(coefs,4);
2009  Z3=VEC_ELEM(coefs,12);
2010  Z4=VEC_ELEM(coefs,3);
2011  Z5=VEC_ELEM(coefs,5);
2012 
2013  eAngle = 0.5*RAD2DEG(std::atan2(Z5,Z4))+90.0;
2014  deFocusAvg = fabs(2*Tm*Tm*(2*Z3-6*Z8)/(PI*lambda));
2015  deFocusDiff = fabs(2*Tm*Tm*(std::sqrt(Z4*Z4+Z5*Z5))/(PI*lambda));
2016 
2017  coefs.initConstant(0);
2018  VEC_ELEM(coefs,0) = 1;
2019  VEC_ELEM(coefs,3) = 1;
2020  VEC_ELEM(coefs,4) = 1;
2021  VEC_ELEM(coefs,5) = 1;
2022  VEC_ELEM(coefs,12) =1;
2023 
2024  fmax -= fmaxStep;
2025 
2026  (*adjust_params)(0) = deFocusAvg+deFocusDiff;
2027  (*adjust_params)(1) = deFocusAvg-deFocusDiff;
2028  (*adjust_params)(2) = eAngle;
2029  (*adjust_params)(4) = K_so_far;
2030  (*adjust_params)(6) = 2;
2031 
2032 
2033  fitness =0;
2036  fitness, iter, steps, false);
2037 
2038  VEC_ELEM(arrayDefocusAvg,i) = ((*adjust_params)(0) +(*adjust_params)(1))/2;
2039  VEC_ELEM(arrayDefocusDiff,i) = ((*adjust_params)(0) -(*adjust_params)(1))/2;
2040  VEC_ELEM(arrayError,i) = (-1)*fitness;
2041 
2042  }
2043  }
2044 
2045  int maxInd=arrayError.maxIndex();
2046 
2047  while ( (VEC_ELEM(arrayDefocusAvg,maxInd) < 3000) || ((VEC_ELEM(arrayDefocusAvg,maxInd) > 50000) && VEC_ELEM(arrayError,maxInd)>-1e3 ))
2048  {
2049  VEC_ELEM(arrayError,maxInd) = -1e3;
2050  VEC_ELEM(arrayDefocusAvg,maxInd) = initial_ctfmodel.DeltafU;
2051  VEC_ELEM(arrayDefocusDiff,maxInd) = initial_ctfmodel.DeltafV;
2052  maxInd=arrayError.maxIndex();
2053  }
2054  if (VEC_ELEM(arrayError,maxInd)<=-1e3)
2055  {
2056  estimate_defoci();
2057  return;
2058  }
2059 
2060  Matrix1D<double> arrayDefocusU(3);
2061  arrayDefocusU.initZeros();
2062 
2063  Matrix1D<double> arrayDefocusV(3);
2064  arrayDefocusV.initZeros();
2065 
2066  Matrix1D<double> arrayError2(3);
2067  arrayError2.initConstant(-1);
2068 
2069  //We want to take care about more parameters
2070  // We optimize for deltaU, deltaV
2071  (*adjust_params)(0) = VEC_ELEM(arrayDefocusAvg,maxInd)+VEC_ELEM(arrayDefocusDiff,maxInd);
2072  (*adjust_params)(1) = VEC_ELEM(arrayDefocusAvg,maxInd)-VEC_ELEM(arrayDefocusDiff,maxInd);
2073  (*adjust_params)(2) = eAngle;
2074  (*adjust_params)(4) = K_so_far;
2075  (*adjust_params)(6) = 2;
2076 
2077  fitness =0;
2080  fitness, iter, steps, false);
2081 
2082  VEC_ELEM(arrayDefocusU,0) = (*adjust_params)(0);
2083  VEC_ELEM(arrayDefocusV,0) = (*adjust_params)(1);
2084  VEC_ELEM(arrayError2,0) = (-1)*fitness;
2085 
2086  // We optimize for deltaU, deltaU
2087  (*adjust_params)(0) = VEC_ELEM(arrayDefocusAvg,maxInd)+VEC_ELEM(arrayDefocusDiff,maxInd);
2088  (*adjust_params)(1) = VEC_ELEM(arrayDefocusAvg,maxInd)+VEC_ELEM(arrayDefocusDiff,maxInd);
2089  (*adjust_params)(2) = eAngle;
2090  (*adjust_params)(4) = K_so_far;
2091  (*adjust_params)(6) = 2;
2092 
2093  fitness =0;
2096  fitness, iter, steps, false);
2097 
2098  VEC_ELEM(arrayDefocusU,1) = (*adjust_params)(0);
2099  VEC_ELEM(arrayDefocusV,1) = (*adjust_params)(1);
2100  VEC_ELEM(arrayError2,1) = (-1)*fitness;
2101 
2102  // We optimize for deltaV, deltaV
2103  (*adjust_params)(0) = VEC_ELEM(arrayDefocusAvg,maxInd)-VEC_ELEM(arrayDefocusDiff,maxInd);
2104  (*adjust_params)(1) = VEC_ELEM(arrayDefocusAvg,maxInd)-VEC_ELEM(arrayDefocusDiff,maxInd);
2105  (*adjust_params)(2) = eAngle;
2106  (*adjust_params)(4) = K_so_far;
2107  (*adjust_params)(6) = 2;
2108 
2109  fitness =0;
2112  fitness, iter, steps, false);
2113 
2114  VEC_ELEM(arrayDefocusU,2) = (*adjust_params)(0);
2115  VEC_ELEM(arrayDefocusV,2) = (*adjust_params)(1);
2116  VEC_ELEM(arrayError2,2) = (-1)*fitness;
2117 
2118  //Here we select the best one
2119  maxInd=arrayError2.maxIndex();
2120  defocusU = VEC_ELEM(arrayDefocusU,maxInd);
2121  defocusV = VEC_ELEM(arrayDefocusV,maxInd);
2122 
2123  (*adjust_params)(0) = defocusU;
2124  (*adjust_params)(1) = defocusV;
2125  (*adjust_params)(2) = eAngle;
2126  (*adjust_params)(4) = K_so_far;
2127  (*adjust_params)(6) = 2;
2128 
2129  while ( (0.5*(defocusU+defocusV) < 2500) || (0.5*(defocusU+defocusV) > 60000) )
2130  {
2131  VEC_ELEM(arrayError2,maxInd) = -1e3;
2132  VEC_ELEM(arrayDefocusU,maxInd) = initial_ctfmodel.DeltafU;
2133  VEC_ELEM(arrayDefocusV,maxInd) = initial_ctfmodel.DeltafV;
2134 
2135  maxInd=arrayError2.maxIndex();
2136  defocusU = VEC_ELEM(arrayDefocusU,maxInd);
2137  defocusV = VEC_ELEM(arrayDefocusV,maxInd);
2138  (*adjust_params)(0) = defocusU;
2139  (*adjust_params)(1) = defocusV;
2140  (*adjust_params)(2) = eAngle;
2141  (*adjust_params)(4) = K_so_far;
2142  (*adjust_params)(6) = 2;
2143  }
2144 
2145  if (VEC_ELEM(arrayError2,maxInd) <= 0)
2146  {
2149 
2150  action = 5;
2151 
2152  steps.resize(ALL_CTF_PARAMETERS);
2153  steps.initConstant(1);
2154  steps(3) = 0; // kV
2155  steps(5) = 0; // The spherical aberration (Cs) is not optimized
2156  if (current_ctfmodel.Q0!=0)
2157  steps(12)=0;
2158 
2160 
2163  global_prm, 0.01, fitness, iter, steps,show_optimization);
2165 
2166  show_inf=0;
2167  action = 3;
2169 
2170  double error = -CTF_fitness(adjust_params->vdata-1,nullptr);
2171  if ( error <= -0.1)
2172  {
2173  *adjust_params = initialGlobalAdjust;
2175  //There is nothing to do and we have to perform an exhaustive search
2176 #ifndef RELEASE_MODE
2177  std::cout << " Entering in estimate_defoci, Performing exhaustive defocus search (SLOW)" << std::endl;
2178 #endif
2179  estimate_defoci();
2180  }
2181  }
2182 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define COPY_ctfmodel_TO_CURRENT_GUESS
void sqrt(Image< double > &op)
int sizeWindowPhase
Size of the average window used during phase direction and unwrapping estimates (used in Zernike esti...
Matrix1D< double > * adjust_params
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
double Tm
Sampling rate.
void powellOptimizer(Matrix1D< double > &p, int i0, int n, double(*f)(double *x, void *), void *prm, double ftol, double &fret, int &iter, const Matrix1D< double > &steps, bool show)
void demodulate(MultidimArray< double > &im, double lambda, int size, int x, int y, int rmin, int rmax, MultidimArray< double > &phase, MultidimArray< double > &mod, Matrix1D< double > &coeffs, int verbose)
constexpr int DEFOCUS_PARAMETERS
glob_prnt iter
doublereal * x
#define i
double max_freq
Maximum frequency to adjust.
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
void CenterFFT(MultidimArray< T > &v, bool forward)
Definition: xmipp_fft.h:291
constexpr int ALL_CTF_PARAMETERS
double * lambda
#define XSIZE(v)
constexpr int FIRST_DEFOCUS_PARAMETER
void mod(const MultidimArray< T > &x, MultidimArray< T > &m, double y)
bool show_optimization
Show convergence values.
double steps
double K
Global gain. By default, 1.
Definition: ctf.h:238
#define RAD2DEG(r)
Definition: xmipp_macros.h:320
double min_freq
Minimum frequency to adjust.
String formatString(const char *format,...)
double Q0
Factor for the importance of the Amplitude contrast.
Definition: ctf.h:261
double lambdaPhase
Regularization factor for the phase direction and unwrapping estimates (used in Zernike estimate) ...
T * vdata
The array itself.
Definition: matrix1d.h:258
#define PI
Definition: tools.h:43
CTFDescription initial_ctfmodel
CTF model.
ProgCTFEstimateFromPSD * global_prm
double fmax
double fitness(double *p)
#define DEBUG_TEXTFILE(str)
double CTF_fitness(double *, void *)

◆ estimate_defoci_Zernike() [2/2]

void ProgCTFEstimateFromPSD::estimate_defoci_Zernike ( )

Definition at line 2184 of file ctf_estimate_from_psd.cpp.

2185 {
2186  if (show_optimization)
2187  std::cout << "Looking for first defoci ...\n";
2188 
2189  DEBUG_TEXTFILE("Step 6.1");
2196  DEBUG_TEXTFILE("Step 6.2");
2198 
2202 
2203  showFirstDefoci();
2204 }
#define COPY_ctfmodel_TO_CURRENT_GUESS
int sizeWindowPhase
Size of the average window used during phase direction and unwrapping estimates (used in Zernike esti...
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
double Tm
Sampling rate.
double max_freq
Maximum frequency to adjust.
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
Image< double > enhanced_ctftomodel_fullsize
CTF amplitude to model.
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
Definition: ctf.h:832
bool show_optimization
Show convergence values.
double min_freq
Minimum frequency to adjust.
void forcePhysicalMeaning()
Definition: ctf.cpp:1781
double lambdaPhase
Regularization factor for the phase direction and unwrapping estimates (used in Zernike estimate) ...
double kV
Accelerating Voltage (in KiloVolts)
Definition: ctf.h:242
CTFDescription initial_ctfmodel
CTF model.
#define DEBUG_TEXTFILE(str)
#define DEBUG_MODEL_TEXTFILE

◆ estimate_envelope_parameters()

void ProgCTFEstimateFromPSD::estimate_envelope_parameters ( )

Definition at line 1576 of file ctf_estimate_from_psd.cpp.

1577 {
1578  if (show_optimization)
1579  std::cout << "Looking for best fitting envelope ...\n";
1580 
1581  // Set the envelope
1583  current_ctfmodel.K = 1.0;
1584  current_ctfmodel.espr = 0.0;
1585  current_ctfmodel.ispr = 0.0;
1586  current_ctfmodel.alpha = 0.0;
1587  current_ctfmodel.DeltaF = 0.0;
1588  current_ctfmodel.DeltaR = 0.0;
1590  current_ctfmodel.envR0 = 0.0;
1591  current_ctfmodel.envR1 = 0.0;
1593 
1594  // Now optimize the envelope
1595  penalize = false;
1596  int iter;
1597  double fitness;
1599  steps.resize(ENVELOPE_PARAMETERS);
1600  steps.initConstant(1);
1601  steps(1) = 0; // Do not optimize Cs
1602  steps(5) = 0; // Do not optimize for alpha, since Ealpha depends on the
1603  // defocus
1604  if (modelSimplification >= 1)
1605  steps(6) = steps(7) = 0; // Do not optimize DeltaF and DeltaR
1607  ENVELOPE_PARAMETERS, CTF_fitness, global_prm, 0.05, fitness, iter, steps,
1609 
1610  // Keep the result in global_prm->adjust
1613 
1614  if (show_optimization)
1615  {
1616  std::cout << "Best envelope Fit:\n" << current_ctfmodel << std::endl;
1617  saveIntermediateResults("step02a_best_envelope_fit");
1618  }
1619 
1620  // Optimize with penalization
1621  if (show_optimization)
1622  std::cout << "Penalizing best fitting envelope ...\n";
1623  penalize = true;
1624  current_penalty = 2;
1625  int imax = CEIL(log(penalty) / log(2.0));
1626  for (int i = 1; i <= imax; i++)
1627  {
1628  if (show_optimization)
1629  std::cout << " Iteration " << i << " penalty="
1630  << current_penalty << std::endl;
1632  ENVELOPE_PARAMETERS, CTF_fitness, global_prm, 0.05, fitness, iter,
1633  steps, show_optimization);
1634  current_penalty *= 2;
1635  current_penalty =
1637  }
1638  // Keep the result in global_prm->adjust
1641 
1642  if (show_optimization)
1643  {
1644  std::cout << "Best envelope Fit:\n" << current_ctfmodel << std::endl;
1645  saveIntermediateResults("step02b_best_penalized_envelope_fit");
1646  }
1647 }
constexpr int ENVELOPE_PARAMETERS
#define COPY_ctfmodel_TO_CURRENT_GUESS
void saveIntermediateResults(const FileName &fn_root, bool generate_profiles)
double envR1
Definition: ctf.h:300
Matrix1D< double > * adjust_params
double espr
Definition: ctf.h:251
void powellOptimizer(Matrix1D< double > &p, int i0, int n, double(*f)(double *x, void *), void *prm, double ftol, double &fret, int &iter, const Matrix1D< double > &steps, bool show)
glob_prnt iter
double envR0
Definition: ctf.h:299
double DeltaF
Longitudinal mechanical displacement (ansgtrom). Typical value 100.
Definition: ctf.h:257
#define i
static constexpr double penalty
void log(Image< double > &op)
constexpr int FIRST_ENVELOPE_PARAMETER
#define CEIL(x)
Definition: xmipp_macros.h:225
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
double ispr
Objective lens stability (deltaI/I) (ppm). Typical value 1.
Definition: ctf.h:253
double DeltaR
Transversal mechanical displacement (ansgtrom). Typical value 3.
Definition: ctf.h:259
bool show_optimization
Show convergence values.
#define XMIPP_MIN(x, y)
Definition: xmipp_macros.h:181
double steps
double K
Global gain. By default, 1.
Definition: ctf.h:238
int modelSimplification
Model simplification.
double Ca
Chromatic aberration (in milimeters). Typical value 2.
Definition: ctf.h:248
void forcePhysicalMeaning()
Definition: ctf.cpp:1781
double alpha
Convergence cone semiangle (in mrad). Typical value 0.5.
Definition: ctf.h:255
double Q0
Factor for the importance of the Amplitude contrast.
Definition: ctf.h:261
CTFDescription initial_ctfmodel
CTF model.
ProgCTFEstimateFromPSD * global_prm
void initConstant(T val)
Definition: matrix1d.cpp:83
double fitness(double *p)
double CTF_fitness(double *, void *)

◆ generate_model_halfplane()

void ProgCTFEstimateFromPSD::generate_model_halfplane ( int  Ydim,
int  Xdim,
MultidimArray< double > &  model 
)

Generate half-plane model at a given size. It is assumed that ROUT_Adjust_CTF has been already run

Definition at line 536 of file ctf_estimate_from_psd.cpp.

538 {
539  Matrix1D<int> idx(2); // Indexes for Fourier plane
540  Matrix1D<double> freq(2); // Frequencies for Fourier plane
541 
542  // Compute the scaled PSD
543  MultidimArray<double> enhancedPSD;
544  enhancedPSD = enhanced_ctftomodel_fullsize();
545  CenterFFT(enhancedPSD, false);
546  selfScaleToSize(xmipp_transformation::BSPLINE3, enhancedPSD, Ydim, Xdim);
547  CenterFFT(enhancedPSD, true);
548 
549  // The left part is the CTF model
553 
554  MultidimArray<int> mask_norm;
555  STARTINGX(enhancedPSD)=STARTINGY(enhancedPSD)=0;
556  mask_norm.initZeros(enhancedPSD);
557  model.resizeNoCopy(enhancedPSD);
559  {
560  if (j <= Xdim / 2)
561  {
562  XX(idx) = j;
563  YY(idx) = i;
564  FFT_idx2digfreq(model, idx, freq);
565  double w=freq.module();
566  if (w>max_freq_psd)
567  continue;
568  if (fabs(XX(freq))>0.03 && fabs(YY(freq))>0.03)
569  mask_norm(i,j)=(int)mask(i,j);
570  digfreq2contfreq(freq, freq, Tm);
571 
572  current_ctfmodel.precomputeValues(XX(freq), YY(freq));
573  model(i, j) = current_ctfmodel.getValuePureAt();
574  model(i, j) *= model(i, j);
575  }
576  }
577  // Normalize the left part so that it has similar values to
578  // the enhanced PSD
579  model.rangeAdjust(enhancedPSD, &mask_norm);
580  // Copy the part of the enhancedPSD
582  {
583  if (!(j <= Xdim / 2))
584  model(i, j) = enhancedPSD(i, j);
585  else
586  {
587  XX(idx) = j;
588  YY(idx) = i;
589  FFT_idx2digfreq(model, idx, freq);
590  double w=freq.module();
591  if (w>max_freq_psd)
592  model(i,j)=0;
593  }
594  }
595  CenterFFT(model, true);
596 }
void resizeNoCopy(const MultidimArray< T1 > &v)
void assignCTFfromParameters(double *p, CTFDescription &ctfmodel, int ia, int l, int modelSimplification)
Matrix1D< double > * adjust_params
double Tm
Sampling rate.
void rangeAdjust(T minF, T maxF)
doublereal * w
void FFT_idx2digfreq(T &v, const Matrix1D< int > &idx, Matrix1D< double > &freq)
Definition: xmipp_fft.h:80
#define STARTINGX(v)
#define i
void selfScaleToSize(int SplineDegree, MultidimArrayGeneric &V1, int Xdim, int Ydim, int Zdim)
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
#define STARTINGY(v)
void CenterFFT(MultidimArray< T > &v, bool forward)
Definition: xmipp_fft.h:291
Image< double > enhanced_ctftomodel_fullsize
CTF amplitude to model.
#define XX(v)
Definition: matrix1d.h:85
constexpr int CTF_PARAMETERS
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
#define j
#define YY(v)
Definition: matrix1d.h:93
#define MATRIX1D_ARRAY(v)
Definition: matrix1d.h:58
int modelSimplification
Model simplification.
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.
double getValuePureAt(bool show=false) const
Compute CTF pure at (U,V). Continuous frequencies.
Definition: ctf.h:452
void digfreq2contfreq(const Matrix1D< double > &digfreq, Matrix1D< double > &contfreq, double pixel_size)
Definition: xmipp_fft.h:125

◆ generate_model_quadrant()

void ProgCTFEstimateFromPSD::generate_model_quadrant ( int  Ydim,
int  Xdim,
MultidimArray< double > &  model 
)

Generate quadrant model at a given size. It is assumed that ROUT_Adjust_CTF has been already run

Definition at line 472 of file ctf_estimate_from_psd.cpp.

474 {
475  Matrix1D<int> idx(2); // Indexes for Fourier plane
476  Matrix1D<double> freq(2); // Frequencies for Fourier plane
477 
478  // Compute the scaled PSD
479  MultidimArray<double> enhancedPSD;
480  enhancedPSD = enhanced_ctftomodel_fullsize();
481  CenterFFT(enhancedPSD, false);
482  selfScaleToSize(xmipp_transformation::BSPLINE3, enhancedPSD, Ydim, Xdim);
483  CenterFFT(enhancedPSD, true);
484 
485  // Generate the CTF model
489 
490  // Write the two model quadrants
491  MultidimArray<int> mask_norm;
492  STARTINGX(enhancedPSD)=STARTINGY(enhancedPSD)=0;
493  mask_norm.initZeros(enhancedPSD);
494  model.resizeNoCopy(enhancedPSD);
496  {
497  if ((j >= Xdim / 2 && i >= Ydim / 2)
498  || (j < Xdim / 2 && i < Ydim / 2))
499  {
500  XX(idx) = j;
501  YY(idx) = i;
502  FFT_idx2digfreq(model, idx, freq);
503  if (fabs(XX(freq))>0.03 && fabs(YY(freq))>0.03)
504  mask_norm(i,j)=(int)mask(i,j);
505  digfreq2contfreq(freq, freq, Tm);
506 
507  current_ctfmodel.precomputeValues(XX(freq), YY(freq));
508  model(i, j) = current_ctfmodel.getValuePureAt();
509  model(i, j) *= model(i, j);
510  }
511  }
512  // Normalize the left part so that it has similar values to
513  // the enhanced PSD
514  model.rangeAdjust(enhancedPSD, &mask_norm);
515  // Copy the part of the enhancedPSD
517  {
518  if (!((j >= Xdim / 2 && i >= Ydim / 2) || (j < Xdim / 2 && i < Ydim / 2)))
519  model(i, j) = enhancedPSD(i, j);
520  else
521  {
522  XX(idx) = j;
523  YY(idx) = i;
524  FFT_idx2digfreq(model, idx, freq);
525  double w=freq.module();
526  if (w>max_freq_psd)
527  model(i,j)=0;
528 
529  }
530  }
531 
532  // Produce a centered image
533  CenterFFT(model, true);
534 }
void resizeNoCopy(const MultidimArray< T1 > &v)
void assignCTFfromParameters(double *p, CTFDescription &ctfmodel, int ia, int l, int modelSimplification)
Matrix1D< double > * adjust_params
double Tm
Sampling rate.
void rangeAdjust(T minF, T maxF)
doublereal * w
void FFT_idx2digfreq(T &v, const Matrix1D< int > &idx, Matrix1D< double > &freq)
Definition: xmipp_fft.h:80
#define STARTINGX(v)
#define i
void selfScaleToSize(int SplineDegree, MultidimArrayGeneric &V1, int Xdim, int Ydim, int Zdim)
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
#define STARTINGY(v)
void CenterFFT(MultidimArray< T > &v, bool forward)
Definition: xmipp_fft.h:291
Image< double > enhanced_ctftomodel_fullsize
CTF amplitude to model.
constexpr int ALL_CTF_PARAMETERS
#define XX(v)
Definition: matrix1d.h:85
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
#define j
#define YY(v)
Definition: matrix1d.h:93
#define MATRIX1D_ARRAY(v)
Definition: matrix1d.h:58
int modelSimplification
Model simplification.
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.
double getValuePureAt(bool show=false) const
Compute CTF pure at (U,V). Continuous frequencies.
Definition: ctf.h:452
void digfreq2contfreq(const Matrix1D< double > &digfreq, Matrix1D< double > &contfreq, double pixel_size)
Definition: xmipp_fft.h:125

◆ generateModelSoFar()

void ProgCTFEstimateFromPSD::generateModelSoFar ( Image< double > &  I,
bool  apply_log = false 
)

Definition at line 322 of file ctf_estimate_from_psd.cpp.

323 {
324  Matrix1D<int> idx(2); // Indexes for Fourier plane
325  Matrix1D<double> freq(2); // Frequencies for Fourier plane
329  I().initZeros(*f);
331  {
332  XX(idx) = j;
333  YY(idx) = i;
334  FFT_idx2digfreq(*f, idx, freq);
335  double w=freq.module();
336  if (w>max_freq_psd)
337  continue;
338  digfreq2contfreq(freq, freq, Tm);
339 
340  // Decide what to save
341  current_ctfmodel.precomputeValues(XX(freq), YY(freq));
342  if (action <= 1)
344  else if (action == 2)
345  {
346  double E = current_ctfmodel.getValueDampingAt();
347  I()(i, j) = current_ctfmodel.getValueNoiseAt() + E * E;
348  }
349  else if (action >= 3 && action <= 6)
350  {
351  double ctf = current_ctfmodel.getValuePureAt();
352  I()(i, j) = current_ctfmodel.getValueNoiseAt() + ctf * ctf;
353  }
354  else
355  {
356  double ctf = current_ctfmodel.getValuePureAt();
357  I()(i, j) = ctf;
358  }
359  if (apply_log)
360  I()(i, j) = 10 * log10(I()(i, j));
361  }
362 }
MultidimArray< double > * f
void assignCTFfromParameters(double *p, CTFDescription &ctfmodel, int ia, int l, int modelSimplification)
Matrix1D< double > * adjust_params
double Tm
Sampling rate.
doublereal * w
void FFT_idx2digfreq(T &v, const Matrix1D< int > &idx, Matrix1D< double > &freq)
Definition: xmipp_fft.h:80
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
constexpr int ALL_CTF_PARAMETERS
#define XX(v)
Definition: matrix1d.h:85
double getValueNoiseAt(bool show=false) const
Compute noise at (X,Y). Continuous frequencies, notice it is squared.
Definition: ctf.h:1121
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
void log10(Image< double > &op)
double getValueDampingAt(bool show=false) const
Compute CTF damping at (U,V). Continuous frequencies.
Definition: ctf.h:424
#define j
#define YY(v)
Definition: matrix1d.h:93
#define MATRIX1D_ARRAY(v)
Definition: matrix1d.h:58
int modelSimplification
Model simplification.
void produceSideInfo()
Produce Side information.
Definition: ctf.cpp:1392
double getValuePureAt(bool show=false) const
Compute CTF pure at (U,V). Continuous frequencies.
Definition: ctf.h:452
void digfreq2contfreq(const Matrix1D< double > &digfreq, Matrix1D< double > &contfreq, double pixel_size)
Definition: xmipp_fft.h:125

◆ produceSideInfo()

void ProgCTFEstimateFromPSD::produceSideInfo ( )

Produce side information.

Definition at line 303 of file ctf_estimate_from_psd.cpp.

304 {
306  adjust.initZeros();
310 
311  // Read the CTF file, supposed to be the uncentered squared amplitudes
312  if (fn_psd != "")
314  f = &(ctftomodel());
315 
318 }
void assignParametersFromCTF(const CTFDescription &ctfmodel, double *p, int ia, int l, int modelSimplification)
MultidimArray< double > * f
Image< double > ctftomodel
CTF amplitude to model.
FileName fn_psd
CTF filename.
constexpr int ALL_CTF_PARAMETERS
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
void initZeros()
Definition: matrix1d.h:592
Matrix1D< double > adjust
Set of parameters for the complete adjustment of the CTF.
void produceSideInfo()
Produce side information.
#define MATRIX1D_ARRAY(v)
Definition: matrix1d.h:58
MultidimArray< double > x_contfreq
Frequencies in axes.
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
void clear()
Clear.
Definition: ctf.cpp:1366
CTFDescription initial_ctfmodel
CTF model.
MultidimArray< double > y_contfreq

◆ readBasicParams()

void ProgCTFEstimateFromPSD::readBasicParams ( XmippProgram program)

Read parameters.

Definition at line 270 of file ctf_estimate_from_psd.cpp.

271 {
273 
275  initial_ctfmodel.readParams(program);
277  REPORT_ERROR(ERR_ARG_INCORRECT,"Defocus cannot be larger than 10 microns (100,000 Angstroms)");
279 
280 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void readBasicParams(XmippProgram *program)
Read parameters.
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
double Tm
Sampling rate.
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
bool enable_CTF
Enable CTF part.
Definition: ctf.h:275
Incorrect argument received.
Definition: xmipp_error.h:113
double Tm
Sampling rate (A/pixel)
Definition: ctf.h:240
bool enable_CTFnoise
Enable CTFnoise part.
Definition: ctf.h:273
CTFDescription initial_ctfmodel
CTF model.
void readParams(XmippProgram *program)
Read parameters from the command line.
Definition: ctf.cpp:1297

◆ readParams()

void ProgCTFEstimateFromPSD::readParams ( )
virtual

Function in which each program will read parameters that it need. If some error occurs the usage will be printed out.

Reimplemented from XmippProgram.

Definition at line 282 of file ctf_estimate_from_psd.cpp.

283 {
284  fn_psd = getParam("--psd");
285  readBasicParams(this);
286 }
void readBasicParams(XmippProgram *program)
Read parameters.
FileName fn_psd
CTF filename.
const char * getParam(const char *param, int arg=0)

◆ run()

void ProgCTFEstimateFromPSD::run ( )
virtual

Run

Reimplemented from XmippProgram.

Definition at line 2478 of file ctf_estimate_from_psd.cpp.

2479 {
2480  CTFDescription ctfmodel;
2481  ROUT_Adjust_CTF(*this, ctfmodel);
2482 }
double ROUT_Adjust_CTF(ProgCTFEstimateFromPSD &prm, CTFDescription &output_ctfmodel, bool standalone)

◆ saveIntermediateResults()

void ProgCTFEstimateFromPSD::saveIntermediateResults ( const FileName fn_root,
bool  generate_profiles = true 
)

Definition at line 369 of file ctf_estimate_from_psd.cpp.

370 {
371  std::ofstream plotX, plotY, plot_radial;
372  Image<double> save;
373  generateModelSoFar(save, false);
374 
375  Image<double> save_ctf;
377  ctfmodelSize, save_ctf());
378  if (fn_root.find("@")==std::string::npos)
379  save_ctf.write(fn_root + "_ctfmodel_halfplane.xmp");
380  else
381  save_ctf.write(fn_root + "_ctfmodel_halfplane.stk");
383  ctfmodelSize, save_ctf());
384  if (fn_root.find("@")==std::string::npos)
385  save_ctf.write(fn_root + "_ctfmodel_quadrant.xmp");
386  else
387  save_ctf.write(fn_root + "_ctfmodel_quadrant.stk");
388 
389  if (!generate_profiles)
390  return;
391  plotX.open((fn_root + "X.txt").c_str());
392  plotY.open((fn_root + "Y.txt").c_str());
393  plot_radial.open((fn_root + "_radial.txt").c_str());
394  if (!plotX)
395  REPORT_ERROR(
397  "save_intermediate_results::Cannot open plot file for writing\n");
398  if (!plotY)
399  REPORT_ERROR(
401  "save_intermediate_results::Cannot open plot file for writing\n");
402  if (!plot_radial)
403  REPORT_ERROR(
405  "save_intermediate_results::Cannot open plot file for writing\n");
406  plotX << "# freq_dig freq_angstrom model psd enhanced logModel logPsd\n";
407  plotY << "# freq_dig freq_angstrom model psd enhanced logModel logPsd\n";
408  //plot_radial << "# freq_dig freq_angstrom model psd enhanced logModel logPsd\n";
409 
410  // Generate cut along X
411  for (int i = STARTINGY(save()); i <= FINISHINGY(save()) / 2; i++)
412  {
413  if (mask(i, 0) <= 0)
414  continue;
415  plotY << w_digfreq(i, 0) << " " << w_contfreq(i, 0) << " "
416  << save()(i, 0) << " " << (*f)(i, 0) << " "
417  << enhanced_ctftomodel()(i, 0) << " "
418  << log10(save()(i, 0)) << " " << log10((*f)(i, 0))
419  << std::endl;
420  }
421 
422  // Generate cut along Y
423  for (int j = STARTINGX(save()); j <= FINISHINGX(save()) / 2; j++)
424  {
425  if (mask(0, j) <= 0)
426  continue;
427  plotX << w_digfreq(0, j) << " " << w_contfreq(0, j) << " "
428  << save()(0, j) << " " << (*f)(0, j) << " "
429  << enhanced_ctftomodel()(0, j)
430  << log10(save()(0, j)) << " " << log10((*f)(0, j))
431  << std::endl;
432  }
433 
434  // Generate radial average
435  MultidimArray<double> radial_CTFmodel_avg, radial_CTFampl_avg,
436  radial_enhanced_avg;
437  MultidimArray<int> radial_N;
438  radial_CTFmodel_avg.initZeros(YSIZE(save()) / 2);
439  radial_CTFampl_avg.initZeros(YSIZE(save()) / 2);
440  radial_enhanced_avg.initZeros(YSIZE(save()) / 2);
441  radial_N.initZeros(YSIZE(save()) / 2);
443  {
444  if (mask(i, j) <= 0)
445  continue;
446  double model2 = save()(i, j);
447 
448  int r = w_digfreq_r(i, j);
449  radial_CTFmodel_avg(r) += model2;
450  radial_CTFampl_avg(r) += (*f)(i, j);
451  radial_enhanced_avg(r) += enhanced_ctftomodel()(i, j);
452  radial_N(r)++;
453  }
454 
455  FOR_ALL_ELEMENTS_IN_ARRAY1D(radial_CTFmodel_avg)
456  {
457  if (radial_N(i) == 0)
458  continue;
459  plot_radial << w_digfreq(i, 0) << " " << w_contfreq(i, 0)
460  << " " << radial_CTFmodel_avg(i) / radial_N(i) << " " << radial_CTFampl_avg(i) / radial_N(i) << " "
461  << radial_enhanced_avg(i) / radial_N(i)
462  << " " << log10(radial_CTFmodel_avg(i) / radial_N(i)) << " " << log10(radial_CTFampl_avg(i) / radial_N(i)) << " "
463  << std::endl;
464  }
465 
466  plotX.close();
467  plotY.close();
468  plot_radial.close();
469 }
void generate_model_quadrant(int Ydim, int Xdim, MultidimArray< double > &model)
#define YSIZE(v)
MultidimArray< double > * f
#define FINISHINGX(v)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
Couldn&#39;t write to file.
Definition: xmipp_error.h:140
MultidimArray< double > w_digfreq
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 STARTINGX(v)
MultidimArray< double > w_digfreq_r
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
#define STARTINGY(v)
int ctfmodelSize
X dimension of particle projections (-1=the same as the psd)
MultidimArray< double > w_contfreq
void generateModelSoFar(Image< double > &I, bool apply_log)
void log10(Image< double > &op)
void generate_model_halfplane(int Ydim, int Xdim, MultidimArray< double > &model)
#define j
#define FINISHINGY(v)
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
Image< double > enhanced_ctftomodel
CTF amplitude to model.
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.

◆ showFirstDefoci()

void ProgCTFEstimateFromPSD::showFirstDefoci ( )

Definition at line 1651 of file ctf_estimate_from_psd.cpp.

1652 {
1653  if (show_optimization)
1654  {
1655  std::cout << "First defocus Fit:\n" << current_ctfmodel << std::endl;
1656  saveIntermediateResults("step03a_first_defocus_fit");
1657  enhanced_ctftomodel.write("step03a_enhanced_PSD.xmp");
1658  Image<double> save, save2, save3;
1659  save().resize(YSIZE(w_digfreq), XSIZE(w_digfreq));
1660  save2().resize(save());
1661  save3().resize(save());
1663  {
1664  save()(i, j) = enhanced_ctftomodel()(i, j);
1665  double f_x = DIRECT_A2D_ELEM(x_contfreq, i, j);
1666  double f_y = DIRECT_A2D_ELEM(y_contfreq, i, j);
1668  double ctf_without_damping =
1670  save2()(i, j) = ctf_without_damping * ctf_without_damping;
1671  save3()(i, j) = -enhanced_ctftomodel()(i, j)
1672  * ctf_without_damping * ctf_without_damping;
1673  }
1674  save.write("step03a_enhanced_PSD.xmp");
1675  save2.write("step03a_fitted_CTF.xmp");
1676  save3.write("step03a_superposition.xmp");
1677  }
1678 }
#define YSIZE(v)
void saveIntermediateResults(const FileName &fn_root, bool generate_profiles)
MultidimArray< double > w_digfreq
#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 i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
#define XSIZE(v)
void precomputeValues(double X, double Y)
Precompute values for a given frequency.
Definition: ctf.h:1002
double getValuePureWithoutDampingAt(bool show=false) const
Compute pure CTF without damping at (U,V). Continuous frequencies.
Definition: ctf.h:542
bool show_optimization
Show convergence values.
#define j
Image< double > enhanced_ctftomodel
CTF amplitude to model.
MultidimArray< double > x_contfreq
Frequencies in axes.
MultidimArray< double > y_contfreq

Member Data Documentation

◆ ctfmodel_defoci

CTFDescription ProgCTFEstimateFromPSD::ctfmodel_defoci

Definition at line 44 of file ctf_estimate_from_psd.h.

◆ current_ctfmodel

CTFDescription ProgCTFEstimateFromPSD::current_ctfmodel

Definition at line 44 of file ctf_estimate_from_psd.h.

◆ initial_ctfmodel

CTFDescription ProgCTFEstimateFromPSD::initial_ctfmodel

CTF model.

Definition at line 44 of file ctf_estimate_from_psd.h.


The documentation for this class was generated from the following files: