53 #ifdef DEBUG_WITH_TEXTFILES 54 std::ofstream fhDebug;
55 #define DEBUG_OPEN_TEXTFILE(fnRoot) fhDebug.open((fnRoot+"_debug.txt").c_str()); 56 #define DEBUG_CLOSE_TEXTFILE fhDebug.close(); 57 #define DEBUG_TEXTFILE(str) fhDebug << time (NULL) << " " << str << std::endl; 58 #define DEBUG_MODEL_TEXTFILE fhDebug << global_ctfmodel << std::endl; 60 #define DEBUG_OPEN_TEXTFILE(fnRoot); 61 #define DEBUG_CLOSE_TEXTFILE ; 62 #define DEBUG_TEXTFILE(str); 63 #define DEBUG_MODEL_TEXTFILE 78 #define ASSIGN_CTF_PARAM(index, paramName) if (ia <= index && l > 0) { ctfmodel.paramName = p[index]; --l; } 82 int l,
int modelSimplification)
119 if (ia <= 26 && l > 0)
121 if (modelSimplification < 3)
132 if (ia <= 27 && l > 0)
134 if (modelSimplification < 3)
148 if (ia <= 29 && l > 0)
150 if (modelSimplification < 3)
174 #define ASSIGN_PARAM_CTF(index, paramName) if (ia <= index && l > 0) { p[index] = ctfmodel.paramName; --l; } 177 int l,
int modelSimplification)
211 if (ia <= 26 && l > 0)
213 if (modelSimplification < 3)
224 if (ia <= 27 && l > 0)
226 if (modelSimplification < 3)
240 if (ia <= 29 && l > 0)
242 if (modelSimplification < 3)
264 #define COPY_ctfmodel_TO_CURRENT_GUESS \ 265 global_prm->assignParametersFromCTF(global_prm->current_ctfmodel, \ 266 MATRIX1D_ARRAY(*global_prm->adjust_params),0,ALL_CTF_PARAMETERS, \ 267 global_prm->modelSimplification); 274 initial_ctfmodel.enable_CTF = initial_ctfmodel.enable_CTFnoise =
true;
275 initial_ctfmodel.readParams(program);
276 if (initial_ctfmodel.DeltafU>100e3 || initial_ctfmodel.DeltafV>100e3)
278 Tm = initial_ctfmodel.Tm;
284 fn_psd = getParam(
"--psd");
285 readBasicParams(
this);
298 defineBasicParams(
this);
307 current_ctfmodel.clear();
308 ctfmodel_defoci.clear();
313 ctftomodel.read(fn_psd);
317 current_ctfmodel.precomputeValues(x_contfreq, y_contfreq);
326 assignCTFfromParameters(
MATRIX1D_ARRAY(*adjust_params), current_ctfmodel,
328 current_ctfmodel.produceSideInfo();
341 current_ctfmodel.precomputeValues(
XX(freq),
YY(freq));
343 I()(
i,
j) = current_ctfmodel.getValueNoiseAt();
344 else if (action == 2)
346 double E = current_ctfmodel.getValueDampingAt();
347 I()(
i,
j) = current_ctfmodel.getValueNoiseAt() + E * E;
349 else if (action >= 3 && action <= 6)
351 double ctf = current_ctfmodel.getValuePureAt();
352 I()(
i,
j) = current_ctfmodel.getValueNoiseAt() + ctf * ctf;
356 double ctf = current_ctfmodel.getValuePureAt();
371 std::ofstream plotX, plotY, plot_radial;
373 generateModelSoFar(save,
false);
376 generate_model_halfplane(ctfmodelSize,
377 ctfmodelSize, save_ctf());
378 if (fn_root.find(
"@")==std::string::npos)
379 save_ctf.
write(fn_root +
"_ctfmodel_halfplane.xmp");
381 save_ctf.
write(fn_root +
"_ctfmodel_halfplane.stk");
382 generate_model_quadrant(ctfmodelSize,
383 ctfmodelSize, save_ctf());
384 if (fn_root.find(
"@")==std::string::npos)
385 save_ctf.
write(fn_root +
"_ctfmodel_quadrant.xmp");
387 save_ctf.
write(fn_root +
"_ctfmodel_quadrant.stk");
389 if (!generate_profiles)
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());
397 "save_intermediate_results::Cannot open plot file for writing\n");
401 "save_intermediate_results::Cannot open plot file for writing\n");
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";
415 plotY << w_digfreq(
i, 0) <<
" " << w_contfreq(
i, 0) <<
" " 416 << save()(
i, 0) <<
" " << (*
f)(
i, 0) <<
" " 417 << enhanced_ctftomodel()(
i, 0) <<
" " 427 plotX << w_digfreq(0,
j) <<
" " << w_contfreq(0,
j) <<
" " 428 << save()(0,
j) <<
" " << (*
f)(0,
j) <<
" " 429 << enhanced_ctftomodel()(0,
j)
446 double model2 = save()(
i,
j);
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);
457 if (radial_N(
i) == 0)
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)) <<
" " 480 enhancedPSD = enhanced_ctftomodel_fullsize();
486 assignCTFfromParameters(
MATRIX1D_ARRAY(*adjust_params), current_ctfmodel,
488 current_ctfmodel.produceSideInfo();
497 if ((
j >= Xdim / 2 &&
i >= Ydim / 2)
498 || (
j < Xdim / 2 &&
i < Ydim / 2))
503 if (fabs(
XX(freq))>0.03 && fabs(
YY(freq))>0.03)
504 mask_norm(
i,
j)=(int)mask(
i,
j);
507 current_ctfmodel.precomputeValues(
XX(freq),
YY(freq));
508 model(
i,
j) = current_ctfmodel.getValuePureAt();
509 model(
i,
j) *= model(
i,
j);
518 if (!((
j >= Xdim / 2 &&
i >= Ydim / 2) || (
j < Xdim / 2 &&
i < Ydim / 2)))
519 model(
i,
j) = enhancedPSD(
i,
j);
544 enhancedPSD = enhanced_ctftomodel_fullsize();
550 assignCTFfromParameters(
MATRIX1D_ARRAY(*adjust_params), current_ctfmodel,
552 current_ctfmodel.produceSideInfo();
568 if (fabs(
XX(freq))>0.03 && fabs(
YY(freq))>0.03)
569 mask_norm(
i,
j)=(int)mask(
i,
j);
572 current_ctfmodel.precomputeValues(
XX(freq),
YY(freq));
573 model(
i,
j) = current_ctfmodel.getValuePureAt();
574 model(
i,
j) *= model(
i,
j);
583 if (!(
j <= Xdim / 2))
584 model(
i,
j) = enhancedPSD(
i,
j);
611 modelSimplification);
614 std::cout <<
"Input vector:";
616 std::cout << p[
i] <<
" ";
617 std::cout << std::endl;
626 std::cout <<
"Input vector:";
628 std::cout << p[
i] <<
" ";
629 std::cout << std::endl;
635 modelSimplification);
638 std::cout <<
"Input vector:";
640 std::cout << p[
i] <<
" ";
641 std::cout << std::endl;
647 modelSimplification);
648 psd_theo_radial_derivative.initZeros();
651 std::cout <<
"Input vector:";
653 std::cout << p[
i] <<
" ";
654 std::cout << std::endl;
658 assignCTFfromParameters(p - 0 + 1, current_ctfmodel, 0,
660 psd_theo_radial.initZeros();
663 std::cout <<
"Input vector:";
665 std::cout << p[
i] <<
" ";
666 std::cout << std::endl;
672 assignCTFfromParameters(p - 0 + 1, current_ctfmodel, 0,
674 psd_theo_radial.initZeros();
677 std::cout <<
"Input vector:";
679 std::cout << p[
i] <<
" ";
680 std::cout << std::endl;
686 current_ctfmodel.produceSideInfo();
689 std::cout <<
"Model:\n" << current_ctfmodel << std::endl;
690 if (!current_ctfmodel.hasPhysicalMeaning())
693 std::cout <<
"Does not have physical meaning\n";
694 return heavy_penalization;
697 if (action > 3 && !noDefocusEstimate
699 (current_ctfmodel.DeltafU - ctfmodel_defoci.DeltafU)
700 / ctfmodel_defoci.DeltafU) > 0.2
702 (current_ctfmodel.DeltafV
703 - ctfmodel_defoci.DeltafV)
704 / ctfmodel_defoci.DeltafU) > 0.2))
707 std::cout <<
"Too large defocus\n";
708 return heavy_penalization;
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;
744 double lowerLimit = 1.1 * min_freq_psd;
745 double upperLimit = 0.9 * max_freq_psd;
747 int XdimW=
XSIZE(w_digfreq);
748 int YdimW=
YSIZE(w_digfreq);
751 for (
int i = 0;
i < YdimW;
i += evaluation_reduction)
752 for (
int j = 0;
j < XdimW;
j += evaluation_reduction)
758 current_ctfmodel.precomputeValues(
i,
j);
759 double bg = current_ctfmodel.getValueNoiseAt();
760 double envelope=0, ctf_without_damping, ctf_with_damping=0;
764 double ctf_with_damping2;
770 dist = fabs(ctf2 - bg);
771 if (penalize && bg > ctf2
774 dist *= current_penalty;
777 envelope = current_ctfmodel.getValueDampingAt();
778 ctf2_th = bg + envelope * envelope;
779 dist = fabs(ctf2 - ctf2_th);
780 if (penalize && ctf2_th < ctf2
783 dist *= current_penalty;
790 envelope = current_ctfmodel.getValueDampingAt();
791 ctf_without_damping =
792 current_ctfmodel.getValuePureWithoutDampingAt();
793 ctf_with_damping = envelope * ctf_without_damping;
794 ctf2_th = bg + ctf_with_damping * ctf_with_damping;
803 double enhanced_ctf =
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;
816 A1D_ELEM(psd_theo_radial,r) += ctf2_th;
821 dist = fabs(ctf2 - ctf2_th) / (envelope * envelope);
823 dist = fabs(ctf2 - ctf2_th);
842 retval = distsum / N;
844 retval = heavy_penalization;
847 std::cout <<
"Fitness1=" << retval << std::endl;
849 if ( (((action >= 3) && (action <= 4)) || (action == 7))
850 && (Ncorr > 0) && (enhanced_weight != 0) )
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);
862 || (fabs(sigma1 - sigma2) / maxSigma > 0.9 && action>=5))
864 retval = heavy_penalization;
866 std::cout <<
"Fitness2=" << heavy_penalization <<
" sigma1=" << sigma1 <<
" sigma2=" << sigma2 << std::endl;
870 correlation_coeff /= sigma1 * sigma2;
872 corr13 = correlation_coeff;
874 retval -= enhanced_weight * correlation_coeff;
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
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;
889 if (action==3 || evaluation_reduction==1)
893 psd_theo_radial_derivative.initZeros();
894 double lowerlimt=1.1*min_freq;
895 double upperlimit=0.9*max_freq;
913 A1D_ELEM(psd_theo_radial_derivative,
i)=diff;
914 maxDiff=
std::max(maxDiff,fabs(diff));
920 double corrRadialDerivative=0,mux=0, muy=0, Ncorr=0, sigmax=0, sigmay=0;
921 double iMaxDiff=1.0/maxDiff;
924 A1D_ELEM(psd_theo_radial_derivative,
i)*=iMaxDiff;
925 double x=
A1D_ELEM(psd_exp_enhanced_radial_derivative,
i);
926 double y=
A1D_ELEM(psd_theo_radial_derivative,
i);
927 corrRadialDerivative+=x*
y;
936 double iNcorr=1.0/Ncorr;
937 corrRadialDerivative*=iNcorr;
940 sigmax=
sqrt(fabs(sigmax*iNcorr-mux*mux));
941 sigmay=
sqrt(fabs(sigmay*iNcorr-muy*muy));
942 corrRadialDerivative=(corrRadialDerivative-mux*muy)/(sigmax*sigmay);
945 retval-=corrRadialDerivative;
949 std::cout <<
"Fitness3=" << -corrRadialDerivative << std::endl;
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");
966 std::cout <<
"Fitness=" << retval << std::endl;
969 saveIntermediateResults(
"PPP");
970 std::cout <<
"Press any key\n";
982 return prm->CTF_fitness_object(p);
996 current_ctfmodel.lookFor(1, dir, freq, 0);
997 if (
XX(freq) == -1 &&
YY(freq) == -1)
1004 w =
XX(freq) /
XX(dir);
1006 w =
YY(freq) /
YY(dir);
1011 current_ctfmodel.lookFor(5, dir, freq, 0);
1012 if (
XX(freq) == -1 &&
YY(freq) == -1)
1019 w =
XX(freq) /
XX(dir);
1021 w =
YY(freq) /
YY(dir);
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;
1034 double w1 = min_freq_psd, w2 = max_freq_psd;
1037 double w1U, w2U, w1V, w2V;
1038 compute_central_region(w1U, w2U, current_ctfmodel.azimuthal_angle);
1039 compute_central_region(w1V, w2V, current_ctfmodel.azimuthal_angle + 90);
1042 min_freq_psd =
XMIPP_MAX(min_freq_psd, w1 - 0.05);
1043 max_freq_psd =
XMIPP_MIN(max_freq_psd, w2 + 0.01);
1050 generateModelSoFar(save);
1054 double w = w_digfreq(
i,
j);
1055 if (w >= w1 && w <= w2)
1059 value_th =
XMIPP_MIN(value_th, max_val * margin);
1061 value_th = max_val * margin;
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;
1074 if (show_optimization)
1075 std::cout <<
"Computing first sqrt background ...\n";
1079 double base_line = 0;
1083 if (w_digfreq(
i,
j) > 0.4)
1086 base_line += (*f)(
i,
j);
1092 current_ctfmodel.base_line = base_line / N;
1100 if (mask(
i,
j) <= 0)
1104 double weight = 1 + max_freq_psd - w_digfreq(
i,
j);
1107 current_ctfmodel.precomputeValues(x_contfreq(
i,
j),y_contfreq(
i,
j));
1108 double explained = current_ctfmodel.getValueNoiseAt();
1109 double unexplained = (*f)(
i,
j) - explained;
1110 if (unexplained <= 0)
1112 unexplained =
log(unexplained);
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;
1125 current_ctfmodel.sqU = current_ctfmodel.sqV =
b(0);
1126 current_ctfmodel.sqrt_K = exp(
b(1));
1127 current_ctfmodel.sqrt_angle = 0;
1131 if (show_optimization)
1133 std::cout <<
"First SQRT Fit:\n" << current_ctfmodel << std::endl;
1134 saveIntermediateResults(
"step01a_first_sqrt_fit");
1143 if (show_optimization)
1144 std::cout <<
"Looking for best fitting sqrt ...\n";
1151 if (show_optimization)
1152 std::cout <<
"Penalizing best fitting sqrt ...\n";
1154 current_penalty = 2;
1156 for (
int i = 1;
i <= imax;
i++)
1158 if (show_optimization)
1159 std::cout <<
" Iteration " <<
i <<
" penalty=" 1160 << current_penalty << std::endl;
1163 steps, show_optimization);
1164 current_penalty *= 2;
1169 current_ctfmodel.forcePhysicalMeaning();
1172 if (show_optimization)
1174 std::cout <<
"Best penalized SQRT Fit:\n" << current_ctfmodel
1176 saveIntermediateResults(
"step01b_best_penalized_sqrt_fit");
1179 center_optimization_focus(
false,
true, 1.5);
1187 if (show_optimization)
1188 std::cout <<
"Computing first background Gaussian parameters ...\n";
1194 double w_max_gauss = 0.25;
1197 if (mask(
i,
j) <= 0)
1199 double w = w_digfreq(
i,
j);
1200 if (w > w_max_gauss)
1204 current_ctfmodel.precomputeValues(x_contfreq(
i,
j),y_contfreq(
i,
j));
1205 radial_CTFmodel_avg(r) += current_ctfmodel.getValueNoiseAt();
1206 radial_CTFampl_avg(r) += (*f)(
i,
j);
1211 double error2_avg = 0;
1217 if (radial_N(
i) == 0)
1219 error(
i) = (radial_CTFampl_avg(
i) - radial_CTFmodel_avg(
i))
1225 error2_avg /= N_avg;
1229 std::cout <<
"Error2 avg=" << error2_avg << std::endl;
1233 bool first =
true, OK_to_proceed =
false;
1234 double error2_min = 0, wmin=0;
1237 if (radial_N(
i) == 0)
1239 double w = w_digfreq(
i, 0);
1241 if (
error(
i) < 0 && first)
1248 if (first && error2 > 0.15 * error2_avg)
1249 OK_to_proceed =
true;
1255 if (!first && error2 > 1.69 * error2_min)
1257 if (first && OK_to_proceed)
1260 error2_min = error2;
1263 if (!first && error2 < error2_min)
1266 error2_min = error2;
1269 std::cout << w <<
" " << error2 <<
" " << wmin <<
" " << std::endl;
1275 max_gauss_freq = wmin;
1278 std::cout <<
"Freq of the minimum error: " << wmin <<
" " << fmin << std::endl;
1283 double error2_max = 0, wmax=0,
fmax;
1286 if (radial_N(
i) == 0)
1288 double w = w_digfreq(
i, 0);
1292 if (
error(
i) < 0 && first)
1300 error2_max = error2;
1303 if (error2 > error2_max)
1306 error2_max = error2;
1309 std::cout << w <<
" " << error2 <<
" " << wmax << std::endl;
1313 fmax = current_ctfmodel.cV = current_ctfmodel.cU = wmax / Tm;
1316 std::cout <<
"Freq of the maximum error: " << wmax <<
" " <<
fmax << std::endl;
1327 if (mask(
i,
j) <= 0)
1329 if (w_digfreq(
i,
j) > wmin)
1331 double fmod = w_contfreq(
i,
j);
1334 double weight = 1 + max_freq_psd - w_digfreq(
i,
j);
1336 current_ctfmodel.precomputeValues(x_contfreq(
i,
j),y_contfreq(
i,
j));
1337 double explained = current_ctfmodel.getValueNoiseAt();
1339 double unexplained = (*f)(
i,
j) - explained;
1340 if (unexplained <= 0)
1342 unexplained =
log(unexplained);
1343 double F = -(fmod -
fmax) * (fmod -
fmax);
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;
1352 if ( (A(0, 0)== 0) && (A(1, 0)== 0) && (A(1, 1)== 0))
1354 std::cout <<
"A matrix es zero" << std::endl;
1359 current_ctfmodel.sigmaU =
XMIPP_MIN(fabs(
b(1)), 95e3);
1360 current_ctfmodel.sigmaV =
XMIPP_MIN(fabs(
b(1)), 95e3);
1362 current_ctfmodel.gaussian_K = exp(
b(0));
1364 current_ctfmodel.forcePhysicalMeaning();
1367 if (show_optimization)
1369 std::cout <<
"First Background Fit:\n" << current_ctfmodel << std::endl;
1370 saveIntermediateResults(
"step01c_first_background_fit");
1372 center_optimization_focus(
false,
true, 1.5);
1381 if (show_optimization)
1382 std::cout <<
"Computing first background Gaussian2 parameters ...\n";
1388 double w_max_gauss = 0.25;
1391 if (mask(
i,
j) <= 0)
1393 double w = w_digfreq(
i,
j);
1394 if (w > w_max_gauss)
1400 current_ctfmodel.precomputeValues(f_x, f_y);
1401 double bg = current_ctfmodel.getValueNoiseAt();
1402 double envelope = current_ctfmodel.getValueDampingAt();
1403 double ctf_without_damping =
1404 current_ctfmodel.getValuePureWithoutDampingAt();
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);
1417 if (radial_N(
i) == 0)
1419 error(
i) = (radial_CTFampl_avg(
i) - radial_CTFmodel_avg(
i))
1423 std::cout <<
"Error:\n" << error << std::endl;
1430 double error_max = 0, wmax=0,
fmax;
1433 if (radial_N(
i) == 0)
1435 double w = w_digfreq(
i, 0);
1438 if (
error(
i) < error_max)
1444 fmax = current_ctfmodel.cV2 = current_ctfmodel.cU2 = wmax / Tm;
1447 std::cout <<
"Freq of the maximum error: " << wmax <<
" " <<
fmax << std::endl;
1458 if (mask(
i,
j) <= 0)
1460 if (w_digfreq(
i,
j) > wmin)
1462 double fmod = w_contfreq(
i,
j);
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())
1473 double weight = 1 + max_freq_psd - w_digfreq(
i,
j);
1478 current_ctfmodel.precomputeValues(f_x, f_y);
1479 double bg = current_ctfmodel.getValueNoiseAt();
1480 double envelope = current_ctfmodel.getValueDampingAt();
1481 double ctf_without_damping =
1482 current_ctfmodel.getValuePureWithoutDampingAt();
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);
1488 if (unexplained <= 0)
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;
1507 current_ctfmodel.sigmaU2 =
XMIPP_MIN(fabs(
b(1)), 95e3);
1508 current_ctfmodel.sigmaV2 =
XMIPP_MIN(fabs(
b(1)), 95e3);
1510 current_ctfmodel.gaussian_K2 = exp(
b(0));
1514 current_ctfmodel.sigmaU2 = current_ctfmodel.sigmaV2 = 0;
1515 current_ctfmodel.gaussian_K2 = 0;
1520 current_ctfmodel.sigmaU2 = current_ctfmodel.sigmaV2 = 0;
1521 current_ctfmodel.gaussian_K2 = 0;
1525 current_ctfmodel.forcePhysicalMeaning();
1534 if (w_digfreq(
i,
j) > wmin)
1536 double fmod = w_contfreq(
i,
j);
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())
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);
1557 if (unexplained <= 0)
1559 std::cout << fmod <<
" " << unexplained <<
" " 1560 << current_ctfmodel.gaussian_K2*exp(-current_ctfmodel.sigmaU2*
1561 (fmod -
fmax)*(fmod -
fmax)) << std::endl;
1565 if (show_optimization)
1567 std::cout <<
"First Background Gaussian 2 Fit:\n" << current_ctfmodel
1569 saveIntermediateResults(
"step04a_first_background2_fit");
1578 if (show_optimization)
1579 std::cout <<
"Looking for best fitting envelope ...\n";
1582 current_ctfmodel.Ca = initial_ctfmodel.Ca;
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;
1589 current_ctfmodel.Q0 = initial_ctfmodel.Q0;
1590 current_ctfmodel.envR0 = 0.0;
1591 current_ctfmodel.envR1 = 0.0;
1604 if (modelSimplification >= 1)
1611 current_ctfmodel.forcePhysicalMeaning();
1614 if (show_optimization)
1616 std::cout <<
"Best envelope Fit:\n" << current_ctfmodel << std::endl;
1617 saveIntermediateResults(
"step02a_best_envelope_fit");
1621 if (show_optimization)
1622 std::cout <<
"Penalizing best fitting envelope ...\n";
1624 current_penalty = 2;
1626 for (
int i = 1;
i <= imax;
i++)
1628 if (show_optimization)
1629 std::cout <<
" Iteration " <<
i <<
" penalty=" 1630 << current_penalty << std::endl;
1633 steps, show_optimization);
1634 current_penalty *= 2;
1639 current_ctfmodel.forcePhysicalMeaning();
1642 if (show_optimization)
1644 std::cout <<
"Best envelope Fit:\n" << current_ctfmodel << std::endl;
1645 saveIntermediateResults(
"step02b_best_penalized_envelope_fit");
1653 if (show_optimization)
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");
1659 save().resize(
YSIZE(w_digfreq),
XSIZE(w_digfreq));
1660 save2().resize(save());
1661 save3().resize(save());
1664 save()(
i,
j) = enhanced_ctftomodel()(
i,
j);
1667 current_ctfmodel.precomputeValues(f_x, f_y);
1668 double ctf_without_damping =
1669 current_ctfmodel.getValuePureWithoutDampingAt();
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;
1674 save.
write(
"step03a_enhanced_PSD.xmp");
1675 save2.
write(
"step03a_fitted_CTF.xmp");
1676 save3.
write(
"step03a_superposition.xmp");
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;
1689 double defocusV, defocusU;
1691 double defocusV0 = 1e3, defocusU0 = 1e3;
1692 double defocusVF = 100e3, defocusUF = 100e3;
1693 double initial_defocusStep = 8e3;
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)
1701 initial_defocusStep =
std::min(defocus_range,20000.0);
1704 initial_ctfmodel.DeltafU
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);
1712 initial_ctfmodel.DeltafU
1714 min_allowed_defocusU =
std::max(1e3,
1715 initial_ctfmodel.DeltafU - maxDeviation);
1716 if (initial_ctfmodel.DeltafV == 0)
1718 defocusV0 = defocusU0;
1719 min_allowed_defocusV = min_allowed_defocusU;
1720 defocusVF = defocusUF;
1721 max_allowed_defocusV = max_allowed_defocusU;
1727 initial_ctfmodel.DeltafV
1729 max_allowed_defocusV =
std::max(100e3,
1730 initial_ctfmodel.DeltafV + maxDeviation);
1733 initial_ctfmodel.DeltafV
1735 min_allowed_defocusV =
std::max(1e3,
1736 initial_ctfmodel.DeltafV - maxDeviation);
1740 double K_so_far = current_ctfmodel.K;
1745 for (
double defocusStep = initial_defocusStep;
1746 defocusStep >=
std::min(5000., defocus_range / 2);
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 +=
1759 for (defocusU = defocusU0, j = 0; defocusU <= defocusUF; defocusU +=
1762 bool first_angle =
true;
1763 if (fabs(defocusU - defocusV) > 30e3)
1765 error(i, j) = heavy_penalization;
1768 for (
double angle = 0; angle < 180; angle += 45)
1773 (*adjust_params)(0) = defocusU;
1774 (*adjust_params)(1) = defocusV;
1775 (*adjust_params)(2) = angle;
1776 (*adjust_params)(4) = K_so_far;
1780 fitness, iter, steps,
false);
1782 if ((first_angle || fitness <
error(i, j))
1783 && (current_ctfmodel.DeltafU >= min_allowed_defocusU
1784 && current_ctfmodel.DeltafU
1785 <= max_allowed_defocusU
1786 && current_ctfmodel.DeltafV
1787 >= min_allowed_defocusV
1788 && current_ctfmodel.DeltafV
1789 <= max_allowed_defocusV))
1792 first_angle =
false;
1793 if (
error(i, j) < best_error || first)
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;
1801 if (show_optimization)
1803 std::cout <<
" (DefocusU,DefocusV)=(" 1804 << defocusU <<
"," << defocusV
1805 <<
"), ang=" << angle <<
" --> (" 1806 << current_ctfmodel.DeltafU <<
"," 1807 << current_ctfmodel.DeltafV <<
")," 1808 << current_ctfmodel.azimuthal_angle
1809 <<
" K=" << current_ctfmodel.K
1810 <<
" error=" <<
error(i, j)
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)
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;
1834 save.
write(
"PPPctf2_with_damping2.xmp");
1835 std::cout <<
"Press any key\n";
1848 double errmin =
error(0, 0), errmax =
error(0, 0);
1849 bool aValidErrorHasBeenFound=
false;
1853 if (
error(ii, jj) != heavy_penalization)
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);
1864 if (show_optimization)
1865 std::cout <<
"Error matrix\n" << error << std::endl;
1866 if (!aValidErrorHasBeenFound)
1871 std::cout <<
"Range=" << errmax - errmin << std::endl;
1872 double best_defocusVmin = best_defocusV, best_defocusVmax =
1874 double best_defocusUmin = best_defocusU, best_defocusUmax =
1876 for (defocusV = defocusV0, i = 0; defocusV <= defocusVF; defocusV +=
1879 for (defocusU = defocusU0, j = 0; defocusU <= defocusUF; defocusU +=
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)
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;
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);
1915 save.
write(
"error.xmp");
1916 std::cout <<
"Press any key: Error saved\n";
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;
1928 current_ctfmodel.forcePhysicalMeaning();
1930 ctfmodel_defoci = current_ctfmodel;
1938 double kV,
double lambdaPhase,
int sizeWindowPhase,
1939 double &defocusU,
double &defocusV,
double &ellipseAngle,
int verbose)
1955 auto x=(int)((0.3*max_freq+0.7*min_freq)*std::cos(
PI/4)*
XSIZE(centeredEnhancedPSD)+
XSIZE(centeredEnhancedPSD)/2);
1963 double K_so_far = current_ctfmodel.K;
1973 max_freq = max_freq*0.8;
1974 double fmax = max_freq;
1991 double fmaxStep = (max_freq-min_freq)/numElem;
1994 sizeWindowPhase = 10;
1998 for (
int i = 1;
i < numElem;
i++)
2000 if (((fmax - min_freq)/min_freq) > 0.5)
2002 demodulate(centeredEnhancedPSD,lambdaPhase,sizeWindowPhase,
2004 (
int)(min_freq*
XSIZE(centeredEnhancedPSD)),
2005 (
int)(fmax*
XSIZE(centeredEnhancedPSD)),
2006 phase, mod, coefs, 0);
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));
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;
2036 fitness, iter, steps,
false);
2038 VEC_ELEM(arrayDefocusAvg,
i) = ((*adjust_params)(0) +(*adjust_params)(1))/2;
2039 VEC_ELEM(arrayDefocusDiff,
i) = ((*adjust_params)(0) -(*adjust_params)(1))/2;
2047 while ( (
VEC_ELEM(arrayDefocusAvg,maxInd) < 3000) || ((
VEC_ELEM(arrayDefocusAvg,maxInd) > 50000) &&
VEC_ELEM(arrayError,maxInd)>-1e3 ))
2049 VEC_ELEM(arrayError,maxInd) = -1e3;
2050 VEC_ELEM(arrayDefocusAvg,maxInd) = initial_ctfmodel.DeltafU;
2051 VEC_ELEM(arrayDefocusDiff,maxInd) = initial_ctfmodel.DeltafV;
2054 if (
VEC_ELEM(arrayError,maxInd)<=-1e3)
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;
2080 fitness, iter, steps,
false);
2082 VEC_ELEM(arrayDefocusU,0) = (*adjust_params)(0);
2083 VEC_ELEM(arrayDefocusV,0) = (*adjust_params)(1);
2084 VEC_ELEM(arrayError2,0) = (-1)*fitness;
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;
2096 fitness, iter, steps,
false);
2098 VEC_ELEM(arrayDefocusU,1) = (*adjust_params)(0);
2099 VEC_ELEM(arrayDefocusV,1) = (*adjust_params)(1);
2100 VEC_ELEM(arrayError2,1) = (-1)*fitness;
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;
2112 fitness, iter, steps,
false);
2114 VEC_ELEM(arrayDefocusU,2) = (*adjust_params)(0);
2115 VEC_ELEM(arrayDefocusV,2) = (*adjust_params)(1);
2116 VEC_ELEM(arrayError2,2) = (-1)*fitness;
2120 defocusU =
VEC_ELEM(arrayDefocusU,maxInd);
2121 defocusV =
VEC_ELEM(arrayDefocusV,maxInd);
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;
2129 while ( (0.5*(defocusU+defocusV) < 2500) || (0.5*(defocusU+defocusV) > 60000) )
2131 VEC_ELEM(arrayError2,maxInd) = -1e3;
2132 VEC_ELEM(arrayDefocusU,maxInd) = initial_ctfmodel.DeltafU;
2133 VEC_ELEM(arrayDefocusV,maxInd) = initial_ctfmodel.DeltafV;
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;
2145 if (
VEC_ELEM(arrayError2,maxInd) <= 0)
2148 ctfmodel_defoci = current_ctfmodel;
2156 if (current_ctfmodel.Q0!=0)
2161 evaluation_reduction = 2;
2163 global_prm, 0.01, fitness, iter, steps,show_optimization);
2168 evaluation_reduction = 1;
2173 *adjust_params = initialGlobalAdjust;
2176 #ifndef RELEASE_MODE 2177 std::cout <<
" Entering in estimate_defoci, Performing exhaustive defocus search (SLOW)" << std::endl;
2186 if (show_optimization)
2187 std::cout <<
"Looking for first defoci ...\n";
2191 estimate_defoci_Zernike(enhanced_ctftomodel_fullsize(),
2192 min_freq,max_freq,Tm,
2193 initial_ctfmodel.kV,
2194 lambdaPhase,sizeWindowPhase,
2195 current_ctfmodel.DeltafU, current_ctfmodel.DeltafV, current_ctfmodel.azimuthal_angle, 0);
2199 current_ctfmodel.forcePhysicalMeaning();
2201 ctfmodel_defoci = current_ctfmodel;
2272 std::cout <<
"Best background Fit:\n" << prm.
current_ctfmodel << std::endl;
2369 std::cout <<
"Best fit with Gaussian2:\n" << prm.
current_ctfmodel << std::endl;
2387 std::cout <<
"Refining amplitude contrast ..." << std::endl;
2440 FileName fn_rootMODEL = fn_rootCTFPARAM;
2441 size_t atPosition=fn_rootCTFPARAM.find(
'@');
2443 if (atPosition!=std::string::npos)
2446 fn_rootCTFPARAM.substr(atPosition+1).c_str());
2448 fn_rootCTFPARAM.substr(atPosition+1).c_str());
2451 fn_rootCTFPARAM=(
String)
"fullMicrograph@"+fn_rootCTFPARAM;
2458 MD.
read(fn_rootCTFPARAM +
".ctfparam_tmp");
2469 fn_rootCTFPARAM = fn_rootCTFPARAM +
".ctfparam_tmp";
2470 fn_rootCTFPARAM.deleteFile();
double sigmaU
Spherical aberration (in milimeters). Typical value 5.6.
#define VECTOR_R2(v, x, y)
double defocus_range
Defocus range.
constexpr int ENVELOPE_PARAMETERS
void generate_model_quadrant(int Ydim, int Xdim, MultidimArray< double > &model)
void min(Image< double > &op1, const Image< double > &op2)
void assignParametersFromCTF(const CTFDescription &ctfmodel, double *p, int ia, int l, int modelSimplification)
#define A2D_ELEM(v, i, j)
double enhanced_weight
Weight of the enhanced image.
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
#define COPY_ctfmodel_TO_CURRENT_GUESS
MultidimArray< double > * f
void estimate_background_sqrt_parameters()
Image< double > ctftomodel
CTF amplitude to model.
__host__ __device__ float2 floor(const float2 v)
bool fastDefocusEstimate
Fast defocus estimate.
FileName removeLastExtension() const
double heavy_penalization
void saveIntermediateResults(const FileName &fn_root, bool generate_profiles)
#define REPORT_ERROR(nerr, ErrormMsg)
CTFDescription1D initial_ctfmodel
void resizeNoCopy(const MultidimArray< T1 > &v)
void precomputeValues(double X)
Precompute values for a given frequency.
void sqrt(Image< double > &op)
constexpr int FIRST_SQRT_PARAMETER
Couldn't write to file.
CTFDescription current_ctfmodel
MultidimArray< double > w_digfreq
void assignCTFfromParameters(double *p, CTFDescription &ctfmodel, int ia, int l, int modelSimplification)
Matrix1D< double > * adjust_params
void readBasicParams(XmippProgram *program)
Read parameters.
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
void estimate_envelope_parameters()
#define ASSIGN_PARAM_CTF(index, paramName)
#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 write(const FileName &fn)
MultidimArray< double > y_digfreq
void readBasicParams(XmippProgram *program)
Read parameters.
MultidimArray< double > w_digfreq_r_iN
void rangeAdjust(T minF, T maxF)
double downsampleFactor
Downsample performed.
void show()
Show parameters.
double cU
Gaussian center for U.
void inv(Matrix2D< T > &result) const
double evaluateIceness(const MultidimArray< double > &enhanced_ctftomodel, double Tm)
void produceSideInfo()
Produce side information.
MultidimArray< double > psd_exp_enhanced_radial_derivative
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
#define DEBUG_CLOSE_TEXTFILE
void FFT_idx2digfreq(T &v, const Matrix1D< int > &idx, Matrix1D< double > &freq)
FileName fn_psd
CTF filename.
MultidimArray< double > w_digfreq_r
double DeltaF
Longitudinal mechanical displacement (ansgtrom). Typical value 100.
static void defineParams(XmippProgram *program)
Define parameters in the command line.
double CTF_fitness_object(double *p)
#define FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
double max_freq
Maximum frequency to adjust.
static constexpr double penalty
double DeltafU
Global gain. By default, 1.
bool enable_CTF
Enable CTF part.
void CenterFFT(MultidimArray< T > &v, bool forward)
Image< double > enhanced_ctftomodel_fullsize
CTF amplitude to model.
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
int ctfmodelSize
X dimension of particle projections (-1=the same as the psd)
MultidimArray< double > w_contfreq
MultidimArray< double > psd_theo_radial
constexpr int ALL_CTF_PARAMETERS
MultidimArray< double > psd_exp_radial
PSD data.
void center_optimization_focus(bool adjust_freq, bool adjust_th, double margin)
void generateModelSoFar(Image< double > &I, bool apply_log)
void log(Image< double > &op)
constexpr int FIRST_ENVELOPE_PARAMETER
void estimate_defoci_Zernike()
void compute_central_region(double &w1, double &w2, double ang)
void contfreq2digfreq(const Matrix1D< double > &contfreq, Matrix1D< double > &digfreq, double pixel_size)
CTFDescription1D ctfmodel_defoci
Incorrect argument received.
bool noDefocusEstimate
No defocus estimate.
#define XMIPP_EQUAL_ACCURACY
void resize(size_t Xdim, bool copy=true)
constexpr int CTF_PARAMETERS
double ispr
Objective lens stability (deltaI/I) (ppm). Typical value 1.
void max(Image< double > &op1, const Image< double > &op2)
double Tm
Sampling rate (A/pixel)
bool refineAmplitudeContrast
Refine amplitude contrast.
Error related to numerical calculation.
constexpr int FIRST_DEFOCUS_PARAMETER
void log10(Image< double > &op)
CTFDescription1D current_ctfmodel
constexpr int PARAMETRIC_CTF_PARAMETERS
double Cs
Spherical aberration (in milimeters). Typical value 5.6.
void mod(const MultidimArray< T > &x, MultidimArray< T > &m, double y)
double DeltaR
Transversal mechanical displacement (ansgtrom). Typical value 3.
double sigmaV
Gaussian width V.
bool show_optimization
Show convergence values.
MultidimArray< double > psd_theo_radial_derivative
void generate_model_halfplane(int Ydim, int Xdim, MultidimArray< double > &model)
void defineParams()
Define Parameters.
MultidimArray< double > x_digfreq
double K
Global gain. By default, 1.
double gaussian_angle
Gaussian angle.
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
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.
void produceSideInfo()
Produce side information.
#define MATRIX1D_ARRAY(v)
int modelSimplification
Model simplification.
FileName withoutExtension() const
constexpr int BACKGROUND_CTF_PARAMETERS
MultidimArray< double > mask_between_zeroes
double Ca
Chromatic aberration (in milimeters). Typical value 2.
void lookFor(int n, const Matrix1D< double > &u, Matrix1D< double > &freq, int iwhat=0)
double min_freq
Minimum frequency to adjust.
void forcePhysicalMeaning()
void estimate_background_gauss_parameters()
#define ASSIGN_CTF_PARAM(index, paramName)
Image< double > enhanced_ctftomodel
CTF amplitude to model.
double alpha
Convergence cone semiangle (in mrad). Typical value 0.5.
String formatString(const char *format,...)
int textToInteger(const char *str)
constexpr int SQRT_CTF_PARAMETERS
double y0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
MultidimArray< double > psd_exp_radial_derivative
MultidimArray< double > x_contfreq
Frequencies in axes.
MultidimArray< double > psd_exp_enhanced_radial
double Q0
Factor for the importance of the Amplitude contrast.
double x0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
void defineParams()
Define Parameters.
double kV
Accelerating Voltage (in KiloVolts)
void initZeros(const MultidimArray< T1 > &op)
MultidimArray< double > mask
Masks.
#define DEBUG_OPEN_TEXTFILE(fnRoot)
bool enable_CTFnoise
Enable CTFnoise part.
double xF
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
T * adaptForNumericalRecipes() const
CTFDescription initial_ctfmodel
CTF model.
static void defineBasicParams(XmippProgram *program)
Define basic parameters.
ProgCTFEstimateFromPSD * global_prm
double ROUT_Adjust_CTF(ProgCTFEstimateFromPSD &prm, CTFDescription &output_ctfmodel, bool standalone)
void estimate_background_gauss_parameters2()
double yF
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
double fitness(double *p)
#define DEBUG_TEXTFILE(str)
#define DEBUG_MODEL_TEXTFILE
MultidimArray< double > y_contfreq
double CTF_fitness(double *, void *)
void digfreq2contfreq(const Matrix1D< double > &digfreq, Matrix1D< double > &contfreq, double pixel_size)
double cV
Gaussian center for V.