source: trunk/test/normalization_test.cc @ 1719

Last change on this file since 1719 was 1719, checked in by Jari Häkkinen, 12 years ago

Added tests for qQuantileNormalizer.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.9 KB
Line 
1// $Id: normalization_test.cc 1719 2009-01-14 15:45:15Z jari $
2
3/*
4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009 Jari Häkkinen
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Suite.h"
24
25#include "yat/normalizer/Centralizer.h"
26#include "yat/normalizer/ColumnNormalizer.h"
27#include "yat/normalizer/Gauss.h"
28#include "yat/normalizer/qQuantileNormalizer.h"
29#include "yat/normalizer/QuantileNormalizer.h"
30#include "yat/normalizer/RowNormalizer.h"
31#include "yat/normalizer/Spearman.h"
32#include "yat/normalizer/Zscore.h"
33
34#include "yat/utility/DataIterator.h"
35#include "yat/utility/FileUtil.h"
36#include "yat/utility/Matrix.h"
37#include "yat/utility/MatrixWeighted.h"
38#include "yat/utility/WeightIterator.h"
39
40#include <fstream>
41#include <limits>
42#include <vector>
43
44using namespace theplu::yat;
45void test_centralizer(test::Suite&);
46void test_column_normalize(test::Suite&);
47void test_gauss_normalize(test::Suite&);
48void test_qquantile_normalize(test::Suite&);
49void test_quantile_normalize(test::Suite&);
50void test_row_normalize(test::Suite&);
51void test_spearman(test::Suite&);
52void test_spearman_weighted(test::Suite&);
53void test_z_score(test::Suite&);
54
55int main(int argc, char* argv[])
56{ 
57  test::Suite suite(argc, argv);
58  suite.err() << "testing normalizations ... " << std::endl;
59
60  test_centralizer(suite);
61  test_column_normalize(suite);
62  test_qquantile_normalize(suite);
63  test_quantile_normalize(suite);
64  test_gauss_normalize(suite);
65  test_row_normalize(suite);
66  test_spearman(suite);
67  test_z_score(suite);
68
69  return suite.return_value();
70}
71
72
73void test_centralizer(test::Suite& suite)
74{
75  suite.err() << "Testing Centralizer\n";
76  std::vector<double> vec;
77  vec.push_back(1);
78  vec.push_back(2);
79  vec.push_back(3);
80  normalizer::Centralizer<> c;
81  c(vec.begin(), vec.end(), vec.begin());
82  for (size_t i=0; i<vec.size(); ++i)
83    suite.add(suite.equal(vec[i], static_cast<double>(i)-1.0));
84
85  std::vector<utility::DataWeight> vec2;
86  vec2.push_back(utility::DataWeight(1,1));
87  vec2.push_back(utility::DataWeight(2,0.5));
88  vec2.push_back(utility::DataWeight(2,0.5));
89  std::vector<utility::DataWeight> vec3(vec2.size());
90  c(vec2.begin(), vec2.end(), vec3.begin());
91  for (size_t i=0; i<vec2.size(); ++i)
92    suite.add(suite.equal(vec3[i].weight(), vec2[i].weight()));
93  suite.add(suite.equal(vec3[0].data(), -0.5));
94  suite.add(suite.equal(vec3[1].data(), 0.5));
95  suite.add(suite.equal(vec3[2].data(), 0.5));
96
97}
98
99
100void test_column_normalize(test::Suite& suite)
101{
102  using namespace normalizer;
103  suite.err() << "Testing ColumnNormalizer\n";
104 
105  utility::Matrix m(2,2);
106  m(0,0) = 0;
107  m(0,1) = 10;
108  m(1,0) = 2;
109  m(1,1) = 4;
110  ColumnNormalizer<Centralizer<> > qn;
111  qn(m, m);
112  suite.err() << "Testing m(0,0)\n";
113  suite.add(suite.equal(m(0,0), -1));
114  suite.err() << "Testing m(0,1)\n";
115  suite.add(suite.equal(m(0,1), 3));
116  suite.err() << "Testing m(1,0)\n";
117  suite.add(suite.equal(m(1,0), 1));
118  suite.err() << "Testing m(1,1)\n";
119  suite.add(suite.equal(m(1,1), -3));
120}
121
122
123void test_qquantile_normalize(test::Suite& suite)
124{
125  using namespace normalizer;
126
127  suite.err() << "Testing qQuantileNormalizer\n";
128  std::string data(test::filename("data/normalization_test.data"));
129  if (utility::FileUtil(data.c_str()).permissions("r")) {
130    suite.add(false);
131    suite.err() << "Cannot access file " << data << '\n';
132    return;
133  }
134  std::ifstream data_stream(data.c_str());
135
136  utility::Matrix m(data_stream);
137
138  suite.err() << "testing number of parts (Q) boundary conditions\n";
139  qQuantileNormalizer(m.column_const_view(0),m.rows());
140  qQuantileNormalizer(m.column_const_view(0),3);
141
142  qQuantileNormalizer qqn(m.column_const_view(0),9);  // first column as target
143  utility::Matrix result(m.rows(),m.columns());
144  qqn(m,result);
145
146  suite.err() << "test that result can be stored in the source matrix\n";
147  qqn(m,m);
148  suite.add(result==m);
149
150  // Enough iteration will make all columns to have the same values as
151  // the target.
152  suite.err() << "Testing that iterative normalization\n";
153  utility::Matrix m2(4,2);
154  m2(0,0) = 0; m2(0,1) = 10;
155  m2(1,0) = 2; m2(1,1) = 4;
156  m2(2,0) = 1; m2(2,1) = 0;
157  m2(3,0) = 3; m2(3,1) = 7;
158  qQuantileNormalizer qqn2(m2.column_const_view(0),m2.rows());
159  utility::Matrix result2(m2.rows(),m2.columns());
160  qqn2(m2,result2);
161  for (int d=0; d<50; ++d)
162    qqn2(result2,result2);
163  suite.add( suite.equal_fix(m2(0,0),result2(2,1),1.0e-12) &&
164             suite.equal_fix(m2(1,0),result2(3,1),1.0e-12) &&
165             suite.equal_fix(m2(2,0),result2(1,1),1.0e-12) &&
166             suite.equal_fix(m2(3,0),result2(0,1),1.0e-12) );
167}
168
169
170void test_quantile_normalize(test::Suite& suite)
171{
172  suite.err() << "Testing QuantileNormalizer\n";
173 
174  utility::Matrix m(2,2);
175  m(0,0) = 0;
176  m(0,1) = 10;
177  m(1,0) = 2;
178  m(1,1) = 4;
179  normalizer::QuantileNormalizer qn;
180  qn(m, m);
181  suite.err() << "Testing m(0,0)\n";
182  suite.add(suite.equal(m(0,0), 2));
183  suite.err() << "Testing m(0,1)\n";
184  suite.add(suite.equal(m(0,1), 6));
185  suite.err() << "Testing m(1,0)\n";
186  suite.add(suite.equal(m(1,0), 6));
187  suite.err() << "Testing m(1,1)\n";
188  suite.add(suite.equal(m(1,1), 2));
189}
190
191void test_row_normalize(test::Suite& suite)
192{
193  using namespace normalizer;
194  suite.err() << "Testing RowNormalizer\n";
195 
196  utility::Matrix m(2,3);
197  m(0,0) = 0;
198  m(0,1) = 10;
199  m(1,0) = 2;
200  m(1,1) = 4;
201  utility::Matrix m2(m);
202  m2.transpose();
203  ColumnNormalizer<Centralizer<> > cn;
204  RowNormalizer<Centralizer<> > rn;
205  cn(m, m);
206  rn(m2, m2);
207  m2.transpose();
208  suite.equal_range(m.begin(), m.end(), m2.begin());
209}
210
211void test_spearman(test::Suite& suite)
212{
213  suite.err() << "Testing Spearman\n";
214  normalizer::Spearman spearman;
215  std::vector<double> vec;
216  vec.push_back(0);
217  vec.push_back(2);
218  vec.push_back(3);
219  vec.push_back(1);
220  spearman(vec.begin(), vec.end(), vec.begin());
221  std::vector<double> correct;
222  correct.push_back(1.0/8);
223  correct.push_back(5.0/8);
224  correct.push_back(7.0/8);
225  correct.push_back(3.0/8);
226  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
227  suite.err() << "Testing Spearman with ties\n";
228  vec[1]=vec[2];
229  correct[1] = correct[2] = (correct[1]+correct[2])/2;
230  spearman(vec.begin(), vec.end(), vec.begin());
231  suite.add(suite.equal_range(vec.begin(), vec.end(), correct.begin()));
232  test_spearman_weighted(suite);
233}
234
235
236void test_gauss_normalize(test::Suite& suite)
237{
238  suite.err() << "Testing Gauss\n";
239  normalizer::Gauss gauss;
240  std::vector<double> vec;
241  vec.push_back(1);
242  gauss(vec.begin(), vec.end(), vec.begin());
243  suite.add(suite.equal(vec.front(), 0));
244  vec.push_back(1);
245  gauss(vec.begin(), vec.end(), vec.begin());
246  suite.add(suite.equal(vec.front(), -vec.back()));
247
248}
249
250void test_spearman_weighted(test::Suite& suite)
251{
252  suite.err() << "Testing Weighted Spearman\n";
253  normalizer::Spearman spearman;
254
255  suite.err() << "Testing that unity weights reproduces unweighted case\n";
256  utility::MatrixWeighted m(1,4,0,1);
257  utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0);
258  m(0,0).data()=0;
259  m(0,1).data()=2;
260  m(0,2).data()=3;
261  m(0,3).data()=1;
262  std::vector<double> correct(m.columns());
263  std::vector<double> correct_w(m.columns(), 1.0);
264  std::copy(utility::data_iterator(m.begin_row(0)),
265            utility::data_iterator(m.end_row(0)),
266            correct.begin());
267  spearman(correct.begin(), correct.end(), correct.begin());
268  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
269
270  using utility::data_iterator;
271  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
272                               data_iterator(res.end_row(0)),
273                               correct.begin()));
274  using utility::weight_iterator;
275  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
276                               weight_iterator(res.end_row(0)),
277                               correct_w.begin()));
278
279  suite.err() << "Testing rescaling of weights\n";
280  for (size_t i=0; i<m.columns(); ++i) {
281    m(0,i).weight() *= 2;
282    correct_w[i] *= 2;
283  }   
284  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
285  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
286                               data_iterator(res.end_row(0)),
287                               correct.begin()));
288  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
289                               weight_iterator(res.end_row(0)),
290                               correct_w.begin()));
291
292 
293  suite.err() << "Testing case with a zero weight\n";
294  m(0,1).data() = std::numeric_limits<double>::quiet_NaN();
295  m(0,1).weight() = 0.0;
296  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
297  suite.add(suite.equal(res(0,0).data(), 0.5/3)); 
298  suite.add(suite.equal(res(0,2).data(), 2.5/3)); 
299  suite.add(suite.equal(res(0,3).data(), 1.5/3)); 
300
301  suite.err() << "Testing case with ties\n";
302  m(0,0).data() = m(0,2).data();
303  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
304  suite.add(suite.equal(res(0,0).data(), 2.0/3)); 
305  suite.add(suite.equal(res(0,2).data(), 2.0/3)); 
306  suite.add(suite.equal(res(0,3).data(), 0.5/3)); 
307}
308
309void test_z_score(test::Suite& suite)
310{
311  suite.err() << "Testing Zscore\n";
312  std::vector<double> vec;
313  vec.push_back(0);
314  vec.push_back(3.14);
315  normalizer::Zscore zscore;
316  zscore(vec.begin(), vec.end(), vec.begin());
317  for (size_t i=0; i<vec.size(); ++i)
318    suite.add(suite.equal(vec[i], 2.0*i-1.0));
319
320  std::vector<utility::DataWeight> vec2;
321  vec2.push_back(utility::DataWeight(1,1));
322  vec2.push_back(utility::DataWeight(2.13,0.5));
323  vec2.push_back(utility::DataWeight(2.13,0.5));
324  std::vector<utility::DataWeight> vec3(vec2.size());
325  zscore(vec2.begin(), vec2.end(), vec3.begin());
326  for (size_t i=0; i<vec2.size(); ++i)
327    suite.add(suite.equal(vec3[i].weight(), vec2[i].weight()));
328  suite.add(suite.equal(vec3[0].data(), -1.0));
329  suite.add(suite.equal(vec3[1].data(), 1.0));
330  suite.add(suite.equal(vec3[2].data(), 1.0));
331}
332
333
Note: See TracBrowser for help on using the repository browser.