54 #ifdef DEBUG_WITH_TEXTFILES 55 std::ofstream fhDebug;
56 #define DEBUG_OPEN_TEXTFILE(fnRoot) fhDebug.open((fnRoot+"_debug.txt").c_str()); 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_TEXTFILE(str); 62 #define DEBUG_MODEL_TEXTFILE 73 #define ASSIGN_CTF_PARAM(index, paramName) if (ia <= index && l > 0) { ctf1Dmodel.paramName = p[index]; --l; } 112 #define ASSIGN_PARAM_CTF(index, paramName) if (ia <= index && l > 0) { p[index] = ctf1Dmodel.paramName; --l; } 149 #define COPY_ctfmodel_TO_CURRENT_GUESS \ 150 global_prm->assignParametersFromCTF_fast(global_prm->current_ctfmodel, \ 151 MATRIX1D_ARRAY(*global_prm->adjust_params),0,ALL_CTF_PARAMETERS); 159 initial_ctfmodel.enable_CTF = initial_ctfmodel.enable_CTFnoise =
true;
160 initial_ctfmodel.readParams(program);
161 initial_ctfmodel2D.readParams(program);
163 if (initial_ctfmodel.Defocus>100e3)
165 Tm = initial_ctfmodel.Tm;
170 fn_psd = getParam(
"--psd");
171 readBasicParams(
this);
182 defineBasicParams(
this);
191 current_ctfmodel.clear();
192 ctfmodel_defoci.clear();
196 ctftomodel.read(fn_psd);
200 current_ctfmodel.precomputeValues(x_contfreq);
208 assignCTFfromParameters_fast(
MATRIX1D_ARRAY(*adjust_params), current_ctfmodel,
210 current_ctfmodel.produceSideInfo();
222 current_ctfmodel.precomputeValues(
XX(freq));
224 I(
i) = current_ctfmodel.getValueNoiseAt();
225 else if (action == 2)
227 double E = current_ctfmodel.getValueDampingAt();
228 I(
i) = current_ctfmodel.getValueNoiseAt() + E * E;
231 else if (action >= 3 && action <= 6)
233 double ctf = current_ctfmodel.getValuePureAt();
234 I(
i) = current_ctfmodel.getValueNoiseAt() + ctf * ctf;
238 double ctf = current_ctfmodel.getValuePureAt();
249 std::ofstream plot_radial;
251 generateModelSoFar_fast(save,
false);
252 if (!generate_profiles)
254 plot_radial.open((fn_root +
"_radial.txt").c_str());
258 "save_intermediate_results::Cannot open plot file for writing\n");
264 radial_CTFmodel_avg.
initZeros(psd_exp_enhanced_radial);
270 double model2 = save(
i);
272 int r = w_digfreq_r(
i);
273 radial_CTFmodel_avg(r) = model2;
275 plot_radial << w_digfreq(r, 0) <<
" " << w_contfreq(r, 0)
276 <<
" " << radial_CTFmodel_avg(r) <<
" " 277 << psd_exp_enhanced_radial(r) <<
" " << psd_exp_radial(r)
278 <<
" " <<
log10(radial_CTFmodel_avg(r)) <<
" " 302 std::cout <<
"Input vector:";
304 std::cout << p[
i] <<
" ";
305 std::cout << std::endl;
314 std::cout <<
"Input vector:";
316 std::cout << p[
i] <<
" ";
317 std::cout << std::endl;
325 std::cout <<
"Input vector:";
327 std::cout << p[
i] <<
" ";
328 std::cout << std::endl;
334 psd_theo_radial_derivative.initZeros();
337 std::cout <<
"Input vector:";
339 std::cout << p[
i] <<
" ";
340 std::cout << std::endl;
344 assignCTFfromParameters_fast(p - 0 + 1, current_ctfmodel, 0,
346 psd_theo_radial.initZeros();
349 std::cout <<
"Input vector:";
351 std::cout << p[
i] <<
" ";
352 std::cout << std::endl;
358 assignCTFfromParameters_fast(p - 0 + 1, current_ctfmodel, 0,
360 psd_theo_radial.initZeros();
363 std::cout <<
"Input vector:";
365 std::cout << p[
i] <<
" ";
366 std::cout << std::endl;
371 current_ctfmodel.produceSideInfo();
373 std::cout <<
"Model:\n" << current_ctfmodel << std::endl;
374 if (!current_ctfmodel.hasPhysicalMeaning())
377 std::cout <<
"Does not have physical meaning\n";
378 return heavy_penalization;
381 && (fabs((current_ctfmodel.Defocus - ctfmodel_defoci.Defocus)
382 / ctfmodel_defoci.Defocus) > 0.2) && !noDefocusEstimate)
385 std::cout <<
"Too large defocus\n";
386 return heavy_penalization;
388 if (initial_ctfmodel.Defocus != 0 && action >= 3 && !selfEstimation && !noDefocusEstimate)
392 if (fabs(initial_ctfmodel.Defocus - current_ctfmodel.Defocus) > defocus_range)
396 std::cout <<
"Too far from hint: Initial (" << initial_ctfmodel.Defocus <<
")" 397 <<
" current guess (" << current_ctfmodel.Defocus <<
") max allowed difference: " 398 << defocus_range << std::endl;
400 return heavy_penalization;
405 if ((initial_ctfmodel.phase_shift != 0.0 || initial_ctfmodel.phase_shift != 1.57079) && action >= 5)
407 if (fabs(initial_ctfmodel.phase_shift - current_ctfmodel.phase_shift) > 0.26)
409 return heavy_penalization;
414 int N = 0, Ncorr = 0;
415 double enhanced_avg = 0;
416 double model_avg = 0;
417 double enhanced_model = 0;
418 double enhanced2 = 0;
420 double lowerLimit = 1.1 * min_freq_psd;
421 double upperLimit = 0.9 * max_freq_psd;
423 int XdimW=
XSIZE(w_digfreq);
431 current_ctfmodel.precomputeValues(
i);
432 double bg = current_ctfmodel.getValueNoiseAt();
434 double envelope=0, ctf_without_damping, ctf_with_damping=0, current_envelope = 0;
438 double ctf_with_damping2;
444 dist = fabs(ctf2 - bg);
445 if (penalize && bg > ctf2 &&
DIRECT_A1D_ELEM(w_digfreq,
i) > max_gauss_freq)
446 dist *= current_penalty;
449 envelope = current_ctfmodel.getValueDampingAt();
450 ctf2_th = bg + envelope * envelope;
451 dist = fabs(ctf2 - ctf2_th);
452 if (penalize && ctf2_th < ctf2 &&
DIRECT_A1D_ELEM(w_digfreq,
i) > max_gauss_freq)
453 dist *= current_penalty;
460 envelope = current_ctfmodel.getValueDampingAt();
461 ctf_without_damping = current_ctfmodel.getValuePureWithoutDampingAt();
463 ctf_with_damping = envelope * ctf_without_damping;
464 ctf2_th = bg + ctf_with_damping * ctf_with_damping;
474 ctf_with_damping2 = ctf_with_damping * ctf_with_damping;
475 enhanced_model += enhanced_ctf * ctf_with_damping2;
476 enhanced2 += enhanced_ctf * enhanced_ctf;
477 model2 += ctf_with_damping2 * ctf_with_damping2;
478 enhanced_avg += enhanced_ctf;
479 model_avg += ctf_with_damping2;
484 A1D_ELEM(psd_theo_radial,r) = ctf2_th;
489 dist = fabs(ctf2 - ctf2_th) / (envelope * envelope);
491 dist = fabs(ctf2 - ctf2_th);
510 { retval = distsum/N ;
513 retval = heavy_penalization;
515 std::cout <<
"Fitness1=" << retval << std::endl;
516 if ( (((action >= 3) && (action <= 4)) || (action == 7))
517 && (Ncorr > 0) && (enhanced_weight != 0) )
521 enhanced_avg /= Ncorr;
522 double correlation_coeff = enhanced_model/Ncorr - model_avg * enhanced_avg;
523 double sigma1 =
sqrt(fabs(enhanced2/Ncorr - enhanced_avg * enhanced_avg));
524 double sigma2 =
sqrt(fabs(model2/Ncorr - model_avg * model_avg));
525 double maxSigma =
std::max(sigma1, sigma2);
527 || (fabs(sigma1 - sigma2) / maxSigma > 0.9 && action>=5))
529 retval = heavy_penalization;
531 std::cout <<
"Fitness2=" << heavy_penalization <<
" sigma1=" << sigma1 <<
" sigma2=" << sigma2 << std::endl;
535 correlation_coeff /= sigma1 * sigma2;
537 corr13 = correlation_coeff;
539 retval -= enhanced_weight * correlation_coeff;
542 std::cout <<
"model_avg=" << model_avg << std::endl;
543 std::cout <<
"enhanced_avg=" << enhanced_avg << std::endl;
544 std::cout <<
"enhanced_model=" << enhanced_model / Ncorr
546 std::cout <<
"sigma1=" << sigma1 << std::endl;
547 std::cout <<
"sigma2=" << sigma2 << std::endl;
548 std::cout <<
"Fitness2=" 549 << -(enhanced_weight * correlation_coeff)
550 <<
" (" << correlation_coeff <<
")" << std::endl;
555 if (action==3 || evaluation_reduction==1)
559 psd_theo_radial_derivative.initZeros();
560 double lowerlimt=1.1*min_freq;
561 double upperlimit=0.9*max_freq;
579 A1D_ELEM(psd_theo_radial_derivative,
i)=diff;
580 maxDiff=
std::max(maxDiff,fabs(diff));
585 double corrRadialDerivative=0,mux=0, muy=0, Ncorr=0, sigmax=0, sigmay=0;
586 double iMaxDiff=1.0/maxDiff;
590 A1D_ELEM(psd_theo_radial_derivative,
i)*=iMaxDiff;
591 double x=
A1D_ELEM(psd_exp_enhanced_radial_derivative,
i);
592 double y=
A1D_ELEM(psd_theo_radial_derivative,
i);
593 corrRadialDerivative+=x*
y;
603 double iNcorr=1.0/Ncorr;
604 corrRadialDerivative*=iNcorr;
607 sigmax=
sqrt(fabs(sigmax*iNcorr-mux*mux));
608 sigmay=
sqrt(fabs(sigmay*iNcorr-muy*muy));
609 corrRadialDerivative=(corrRadialDerivative-mux*muy)/(sigmax*sigmay);
612 retval-=corrRadialDerivative;
615 std::cout <<
"Fitness3=" << -corrRadialDerivative << std::endl;
618 psd_exp_enhanced_radial.write(
"PPPexpRadial_fast.txt");
619 psd_theo_radial.write(
"PPPtheoRadial.txt");
620 psd_exp_enhanced_radial_derivative.write(
"PPPexpRadialDerivative_fast.txt");
621 psd_theo_radial_derivative.write(
"PPPtheoRadialDerivative.txt");
633 return prm->CTF_fitness_object_fast(p);
639 if (show_optimization)
640 std::cout <<
"Freq frame before focusing=" << min_freq_psd <<
"," 641 << max_freq_psd << std::endl <<
"Value_th before focusing=" 642 << value_th << std::endl;
644 double w1 = min_freq_psd, w2 = max_freq_psd;
650 generateModelSoFar_fast(save,
false);
654 double w = w_digfreq(
i);
655 if (w >= w1 && w <= w2)
659 value_th =
XMIPP_MIN(value_th, max_val * margin);
661 value_th = max_val * margin;
665 if (show_optimization)
666 std::cout <<
"Freq frame after focusing=" << min_freq_psd <<
"," 667 << max_freq_psd << std::endl <<
"Value_th after focusing=" 668 << value_th << std::endl;
674 if (show_optimization)
675 std::cout <<
"Computing first sqrt background ...\n";
679 double base_line = 0;
682 if (w_digfreq(
i) > 0.4)
685 base_line += psd_exp_radial(
i);
690 current_ctfmodel.base_line = base_line / N;
704 double weight = 1 + max_freq_psd - w_digfreq(
i);
707 current_ctfmodel.precomputeValues(x_contfreq(
i));
708 double explained = current_ctfmodel.getValueNoiseAt();
709 double unexplained = psd_exp_radial(
i) - explained;
710 if (unexplained <= 0)
712 unexplained =
log(unexplained);
714 double X = -
sqrt(w_contfreq(
i));
715 A(0, 0) += weight * X * X;
716 A(0, 1) += weight * X;
717 A(1, 1) += weight * 1;
718 b(0) += X * weight * unexplained;
719 b(1) += weight * unexplained;
725 current_ctfmodel.sq =
b(0);
726 current_ctfmodel.sqrt_K = exp(
b(1));
729 if (show_optimization)
731 std::cout <<
"First SQRT Fit:\n" << current_ctfmodel << std::endl;
732 saveIntermediateResults_fast(
"step01a_first_sqrt_fit_fast",
true);
742 if (show_optimization)
743 std::cout <<
"Looking for best fitting sqrt ...\n";
751 if (show_optimization)
752 std::cout <<
"Penalizing best fitting sqrt ...\n";
757 for (
int i = 1;
i <= imax;
i++)
759 if (show_optimization)
760 std::cout <<
" Iteration " <<
i <<
" penalty=" 761 << current_penalty << std::endl;
764 steps, show_optimization);
765 current_penalty *= 2;
770 current_ctfmodel.forcePhysicalMeaning();
773 if (show_optimization)
775 std::cout <<
"Best penalized SQRT Fit:\n" << current_ctfmodel
777 saveIntermediateResults_fast(
"step01b_best_penalized_sqrt_fit_fast",
true);
780 center_optimization_focus_fast(
true, 1.5);
789 if (show_optimization)
790 std::cout <<
"Computing first background Gaussian parameters ...\n";
796 double w_max_gauss = 0.25;
802 double w = w_digfreq(
i);
809 current_ctfmodel.precomputeValues(x_contfreq(
i));
810 radial_CTFmodel_avg(r) += current_ctfmodel.getValueNoiseAt();
811 radial_CTFampl_avg(r) += psd_exp_radial(
i);
816 double error2_avg = 0;
822 if (radial_N(
i) == 0)
824 error(
i) = (radial_CTFampl_avg(
i) - radial_CTFmodel_avg(
i))
834 std::cout <<
"Error2 avg=" << error2_avg << std::endl;
838 bool first =
true, OK_to_proceed =
false;
839 double error2_min = 0, wmin=0;
842 if (radial_N(
i) == 0)
845 double w = w_digfreq(
i);
847 if (
error(
i) < 0 && first){
859 if (first && error2 > 0.15 * error2_avg)
860 OK_to_proceed =
true;
866 if (!first && error2 > 1.69 * error2_min)
868 if (first && OK_to_proceed)
874 if (!first && error2 < error2_min)
880 std::cout << w <<
" " << error2 <<
" " << wmin <<
" " << std::endl;
886 max_gauss_freq = wmin;
889 std::cout <<
"Freq of the minimum error: " << wmin <<
" " << fmin << std::endl;
894 double error2_max = 0, wmax=0,
fmax;
897 if (radial_N(
i) == 0)
899 double w = w_digfreq(
i);
903 if (
error(
i) < 0 && first)
914 if (error2 > error2_max)
920 std::cout << w <<
" " << error2 <<
" " << wmax << std::endl;
924 fmax = current_ctfmodel.Gc1 = wmax / Tm;
927 std::cout <<
"Freq of the maximum error: " << wmax <<
" " <<
fmax << std::endl;
943 if (w_digfreq(
i) > wmin)
946 double fmod = w_contfreq(
i);
949 double weight = 1 + max_freq_psd - w_digfreq(
i);
952 current_ctfmodel.precomputeValues(x_contfreq(
i));
953 double explained = current_ctfmodel.getValueNoiseAt();
954 double unexplained = psd_exp_radial(
i) - explained;
955 if (unexplained <= 0)
957 unexplained =
log(unexplained);
958 double F = -(fmod -
fmax) * (fmod -
fmax);
960 A(0, 0) += weight * 1;
961 A(0, 1) += weight * F;
962 A(1, 1) += weight * F * F;
963 b(0) += weight * unexplained;
964 b(1) += F * weight * unexplained;
969 if ( (A(0, 0)== 0) && (A(1, 0)== 0) && (A(1, 1)== 0))
971 std::cout <<
"Matrix A is zeros" << std::endl;
977 current_ctfmodel.sigma1 =
XMIPP_MIN(fabs(
b(1)), 95e3);
979 current_ctfmodel.gaussian_K = exp(
b(0));
981 current_ctfmodel.forcePhysicalMeaning();
984 if (show_optimization)
986 std::cout <<
"First Background Fit:\n" << current_ctfmodel << std::endl;
987 saveIntermediateResults_fast(
"step01c_first_background_fit_fast",
true);
989 center_optimization_focus_fast(
true, 1.5);
998 if (show_optimization)
999 std::cout <<
"Looking for best fitting envelope ...\n";
1002 current_ctfmodel.Ca = initial_ctfmodel.Ca;
1003 current_ctfmodel.K = 1.0;
1004 current_ctfmodel.espr = 0.0;
1005 current_ctfmodel.ispr = 0.0;
1006 current_ctfmodel.alpha = 0.0;
1007 current_ctfmodel.DeltaF = 0.0;
1008 current_ctfmodel.DeltaR = 0.0;
1009 current_ctfmodel.Q0 = initial_ctfmodel.Q0;
1010 current_ctfmodel.envR0 = 0.0;
1011 current_ctfmodel.envR1 = 0.0;
1025 if (modelSimplification >= 1)
1032 current_ctfmodel.forcePhysicalMeaning();
1035 if (show_optimization)
1037 std::cout <<
"Best envelope Fit:\n" << current_ctfmodel << std::endl;
1038 saveIntermediateResults_fast(
"step02a_best_envelope_fit_fast",
true);
1042 if (show_optimization)
1043 std::cout <<
"Penalizing best fitting envelope ...\n";
1045 current_penalty = 2;
1047 for (
int i = 1;
i <= imax;
i++)
1049 if (show_optimization)
1050 std::cout <<
" Iteration " <<
i <<
" penalty=" 1051 << current_penalty << std::endl;
1054 steps, show_optimization);
1055 current_penalty *= 2;
1060 current_ctfmodel.forcePhysicalMeaning();
1063 if (show_optimization)
1065 std::cout <<
"Best envelope Fit:\n" << current_ctfmodel << std::endl;
1066 saveIntermediateResults_fast(
"step02b_best_penalized_envelope_fit_fast",
true);
1085 (*adjust_params)(0) = initial_ctfmodel.Defocus;
1086 (*adjust_params)(2) = current_ctfmodel.K;
1089 fitness, iter, steps,
false);
1107 generateModelSoFar_fast(background,
false);
1112 std::cout <<
"background\n" << background << std::endl;
1113 std::cout <<
"--------------------------------------" << std::endl;
1115 std::cout <<
"psd_exp_radial\n" << psd_exp_radial << std::endl;
1116 std::cout <<
"--------------------------------------" << std::endl;
1121 if(w_digfreq(
i)>min_freq && w_digfreq(
i)<max_freq)
1122 psd_background(
i)= background(
i)-psd_exp_radial(
i);
1136 psd_background_filter.
initZeros(psd_background);
1138 matlab_filter(B_coeff,A_coeff,psd_background,psd_background_filter,aux_filter);
1139 psd_background = psd_background_filter;
1143 std::cout <<
"psdbackground\n" << psd_background << std::endl;
1144 std::cout <<
"--------------------------------------" << std::endl;
1146 psd_exp_radial2.
initZeros(psd_exp_radial);
1148 double deltaW=1.0/(2*
XSIZE(w_digfreq)*current_ctfmodel.Tm);
1149 double deltaW2=1.0/(
XSIZE(w_digfreq)*pow(2*current_ctfmodel.Tm,2));
1152 double w2=
i*deltaW2;
1154 double widx=w/deltaW;
1155 size_t lowerIdx=
floor(widx);
1156 double weight=widx-
floor(widx);
1157 if(
i <
XSIZE(psd_exp_radial2)-1)
1159 double value =(1-weight)*
A1D_ELEM(psd_background,lowerIdx)
1160 +weight*
A1D_ELEM(psd_background,lowerIdx+1);
1162 A1D_ELEM(psd_exp_radial2,
i) = value*value;
1166 std::cout <<
"psd_exp_radial2\n" << psd_exp_radial2 << std::endl;
1167 std::cout <<
"--------------------------------------" << std::endl;
1172 const double minDefocus=2000;
1173 int startIndex = ceil(
std::max((
double)7,minDefocus*current_ctfmodel.lambda/(2*pow(2*current_ctfmodel.Tm,2))));
1175 std::cout <<
"Start index=" << minDefocus*current_ctfmodel.lambda/(2*pow(2*current_ctfmodel.Tm,2)) << std::endl;
1176 std::cout <<
"--------------------------------------" << std::endl;
1178 if (current_ctfmodel.VPP_radius != 0.0)
1183 amplitud.push_back(
abs(psd_fft[
i]));
1185 std::cout <<
abs(psd_fft[i]) << std::endl;
1190 std::cout <<
"--------------------------------------" << std::endl;
1195 double totalSum = std::accumulate(amplitud.begin(), amplitud.end(), 0.0);
1197 double amplitudMean = totalSum / amplitud.size();
1199 for(
double i : amplitud)
1201 double diff=amplitud[
i]-amplitudMean;
1204 double amplitudSD =
sqrt(sdSum/amplitud.size());
1205 double differenceSD = 3*amplitudSD;
1206 for(
double i : amplitud)
1208 if (amplitud[
i]>amplitudMean+differenceSD){
1209 amplitud[
i] = amplitudMean+differenceSD;
1214 double maxValue=-1e38;
1217 std::cout <<
"Looking for maximum ...\n";
1219 for (
size_t i=1;
i<amplitud.size()-1;
i++)
1222 std::cout <<
"i=" <<
i <<
" amplitude i= " << amplitud[
i] << std::endl;
1224 if (amplitud[
i]>maxValue && amplitud[
i]>=amplitud[
i+1])
1226 maxValue=amplitud[
i];
1229 std::cout <<
"New maximum " <<
i <<
" maxValue=" << maxValue << std::endl;
1236 current_ctfmodel.Defocus = (
floor(finalIndex+startIndex+1)*pow(2*current_ctfmodel.Tm,2))/
1237 current_ctfmodel.lambda;
1240 std::cout <<
"maxValue: " << maxValue << std::endl;
1241 std::cout <<
"finalIndex: " << finalIndex << std::endl;
1242 std::cout <<
"defocus: " << current_ctfmodel.Defocus << std::endl;
1246 current_ctfmodel.forcePhysicalMeaning();
1248 ctfmodel_defoci = current_ctfmodel;
1250 if (show_optimization)
1252 std::cout <<
"First defocus Fit:\n" << ctfmodel_defoci << std::endl;
1253 saveIntermediateResults_fast(
"step03a_first_defocus_fit_fast",
true);
1264 if (show_optimization)
1265 std::cout <<
"Computing first background Gaussian2 parameters ...\n";
1271 double w_max_gauss = 0.25;
1276 double w = w_digfreq(
i);
1277 if (w > w_max_gauss)
1282 current_ctfmodel.precomputeValues(f_x);
1283 double bg = current_ctfmodel.getValueNoiseAt();
1284 double envelope = current_ctfmodel.getValueDampingAt();
1285 double ctf_without_damping =
1286 current_ctfmodel.getValuePureWithoutDampingAt();
1287 double ctf_with_damping = envelope * ctf_without_damping;
1288 double ctf2_th = bg + ctf_with_damping * ctf_with_damping;
1289 radial_CTFmodel_avg(r) += ctf2_th;
1290 radial_CTFampl_avg(r) += psd_exp_radial(
i);
1299 if (radial_N(
i) == 0)
1301 error(
i) = (radial_CTFampl_avg(
i) - radial_CTFmodel_avg(
i))/ radial_N(
i);
1304 std::cout <<
"Error:\n" << error << std::endl;
1311 double error_max = 0, wmax=0,
fmax;
1314 if (radial_N(
i) == 0)
1316 double w = w_digfreq(
i);
1319 if (
error(
i) < error_max)
1325 fmax = current_ctfmodel.Gc2 = wmax / Tm;
1328 std::cout <<
"Freq of the maximum error: " << wmax <<
" " <<
fmax << std::endl;
1342 if (w_digfreq(
i) > wmin)
1344 double fmod = w_contfreq(
i);
1348 u = x_contfreq(
i) / fmod;
1349 current_ctfmodel.lookFor(1,
u, fzero, 0);
1350 if (fmod > fzero.
module())
1354 double weight = 1 + max_freq_psd - w_digfreq(
i);
1357 current_ctfmodel.precomputeValues(f_x);
1358 double bg = current_ctfmodel.getValueNoiseAt();
1359 double envelope = current_ctfmodel.getValueDampingAt();
1360 double ctf_without_damping =
1361 current_ctfmodel.getValuePureWithoutDampingAt();
1362 double ctf_with_damping = envelope * ctf_without_damping;
1363 double ctf2_th = bg + ctf_with_damping * ctf_with_damping;
1364 double explained = ctf2_th;
1365 double unexplained = explained - psd_exp_radial(
i);
1366 if (unexplained <= 0)
1368 unexplained =
log(unexplained);
1369 double F = -(fmod -
fmax) * (fmod -
fmax);
1370 A(0, 0) += weight * 1;
1371 A(0, 1) += weight * F;
1372 A(1, 1) += weight * F * F;
1373 b(0) += weight * unexplained;
1374 b(1) += F * weight * unexplained;
1386 current_ctfmodel.sigma2 =
XMIPP_MIN(fabs(
b(1)), 95e3);
1388 current_ctfmodel.gaussian_K2 = exp(
b(0));
1392 current_ctfmodel.sigma2 = 0;
1393 current_ctfmodel.gaussian_K2 = 0;
1398 current_ctfmodel.sigma2 = 0;
1399 current_ctfmodel.gaussian_K2 = 0;
1403 current_ctfmodel.forcePhysicalMeaning();
1406 if (show_optimization)
1408 std::cout <<
"First Background Gaussian 2 Fit:\n" << current_ctfmodel
1410 saveIntermediateResults_fast(
"step04a_first_background2_fit_fast",
true);
1478 std::cout <<
"Best background Fit:\n" << prm.
current_ctfmodel << std::endl;
1564 prm2D->noDefocusEstimate=
true;
1574 if (prm2D->initial_ctfmodel.Q0 != 0)
1576 if (prm2D->modelSimplification >= 3)
1578 if (prm2D->modelSimplification >= 2)
1580 if (prm2D->modelSimplification >= 1)
1582 if(prm2D->initial_ctfmodel.VPP_radius == 0)
1592 prm2D, 0.01, fitness, iter, steps, prm2D->show_optimization);
1594 prm2D->current_ctfmodel.forcePhysicalMeaning();
1597 if (!prm.
noDefocusEstimate && prm2D->current_ctfmodel.DeltafV > prm2D->current_ctfmodel.DeltafU)
1600 temp = prm2D->current_ctfmodel.DeltafU;
1601 prm2D->current_ctfmodel.DeltafU = prm2D->current_ctfmodel.DeltafV;
1602 prm2D->current_ctfmodel.DeltafV = temp;
1603 prm2D->current_ctfmodel.azimuthal_angle -= 90;
1606 if (prm2D->show_optimization)
1608 std::cout <<
"Best fit with 2D parameters:\n" << prm2D->current_ctfmodel << std::endl;
1609 prm2D->saveIntermediateResults(
"step05b_estimate_2D_parameters",
true);
1616 if (prm2D->fn_psd !=
"")
1619 prm2D->mask_between_zeroes.initZeros(prm2D->mask);
1626 prm2D->current_ctfmodel.lookFor(3,
u, z3, 0);
1629 prm2D->current_ctfmodel.lookFor(1,
u, z1, 0);
1636 prm2D->current_ctfmodel.lookFor(3,
u, z3, 0);
1637 prm2D->current_ctfmodel.lookFor(1,
u, z1, 0);
1638 double z1m = z1.module();
1639 double z3m = z3.
module();
1643 if (wn<=z3m && wn>=z1m)
1647 CTF_fitness(prm2D->adjust_params->adaptForNumericalRecipes(), prm2D);
1651 FileName fn_rootMODEL = fn_rootCTFPARAM;
1652 size_t atPosition=fn_rootCTFPARAM.find(
'@');
1654 if (atPosition!=std::string::npos)
1657 fn_rootCTFPARAM.substr(atPosition+1).c_str());
1659 fn_rootCTFPARAM.substr(atPosition+1).c_str());
1662 fn_rootCTFPARAM=(
String)
"fullMicrograph@"+fn_rootCTFPARAM;
1664 prm2D->saveIntermediateResults(fn_rootMODEL,
false);
1665 prm2D->current_ctfmodel.Tm /= prm2D->downsampleFactor;
1666 prm2D->current_ctfmodel.azimuthal_angle = std::fmod(prm2D->current_ctfmodel.azimuthal_angle,360.);
1667 prm2D->current_ctfmodel.phase_shift = (prm2D->current_ctfmodel.phase_shift*180)/3.14;
1668 if(prm2D->current_ctfmodel.phase_shift<0.0)
1669 prm2D->current_ctfmodel.phase_shift = 0.0;
1671 prm2D->current_ctfmodel.write(fn_rootCTFPARAM +
".ctfparam_tmp");
1673 MD.
read(fn_rootCTFPARAM +
".ctfparam_tmp");
1684 fn_rootCTFPARAM = fn_rootCTFPARAM +
".ctfparam_tmp";
1686 fn_rootCTFPARAM.deleteFile();
1688 output_ctfmodel = prm2D->current_ctfmodel;
#define DEBUG_TEXTFILE(str)
#define VECTOR_R2(v, x, y)
MultidimArray< double > * f
Image< double > ctftomodel
CTF amplitude to model.
__host__ __device__ float2 floor(const float2 v)
FileName removeLastExtension() const
double heavy_penalization
void center_optimization_focus_fast(bool adjust_th, double margin)
#define REPORT_ERROR(nerr, ErrormMsg)
void estimate_background_gauss_parameters_fast()
void estimate_envelope_parameters_fast()
CTFDescription1D initial_ctfmodel
constexpr int FIRST_SQRT_PARAMETER
void sqrt(Image< double > &op)
constexpr int DEFOCUS_PARAMETERS
Couldn't write to file.
Matrix1D< double > * adjust_params
void readBasicParams(XmippProgram *program)
Read parameters.
double DeltafV
Defocus in V (in Angstroms). Negative values are underfocused.
void defineParams()
Define Parameters.
constexpr int CTF_PARAMETERS
void readBasicParams(XmippProgram *program)
Read parameters.
void estimate_background_sqrt_parameters_fast()
void show()
Show parameters.
void inv(Matrix2D< T > &result) const
double evaluateIceness(const MultidimArray< double > &enhanced_ctftomodel, double Tm)
void abs(Image< double > &op)
#define DEBUG_OPEN_TEXTFILE(fnRoot)
#define COPY_ctfmodel_TO_CURRENT_GUESS
void FFT_idx2digfreq(T &v, const Matrix1D< int > &idx, Matrix1D< double > &freq)
FileName fn_psd
CTF filename.
constexpr int ENVELOPE_PARAMETERS
static void defineParams(XmippProgram *program)
Define parameters in the command line.
constexpr int ALL_CTF_PARAMETERS2D
double max_freq
Maximum frequency to adjust.
static constexpr double penalty
double DeltafU
Global gain. By default, 1.
bool enable_CTF
Enable CTF part.
double azimuthal_angle
Azimuthal angle (between X and U) in degrees.
void readParams()
Read parameters.
#define DIRECT_A1D_ELEM(v, i)
#define ASSIGN_PARAM_CTF(index, paramName)
void produceSideInfo()
Produce side information.
void log(Image< double > &op)
constexpr int FIRST_ENVELOPE_PARAMETER
double CTF_fitness_fast(double *, void *)
double ROUT_Adjust_CTFFast(ProgCTFEstimateFromPSDFast &prm, CTFDescription1D &output_ctfmodel, bool standalone)
Incorrect argument received.
bool noDefocusEstimate
No defocus estimate.
#define XMIPP_EQUAL_ACCURACY
void resize(size_t Xdim, bool copy=true)
constexpr int ALL_CTF_PARAMETERS
double CTF_fitness(double *p, void *vprm)
void max(Image< double > &op1, const Image< double > &op2)
#define FOR_ALL_DIRECT_ELEMENTS_IN_MULTIDIMARRAY(v)
double Tm
Sampling rate (A/pixel)
bool refineAmplitudeContrast
Refine amplitude contrast.
Error related to numerical calculation.
#define DIRECT_MULTIDIM_ELEM(v, n)
constexpr int FIRST_DEFOCUS_PARAMETER
void log10(Image< double > &op)
CTFDescription1D current_ctfmodel
double Cs
Spherical aberration (in milimeters). Typical value 5.6.
#define DEBUG_MODEL_TEXTFILE
void generateModelSoFar_fast(MultidimArray< double > &I, bool apply_log)
constexpr int PARAMETRIC_CTF_PARAMETERS
void estimate_defoci_fast()
bool show_optimization
Show convergence values.
void forcePhysicalMeaning()
static void defineBasicParams(XmippProgram *program)
Define basic parameters.
void defineParams()
Define Parameters.
double Defocus
Defocus (in Angstroms). Negative values are underfocused.
double CTF_fitness_object_fast(double *p)
CTFDescription initial_ctfmodel2D
void saveIntermediateResults_fast(const FileName &fn_root, bool generate_profiles)
void estimate_background_gauss_parameters2_fast()
void matlab_filter(Matrix1D< double > &B, Matrix1D< double > &A, const MultidimArray< double > &X, MultidimArray< double > &Y, MultidimArray< double > &Z)
#define FOR_ALL_ELEMENTS_IN_ARRAY1D(v)
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
double min_freq
Minimum frequency to adjust.
void assignCTFfromParameters_fast(double *p, CTFDescription1D &ctf1Dmodel, int ia, int l)
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...
void assignParametersFromCTF_fast(const CTFDescription1D &ctfmodel, double *p, int ia, int l)
double Q0
Factor for the importance of the Amplitude contrast.
ProgCTFEstimateFromPSDFast * global_prm
double x0
In the case of local CTF determination x0,xF,y0,yF determines the region where the CTF is determined...
#define ASSIGN_CTF_PARAM(index, paramName)
double kV
Accelerating Voltage (in KiloVolts)
void initZeros(const MultidimArray< T1 > &op)
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...
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)
constexpr int BACKGROUND_CTF_PARAMETERS
void digfreq2contfreq(const Matrix1D< double > &digfreq, Matrix1D< double > &contfreq, double pixel_size)
constexpr int SQRT_CTF_PARAMETERS