IMP  2.0.0
The Integrative Modeling Platform
scores2D.h
Go to the documentation of this file.
1 /*!
2  * \file scores2D.h
3  * \brief Scoring functions for 2D
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_SCORES_2D_H
8 #define IMPEM2D_SCORES_2D_H
9 
10 #include "IMP/em2d/em2d_config.h"
11 #include "IMP/em2d/Image.h"
14 #include "IMP/Object.h"
15 #include "IMP/VersionInfo.h"
17 #include <functional>
18 
19 IMPEM2D_BEGIN_NAMESPACE
20 
21 //! angle in the axis-angle representation of the rotation
22 //! that takes first to second
23 IMPEM2DEXPORT double get_rotation_error(const RegistrationResult &rr1,
24  const RegistrationResult &rr2);
25 
26 
27 //! Distance between the two in-plane translations
28 IMPEM2DEXPORT double get_shift_error(const RegistrationResult &rr1,
29  const RegistrationResult &rr2);
30 
31 IMPEM2DEXPORT double get_average_rotation_error(
32  const RegistrationResults &correct_RRs,
33  const RegistrationResults &computed_RRs);
34 
35 IMPEM2DEXPORT double get_average_shift_error(
36  const RegistrationResults &correct_RRs,
37  const RegistrationResults &computed_RRs);
38 
39 //! Computes the cross-correlation coefficient between to matrices
40 IMPEM2DEXPORT double get_cross_correlation_coefficient(const cv::Mat &m1,
41  const cv::Mat &m2);
42 
43 //! Get the gloal score given a set of individual registration results
44 //! from images
45 /*!
46  \note The function checks what time of registration results are given.
47  - If the registration results correspond to a coarse registration,
48  the score is based on the cross-correlation (currently, the score
49  is a mean of the cross-correlation coefficients.
50  - If the registration results are obtained after a fine registration,
51  the score is the average of all the registration scores. This score
52 g depends on the function selected. Eg. EM2DScore.
53 */
54 IMPEM2DEXPORT double get_global_score(const RegistrationResults &RRs);
55 
56 
57 //! Base class for all scoring functions related to em2d
58 class IMPEM2DEXPORT ScoreFunction: public IMP::base::Object {
59 public:
60  ScoreFunction(): Object("ScoreFunction%1%") {}
61 
62  //! Given an image and a projection, returns the appropiate score
63  double get_score(Image *image, Image *projection) const {
64  // trying to use the non-virtual interface (Alexandrescu, 39)
65  return get_private_score(image, projection);
66  }
67 
68  void set_variance_image(Image *var) {
69  set_variance_image_private(var);
70  }
71 
72  IMP_OBJECT_METHODS(ScoreFunction);
73 
74 protected:
75  // Number of particle images used to get the class average
76  unsigned int n_members_;
77 
78 private:
79  virtual double get_private_score(Image *image, Image *projection) const = 0;
80  virtual void set_variance_image_private(Image *image) {IMP_UNUSED(image);};
81 
82 
83 };
85 
86 //! Score based on Chi^2 = ((pixels_iamge - pixels_projection)/stddev_image)^2
87 class IMPEM2DEXPORT ChiSquaredScore: public ScoreFunction {
88 public:
90 
91 private:
92  mutable Pointer<Image> variance_;
93  double get_private_score(Image *, Image *) const;
94  void set_variance_imag_private(Image *var) {variance_ = var;}
95 };
97 
98 
99 //! EM2DScore, based on squared differences
100 //! (pixels_iamge - pixels_projection)**2
101 class IMPEM2DEXPORT EM2DScore: public ScoreFunction {
102 public:
103  EM2DScore(): ScoreFunction() {}
104 private:
105  double get_private_score(Image *image, Image *projection) const {
106  return 1 - get_cross_correlation_coefficient(image->get_data(),
107  projection->get_data());
108  }
109 };
111 
112 class IMPEM2DEXPORT MeanAbsoluteDifference: public ScoreFunction {
113 public:
114  MeanAbsoluteDifference(): ScoreFunction() {}
115 private:
116  double get_private_score(Image *image, Image *projection) const;
117 };
118 IMP_OBJECTS(MeanAbsoluteDifference, MeanAbsoluteDifferences);
119 
120 
121 //! Comparison by value of the ccc
122 template<class T>
124 #ifndef SWIG
125 :
126  public std::binary_function< T , T ,bool>
127 #endif
128  {
129 public:
130  bool operator()(const T &a, const T &b) const {
131  return a.get_ccc() >= b.get_ccc();
132  }
133  void show(std::ostream &) const {}
134 };
135 
136 
137 //! Comparison of pairs by checking the second element
138 template<class T>
140 #ifndef SWIG
141 :
142  public std::binary_function< T, T, bool>
143 #endif
144  {
145 public:
146  bool operator()(const T &a, const T &b) const {
147  return a.second < b.second;
148  }
149  void show(std::ostream &) const {}
150 };
151 
152 
153 
154 //! Compare two classes that return a score
155 template<class T>
157 #ifndef SWIG
158 :
159  public std::binary_function< T , T ,bool>
160 #endif
161  {
162 public:
163  bool operator()(const T &a,const T &b) const {
164  return a.get_score() < b.get_score();
165  }
166  void show(std::ostream &) const {}
167 };
168 
169 IMPEM2D_END_NAMESPACE
170 
171 #endif /* IMPEM2D_SCORES_2D_H */