IMP  2.1.1
The Integrative Modeling Platform
ProjectionFinder.h
Go to the documentation of this file.
1 /**
2  * \file ProjectionFinder.h
3  * \brief Coarse registration of 2D projections from a 3D volume
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6 */
7 
8 #ifndef IMPEM2D_PROJECTION_FINDER_H
9 #define IMPEM2D_PROJECTION_FINDER_H
10 
11 #include "IMP/em2d/em2d_config.h"
13 #include "IMP/em2d/align2D.h"
14 #include "IMP/em2d/project.h"
17 #include "IMP/em2d/Image.h"
18 #include "IMP/em2d/scores2D.h"
20 #include "IMP/algebra/Vector3D.h"
21 #include "IMP/algebra/Vector2D.h"
22 #include "IMP/algebra/Rotation3D.h"
23 #include "IMP/algebra/Rotation2D.h"
24 #include "IMP/base/Pointer.h"
25 #include "IMP/Particle.h"
26 #include <string>
27 
28 IMPEM2D_BEGIN_NAMESPACE
29 
30 
31 //! Methods for registration used by the finder
32 const unsigned int ALIGN2D_NO_PREPROCESSING = 0;
33 const unsigned int ALIGN2D_PREPROCESSING = 1;
34 const unsigned int ALIGN2D_WITH_CENTERS = 2;
35 
36 
37 class IMPEM2DEXPORT Em2DRestraintParameters: public ProjectingParameters {
38 
39  void init_defaults() {
40  coarse_registration_method = ALIGN2D_PREPROCESSING;
41  save_match_images = false;
42  optimization_steps = 5;
43  simplex_initial_length = 0.1;
44  simplex_minimum_size = 0.01;
45  }
46 
47 public:
48 
49  // Number of model projections to generate when scoring
50  unsigned int n_projections;
51  unsigned int coarse_registration_method;
52  bool save_match_images;
53  unsigned int optimization_steps;
54  double simplex_initial_length;
55  double simplex_minimum_size;
56 
57  Em2DRestraintParameters() {init_defaults();};
58 
59  Em2DRestraintParameters(double ps, double res, unsigned int n_proj=20):
60  ProjectingParameters(ps, res), n_projections(n_proj) {
61  init_defaults();
62  }
63 
64  void show(std::ostream &out = std::cout) const {
65  out << "Em2DRestraintParameters: " << std::endl
66  << "pixel_size " << pixel_size << " resolution " << resolution
67  << " coarse_registration_method " << coarse_registration_method
68  << " optimization_steps " << optimization_steps
69  << " simplex_initial_length " << simplex_initial_length
70  << " simplex_minimum_size " << simplex_minimum_size << std::endl;};
71 };
72 IMP_VALUES(Em2DRestraintParameters,Em2DRestraintParametersList);
73 
74 
75 
76 
77 //! class to perform registration of model projections to images images
78 class IMPEM2DEXPORT ProjectionFinder: public IMP::base::Object {
79 public:
80 
81  ProjectionFinder(): Object("ProjectionFinder%1%"),
82  parameters_setup_(false),registration_done_(false) {};
83 
84  //! Initializes the parameters to generate and match projections
85  void setup(ScoreFunction *score_function,
86  const Em2DRestraintParameters &params) {
87 
88  score_function_= score_function;
89  score_function_->set_was_used(true);
90  params_ = params;
91  masks_manager_ = MasksManagerPtr(new MasksManager);
92  masks_manager_->setup_kernel(params.resolution,params.pixel_size);
93  fast_optimization_mode_ = false;
94  parameters_setup_=true;
95  preprocessing_time_=0.0;
96  coarse_registration_time_=0.0;
97  fine_registration_time_ =0.0;
98  }
99 
100 
101  //! Set EM images to use as restraints
102  void set_subjects(const em2d::Images &subjects);
103 
104  //! Set the projections of the model to use for initial coarse correlation
105  void set_projections(const em2d::Images &projections);
106 
107  //! Set the projections of the model to use for initial coarse correlation
108  void set_variance_images(const em2d::Images &variances);
109 
110 
111  //! Set the particles where the em2D restraint is applied
112  void set_model_particles(const kernel::ParticlesTemp &ps);
113 
114  //! The projections of the model that best match the subject EM images
115  //! are saved.
116  /*!
117  Their name will be: coarse-match-i.spi for coarse registration and
118  fine_match-i.spi for full registration.
119  */
120  void set_save_match_images(bool x) {
121  params_.save_match_images = x;
122  }
123 
124  bool get_save_match_images() const {
125  return params_.save_match_images;
126  }
127  //! With this fast mode, only the first number n of best scoring
128  //! projections during the coarse search are refined.
129  //! Saves vast times of computation time with some loss of accuracy.
130  //! Try starting with 1 (risky) or 2, and increased it for get more chances
131  //! of finding the best projection
132  void set_fast_mode(unsigned int n);
133 
134  //! Recover the registration results. Only works if a registration has been
135  //! done previously
136  RegistrationResults get_registration_results() const;
137 
138  //! Coarse registration of all the images using the projections
139  //! Based in 2D alignments of the images
140  /**
141  \note Given that this registration is based on 2D alignment maximizing the
142  cross correlation, the a better score is the best correlation
143  **/
144  void get_coarse_registration();
145 
146  //! Performs complete registration of projections against the images.
147  //! This meaning the coarse registration followed by simplex optimization
148  void get_complete_registration();
149 
150  //! Get the em2d score for a model after the registration performed:
151  //! coarse or complete.
152  double get_global_score() const;
153 
154  void show(std::ostream &out) const;
155 
156  //! Time employed for preprocessing
157  double get_preprocessing_time() const;
158 
159  //! Time employed for the coarse registration part
160  double get_coarse_registration_time() const;
161 
162  //! Time employed for the fine registration part
163  double get_fine_registration_time() const;
164 
165  unsigned int get_number_of_subjects() const {
166  return subjects_.size();
167  }
168 
169  void set_number_of_class_averages_members(Ints n_members) {
170  n_members_ = n_members;
171  }
172 
173  unsigned int get_number_of_projections() const {
174  return projections_.size();
175  }
176 
178 
179 protected:
180 
181  double preprocessing_time_,coarse_registration_time_,fine_registration_time_;
182  //! Coarse registration for one subject
183  void get_coarse_registrations_for_subject(unsigned int i,
184  RegistrationResults &coarse_RRs);
185 
186  void do_preprocess_projection(unsigned int j);
187  void do_preprocess_subject(unsigned int i);
188 
189  //! Computes the weighted centroid and the FFT of the polar-resampled
190  //! autocorrelation.
191  void do_preprocess_for_fast_coarse_registration(const cv::Mat &m,
192  algebra::Vector2D &center,
193  cv::Mat &POLAR_AUTOC);
194  //! Main parameters
196  em2d::Images variances_;
197  em2d::Images projections_;
198  RegistrationResults registration_results_;
199  kernel::ParticlesTemp model_particles_;
200  Ints n_members_;
201  base::Pointer<ScoreFunction> score_function_;
202 
203  bool particles_set_,
204  parameters_setup_,
205  registration_done_,
206  fast_optimization_mode_;
207 
208 
209  unsigned int number_of_optimized_projections_;
210  // FFT of subjects (storing the FFT of projections is not neccessary
211  std::vector< cv::Mat > SUBJECTS_;
212  // FFT of the autocorrelation resampled images
213  std::vector< cv::Mat > SUBJECTS_POLAR_AUTOC_;
214  std::vector< cv::Mat > PROJECTIONS_POLAR_AUTOC_;
215  algebra::Vector2Ds subjects_cog_;
216  algebra::Vector2Ds projections_cog_;
217  PolarResamplingParameters polar_params_;
218 
219  Em2DRestraintParameters params_;
220 /**
221  MasksManager masks_manager_;
222 **/
223  MasksManagerPtr masks_manager_;
224 };
225 
227 
228 IMPEM2D_END_NAMESPACE
229 
230 #endif /* IMPEM2D_PROJECTION_FINDER_H */
Generation of projections from models or density maps Copyright 2007-2013 IMP Inventors. All rights reserved.
Classes and operations related with rotations.
projection masks Copyright 2007-2013 IMP Inventors. All rights reserved.
A nullptr-initialized pointer to an IMP Object.
void set_was_used(bool tf) const
Simple 2D vector class.
Alignment of images in 2D Copyright 2007-2013 IMP Inventors. All rights reserved. ...
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
A smart pointer to a reference counted object.
Definition: base/Pointer.h:87
inteface with OpenCV Copyright 2007-2013 IMP Inventors. All rights reserved.
const unsigned int ALIGN2D_NO_PREPROCESSING
Methods for registration used by the finder.
IMP images for Electron Microscopy using openCV matrices Copyright 2007-2013 IMP Inventors. All rights reserved.
Scoring functions for 2D Copyright 2007-2013 IMP Inventors. All rights reserved.
IMP::base::Vector< RegistrationResult > RegistrationResults
Base class for all scoring functions related to em2d.
Definition: scores2D.h:58
Funtions related with rotations in em2d Copyright 2007-2013 IMP Inventors. All rights reserved...
em2d::Images subjects_
Main parameters.
void setup(ScoreFunction *score_function, const Em2DRestraintParameters &params)
Initializes the parameters to generate and match projections.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Common base class for heavy weight IMP objects.
Simple 3D rotation class.
#define IMP_OBJECTS(Name, PluralName)
Define the types for storing sets of objects.
Parameters needed for the core projection routine.
Definition: project.h:31
Import IMP/kernel/Particle.h in the namespace.
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
Simple 3D vector class.
void show(std::ostream &out=std::cout) const
Definition: project.h:49
double get_global_score(const RegistrationResults &RRs)
Registration results class Copyright 2007-2013 IMP Inventors. All rights reserved.
class to perform registration of model projections to images images
Manage of projection masks.
IMP::base::Vector< Int > Ints
Standard way to pass a bunch of Int values.
Definition: base/types.h:49