Xmipp  v3.23.11-Nereus
aalign_significant.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  * David Strelak (davidstrelak@gmail.com)
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
27 #ifndef AALIGN_SIGNIFICANT
28 #define AALIGN_SIGNIFICANT
29 
30 #include <core/xmipp_program.h>
31 #include "data/dimensions.h"
32 #include <CTPL/ctpl_stl.h>
33 #include "data/cpu.h"
36 #include "core/geometry.h"
38 #include "core/transformations.h"
39 
43 namespace Alignment {
44 
45 template<typename T>
47 {
48 public:
49  virtual void readParams() override;
50  virtual void defineParams() override;
51  virtual void show() const override;
52  virtual void run() override;
53 
54 protected:
55  struct Settings {
56  Dimensions refDims = Dimensions(0); // there should be less reference images than experimental images
57  Dimensions otherDims = Dimensions(0); // there should be more reference images than experimental images
58 
59  unsigned cpuThreads;
60  };
61 
62  struct Assignment {
63  Assignment(size_t r, size_t i, float w, float m, const Matrix2D<float> &p) :
64  refIndex(r), imgIndex(i), weight(w), merit(m), pose(p) {};
65  size_t refIndex;
66  size_t imgIndex;
67  float weight;
68  float merit;
70  };
71 
72  virtual void check() const;
73  const Settings &getSettings() {
74  return m_settings;
75  }
76  virtual std::vector<AlignmentEstimation> align(const T *ref, const T *others) = 0;
77  virtual void updateRefs(T *refs, const T *others, const std::vector<Assignment> &assignments) = 0;
78 
80  return m_threadPool;
81  }
82 
83  void updateRefXmd(size_t refIndex, std::vector<Assignment> &images);
84 
85 private:
86  struct DataHelper {
87  FileName fn;
88  MetaDataVec md;
89  Dimensions dims = Dimensions(0);
90  std::unique_ptr<T[]> data;
91  // reference data only (will be empty for experimental images)
92  std::vector<float> rots; // valid only for
93  std::vector<float> tilts;
94  std::vector<int> indexes; // as in the metadata file
95  // image data only (will be empty for reference images)
96  std::vector<size_t> rowIds;
97  };
98 
99  struct UpdateRefHelper {
100  bool doUpdate;
101  std::vector<MetaDataVec> imgBlocks;
102  MetaDataVec refBlock;
103  FileName fnXmd;
104  FileName fnStk;
105  };
106 
107  struct WeightCompHelper {
108  WeightCompHelper(float c, size_t ref, size_t img) :
109  refIndex(ref), imgIndex(img), merit(c) {};
110  size_t refIndex;
111  size_t imgIndex;
112  float merit;
113  };
114 
115 
116  DataHelper m_imagesToAlign;
117  DataHelper m_referenceImages;
118  FileName m_fnOut;
119  UpdateRefHelper m_updateHelper;
120  float m_angDistance;
121  Settings m_settings;
122  size_t m_noOfBestToKeep;
123  bool m_allowDataSwap;
124  bool m_useWeightInsteadOfCC;
125 
126  std::vector<std::vector<float>> m_weights;
127  std::vector<Assignment> m_assignments;
128 
129  ctpl::thread_pool m_threadPool;
130 
131  template<bool IS_REF>
132  void load(DataHelper &h);
133  Dimensions crop(const Dimensions &d, DataHelper &h);
134  template<bool IS_ESTIMATION_TRANSPOSED>
135  void computeWeights(const std::vector<AlignmentEstimation> &est);
136  template<bool IS_ESTIMATION_TRANSPOSED>
137  void computeWeightsAndSave(
138  const std::vector<AlignmentEstimation> &est,
139  size_t refIndex);
140  void computeWeightsAndSave(
141  std::vector<WeightCompHelper> &figsOfMerit,
142  size_t refIndex);
143  template<bool IS_ESTIMATION_TRANSPOSED, bool USE_WEIGHT>
144  void computeAssignment(
145  const std::vector<AlignmentEstimation> &est);
146  template<bool USE_WEIGHT>
147  void storeAlignedImages();
148  void fillRow(MDRow &row,
149  const Matrix2D<float> &pose,
150  size_t refIndex,
151  double weight,
152  size_t imgIndex,
153  double maxVote);
154  void fillRow(MDRow &row,
155  const Matrix2D<float> &pose,
156  size_t refIndex,
157  double weight,
158  size_t imgIndex);
159  void extractMax(
160  std::vector<float> &data,
161  size_t &pos, float &val);
162 
163  void updateSettings();
164  void updateRefs();
165  void saveRefStk();
166  void checkLogDelete(const FileName &fn);
167  void saveRefXmd();
168 
169  inline int getRefMetaIndex(size_t refIndex) {
170  return m_referenceImages.indexes.at(refIndex);
171  }
172 
173  inline size_t getImgMetaIndex(const MDRow &row, size_t imgIndex) {
174  // by default, use our index
175  size_t index = imgIndex + 1; // within metadata file, index images from one (1)
176  // if the label is present, use its value. Otherwise, use default value
177  row.getValue(MDL_IMAGE_IDX, index);
178  return index;
179  }
180 
181  inline void getImgRow(MDRowVec &row, size_t imgIndex) {
182  m_imagesToAlign.md.getRow(row, m_imagesToAlign.rowIds.at(imgIndex));
183  }
184 
185  void validate(const DataHelper &h, bool isRefData);
186 };
187 
188 } /* namespace Alignment */
190 #endif /* AALIGN_SIGNIFICANT */
doublereal * c
Index of an image within a list (size_t)
Assignment(size_t r, size_t i, float w, float m, const Matrix2D< float > &p)
doublereal * w
#define i
doublereal * d
virtual void readParams() override
T & getValue(MDLabel label)
viol index
virtual std::vector< AlignmentEstimation > align(const T *ref, const T *others)=0
virtual void defineParams() override
virtual void show() const override
int m
void updateRefXmd(size_t refIndex, std::vector< Assignment > &images)
ctpl::thread_pool & getThreadPool()