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