55 if (aux ==
"periodogram")
65 if (mode ==
"micrograph")
67 else if (mode ==
"regions")
72 else if (mode ==
"particles")
94 addUsageLine(
"The PSD of the micrograph is first estimated using periodogram averaging or ");
95 addUsageLine(
"ARMA models ([[http://www.ncbi.nlm.nih.gov/pubmed/12623169][See article]]). ");
96 addUsageLine(
"Then, the PSD is enhanced ([[http://www.ncbi.nlm.nih.gov/pubmed/16987671][See article]]). ");
97 addUsageLine(
"And finally, the CTF is fitted to the PSD, being guided by the enhanced PSD ");
98 addUsageLine(
"([[http://www.ncbi.nlm.nih.gov/pubmed/17911028][See article]]).");
99 addParamsLine(
" --micrograph <file> : File with the micrograph");
100 addParamsLine(
" [--oroot <rootname=\"\">] : Rootname for output");
101 addParamsLine(
" : If not given, the micrograph without extensions is taken");
102 addParamsLine(
" :++ rootname.psd or .psdstk contains the PSD or PSDs");
104 addParamsLine(
" [--psd_estimator <method=periodogram>] : Method for estimating the PSD");
109 addParamsLine(
" [--overlap <o=0.5>] : Overlap (0=no overlap, 1=full overlap)");
110 addParamsLine(
" [--skipBorders <s=2>] : Number of pieces around the border to skip");
111 addParamsLine(
" [--Nsubpiece <N=1>] : Each piece is further subdivided into NxN subpieces.");
112 addParamsLine(
" : This option is useful for small micrographs in which ");
113 addParamsLine(
" : not many pieces of size pieceDim x pieceDim can be defined. ");
114 addParamsLine(
" :++ Note that this is not the same as defining a smaller pieceDim. ");
115 addParamsLine(
" :++ Defining a smaller pieceDim, would result in a small PSD, while ");
116 addParamsLine(
" :++ subdividing the piece results in a large PSD, although smoother.");
117 addParamsLine(
" [--mode <mode=micrograph>] : How many PSDs are to be estimated");
119 addParamsLine(
" micrograph : Single PSD for the whole micrograph");
120 addParamsLine(
" regions <file=\"\"> : The micrograph is divided into a region grid ");
122 addParamsLine(
" : The file is metadata with the position of each particle within the micrograph");
124 addParamsLine(
" : The file is metadata with the position of each particle within the micrograph");
126 addParamsLine(
" [--dont_estimate_ctf] : Do not fit a CTF to PSDs");
127 addParamsLine(
" [--acceleration1D] : Accelerate PSD estimation");
132 addExampleLine(
"xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --dont_estimate_ctf");
133 addExampleLine(
"Estimate a single CTF for the whole micrograph",
false);
134 addExampleLine(
"xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --sampling_rate 1.4 --voltage 200 --spherical_aberration 2.5");
135 addExampleLine(
"Estimate a single CTF for the whole micrograph providing a starting point for the defocus",
false);
136 addExampleLine(
"xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --sampling_rate 1.4 --voltage 200 --spherical_aberration 2.5 --defocusU -15000");
138 addExampleLine(
"xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --mode regions micrograph.pos --sampling_rate 1.4 --voltage 200 --spherical_aberration 2.5 --defocusU -15000");
140 addExampleLine(
"xmipp_ctf_estimate_from_micrograph --micrograph micrograph.mrc --mode particles micrograph.pos --sampling_rate 1.4 --voltage 200 --spherical_aberration 2.5 --defocusU -15000");
153 T iHalfsize = (T)2 /
YSIZE(pieceSmoother);
154 const T alpha = 0.025;
155 const T alpha1 = 1 - alpha;
156 const T ialpha = 1.0 / alpha;
158 auto computeMaskValue = [&] (T fraction) {
159 return 0.5 * (1 + cos(
PI * ((fraction - 1) * ialpha + 1)));
165 T iFraction = fabs(
i * iHalfsize);
166 if (iFraction > alpha1)
168 T maskValue = computeMaskValue(iFraction);
179 T jFraction = fabs(
j * iHalfsize);
180 if (jFraction > alpha1)
182 T maskValue = computeMaskValue(jFraction);
207 save.
write(
"PPPpiece.xmp");
225 for (i = 0, ib = i0; i < small_Ydim; i++, ib++)
226 for (j = 0, jb = j0; j < small_Xdim; j++, jb++)
230 piece *= pieceSmoother;
235 save.
write(
"PPPsmall_piece.xmp");
249 small_psd *= small_psd;
250 small_psd *= small_Ydim * small_Xdim;
255 save.
write(
"PPPsmall_psd.xmp");
268 save.
write(
"PPPpsd1.xmp");
279 save.
write(
"PPPpsd2.xmp");
280 std::cout <<
"Press any key\n";
296 auto iterPosFile = posFile.
ids().begin();
300 size_t Ndim, Zdim, Ydim , Xdim;
311 int div_NumberX=1, div_NumberY=1;
313 div_Number = posFile.
size();
318 div_Number = div_NumberX * div_NumberY;
326 div_Number = div_NumberX * div_NumberY;
333 psd().resizeNoCopy(piece);
351 std::cerr <<
"Computing models of each piece ...\n";
367 size_t piecei = 0, piecej = 0;
369 int actualDiv_Number = 0;
371 for (
size_t nIm = 1; nIm <= Ndim; nIm++)
374 std::cout <<
"Micrograph number: " << nIm << std::endl;
376 while (N <= div_Number)
379 int blocki=0, blockj=0;
388 piecej -= (int) (pieceDim / 2);
389 piecei -= (int) (pieceDim / 2);
399 step = (int) ((1 -
overlap) * step);
400 blocki = (N - 1) / div_NumberX;
401 blockj = (N - 1) % div_NumberX;
406 piecei = blocki * step;
407 piecej = blockj * step;
411 if (piecei + pieceDim > Ydim)
413 if (piecej + pieceDim > Xdim)
421 window2D( M_in(), piece, piecei, piecej, piecei +
YSIZE(piece) - 1, piecej +
XSIZE(piece) - 1);
424 piece *= pieceSmoother;
452 actualDiv_Number += 1;
468 if (
XSIZE(PCAmask) == 0)
479 if (w > 0.05 && w < 0.4)
502 "Bootstrapping is only available for micrograph averages");
505 fn_psd_piece.
compose(N, fn_psd);
506 psd.
write(fn_psd_piece);
516 ctfmodel.
x0 = piecej;
517 ctfmodel.
xF = (piecej + pieceDim-1);
518 ctfmodel.
y0 = piecei;
519 ctfmodel.
yF = (piecei + pieceDim-1);
527 A2D_ELEM(Xm,idxi,idxj)=(piecei+pieceDim/2)*ctfmodel.
Tm;
528 A2D_ELEM(Ym,idxi,idxj)=(piecej+pieceDim/2)*ctfmodel.
Tm;
534 ctf1Dmodel.x0 = piecej;
535 ctf1Dmodel.xF = (piecej + pieceDim-1);
536 ctf1Dmodel.y0 = piecei;
537 ctf1Dmodel.yF = (piecei + pieceDim-1);
540 A2D_ELEM(defocusPlanefittingU,idxi,idxj)=ctf1Dmodel.Defocus;
542 A2D_ELEM(Xm,idxi,idxj)=(piecei+pieceDim/2)*ctf1Dmodel.Tm;
543 A2D_ELEM(Ym,idxi,idxj)=(piecej+pieceDim/2)*ctf1Dmodel.Tm;
572 double idiv_Number = 1.0 / actualDiv_Number;
584 psd_avg.
write(fn_psd);
589 std::cerr <<
"Adjusting CTF model to the PSD ...\n";
614 save().initZeros(psd());
619 save.
write(
"PPPbasis.xmp");
626 double pavg = p.
sum(
true);
628 pstd = (pstd < 0) ? 0 :
sqrt(pstd);
642 ctfmodel.
xF = (Xdim-1);
644 ctfmodel.
yF = (Ydim-1);
651 ctf1Dmodel.
xF = (Xdim-1);
653 ctf1Dmodel.
yF = (Ydim-1);
679 std::cerr <<
"Computing bootstrap ...\n";
686 CTFs(
n, 0) = ctfmodel.
Tm;
687 CTFs(
n, 1) = ctfmodel.
kV;
691 CTFs(
n, 5) = ctfmodel.
Cs;
692 CTFs(
n, 6) = ctfmodel.
Ca;
693 CTFs(
n, 7) = ctfmodel.
espr;
694 CTFs(
n, 8) = ctfmodel.
ispr;
695 CTFs(
n, 9) = ctfmodel.
alpha;
698 CTFs(
n, 12) = ctfmodel.
Q0;
699 CTFs(
n, 13) = ctfmodel.
K;
703 CTFs(
n, 17) = ctfmodel.
cU;
704 CTFs(
n, 18) = ctfmodel.
cV;
707 CTFs(
n, 21) = ctfmodel.
sqU;
708 CTFs(
n, 22) = ctfmodel.
sqV;
714 CTFs(
n, 28) = ctfmodel.
cU2;
715 CTFs(
n, 29) = ctfmodel.
cV2;
719 std::string command = (std::string)
"mv -i " + fnBase
720 +
".ctfparam " + fnBase +
"_bootstrap_" 722 if (system(command.c_str())==-1)
724 command = (std::string)
"mv -i " + fnBase
725 +
".ctfmodel_quadrant " + fnBase +
"_bootstrap_" 727 if (system(command.c_str())==-1)
729 command = (std::string)
"mv -i " + fnBase
730 +
".ctfmodel_halfplane " + fnBase +
"_bootstrap_" 732 if (system(command.c_str())==-1)
747 std::cerr <<
"Computing bootstrap ...\n";
753 CTFs(
n, 0) = ctf1Dmodel.
Tm;
754 CTFs(
n, 1) = ctf1Dmodel.
kV;
756 CTFs(
n, 3) = ctf1Dmodel.
Cs;
757 CTFs(
n, 4) = ctf1Dmodel.
Ca;
758 CTFs(
n, 5) = ctf1Dmodel.
espr;
759 CTFs(
n, 6) = ctf1Dmodel.
ispr;
760 CTFs(
n, 7) = ctf1Dmodel.
alpha;
761 CTFs(
n, 8) = ctf1Dmodel.
DeltaF;
762 CTFs(
n, 9) = ctf1Dmodel.
DeltaR;
763 CTFs(
n, 10) = ctf1Dmodel.
Q0;
764 CTFs(
n, 11) = ctf1Dmodel.
K;
766 CTFs(
n, 13) = ctf1Dmodel.
sigma1;
767 CTFs(
n, 14) = ctf1Dmodel.
Gc1;
768 CTFs(
n, 15) = ctf1Dmodel.
sqrt_K;
769 CTFs(
n, 16) = ctf1Dmodel.
sq;
772 CTFs(
n, 19) = ctf1Dmodel.
sigma2;
773 CTFs(
n, 20) = ctf1Dmodel.
Gc2;
776 std::string command = (std::string)
"mv -i " + fnBase
777 +
".ctfparam " + fnBase +
"_bootstrap_" 779 if (system(command.c_str())==-1)
781 command = (std::string)
"mv -i " + fnBase
782 +
".ctf1Dmodel_quadrant " + fnBase +
"_bootstrap_" 784 if (system(command.c_str())==-1)
786 command = (std::string)
"mv -i " + fnBase
787 +
".ctf1Dmodel_halfplane " + fnBase +
"_bootstrap_" 789 if (system(command.c_str())==-1)
805 double pU0 = 0, pU1 = 0, pU2 = 0;
806 planeFit(defocusPlanefittingU, Xm, Ym, pU0, pU1, pU2);
807 double pV0 = 0, pV1 = 0, pV2 = 0;
808 planeFit(defocusPlanefittingV, Xm, Ym, pV0, pV1, pV2);
812 double Tm, downsampling;
834 FileName fn_img, fn_psd_piece, fn_ctfparam_piece;
836 for (
size_t objId : posFile.
ids())
843 int N = idx_Y * div_NumberX + idx_X + 1;
845 fn_psd_piece.
compose(N, fn_psd);
874 size_t IXdim, IYdim, IZdim;
888 double pieceDim2 =
XSIZE(piece) *
XSIZE(piece);
889 for (
size_t i = 0;
i < (IYdim -
YSIZE(piece));
i+=
YSIZE(piece))
890 for (
size_t j = 0;
j < (IXdim -
XSIZE(piece));
j+=
XSIZE(piece), pieceNumber++)
892 if ((pieceNumber + 1) % Nthreads != id)
897 for (
size_t k = 0;
k <
YSIZE(piece);
k++)
898 for (
size_t l = 0; l <
XSIZE(piece); l++)
902 piece *= pieceSmoother;
912 double magnitude2=re*re+im*im;
919 args->Nprocessed += Nprocessed;
920 *(args->PSD) += localPSD;
921 args->mutex->unlock();
927 size_t Xdim, Ydim, Zdim, Ndim;
929 int minSize = 2 * (
std::max(Xdim, Ydim) / 10);
949 I.
read(fnMicrograph);
970 auto thMgr = std::unique_ptr<ThreadManager>(std::make_unique<ThreadManager>(numberOfThreads, &args));
977 prog2.filter_w2 = 0.2;
978 prog2.decay_width = 0.02;
979 prog2.mask_w1 = 0.005;
982 prog2.applyFilter(*(args.
PSD));
983 enhancedPSD = *(args.
PSD);
985 auto downXdim = (int) ((
double)
XSIZE(enhancedPSD) / downsampling);
989 enhancedPSD.
selfWindow(firstIndex, firstIndex, lastIndex, lastIndex);
double sigmaU
Spherical aberration (in milimeters). Typical value 5.6.
void run(ThreadFunction function, void *data=NULL)
Just to locate unclassified errors.
#define VECTOR_R2(v, x, y)
void planeFit(const MultidimArray< double > &z, const MultidimArray< double > &x, const MultidimArray< double > &y, double &p0, double &p1, double &p2)
void init_progress_bar(long total)
void selfWindow(int n0, int z0, int y0, int x0, int nF, int zF, int yF, int xF, T init_value=0)
void min(Image< double > &op1, const Image< double > &op2)
#define A2D_ELEM(v, i, j)
void addVector(const MultidimArray< float > &_v)
Add vector.
double getDoubleParam(const char *param, int arg=0)
void threshold(const String &type, T a, T b=0, MultidimArray< int > *mask=NULL)
__host__ __device__ float2 floor(const float2 v)
double gaussian_angle2
Second Gaussian angle.
#define REPORT_ERROR(nerr, ErrormMsg)
TPSD_mode psd_mode
Partition mode.
void projectOnPCABasis(Matrix2D< double > &CtY)
Project on basis.
void resizeNoCopy(const MultidimArray< T1 > &v)
void threadFastEstimateEnhancedPSD(ThreadArgument &thArg)
void sqrt(Image< double > &op)
MultidimArray< double > * PSD
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
static void constructPieceSmoother(const MultidimArray< T > &piece, MultidimArray< T > &pieceSmoother)
#define DIRECT_A2D_ELEM(v, i, j)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
void readBasicParams(XmippProgram *program)
Read parameters.
void toVector(Matrix1D< T > &op1) const
void readBasicParams(XmippProgram *program)
Read parameters.
double cU2
Second Gaussian center for U.
void compose(const String &str, const size_t no, const String &ext="")
double cU
Gaussian center for U.
void PSD_piece_by_averaging(MultidimArray< double > &piece, MultidimArray< double > &psd)
MultidimArray< int > * pieceMask
bool estimate_ctf
Estimate a CTF for each PSD.
String integerToString(int I, int _width, char fill_with)
double gaussian_K
Gain for the gaussian term.
void FFT_idx2digfreq(T &v, const Matrix1D< int > &idx, Matrix1D< double > &freq)
FileName fn_psd
CTF filename.
double DeltaF
Longitudinal mechanical displacement (ansgtrom). Typical value 100.
double sqrt_angle
Sqrt angle.
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
double DeltafU
Global gain. By default, 1.
void CenterFFT(MultidimArray< T > &v, bool forward)
void standardarizeVariables()
Standardarize variables.
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
double Gc2
Second Gaussian center.
double sigma2
Second Gaussian width.
bool bootstrap
Bootstrap estimation.
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
void run()
Process the whole thing.
double base_line
Global base_line.
double sqU
Gain for the square root term.
double Gc1
Gaussian center.
const char * getParam(const char *param, int arg=0)
if(fabs(c[*nmax+ *nmax *c_dim1])==0.e0)
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const
FileName fn_root
Output rootname.
bool acceleration1D
Accelerate PSD estimation.
void CausalARMA(MultidimArray< double > &Img, ARMA_parameters &prm)
double gaussian_K2
Gain for the second Gaussian term.
double ROUT_Adjust_CTFFast(ProgCTFEstimateFromPSDFast &prm, CTFDescription1D &output_ctfmodel, bool standalone)
void normalize_ramp(MultidimArray< double > &I, MultidimArray< int > *bg_mask)
Incorrect argument received.
void progress_bar(long rlen)
ProgCTFEstimateFromPSDFast prmEstimateCTFFromPSDFast
Parameters for adjust_CTF program.
double ispr
Objective lens stability (deltaI/I) (ppm). Typical value 1.
void max(Image< double > &op1, const Image< double > &op2)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
double Tm
Sampling rate (A/pixel)
Error related to numerical calculation.
TPSDEstimator_mode PSDEstimator_mode
#define DIRECT_MULTIDIM_ELEM(v, n)
void addExampleLine(const char *example, bool verbatim=true)
double sum(bool average=false) const
std::vector< MultidimArray< double > > PCAbasis
void window2D(const MultidimArray< T > &Ibig, MultidimArray< T > &Ismall, size_t y0, size_t x0, size_t yF, size_t xF)
int verbose
Verbosity level.
static void defineBasicParams(XmippProgram *program)
Define basic parameters.
double Cs
Spherical aberration (in milimeters). Typical value 5.6.
int pieceDim
Dimension of micrograph pieces.
void FourierTransform(const MultidimArray< double > &in, MultidimArray< std::complex< double > > &out)
ProgCTFEstimateFromPSD prmEstimateCTFFromPSD
Parameters for adjust_CTF program.
void * workClass
The class in which threads will be working.
double DeltaR
Transversal mechanical displacement (ansgtrom). Typical value 3.
double sigmaV
Gaussian width V.
bool show_optimization
Show convergence values.
#define NEXT_POWER_OF_2(x)
double Defocus
Defocus (in Angstroms). Negative values are underfocused.
double K
Global gain. By default, 1.
void ARMAFilter(MultidimArray< double > &Img, MultidimArray< double > &Filter, ARMA_parameters &prm)
double gaussian_angle
Gaussian angle.
bool isLocalCTF
Local CTF determination.
MultidimArray< double > * pieceSmoother
ARMA_parameters ARMA_prm
Parameters for ARMA.
FileName withoutExtension() const
double filter_w1
Bandpass filter low frequency (in Fourier space, max 0.5)
double Ca
Chromatic aberration (in milimeters). Typical value 2.
#define FIRST_XMIPP_INDEX(size)
double alpha
Convergence cone semiangle (in mrad). Typical value 0.5.
bool fileExists(const char *filename)
String formatString(const char *format,...)
int thread_id
The thread id.
void readParams(XmippProgram *program)
Read parameters from command line.
static void defineParams(XmippProgram *program)
Define parameters.
double y0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
void learnPCABasis(size_t NPCA, size_t Niter)
Learn basis.
bool checkParam(const char *param)
double Q0
Factor for the importance of the Amplitude contrast.
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
double x0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
double kV
Accelerating Voltage (in KiloVolts)
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
double cV2
Second Gaussian center for V.
FileName fn_micrograph
Micrograph filename.
#define LAST_XMIPP_INDEX(size)
double xF
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
int getIntParam(const char *param, int arg=0)
double sigma1
Gaussian width.
double sqrt_K
Gain for the square root term.
static void defineBasicParams(XmippProgram *program)
Define basic parameters.
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false)
void FFT_magnitude(const MultidimArray< std::complex< double > > &v, MultidimArray< double > &mag)
Incorrect value received.
double ROUT_Adjust_CTF(ProgCTFEstimateFromPSD &prm, CTFDescription &output_ctfmodel, bool standalone)
double yF
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
void fastEstimateEnhancedPSD(const FileName &fnMicrograph, double downsampling, MultidimArray< double > &enhancedPSD, int numberOfThreads)
ProgCTFEstimateFromMicrograph()
double sigmaU2
Second Gaussian width U.
void addParamsLine(const String &line)
void statisticsAdjust(U avgF, U stddevF)
FileName fn_pos
Position file.
double sigmaV2
Second Gaussian width V.
double cV
Gaussian center for V.
void getDimensions(size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim) const