00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OPENMS_ANALYSIS_MAPMATCHING_MAPMATCHERREGRESSION_H
00029 #define OPENMS_ANALYSIS_MAPMATCHING_MAPMATCHERREGRESSION_H
00030
00031 #include <OpenMS/KERNEL/Feature.h>
00032 #include <OpenMS/ANALYSIS/MAPMATCHING/LinearMapping.h>
00033 #include <OpenMS/ANALYSIS/MAPMATCHING/BaseMapMatcher.h>
00034
00035 #include <iostream>
00036 #include <utility>
00037
00038 #include <gsl/gsl_fit.h>
00039
00040 #define DEBUG_MAPMATCHING
00041 #undef DEBUG_MAPMATCHING
00042
00043 namespace OpenMS
00044 {
00045
00050 template <typename ElementT = Feature >
00051 class MapMatcherRegression
00052 : public BaseMapMatcher<ElementT>
00053 {
00054 public:
00055
00059
00060 typedef BaseMapMatcher<ElementT> Base;
00061 typedef typename Base::ElementType ElementType;
00062 typedef typename Base::ElementPairVector ElementPairVector;
00064
00066 MapMatcherRegression()
00067 : BaseMapMatcher<ElementType>()
00068 {}
00069
00071 bool operator == (const MapMatcherRegression& rhs)
00072 {
00073 return (BaseMapMatcher<ElementType>::operator == (rhs) );
00074 }
00075
00077 virtual ~MapMatcherRegression()
00078 {}
00079
00081 void estimateTransform()
00082 {
00083 for ( typename Grid::iterator grid_iter = this->grid_.begin();
00084 grid_iter != this->grid_.end();
00085 ++grid_iter
00086 )
00087 {
00088 ElementPairVector selection;
00089
00090 #ifdef DEBUG_MAPMATCHING
00091
00092 std::cout << "Estimate the transformation with : " << this->element_pairs_.size() << " pairs " << std::endl;
00093 #endif
00094
00095 for (typename ElementPairVector::iterator pair_iter = this->element_pairs_.begin();
00096 pair_iter != this->element_pairs_.end();
00097 ++pair_iter)
00098 {
00099
00100
00101 if (grid_iter->encloses(pair_iter->getFirst().getPosition())
00102 && pair_iter->getQuality() > this->min_quality_ )
00103 {
00104 selection.push_back(*pair_iter);
00105
00106 #ifdef DEBUG_MAPMATCHING
00107
00108 #endif
00109
00110 }
00111 }
00112
00113
00114 int num = selection.size();
00115 if (num > 2)
00116 {
00117
00118 double* x = new double[num];
00119 double* y = new double[num];
00120
00121 grid_iter->getMappings().clear();
00122
00123
00124 for (UInt d=0; d<2;d++)
00125 {
00126
00127 for (int i=0; i<num;i++)
00128 {
00129 x[i] = selection[i].getFirst().getPosition()[d];
00130 y[i] = selection[i].getSecond().getPosition()[d];
00131 }
00132
00133
00134 double slope, intercept, cov00, cov01, cov11, sumsq;
00135
00136 gsl_fit_linear(x, 1, y, 1, num, &intercept, &slope, &cov00, &cov01, &cov11, &sumsq);
00137
00138 #ifdef DEBUG_MAPMATCHING
00139
00140 std::cout << "Estimating transform for dimension " << d << std::endl;
00141 std::cout << "Best fit: Y = " << intercept << " + " << slope << "* X" << std::endl;
00142 std::cout << "Sumsquares: " << sumsq << std::endl;
00143 std::cout << "Pearson: " << (cov01*cov01)/(cov00*cov11) << std::endl;
00144 #endif
00145
00146
00147 LinearMapping tmp_mapping;
00148 tmp_mapping.setSlope(slope);
00149 tmp_mapping.setIntercept(intercept);
00150 grid_iter->getMappings().push_back(tmp_mapping);
00151
00152 }
00153
00154 delete [] x;
00155 delete [] y;
00156
00157 }
00158 }
00159
00160 }
00161
00162 }
00163 ;
00164
00165 }
00166
00167 #endif // OPENMS_ANALYSIS_MAPMATCHING_MAPMATCHERREGRESSION_H