Xmipp  v3.23.11-Nereus
micrograph_automatic_picking2.h
Go to the documentation of this file.
1 /***************************************************************************
2 *
3 * Authors: Carlos Oscar coss@cnb.csic.es (2011)
4 * Vahid Abrishami vabrishami@cnb.csic (2012)
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 MICROGRAPH_AUTOMATIC_PICKING2_H
28 #define MICROGRAPH_AUTOMATIC_PICKING2_H
29 
31 #include "core/xmipp_image.h"
32 #include "core/xmipp_threads.h"
33 #include "data/basic_pca.h"
34 #include "data/micrograph.h"
36 
37 class FeaturesThread;
38 
41 
42 /* Particle ---------------------------------------------------------------- */
43 class Particle2
44 {
45 public:
46  FileName micrograph; // Micrograph
47  int x, y; // position in micrograph
48  char status; // rejected=0, selected=1 or moved=2
49  MultidimArray<double> vec; // vector of that particle
50  double cost; // Associated cost
51 
52  // Print
53  friend std::ostream & operator << (std::ostream &_out, const Particle2 &_p);
54 
55  // Read
56  void read(std::istream &_in, int _vec_size);
57 };
58 
59 /* Automatic particle picking ---------------------------------------------- */
62 {
63 public:
64 
65  static const int NangSteps=120;
66  int particle_size, particle_radius, filter_num, proc_prec, NPCA, NRPCA, corr_num;
67  int num_correlation, num_features, Nthreads, fast, NRsteps;
68 
75 
76  Image<double> microImage, micrographStack, micrographStackPre, microImagePrev;
77 
78  std::vector<Particle2> auto_candidates;
79  std::vector<Particle2> rejected_particles;
80  std::vector<Particle2> accepted_particles;
81  std::vector<Particle2> negative_candidates;
82  std::vector<MDRowSql> micList;
83 
84  FileName fn_micrograph, fn_model, fnPCAModel, fnPCARotModel, fnAvgModel;
85  FileName fnVector, fnSVMModel, fnSVMModel2, fnInvariant, fnParticles;
86 
87  double scaleRate;
88  MultidimArray<double> convolveRes, filterBankStack, positiveParticleStack, negativeParticleStack;
89  MultidimArray<double> positiveInvariatnStack, negativeInvariatnStack, autoFeatVec;
90  MultidimArray<double> pcaModel, pcaRotModel, particleAvg, dataSet, dataSet1, classLabel;
91  MultidimArray<double> classLabel1, labelSet, dataSetNormal;
92 
93 public:
95 // AutoParticlePicking2(int particle_size, int filter_num = 6, int corr_num = 2, int NPCA = 4,
96 // const FileName &model_name=NULL, const FileName &micsFn=NULL);
97  AutoParticlePicking2(int particle_size, int filter_num = 6, int corr_num = 2, int NPCA = 4,
98  const FileName &model_name=nullptr, const std::vector<MDRowSql> &vMicList={});
99 
100  AutoParticlePicking2()=default;
101 
104 
105  void setSize(int pSize);
106 
108  void readMic(const FileName &fn_micrograph, int keepPrev);
109 
110  void filterBankGenerator();
111 
112  void batchBuildInvariant(const std::vector<MDRowSql> &MD);
113 
114  void buildInvariant(const std::vector<MDRowSql> &MD);
115  void extractInvariant();
116 
117  void extractPositiveInvariant();
118 
119  void extractNegativeInvariant();
120 
121  void trainPCA();
122 
123  void add2Dataset(int flagNegPos);
124 
125  void train(const std::vector<MDRowSql> &MD, bool corrFlag, int x, int y, int width, int height);
126 
127  void correction(const std::vector<MDRowSql> &addedParticlesMD,const std::vector<MDRowSql> &removedParticlesMD);
128 
129  void add2Dataset(const MetaData &removedParticlesMD);
130 
131  void saveTrainingSet();
132 
133  int automaticallySelectParticles(FileName fnmicrograph, int proc_prec, std::vector<MDRowSql> &md);
134 
135  int automaticWithouThread(FileName fnmicrograph, int proc_prec, const FileName &fn);
136 
137  void saveAutoParticles(MetaData &md);
138 
139  void saveAutoParticles(std::vector<MDRowSql> &md);
141  static void defineParams(XmippProgram * program);
142 
144  void readParams(XmippProgram * program);
145 
147  void readMicrograph();
148 
149  //Check the distance between a point and positive samples in micrograph
150  bool checkDist(Particle2 &p);
151 
152  /*
153  * This method extracts statics features such as
154  * average, variance and quantiles of a particle.
155  */
156  void extractStatics(MultidimArray<double> &inputVec,
157  MultidimArray<double> &features);
158 
160  void convert2PolarFourier(MultidimArray<double> &particleImage,
161  MultidimArray< std::complex< double > > &polar);
162 
164  void polarCorrelation(const MultidimArray< std::complex< double > > &fourierPolarStack,
165  MultidimArray<double> &IpolarCorr);
166 
168  void applyConvolution(bool fast);
169 
171  double PCAProject(MultidimArray<double> &pcaBasis,
173 
174  /*
175  * Extract the particle from the micrograph at x and y
176  * and normalize it if the flag normal is true.
177  */
178  void extractParticle(const int x, const int y,
179  MultidimArray<double> &filter,
180  MultidimArray<double> &particleImage,
181  bool normal);
182 
183  /* Extract non partiles from the micrograph and put these
184  * positions in a vector. Later we select some of them in a
185  * random manner in order to train the classifier with negative
186  * samples.
187  */
188  void extractNonParticle(std::vector<Particle2> &negativePosition);
189 
190  /*
191  * Extract the invariants from the particles and non particles
192  * in the micrograph.The invariants are the correlations between
193  * different channels in polar form.
194  */
195  void extractInvariant(const FileName &fnInvariantFeat,
196  const FileName &fnParticles,
197  bool avgFlag);
198 
199  /*
200  * This method extracts the invariants from the particles
201  * in a micrograph. If the flag is true then the average
202  * of the particles is also computed.The particles are
203  * saved in fnParticles.
204  */
205  void extractPositiveInvariant(const FileName &fnInvariantFeat,
206  const FileName &fnParticles,
207  bool avgFlag);
208 
209  /*
210  * This method extracts the invariants from the non particles
211  * in a micrograph.The non particles are saved in fnParticles.
212  */
213  void extractNegativeInvariant(const FileName &fnInvariantFeat,
214  const FileName &fnParticles);
215 
216  /*
217  * This method is used in order to extract all the features
218  * from an input (Can be particle or non particle)
219  */
220  void buildVector(MultidimArray<double> &inputVec,
221  MultidimArray<double> &staticVec,
222  MultidimArray<double> &featureVec,
223  MultidimArray<double> &pieceImage);
224 
225  /*Extract the invariants from just one particle at x,y
226  *The invariants are the correlations between different channels
227  *in polar form.
228  */
229  void buildInvariant(MultidimArray<double> &invariantChannel,
230  int x,int y, int pre);
231 
232  /*
233  * This method does a convolution in order to find an approximation
234  * about the place of the particles.
235  */
236  void buildSearchSpace(std::vector<Particle2> &positionArray,bool fast);
237 
238  /*
239  * This method is used in order to train an support vector
240  * machine. It receives the file which contains the dataset
241  * and also which classifier we want to train. In this case
242  * we have two classifiers.
243  */
244  void trainSVM(const FileName &fnModel,int numClassifier);
245 
246  /*
247  * This method uses the extracted invariants from the
248  * particles in order to find some pca basis to reduce
249  * the number of features.
250  */
251  void trainPCA(const FileName &fnPositiveFeat);
252 
253  /*
254  * This method is used to generate some pca basis according
255  * to different rotation of the template.
256  */
257  void trainRotPCA(const FileName &fnAvgModel,const FileName &fnPCARotModel);
258 
259  /*
260  * Extracts all the features related to particles and non
261  * particles and put them in an array with the related
262  * labels.
263  */
264  void add2Dataset(const FileName &fnInvariantFeat,
265  const FileName &fnParticles,int lable);
266 
267  /*
268  * It is the same as previous one but it does not extract
269  * the features. It just puts the data in an array for dataset.
270  */
271  void add2Dataset();
272 
273  /* Normalize the data of a dataset according to
274  * a and b.
275  */
276  void normalizeDataset(int a,int b);
277 
279  void savePCAModel(const FileName &fn);
280 
282  void saveTrainingSet(const FileName &fn_root);
283 
284 
286  void loadTrainingSet(const FileName &fn_root);
287 
288  /*
289  * This method generates two different datasets. One for the
290  * particles and non particles and the other one for the
291  * particles and the false positives.
292  */
293  void generateTrainSet();
294 
295  /*
296  * This method generate feature vectors for the candidates
297  * of a micrographs which are obtained by a cross-correlation
298  */
299  void generateFeatVec(const FileName &fnmicrograph, int proc_prec, std::vector<Particle2> &positionArray);
300 
301  /*
302  * Read the next micrograph from the list of the micrographs
303  */
304  int readNextMic(FileName &fnmicrograph);
305 
306  int getParticlesThreshold();
307 
308 };
309 
314 {
316 } ;
317 
321 class FeaturesThread: public Thread
322 {
323 private:
324  /* Condtions to be used to notifiy the thread to work(condIn)
325  * and to know the thread has finished (condOut)
326  */
327  Condition condIn, condOut;
328  bool waitingForResults; // Flag to know if the main thread is waiting
330 
331 public:
333  std::vector<Particle2> positionArray;
336 
338  ~FeaturesThread();
339 
340  void setMicrograph(const FileName &fnMic, int proc_prec);
341  void run();
342  /* This function should be called from outside to wait for results. */
343  void waitForResults();
344  /* Notify the thread to start working another micrograph */
345  void workOnMicrograph(const FileName &fnMic, int proc_prec);
346  /* Stop the work because the micrograph that the thread is working is not
347  * the next one the user has clicked.
348  */
349  void cancelWork();
350  /* Call the picker generateFeatVec */
351  void generateFeatures();
352 }
353 ; //class FeaturesThread
354 
356 {
357 public:
369  std::unique_ptr<AutoParticlePicking2> autoPicking;
370 public:
372  void readParams();
373 
375  void show();
376 
378  void defineParams();
379 
381  void run();
382 };
383 
385 #endif
AutoParticlePicking2 * picker
MultidimArray< double > labelSet
void read(std::istream &_in, int _vec_size)
doublereal * b
MultidimArray< double > positiveParticleStack
std::vector< Particle2 > rejected_particles
MultidimArray< double > pcaRotModel
std::vector< Particle2 > accepted_particles
MultidimArray< double > positiveInvariatnStack
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
std::vector< Particle2 > negative_candidates
std::vector< Particle2 > positionArray
int m
std::vector< Particle2 > auto_candidates
ProgImageRotationalPCA rotPcaAnalyzer
FileName fn_train
Training coordinates.
std::string String
Definition: xmipp_strings.h:34
std::unique_ptr< AutoParticlePicking2 > autoPicking
friend std::ostream & operator<<(std::ostream &_out, const Particle2 &_p)
PCAMahalanobisAnalyzer pcaAnalyzer
doublereal * a
MultidimArray< double > vec
std::vector< MDRowSql > micList
FileName fn_micrograph
Micrograph filename.