ProteoWizard
bucket1dtest.cpp
Go to the documentation of this file.
1 //
2 // $Id: bucket1dtest.cpp 5313 2013-12-17 18:06:54Z chambm $
3 //
4 //
5 // Original author: Witold Wolski <wewolski@gmail.com>
6 //
7 // Copyright : ETH Zurich
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
24 
25 #include <algorithm>
26 #include <boost/timer.hpp>
27 #include <boost/cstdint.hpp>
28 
30 
33 
34 namespace {
35 
36 using namespace pwiz::util;
38 typedef boost::int32_t int32_t;
39  void testBin1D(){
40  double ref [] = {2., 2.1 , 5. , 5.1 , 7.1 , 7.15 , 10. , 10.1};
41  std::vector<double> breaks;
42  breaks.assign(ref , ref + sizeof(ref)/sizeof(double));
43 
44  ralab::base::resample::Bin1D bin(breaks);
45  unit_assert_operator_equal(bin(2.-1e-8) , 0);
46  unit_assert_operator_equal(bin(2.+1e-8) , 1);
47  unit_assert_operator_equal(bin(2.1) ,1);
48  unit_assert_operator_equal(bin(2.1 + 1e-9) , 2);
49  }
50 
51  void testBin1D2(){
52 double epsilon = 1e-8;
53  double ref [] = {2., 2.1 , 5. , 5.1 , 7.1 , 7.15 , 10. , 10.1};
54  std::vector<double> breaks;
55  breaks.assign(ref , ref + sizeof(ref)/sizeof(double));
56 
57  ralab::base::resample::Bin1D bin(breaks);
58  std::vector<int32_t> idx;
59  std::vector<double> dist;
60  bin(2. -1e-8 , 2. + 2e-4, idx, dist ) ;
61  unit_assert_operator_equal(idx[0],-1);
63  unit_assert_equal(dist[0],1e-8,epsilon);
64  unit_assert_equal(dist[1],2e-4,epsilon);
65 
66  bin(2. -1e-8 , 2.1 + 2e-4, idx, dist ) ;
67  unit_assert_operator_equal(idx[0],-1);
70  unit_assert_equal(dist[0], 1e-8 ,epsilon);
71  unit_assert_equal(dist[1], 0.1 ,epsilon);
72  unit_assert_equal(dist[2], 2e-4 ,epsilon);
73 
74  bin(2. -1e-8 , 5.1 + 2e-4, idx, dist ) ;
75  double resd [] = {1e-8, 0.1, 2.9, 0.1, 2e-4};
76  int idxr[] = {-1,0,1,2,3};
77  bool x = std::equal(idxr , idxr + sizeof(idxr)/sizeof(int), idx.begin());
78  unit_assert(x);
79  x = std::equal(resd , resd + sizeof(resd)/sizeof(double), dist.begin(), ralab::base::resample::DaCompFunctor<double>(1e-8));
80  unit_assert(x);
81 
82  bin(2. - 2e-4 , 2. - 1e-4, idx, dist ) ;
83  unit_assert_operator_equal(idx[0],-1);
84  unit_assert_equal(dist[0],1e-4,1e-14);
85  bin(2.1 - 2e-4 , 2.1 - 1e-4, idx, dist ) ;
87  unit_assert_equal(dist[0],1e-4,1e-14);
88  bin(2.1 - 1e-4 , 7.1 + 2e-4, idx, dist ) ;
89 
90 
91  //testing end span
92  bin(10.1 - 1e-4 , 10.1 + 2e-4, idx, dist ) ;
93 
94  bin(5.1 - 1e-4 , 10.1 + 2e-4, idx, dist ) ;
95 
96  bin(10.1 + 1e-4 , 10.1 + 2e-4, idx, dist ) ;
97 
98 
99  }
100 
101 
102  void testHist()
103  {
104  std::vector<double> breaks;
105  std::vector<uint32_t> indicator;
106  // 0 1 2 3 4 5 6
107  double ref [] = {2., 2.1 , 5. , 5.1 , 7.1 , 7.15 , 10. , 10.1};
108  // We cover 0.1+0.1+0.05+0.1 = 0.35 //
109  breaks.assign(ref , ref + sizeof(ref)/sizeof(double));
110 
111  /*!\brief length indic is length(ref) - 1
112  */
113  // 0 1 2 3 4 5 6
114  uint32_t indic[] = { 1 , 0 , 1 , 0 , 1 , 0 , 1};
115 
116  indicator.assign(
117  indic
118  ,indic + sizeof(indic)/sizeof(uint32_t)
119  );
120 
121  ralab::base::resample::Bucket1D b1d( breaks, indicator);
122  std::vector<double> sample;
123  std::pair< size_t , bool > rb;
124  rb = b1d.operator()( 1. );
125  rb = b1d.operator()( 2. );
126  rb = b1d.operator()( 2.05 );
127  rb = b1d.operator()( 2.1 );
128  rb = b1d.operator()( 4. );
129  rb = b1d.operator()( 5. );
130  rb = b1d.operator()( 5.01 );
131  rb = b1d.operator()( 5.1 );
132  rb = b1d.operator()(6.);
133  rb = b1d.operator()(7.);
134  rb = b1d.operator()(7.12); // 4,true
135  rb = b1d.operator()(8.);
136  rb = b1d.operator()( 10.1 );
137  rb = b1d.operator()( 13. );
138 
139  sample.push_back( 1. );//false
140  sample.push_back( 2. );//false
141  sample.push_back( 2.05 );//#2 -> 0
142  sample.push_back( 2.1 );//#3 -> 0
143  sample.push_back( 4. );//false
144  sample.push_back( 5. );//false
145  sample.push_back( 5.01 );//#6 -> 2
146  sample.push_back( 5.1 );//#7 -> 2
147  sample.push_back( 10.1 );//#8 -> 6
148  sample.push_back( 13. );//false
149 
150  std::vector< std::pair< size_t , size_t > > res;
151  b1d( sample.begin() , sample.end() , res );
152  unit_assert(res[0].first == 0 && res[0].second == 2);
153  unit_assert(res[1].first == 0 && res[1].second == 3);
154  }
155 
156 
157 }//end namespace
158 
159 int main(int argc, char **argv) {
160  testBin1D();
161 testBin1D2();
162 testHist();
163 }
164 
165 
166 
167 
boost::uint32_t uint32_t
Definition: filter.hpp:47
const double epsilon
Definition: DiffTest.cpp:41
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
boost::int32_t int32_t
Definition: bin1d.hpp:40
void breaks(double minMass, double maxMass, TMassComparator tmassComp, std::vector< double > &breaks, bool exact=false)
Segment mass range according to Mass Compare functor could be used to histogram a dataset or to compu...
Definition: breakspec.hpp:41
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
KernelTraitsBase< Kernel >::space_type::abscissa_type x
#define unit_assert(x)
Definition: unit.hpp:85
Da Comparator - constant mass error.
int main(int argc, char **argv)