45 #define FN_ITER(iter, suffix) formatString("%sextra/iter%03d/%s",fn_root.c_str(), (iter), (suffix)) 47 #define FN_ITER_BASE(iter) FN_ITER(iter, "vol") 48 #define FN_INITIAL_BASE FN_ITER_BASE(0) 49 #define FN_PROJECTIONS_MD FN_EXTRA("projections.xmd") 50 #define FN_PROJECTIONS FN_EXTRA("projections.stk") 52 #define FN_NOISE_VOLBASE FN_EXTRA("noise_vol") 53 #define FN_CREF_VOLBASE FN_EXTRA("cref_vol") 55 #define COMPOSE_VOL_FN(fn, volno, base) fn.compose(base, volno, "vol") 78 addUsageLine(
"Separate structurally heterogeneous data sets into homogeneous classes by a");
79 addUsageLine(
"multi-reference 3D-angular refinement using a maximum-likelihood(ML) target function.");
90 addParamsLine(
" [ --ang <float=10> ] : Angular sampling (degrees) ");
94 addParamsLine(
"--recons <recons_type=wlsART> : Reconstruction method to be used");
98 addParamsLine(
" [ --nostart ] : Start wlsART reconstructions from all-zero volumes ");
100 addParamsLine(
" [ --low_pass <freq=-1> ] : Low-pass filter volume every iteration ");
101 addParamsLine(
" [ --sym_mask <maskfile=\"\"> ] : Local symmetry (only inside mask) ");
102 addParamsLine(
" [ --tilt <min=0> <max=180> ] : Minimum and maximum values for restriction tilt angle search ");
103 addParamsLine(
" [ --perturb ] : Randomly perturb reference projection directions ");
117 bool do_restart =
false;
303 std::cout <<
" -----------------------------------------------------------------" << std::endl;
304 std::cout <<
" | Read more about this program in the following publication: |" << std::endl;
306 std::cout <<
" | Scheres ea. (2007) Structure, 15, 1167-1177 |" << std::endl;
308 std::cout <<
" | Scheres ea. (2007) Nature Methods, 4, 27-29 |" << std::endl;
309 std::cout <<
" | |" << std::endl;
310 std::cout <<
" | *** Please cite it if this program is of use to you! *** |" << std::endl;
311 std::cout <<
" -----------------------------------------------------------------" << std::endl;
312 std::cout <<
"--> Maximum-likelihood multi-reference 3D-refinement" << std::endl;
314 std::cout <<
" Initial reference volume : " <<
fn_ref << std::endl;
317 std::cout <<
" Selfile with references : " <<
fn_ref << std::endl;
318 std::cout <<
" with # of volumes : " <<
Nvols << std::endl;
320 std::cout <<
" Experimental images: : " <<
fn_sel << std::endl;
321 std::cout <<
" Angular sampling rate : " <<
angular << std::endl;
322 std::cout <<
" Symmetry group: : " <<
fn_sym << std::endl;
324 std::cout <<
" Local symmetry mask : " <<
fn_symmask << std::endl;
325 std::cout <<
" Output rootname : " <<
fn_root << std::endl;
326 std::cout <<
" Convergence criterion : " <<
eps << std::endl;
328 std::cout <<
" Low-pass filter : " <<
lowpass << std::endl;
332 std::cout <<
" -> Start wlsART reconstructions from all-zero volumes " << std::endl;
334 std::cout <<
" -> Use fourier-interpolation instead of wlsART for reconstruction" << std::endl;
336 std::cout <<
" -> Perform probabilistic solvent flattening" << std::endl;
337 std::cout <<
" -----------------------------------------------------------------" << std::endl;
372 LOG(
"before ml2d->produceSideInfo");
389 bool converged =
false;
400 bool doProject =
false;
411 std::cout <<
formatString(
"--> 3D-EM volume refinement: iteration %d of %d",
iter,
Niter) << std::endl;
413 LOG(
"==============================================");
432 img().setXmippOrigin();
438 LOG(
"Calling ML2D Expectation");
441 LOG(
"Calling ML2D Maximization");
448 LOG(
"Writing ML2D references");
479 LOG(
"Calling ml2d->endIteration");
485 std::cout << (converged ?
486 "--> Optimization converged!" :
487 "--> Optimization was stopped before convergence was reached!")
492 LOG(
"Calling ml2d->writeOutputFiles, OUT_FINAL");
500 size_t dim,
idum, idumLong;
512 img().initZeros(dim, dim, dim);
528 double rot, tilt,
psi = 0.;
529 size_t nl, nr_dir, id, bar_step;
543 std::cout <<
formatString(
"--> projecting %d volumes x %d projections...",
Nvols, nr_projections) << std::endl;
559 vol().setXmippOrigin();
565 fn_tmp.compose(nr_dir, fn_base);
572 projectVolume(vol(), proj, vol().rowNumber(), vol().colNumber(), rot, tilt, psi);
587 if (
verbose && (nr_dir % bar_step == 0))
596 std::cout <<
" -----------------------------------------------------------------" << std::endl;
603 mdProj.
write(fn_tmp);
622 for (
size_t objId : mdNoise.
ids())
626 img().addNoise(0, 1,
"gaussian");
628 if (Iref[refno].weight() > 1.)
629 img() /=
sqrt(Iref[refno].weight());
630 fn_img.
compose(++refno, fn_noise);
637 mdNoise.
write(fn_noise);
656 arguments +=
" --weight";
657 program->read(arguments);
665 arguments +=
" --WLS";
667 arguments +=
" --sym " +
fn_sym;
669 arguments +=
" -n 10";
671 arguments +=
" -l 0.2";
673 arguments +=
" -k 0.5";
678 arguments +=
" --save_basis";
683 arguments +=
" --start ";
687 program->read(arguments);
740 FileName fn_vol, fn_vol_prev, fn_one;
752 for (
int volno = 1; volno <= (int)
Nvols; ++volno)
754 volno_index =
Nvols *
i + volno - 1;
762 fn_one.
compose(fn_base, volno,
"projections.xmd");
768 reconsProgram->
run();
769 delete reconsProgram;
773 if (
i == 0 &&
rank == 0)
804 double volweight, weight, rot, tilt,
psi = 0.;
813 proj().resize(dim, dim);
814 proj().setXmippOrigin();
821 std::cout <<
"--> calculating 3D-SSNR ..." << std::endl;
827 double inv_dim2 = 1. / (double)(dim * dim);
829 for (
int volno = 1; volno <= (int)
Nvols; ++volno)
838 nvol().setXmippOrigin();
839 vol().setXmippOrigin();
848 bool first_time =
true;
850 for (
size_t objId: mdNoiseOne.
ids())
887 radialAverage(alpha_T, center, alpha_signal, radial_count,
true);
888 radialAverage(alpha_N, center, alpha_noise, radial_count,
true);
889 radialAverage(Msignal, center, input_signal, radial_count,
true);
895 input_signal *= 1./volweight;
901 dAi(input_signal,
i) = (
dAi(alpha_signal,
i) > 0.) ?
dAi(input_signal,
i) *
dAi(alpha_noise,
i) /
dAi(alpha_signal,
i) : 0.;
907 spectral_signal = input_signal;
908 avg_alphaN = alpha_noise;
909 avg_alphaS = alpha_signal;
913 spectral_signal += input_signal;
914 avg_alphaN += alpha_noise;
915 avg_alphaS += alpha_signal;
920 double inv_Nvols = 1. / (double)
Nvols;
921 spectral_signal *= inv_Nvols;
922 avg_alphaN *= inv_Nvols;
923 avg_alphaS *= inv_Nvols;
928 std::ofstream out(fn_tmp.c_str(), std::ios::out);
929 out <<
"# signal 1/alpha alpha-S alpha-N" << std::endl;
932 if (
i > 0 &&
i < dim / 2)
980 for (
size_t volno = 1; volno <=
Nvols; ++volno)
1017 LOG(
" ProgMLRefine3D::postProcessVolumes READING vol");
1019 vol().setXmippOrigin();
1024 LOG(
" ProgMLRefine3D::postProcessVolumes writing ORIGINAL vol");
1030 LOG(
" ProgMLRefine3D::postProcessVolumes applying SYMMETRY");
1031 Vaux().resize(vol());
1037 Vsymmask().setXmippOrigin();
1038 if (Vsymmask().computeMax() > 1. || Vsymmask().computeMin() < 0.)
1058 LOG(
" ProgMLRefine3D::postProcessVolumes applying LOWPASS");
1070 LOG(
" ProgMLRefine3D::postProcessVolumes applying SOLVENT");
1077 segm_prm.
fn_vol = fn_vol;
1078 segm_prm.
fn_mask = fn_vol +
".solv";
1100 if (Vsolv().computeMax() > 1. || Vsolv().computeMin() < 0.)
1106 Vsolv().threshold(
"below", 0.5, 0.);
1117 double nr_vox, max_vox = 0.;
1121 for (
int o = 0; o <= object_no; o++)
1126 Vaux(
k,
i,
j) = Vaux(
k,
i,
j) == o;
1128 nr_vox = Vaux().sum();
1129 if (o != 0 && (nr_vox > max_vox))
1145 dilate3D(Vsolv(), Vaux(), 18, 0, 1);
1146 Vsum() = Vsum() + Vaux();
1149 Vsum() /= (double)(dilate_solvent + 1);
1154 Vsolv() = 1. - Vsolv();
1155 double solvavg = 0., sumsolv = 0.;
1159 sumsolv +=
dAkij(Vsolv(),
k,
i,
j);
1169 LOG(
"Before WRITING vol");
1171 LOG(
"After WRITING vol");
1175 std::cout <<
" -----------------------------------------------------------------" << std::endl;
1185 FileName fn_base, fn_base_old, fn_vol;
1188 double signal, change;
1190 bool converged =
true;
1196 std::cout <<
"--> Checking convergence " << std::endl;
1201 for (
size_t volno = 1; volno <=
Nvols; ++volno)
1208 vol().setXmippOrigin();
1209 dim = vol().rowNumber();
1210 old_vol().initZeros(vol());
1211 diff_vol().initZeros(vol());
1214 mask_prm.
R1 = dim / 2;
1220 old_vol.
read(fn_vol);
1222 diff_vol() = vol() - old_vol();
1225 change = diff_vol().sum2();
1226 signal = old_vol().sum2();
1229 if (change / signal >
eps)
1232 std::cout <<
formatString(
"--> Relative signal change volume %d = %f", volno, change / signal) << std::endl;
void setSampling(double sampling)
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)
virtual void maximization()=0
Update all model parameters, adapted for IEM blocks use.
void resize(size_t Ndim, size_t Zdim, size_t Ydim, size_t Xdim, bool copy=true)
double getDoubleParam(const char *param, int arg=0)
virtual void read(int argc, const char **argv, bool reportErrors=true)
#define REPORT_ERROR(nerr, ErrormMsg)
FileName replaceExtension(const String &newExt) const
void run()
Provides implementation of the run function.
virtual void produceSideInfo()
virtual void produceSideInfo()=0
Try to merge produceSideInfo1 and 2.
virtual void show() const
void symmetrizeVolume(const SymList &SL, const MultidimArray< double > &V_in, MultidimArray< double > &V_out, int spline, bool wrap, bool do_outside_avg, bool sum, bool helical, bool dihedral, bool helicalDihedral, double rotHelical, double rotPhaseHelical, double zHelical, double heightFraction, const MultidimArray< double > *mask, int Cn)
void sqrt(Image< double > &op)
virtual void destroyThreads()
Exit threads and free memory.
#define COMPOSE_VOL_FN(fn, volno, base)
int readSymmetryFile(FileName fn_sym, double accuracy=SYM_ACCURACY)
FileName insertBeforeExtension(const String &str) const
#define EMPTY_PROJECTIONS
StringVector reconsOutFnBase
void initProgress(size_t total, size_t stepBin=60)
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)
virtual void createThreads()
Create working threads.
bool isSymmetryGroup(FileName fn_sym, int &pgGroup, int &pgOrder)
void compose(const String &str, const size_t no, const String &ext="")
String integerToString(int I, int _width, char fill_with)
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY3D(V)
void removeRedundantPointsExhaustive(const int symmetry, int sym_order, bool only_half_sphere, double max_ang)
FileName fn_mask
Output mask. If not given it is not written.
String floatToString(float F, int _width, int _prec)
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY1D(v)
virtual void createEmptyFiles(int type)
virtual void defineAdditionalParams(XmippProgram *prog, const char *sectionLine)
virtual bool checkConvergence()
Convergency check.
void segment(Image< double > &mask)
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
void CenterFFT(MultidimArray< T > &v, bool forward)
FileName replaceSubstring(const String &subOld, const String &subNew) const
int argc
Original command line arguments.
void apply_mask(const MultidimArray< T > &I, MultidimArray< T > &result, T subs_val=0, bool apply_geo=false)
#define DIRECT_A1D_ELEM(v, i)
#define FOR_ALL_ELEMENTS_IN_ARRAY3D(V)
void computeSamplingPoints(bool only_half_sphere=true, double max_tilt=180, double min_tilt=0)
MultidimArray< double > spectral_signal
const char * getParam(const char *param, int arg=0)
virtual void defineBasicParams(XmippProgram *prog)
#define FN_ITER_BASE(iter)
FileName getIterExtraPath(const FileName &fn_root, int iter, bool makePath)
void readParams()
Read additional arguments for 3D-process from command line.
void progress_bar(long rlen)
void defineParams()
Define the parameters accepted.
void apply_cont_mask(const MultidimArray< double > &mask, const MultidimArray< T > &m_in, MultidimArray< T > &m_out)
Error related to numerical calculation.
void createSampling()
Create sampling for projecting volumes.
void radialAverage(const MultidimArray< double > &VolFourierMag, const MultidimArray< double > &V, MultidimArray< double > &radial_mean)
#define dAkij(V, k, i, j)
virtual void calculate3DSSNR(MultidimArray< double > &spectral_signal)
Calculate 3D SSNR according to Unser ea. (2005)
void projectVolume(FourierProjector &projector, Projection &P, int Ydim, int Xdim, double rot, double tilt, double psi, const MultidimArray< double > *ctf)
void createEmptyFile(const FileName &filename, int xdim, int ydim, int Zdim, size_t select_img, bool isStack, int mode, int _swapWrite, const MDRowVec *md)
int verbose
Verbosity level.
bool contains(const String &str) const
void FourierTransform(const MultidimArray< double > &in, MultidimArray< std::complex< double > > &out)
#define STR_EQUAL(str1, str2)
virtual void defineHiddenParams(XmippProgram *prog)
virtual void projectVolumes(MetaData &mdProj)
virtual void reconstructVolumes()
reconstruction by (weighted ART) or Fourier interpolation
FileName fn_vol
Input volume.
virtual void produceSideInfo2()
std::vector< Matrix1D< double > > no_redundant_sampling_points_angles
void setProgress(size_t value=0)
void dilate3D(const MultidimArray< double > &in, MultidimArray< double > &out, int neig, int count, int size)
void generate_mask(bool apply_geo=false)
std::vector< Image< double > > Iref
#define BINARY_CIRCULAR_MASK
double psi(const double x)
#define SUM_INIT(var, value)
String formatString(const char *format,...)
virtual void copyVolumes()
virtual void postProcessVolumes()
Masking, filtering etc. of the volume.
bool checkParam(const char *param)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
virtual void expectation()=0
Integrate over all experimental images.
#define FN_PROJECTIONS_MD
virtual void produceSideInfo2()=0
Try to merge produceSideInfo1 and 2.
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
int labelImage3D(const MultidimArray< double > &V, MultidimArray< double > &label)
void RaisedCosineMask(MultidimArray< double > &mask, double r1, double r2, int mode, double x0, double y0, double z0)
int getIntParam(const char *param, int arg=0)
virtual ProgReconsBase * createReconsProgram(FileName &input, FileName &output)
Create the program to be used for reconstruction of the volumes.
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)
std::vector< Image< double > > Iold
Incorrect value received.
virtual void endIteration()
Do some task at the end of iteration.
void addParamsLine(const String &line)
void applyMaskSpace(MultidimArray< double > &v)
void updateVolumesMetadata()
ProgMLRefine3D(bool fourier=false)
virtual void writeOutputFiles(const ModelML2D &model, OutputType outputType=OUT_FINAL)=0
Write output files.
virtual void makeNoiseImages()
(For mpi-version only:) calculate noise averages and write to disc
bool do_prob
From here on by Sjors.