Xmipp  v3.23.11-Nereus
ml2d.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Sjors Scheres scheres@cnb.csic.es (2004)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef _ML2D_H
27 #define _ML2D_H
28 
29 #include <sys/time.h>
30 #include <pthread.h>
31 #include <random>
32 #include <vector>
33 #include <core/xmipp_fftw.h>
34 #include <core/xmipp_fft.h>
35 #include <core/args.h>
36 #include <core/xmipp_funcs.h>
38 #include <core/xmipp_image.h>
39 #include <core/geometry.h>
40 #include <data/filters.h>
41 #include <data/mask.h>
42 #include <data/ctf.h>
43 #include <core/xmipp_threads.h>
44 #include <core/xmipp_program.h>
45 #include <core/xmipp_log.h>
46 
47 
48 #define SIGNIFICANT_WEIGHT_LOW 1e-8
49 #define SMALLANGLE 1.75
50 #define DATALINELENGTH 12
51 
53 #define FN_EXTRA(file) formatString("%sextra/%s", fn_root.c_str(), file)
54 #define FN_IMAGES_MD(base) (base) + "images.xmd"
55 #define FN_CLASSES_MD(base) formatString("classes@%sclasses.xmd", (base).c_str())
56 #define FN_LOGS_MD(base) formatString("info@%sclasses.xmd", (base).c_str())
57 #define FN_CLASS_IMAGES_MD(base, ref) formatString("class%06d_images@%sclasses.xmd", (ref), (base).c_str())
58 #define FN_CLASSES_STK(base) (base) + "classes.stk"
59 
61 #define FOR_ALL_GLOBAL_IMAGES() \
62  for (size_t imgno = 0; imgno < nr_images_global; ++imgno)
63 
64 #define FOR_ALL_LOCAL_IMAGES() \
65  for (size_t imgno = myFirstImg; imgno <= myLastImg; ++imgno)
66 
67 #define IMG_LOCAL_INDEX (imgno - myFirstImg)
68 #define IMG_REAL_INDEX(imgno) (img_id[(imgno)])
69 #define IMG_BLOCK(imgno) (imgno) % blocks
70 
71 //For MPI
72 #define IS_MASTER (rank == 0)
73 //threads tasks
75 //output types constants
77 
78 
79 class ModelML2D
80 {
81 public:
83  int n_ref;
85  std::vector < Image<double> > Iref, WsumMref;
91  std::vector<double> sumwsc, sumw_mirror;
93  std::vector<double> alpha_k, mirror_fraction, scale;
99  double LL;
100 
102  int dim;
105 
106  ModelML2D();
107  ModelML2D(int n_ref);
108 
109  void initData();
110  void setNRef(int n_ref);
111  void combineModel(const ModelML2D &model, int sign);
112  void addModel(const ModelML2D &model);
113  void substractModel(const ModelML2D &model);
114 
115  void update();
117  void print(int tabs = 0) const;
118 }
119 ;//close class ModelML2D
120 
121 
122 
123 
129 {
130 public:
132  FileName fn_img, fn_ref, fn_root, fn_frac, fn_sig, fn_doc, fn_oext, fn_scratch, fn_control;
135  //double sigma_noise2;
137  bool do_mirror;
145  int istart;
147  int iter;
149  int Niter;
151  size_t dim, dim2, hdim;
152  double ddim2;
154  size_t nr_psi;
156  size_t nr_flip;
158  double psi_step;
160  double psi_max;
168  size_t myFirstImg, myLastImg;
170  double eps;
172  MetaDataDb MDimg, MDref, MDlog;
174  std::vector<Matrix2D<double> > F;
176  std::vector < Image<double> > Iold;
180  bool fast_mode;
182  double C_fast;
184  std::vector<Matrix1D<double> > Vtrans;
188  bool limit_rot;
190  double search_rot;
192  bool save_mem1, save_mem2, save_mem3;
194  std::vector<double> imgs_oldphi, imgs_oldtheta;
196  bool do_ML3D;
200  std::vector<std::vector<double> > imgs_offsets;
204  double df, df2, dfsigma2;
206 
207  bool do_norm;
209  std::vector<double> imgs_scale, imgs_bgmean, imgs_trymindiff;
211  std::vector<int> imgs_optrefno;
219  std::vector<double> conv;
220  //Just for debbuging
222 
224  ModelML2D model, *current_model;
225  //Some incremental stuff
226  //Number of blocks for IEM
227  size_t blocks;
228  //Current processing block
230 
231  //Vector of image IDs in the MetaData object (change order for randomize)
232  std::vector<size_t> img_id;
237 
239  bool referenceExclusive, allowFastOption, allowThreads, allowIEM, allowRestart;
241  int defaultNiter, defaultStartingIter;
242 
244  int threads;
245 
251 
254 
258  void initSamplingStuff();
259 
260 public:
261  /* Constructor for some initializations */
262  ML2DBaseProgram();
264  virtual void produceSideInfo() = 0;
266  virtual void produceSideInfo2() = 0;
267 
268  /* This function will perform an ML iteration
269  * usually composed by 'expectation' and 'maximization'
270  */
271  virtual void iteration() = 0;
272 
274  virtual void expectation() = 0;
275 
277  virtual void endIteration();
278 
280  virtual void maximization() = 0;
281 
283  virtual void setNumberOfLocalImages();
284 
286  virtual void randomizeImagesOrder();
287 
289  virtual void createThreads()
290  {}
291  ;//by default do nothing
292 
294  virtual void destroyThreads()
295  {}
296  ;//by default do nothing
297 
299  virtual void writeOutputFiles(const ModelML2D &model, OutputType outputType = OUT_FINAL) = 0;
300 
301 
302  virtual bool checkConvergence();
303 
305  virtual void addPartialDocfileData(const MultidimArray<double> &data, size_t first, size_t last) = 0;
307  virtual void run();
308 
312  virtual void defineBasicParams(XmippProgram * prog);
313  virtual void defineAdditionalParams(XmippProgram * prog, const char * sectionLine);
314  virtual void defineHiddenParams(XmippProgram *prog);
315 
316  int seed;
317 }
318 ;//end of class ML2DBaseProgram
319 
321 FileName getIterExtraPath(const FileName &fn_root, int iter, bool makePath=true);
322 
323 
325 #endif
size_t nr_psi
Definition: ml2d.h:154
std::vector< Matrix1D< double > > Vtrans
Definition: ml2d.h:184
std::vector< size_t > img_id
Definition: ml2d.h:232
double ddim2
Definition: ml2d.h:152
bool do_generate_refs
Definition: ml2d.h:198
ModelML2D()
Definition: ml2d.cpp:317
int refs_per_class
Definition: ml2d.h:217
Definition: ml2d.h:76
Definition: ml2d.h:76
size_t nr_flip
Definition: ml2d.h:156
bool zero_offsets
Definition: ml2d.h:186
n The following was calculated during iteration
double LL
Definition: ml2d.h:99
double sign
size_t current_block
Definition: ml2d.h:229
double dfsigma2
Definition: ml2d.h:204
ModelML2D model
Definition: ml2d.h:224
virtual void destroyThreads()
Exit threads and free memory.
Definition: ml2d.h:294
bool fix_fractions
Definition: ml2d.h:139
int dim
Definition: ml2d.h:102
double average_scale
Definition: ml2d.h:213
virtual void createThreads()
Create working threads.
Definition: ml2d.h:289
bool do_student_sigma_trick
Definition: ml2d.h:104
bool referenceExclusive
Definition: ml2d.h:239
size_t myLastImg
Definition: ml2d.h:168
std::vector< double > mirror_fraction
Definition: ml2d.h:93
void substractModel(const ModelML2D &model)
Definition: ml2d.cpp:409
int n_ref
Definition: ml2d.h:83
double psi_step
Definition: ml2d.h:158
OutputType
Definition: ml2d.h:76
ThreadTask
Definition: ml2d.h:74
void setNRef(int n_ref)
Definition: ml2d.cpp:342
glob_prnt iter
size_t hdim
Definition: ml2d.h:151
double psi_max
Definition: ml2d.h:160
void update()
Definition: ml2d.cpp:414
bool do_norm
Re-normalize internally.
Definition: ml2d.h:207
void print(int tabs=0) const
Just for debugging now.
Definition: ml2d.cpp:475
String defaultRoot
Definition: ml2d.h:240
bool do_ML3D
Definition: ml2d.h:196
ProgTransformDimRed * prog
size_t nr_images_local
Definition: ml2d.h:166
void combineModel(const ModelML2D &model, int sign)
Definition: ml2d.cpp:360
glob_log first
double search_rot
Definition: ml2d.h:190
MultidimArray< double > spectral_signal
Definition: ml2d.h:253
bool limit_rot
Definition: ml2d.h:188
std::vector< double > imgs_oldtheta
Definition: ml2d.h:194
std::vector< Matrix2D< double > > F
Definition: ml2d.h:174
double sumw_allrefs
Definition: ml2d.h:95
FileName getIterExtraPath(const FileName &fn_root, int iter, bool makePath=true)
Definition: ml2d.cpp:305
bool do_restart
Definition: ml2d.h:236
double wsum_sigma_offset
Definition: ml2d.h:89
double sumw_allrefs2
Definition: ml2d.h:95
MultidimArray< double > docfiledata
Definition: ml2d.h:234
double trymindiff_factor
Definition: ml2d.h:202
std::vector< std::vector< double > > imgs_offsets
Definition: ml2d.h:200
int istart
Definition: ml2d.h:145
std::vector< double > alpha_k
Definition: ml2d.h:93
double avePmax
Definition: ml2d.h:97
int factor_nref
Definition: ml2d.h:215
MetaDataDb MDref
Definition: ml2d.h:172
std::vector< double > sumwsc
Definition: ml2d.h:91
std::vector< int > imgs_optrefno
Definition: ml2d.h:211
FileName fn_sig
Definition: ml2d.h:132
bool fix_sigma_noise
Definition: ml2d.h:143
void initData()
Definition: ml2d.cpp:328
int defaultStartingIter
Definition: ml2d.h:241
Definition: ml2d.h:76
double sigma_noise
Definition: ml2d.h:87
std::vector< Image< double > > Iref
Definition: ml2d.h:85
Definition: ml2d.h:74
double C_fast
Definition: ml2d.h:182
double sumfracweight
Definition: ml2d.h:97
double sigma_offset
Definition: ml2d.h:89
std::string String
Definition: xmipp_strings.h:34
bool do_student
Definition: ml2d.h:104
Definition: ml2d.h:79
MultidimArray< double > P_phi
Definition: ml2d.h:178
String outRefsMd
Definition: ml2d.h:250
bool do_mirror
Definition: ml2d.h:137
bool fast_mode
Definition: ml2d.h:180
bool fix_sigma_offset
Definition: ml2d.h:141
size_t nr_images_global
Definition: ml2d.h:164
std::vector< double > imgs_trymindiff
Definition: ml2d.h:209
std::vector< double > sumw_mirror
Definition: ml2d.h:91
std::vector< Image< double > > WsumMref
Definition: ml2d.h:85
Definition: ml2d.h:76
size_t nr_nomirror_flips
Definition: ml2d.h:162
std::vector< Image< double > > Iold
Definition: ml2d.h:176
std::vector< double > conv
Definition: ml2d.h:219
size_t blocks
Definition: ml2d.h:227
std::vector< double > scale
Definition: ml2d.h:93
Definition: ml2d.h:76
int threads
Definition: ml2d.h:244
bool do_norm
Definition: ml2d.h:104
bool save_mem3
Definition: ml2d.h:192
size_t current_image
Definition: ml2d.h:221
void addModel(const ModelML2D &model)
Definition: ml2d.cpp:404
double eps
Definition: ml2d.h:170
String cline
Definition: ml2d.h:134
double wsum_sigma_noise
Definition: ml2d.h:87