Xmipp  v3.23.11-Nereus
mpi_classify_CL2D.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar coss@cnb.csic.es (2009)
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 #ifndef _PROG_VQ_PROJECTIONS
26 #define _PROG_VQ_PROJECTIONS
27 
28 #include "core/metadata_db.h"
29 #include "data/polar.h"
30 #include "core/histogram.h"
31 #include "data/numerical_tools.h"
32 #include "core/xmipp_program.h"
33 
34 class MpiNode;
35 template<typename T>
36 class Image;
37 
43 {
44 public:
45  double corr; // Negative corrCodes indicate invalid particles
46  double likelihood; // Only valid if robust criterion
47  double shiftx;
48  double shifty;
49  double psi;
50  size_t objId;
51  bool flip;
52 
55 
57  void readAlignment(const Matrix2D<double> &M);
58 
60  void copyAlignment(const CL2DAssignment &alignment);
61 };
62 
64 std::ostream & operator << (std::ostream &out, const CL2DAssignment& assigned);
65 
67 class CL2DClass {
68 public:
69  // Projection
71 
72  // Update for next iteration
74 
75  // Polar Fourier transform of the projection at full size
77 
78  // Rotational correlation for best_rotation
80 
81  // Plans for the best_rotation
83 
84  // Correlation aux
86 
87  // Rotational correlation aux
89 
90  // List of images assigned
91  std::vector<CL2DAssignment> currentListImg;
92 
93  // List of images assigned
94  std::vector<CL2DAssignment> nextListImg;
95 
96  // Correlations of the next non-class members
97  std::vector<double> nextNonClassCorr;
98 
99  // Histogram of the correlations of the current class members
101 
102  // Histogram of the correlations of the current non-class members
104 
105  // List of neighbour indexes
106  std::vector<int> neighboursIdx;
107 
109  CL2DClass();
110 
112  CL2DClass(const CL2DClass &other);
113  CL2DClass(const CL2DClass &&)=delete;
114 
116  ~CL2DClass();
117 
119  CL2DClass & operator =(const CL2DClass &other);
120  CL2DClass & operator =(const CL2DClass &&)=delete;
121 
123  void updateProjection(const MultidimArray<double> &I,
124  const CL2DAssignment &assigned,
125  bool force=false);
126 
128  inline void updateNonProjection(double corr, bool force=false)
129  {
130  if (corr>0 || force)
131  nextNonClassCorr.push_back(corr);
132  }
133 
135  void transferUpdate(bool centerReference=true);
136 
140  void fitBasic(MultidimArray<double> &I, CL2DAssignment &result, bool reverse=false);
141 
143  void fit(MultidimArray<double> &I, CL2DAssignment &result);
144 
146  void lookForNeighbours(const std::vector<CL2DClass *> listP, int K);
147 private:
148  Polar<double> fitBasic_aux;
149 };
150 
152 {
153  bool operator()(CL2DClass* const& rpStart, CL2DClass* const& rpEnd)
154  {
155  return rpStart->currentListImg.size() > rpEnd->currentListImg.size();
156  }
157 };
158 
160 class CL2D {
161 public:
163  size_t Nimgs=0;
164 
166  MetaDataDb *SF=nullptr;
167 
169  std::vector<CL2DClass *> P;
170 
172  CL2D() {}
173 
175  ~CL2D();
176 
177  CL2D(const CL2D &)=delete;
178  CL2D(const CL2D &&)=delete;
179  CL2D & operator=(const CL2D &)=delete;
180  CL2D & operator=(const CL2D &&)=delete;
181 
183  void readImage(Image<double> &I, size_t objId, bool applyGeo) const;
184 
186  void initialize(MetaDataDb &_SF,
187  std::vector< MultidimArray<double> > &_codes0);
188 
190  void shareAssignments(bool shareAssignment, bool shareUpdates, bool shareNonCorr);
191 
193  void shareSplitAssignments(Matrix1D<int> &assignment, CL2DClass *node1, CL2DClass *node2) const;
194 
196  void write(const FileName &fnODir, const FileName &fnRoot, int level) const;
197 
201  void lookNode(MultidimArray<double> &I, int oldnode,
202  int &newnode, CL2DAssignment &bestAssignment);
203 
205  void transferUpdates();
206 
208  void run(const FileName &fnODir, const FileName &fnOut, int level);
209 
212  int cleanEmptyNodes();
213 
215  void splitNode(CL2DClass *node,
216  CL2DClass *&node1, CL2DClass *&node2,
217  std::vector<size_t> &finalAssignment) const;
218 
220  void splitFirstNode();
221 };
222 
225 public:
228 
231 
234 
237 
239  int Niter;
240 
242  int Ncodes0;
243 
245  int Ncodes;
246 
249 
251  double PminSize;
252 
255 
258 
261 
264 
267 
269  double maxShift;
270 
273 
276 
279 
281  double threshold;
282 
285 
287  ProgClassifyCL2D(int argc, char** argv);
288 
290  ~ProgClassifyCL2D();
291 
292  ProgClassifyCL2D(const ProgClassifyCL2D &)=delete;
293  ProgClassifyCL2D(const ProgClassifyCL2D &&)=delete;
294  ProgClassifyCL2D & operator =(const ProgClassifyCL2D &)=delete;
295  ProgClassifyCL2D & operator =(const ProgClassifyCL2D &&)=delete;
296 
297 
299  void readParams();
300 
302  void show() const;
303 
305  void defineParams();
306 
308  void produceSideInfo();
309 
311  void run();
312 public:
313  // Selfile with all the input images
315 
316  // Object Ids
317  std::vector<size_t> objId;
318 
319  // Structure for the classes
321 
322  // Mpi node
324 
325  // Maxshift squared
326  double maxShift2;
327 
328  // Gaussian interpolator
330 
331  // Image dimensions
332  size_t Ydim, Xdim;
333 
336 
338  double sigma;
339 };
341 #endif
std::vector< CL2DAssignment > nextListImg
FileName fnSel
Input selfile with the images to quantify.
MultidimArray< double > P
int Nneighbours
Number of neighbours.
FileName fnODir
Output directory.
void write(std::ostream &os, const datablock &db)
Definition: cif2pdb.cpp:3747
int Niter
Number of iterations.
int NSplitTrials
MaxTrials to split.
Histogram1D histClass
FileName fnCodes0
Input selfile with initial codes.
int Ncodes0
Initial number of code vectors.
double PminSize
Minimum size of a node.
CorrelationAux corrAux
bool classicalMultiref
Classical Multiref.
bool classifyAllImages
Clasify all images.
int Ncodes
Final number of code vectors.
bool normalizeImages
Normalize input images.
MultidimArray< double > Pupdate
std::vector< size_t > objId
void copyAlignment(const CL2DAssignment &alignment)
Copy alignment.
std::ostream & operator<<(std::ostream &out, const CL2DAssignment &assigned)
Show.
MpiNode * node
std::vector< double > nextNonClassCorr
bool operator()(CL2DClass *const &rpStart, CL2DClass *const &rpEnd)
FileName fnOut
bool alignImages
Don&#39;t align images.
void updateNonProjection(double corr, bool force=false)
double sigma
Noise in the images.
Polar< std::complex< double > > polarFourierP
MultidimArray< double > rotationalCorr
double maxShift
Maximum shift.
bool useCorrelation
Use Correlation instead of Correntropy.
RotationalCorrelationAux rotAux
Definition: polar.h:67
std::vector< CL2DAssignment > currentListImg
FileName fnOut
Output rootname.
bool mirrorImages
Mirror.
bool useThresholdMask
Use threshold mask.
bool classicalSplit
Use ClassicalCriterion at split.
constexpr int K
double threshold
Threshold to use.
std::vector< CL2DClass * > P
List of nodes.
std::vector< int > neighboursIdx
MultidimArray< int > mask
Mask for the background.
GaussianInterpolator gaussianInterpolator
Histogram1D histNonClass
void readAlignment(const Matrix2D< double > &M)
Read alignment parameters.
Polar_fftw_plans * plans
CL2DAssignment()
Empty constructor.