Xmipp  v3.23.11-Nereus
Classes | Functions
adjust_ctf (Adjust CTF parameters to PSD)
Collaboration diagram for adjust_ctf (Adjust CTF parameters to PSD):

Classes

class  ProgCTFEstimateFromPSD
 

Functions

double evaluateIceness (const MultidimArray< double > &enhanced_ctftomodel, double Tm)
 
double ROUT_Adjust_CTF (ProgCTFEstimateFromPSD &prm, CTFDescription &output_ctfmodel, bool standalone=true)
 

Detailed Description

Function Documentation

◆ evaluateIceness()

double evaluateIceness ( const MultidimArray< double > &  enhanced_ctftomodel,
double  Tm 
)

◆ ROUT_Adjust_CTF()

double ROUT_Adjust_CTF ( ProgCTFEstimateFromPSD prm,
CTFDescription output_ctfmodel,
bool  standalone = true 
)

Core of the Adjust CTF routine. This is the routine which does everything. It returns the fitting error committed in the best fit.

Definition at line 2208 of file ctf_estimate_from_psd.cpp.

2209 {
2211  global_prm = &prm;
2212  if (standalone || prm.show_optimization)
2213  prm.show();
2214  prm.produceSideInfo();
2215  DEBUG_TEXTFILE(formatString("After producing side info: Avg=%f",prm.ctftomodel().computeAvg()));
2217 
2218  // Build initial frequency mask
2219  prm.value_th = -1;
2220  prm.min_freq_psd = prm.min_freq;
2221  prm.max_freq_psd = prm.max_freq;
2222 
2223  // Set some global variables
2224  prm.adjust_params = &prm.adjust;
2225  prm.penalize = false;
2226  prm.max_gauss_freq = 0;
2227  prm.heavy_penalization = prm.f->computeMax() * XSIZE(*prm.f) * YSIZE(*prm.f);
2228  prm.show_inf = 0;
2229 
2230  // Some variables needed by all steps
2231  int iter;
2232  double fitness;
2234 
2235  /************************************************************************/
2236  /* STEPs 1, 2, 3 and 4: Find background which best fits the CTF */
2237  /************************************************************************/
2238 
2239  prm.current_ctfmodel.enable_CTFnoise = true;
2240  prm.current_ctfmodel.enable_CTF = false;
2241  prm.evaluation_reduction = 4;
2242 
2243  // If initial parameters were not supplied for the gaussian curve,
2244  // estimate them from the CTF file
2245  prm.action = 0;
2246  if (prm.adjust(FIRST_SQRT_PARAMETER) == 0)
2247  {
2250  }
2251 
2252  // Optimize the current background
2253  prm.action = 1;
2254  prm.penalize = true;
2255  prm.current_penalty = prm.penalty;
2257  steps.initConstant(1);
2258  if (!prm.modelSimplification >= 3)
2259  steps(7) = steps(8) = steps(10) = 0;
2261  BACKGROUND_CTF_PARAMETERS, CTF_fitness, global_prm, 0.01, fitness, iter,
2262  steps, prm.show_optimization);
2263 
2264  // Make sure that the model has physical meaning
2265  // (In some machines due to numerical imprecission this check is necessary
2266  // at the end)
2269 
2270  if (prm.show_optimization)
2271  {
2272  std::cout << "Best background Fit:\n" << prm.current_ctfmodel << std::endl;
2273  prm.saveIntermediateResults("step01d_best_background_fit");
2274  }
2275  DEBUG_TEXTFILE(formatString("Step 4: CTF_fitness=%f",CTF_fitness));
2277 
2278  /************************************************************************/
2279  /* STEPs 5 and 6: Find envelope which best fits the CTF */
2280  /************************************************************************/
2281  prm.action = 2;
2282  prm.current_ctfmodel.enable_CTF = true;
2285  if (prm.initial_ctfmodel.K == 0)
2286  {
2289  if (prm.initial_ctfmodel.Q0 != 0)
2292  }
2293  else
2294  {
2309  }
2310  DEBUG_TEXTFILE(formatString("Step 6: espr=%f",prm.current_ctfmodel.espr));
2312  /************************************************************************/
2313  /* STEP 7: the defocus and angular parameters */
2314  /************************************************************************/
2315 
2316  prm.action = 3;
2317  prm.evaluation_reduction = 1;
2318  if (prm.fastDefocusEstimate)
2320  else if (!prm.noDefocusEstimate)
2321  prm.estimate_defoci();
2322  else
2323  {
2326  if (prm.current_ctfmodel.DeltafV==0.0)
2328  }
2329 
2330  DEBUG_TEXTFILE(formatString("Step 7: DeltafU=%f",prm.current_ctfmodel.DeltafU));
2331  DEBUG_TEXTFILE(formatString("Step 7: DeltafV=%f",prm.current_ctfmodel.DeltafV));
2332  DEBUG_TEXTFILE(formatString("Step 7: azimutalAngle=%f",prm.current_ctfmodel.azimuthal_angle));
2334 
2335  //This line is to test the results obtained
2336  //exit(1);
2337 
2338  /************************************************************************/
2339  /* STEPs 9, 10 and 11: all parameters included second Gaussian */
2340  /************************************************************************/
2341  prm.action = 5;
2342  if (prm.modelSimplification < 2)
2344 
2345  steps.resize(ALL_CTF_PARAMETERS);
2346  steps.initConstant(1);
2347  steps(3) = 0; // kV
2348  steps(5) = 0; // The spherical aberration (Cs) is not optimized
2349  steps(37) = 0; //VPP radius not optimized
2350  if (prm.initial_ctfmodel.Q0 != 0)
2351  steps(15) = 0; // Q0
2352  if (prm.modelSimplification >= 3)
2353  steps(20) = steps(21) = steps(23) = 0;
2354  if (prm.modelSimplification >= 2)
2355  steps(24) = steps(25) = steps(26) = steps(27) = steps(28) = steps(29) = 0;
2356  if (prm.modelSimplification >= 1)
2357  steps(10) = steps(11) = 0;
2358  if (std::floor(prm.initial_ctfmodel.VPP_radius) == 0)
2359  steps(36) = 0; //VPP phase shift
2360 
2362  global_prm, 0.01, fitness, iter, steps, prm.show_optimization);
2363 
2366 
2367  if (prm.show_optimization)
2368  {
2369  std::cout << "Best fit with Gaussian2:\n" << prm.current_ctfmodel << std::endl;
2370  prm.saveIntermediateResults("step04b_best_fit_with_gaussian2");
2371  }
2372 
2373  prm.evaluation_reduction = 2;
2375  global_prm, 0.01, fitness, iter, steps, prm.show_optimization);
2378 
2379  prm.evaluation_reduction = 1;
2381  global_prm, 0.005, fitness, iter, steps, prm.show_optimization);
2384 
2385  if (prm.refineAmplitudeContrast)
2386  {
2387  std::cout << "Refining amplitude contrast ..." << std::endl;
2388  steps(15) = 1; // Q0
2390  global_prm, 0.005, fitness, iter, steps, prm.show_optimization);
2393  }
2394 
2395  if (prm.show_optimization)
2396  {
2397  std::cout << "Best fit:\n" << prm.current_ctfmodel << std::endl;
2398  prm.saveIntermediateResults("step04c_best_fit");
2399  }
2400  DEBUG_TEXTFILE(formatString("Step 11: DeltafU=%f fitness=%f",prm.current_ctfmodel.DeltafU,fitness));
2402 
2403  //We adopt that always DeltafU > DeltafV so if this is not the case we change the values and the angle
2405  {
2406  double temp;
2407  temp = prm.current_ctfmodel.DeltafU;
2409  prm.current_ctfmodel.DeltafV = temp;
2412  }
2413 
2414  /************************************************************************/
2415  /* STEP 12: Produce output */
2416  /************************************************************************/
2417 
2418  prm.action = 7;
2419 
2420  if (prm.fn_psd != "")
2421  {
2422  // Define mask between first and third zero
2424  Matrix1D<double> u(2), z1(2), z3(2);
2426  {
2427  VECTOR_R2(u, prm.x_digfreq(i, j), prm.y_digfreq(i, j));
2428  u /= u.module();
2429  prm.current_ctfmodel.lookFor(1, u, z1, 0);
2430  prm.current_ctfmodel.lookFor(3, u, z3, 0);
2431  if (z1.module() < prm.w_contfreq(i, j)
2432  && prm.w_contfreq(i, j) < z3.module())
2433  prm.mask_between_zeroes(i, j) = 1;
2434  }
2435  // Evaluate the correlation in this region
2437  // Save results
2438  FileName fn_rootCTFPARAM = prm.fn_psd.withoutExtension();
2439 
2440  FileName fn_rootMODEL = fn_rootCTFPARAM;
2441  size_t atPosition=fn_rootCTFPARAM.find('@');
2442 
2443  if (atPosition!=std::string::npos)
2444  {
2445  fn_rootMODEL=formatString("%03d@%s",textToInteger(fn_rootCTFPARAM.substr(0, atPosition)),
2446  fn_rootCTFPARAM.substr(atPosition+1).c_str());
2447  fn_rootCTFPARAM=formatString("region%03d@%s",textToInteger(fn_rootCTFPARAM.substr(0, atPosition)),
2448  fn_rootCTFPARAM.substr(atPosition+1).c_str());
2449  }
2450  else
2451  fn_rootCTFPARAM=(String)"fullMicrograph@"+fn_rootCTFPARAM;
2452 
2453  prm.saveIntermediateResults(fn_rootMODEL, false);
2456  prm.current_ctfmodel.write(fn_rootCTFPARAM + ".ctfparam_tmp");
2457  MetaDataVec MD;
2458  MD.read(fn_rootCTFPARAM + ".ctfparam_tmp");
2459  size_t id = MD.firstRowId();
2460  MD.setValue(MDL_CTF_X0, (double)output_ctfmodel.x0*prm.Tm, id);
2461  MD.setValue(MDL_CTF_XF, (double)output_ctfmodel.xF*prm.Tm, id);
2462  MD.setValue(MDL_CTF_Y0, (double)output_ctfmodel.y0*prm.Tm, id);
2463  MD.setValue(MDL_CTF_YF, (double)output_ctfmodel.yF*prm.Tm, id);
2464  MD.setValue(MDL_CTF_CRIT_FITTINGSCORE, fitness, id);
2468  MD.write(fn_rootCTFPARAM + ".ctfparam",MD_APPEND);
2469  fn_rootCTFPARAM = fn_rootCTFPARAM + ".ctfparam_tmp";
2470  fn_rootCTFPARAM.deleteFile();
2471  }
2472  output_ctfmodel = prm.current_ctfmodel;
2473 
2475  return fitness;
2476 }
#define VECTOR_R2(v, x, y)
Definition: matrix1d.h:112
The CTF is valid within (x0,y0) to (xF,yF) in the micrograph coordinates.
#define YSIZE(v)
#define COPY_ctfmodel_TO_CURRENT_GUESS
MultidimArray< double > * f
Image< double > ctftomodel
CTF amplitude to model.
__host__ __device__ float2 floor(const float2 v)
bool fastDefocusEstimate
Fast defocus estimate.
FileName removeLastExtension() const
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void saveIntermediateResults(const FileName &fn_root, bool generate_profiles)
constexpr int FIRST_SQRT_PARAMETER
Matrix1D< double > * adjust_params
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
Definition: ctf.h:830
void write(const FileName &fn)
Definition: ctf.cpp:1275
MultidimArray< double > y_digfreq
double Tm
Sampling rate.
double espr
Definition: ctf.h:251
double downsampleFactor
Downsample performed.
void show()
Show parameters.
double evaluateIceness(const MultidimArray< double > &enhanced_ctftomodel, double Tm)
void produceSideInfo()
Produce side information.
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
double phase_shift
Definition: ctf.h:305
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
#define DEBUG_CLOSE_TEXTFILE
The CTF is valid within (x0,y0) to (xF,yF) in the micrograph coordinates.
FileName fn_psd
CTF filename.
double DeltaF
Longitudinal mechanical displacement (ansgtrom). Typical value 100.
Definition: ctf.h:257
#define i
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
Correlation between the 1st and 3rd ring of the CTF.
double max_freq
Maximum frequency to adjust.
static constexpr double penalty
double DeltafU
Global gain. By default, 1.
Definition: ctf.h:828
bool enable_CTF
Enable CTF part.
Definition: ctf.h:275
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
Definition: ctf.h:832
MultidimArray< double > w_contfreq
constexpr int ALL_CTF_PARAMETERS
Score of the fitting.
MultidimArray< double > psd_exp_radial
PSD data.
bool setValue(const MDObject &mdValueIn, size_t id)
size_t firstRowId() const override
bool noDefocusEstimate
No defocus estimate.
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
#define XSIZE(v)
double ispr
Objective lens stability (deltaI/I) (ppm). Typical value 1.
Definition: ctf.h:253
double Tm
Sampling rate (A/pixel)
Definition: ctf.h:240
bool refineAmplitudeContrast
Refine amplitude contrast.
double Cs
Spherical aberration (in milimeters). Typical value 5.6.
Definition: ctf.h:246
double DeltaR
Transversal mechanical displacement (ansgtrom). Typical value 3.
Definition: ctf.h:259
bool show_optimization
Show convergence values.
#define j
double steps
Downsampling performed to estimate the CTF.
MultidimArray< double > x_digfreq
double K
Global gain. By default, 1.
Definition: ctf.h:238
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)
Matrix1D< double > adjust
Set of parameters for the complete adjustment of the CTF.
int modelSimplification
Model simplification.
double VPP_radius
Definition: ctf.h:306
FileName withoutExtension() const
constexpr int BACKGROUND_CTF_PARAMETERS
MultidimArray< double > mask_between_zeroes
double Ca
Chromatic aberration (in milimeters). Typical value 2.
Definition: ctf.h:248
Iceness of the micrograph.
void lookFor(int n, const Matrix1D< double > &u, Matrix1D< double > &freq, int iwhat=0)
Definition: ctf.cpp:1428
double min_freq
Minimum frequency to adjust.
std::string String
Definition: xmipp_strings.h:34
void forcePhysicalMeaning()
Definition: ctf.cpp:1781
double alpha
Convergence cone semiangle (in mrad). Typical value 0.5.
Definition: ctf.h:255
String formatString(const char *format,...)
int textToInteger(const char *str)
double y0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
Definition: ctf.h:267
doublereal * u
The CTF is valid within (x0,y0) to (xF,yF) in the micrograph coordinates.
double Q0
Factor for the importance of the Amplitude contrast.
Definition: ctf.h:261
double x0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
Definition: ctf.h:263
ProgClassifyCL2D * prm
double kV
Accelerating Voltage (in KiloVolts)
Definition: ctf.h:242
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.
#define DEBUG_OPEN_TEXTFILE(fnRoot)
bool enable_CTFnoise
Enable CTFnoise part.
Definition: ctf.h:273
double xF
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
Definition: ctf.h:265
T * adaptForNumericalRecipes() const
Definition: matrix1d.h:844
CTFDescription initial_ctfmodel
CTF model.
ProgCTFEstimateFromPSD * global_prm
void initConstant(T val)
Definition: matrix1d.cpp:83
double yF
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
Definition: ctf.h:269
double fitness(double *p)
#define DEBUG_TEXTFILE(str)
#define DEBUG_MODEL_TEXTFILE
double CTF_fitness(double *, void *)
T computeMax() const
The CTF is valid within (x0,y0) to (xF,yF) in the micrograph coordinates.