source: trunk/test/normalization_test.cc @ 1509

Last change on this file since 1509 was 1509, checked in by Peter, 14 years ago

fixes #437 and added support for having known issues in test::Suite

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1// $Id: normalization_test.cc 1509 2008-09-17 20:56:30Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Suite.h"
23
24#include "yat/normalizer/Centralizer.h"
25#include "yat/normalizer/ColumnNormalizer.h"
26#include "yat/utility/DataIterator.h"
27#include "yat/normalizer/QuantileNormalizer.h"
28#include "yat/normalizer/Spearman.h"
29
30#include "yat/utility/DataIterator.h"
31#include "yat/utility/Matrix.h"
32#include "yat/utility/MatrixWeighted.h"
33#include "yat/utility/WeightIterator.h"
34
35#include <limits>
36#include <vector>
37
38using namespace theplu::yat;
39void test_centralizer(test::Suite&);
40void test_column_normalize(test::Suite&);
41void test_quantile_normalize(test::Suite&);
42void test_spearman(test::Suite&);
43void test_spearman_weighted(test::Suite&);
44
45int main(int argc, char* argv[])
46{ 
47  test::Suite suite(argc, argv);
48  suite.err() << "testing normalizations ... " << std::endl;
49
50  test_centralizer(suite);
51  test_column_normalize(suite);
52  test_quantile_normalize(suite);
53  test_spearman(suite);
54
55  return suite.return_value();
56}
57
58
59void test_centralizer(test::Suite& suite)
60{
61  suite.err() << "Testing Centralizer\n";
62  std::vector<double> vec;
63  vec.push_back(1);
64  vec.push_back(2);
65  vec.push_back(3);
66  normalizer::Centralizer<> c;
67  c(vec.begin(), vec.end(), vec.begin());
68  for (size_t i=0; i<vec.size(); ++i)
69    suite.add(suite.equal(vec[i], static_cast<double>(i)-1.0));
70}
71
72
73void test_column_normalize(test::Suite& suite)
74{
75  using namespace normalizer;
76  suite.err() << "Testing ColumnNormalizer\n";
77 
78  utility::Matrix m(2,2);
79  m(0,0) = 0;
80  m(0,1) = 10;
81  m(1,0) = 2;
82  m(1,1) = 4;
83  ColumnNormalizer<Centralizer<> > qn;
84  qn(m, m);
85  suite.err() << "Testing m(0,0)\n";
86  suite.add(suite.equal(m(0,0), -1));
87  suite.err() << "Testing m(0,1)\n";
88  suite.add(suite.equal(m(0,1), 3));
89  suite.err() << "Testing m(1,0)\n";
90  suite.add(suite.equal(m(1,0), 1));
91  suite.err() << "Testing m(1,1)\n";
92  suite.add(suite.equal(m(1,1), -3));
93}
94
95void test_quantile_normalize(test::Suite& suite)
96{
97  suite.err() << "Testing QuantileNormalizer\n";
98 
99  utility::Matrix m(2,2);
100  m(0,0) = 0;
101  m(0,1) = 10;
102  m(1,0) = 2;
103  m(1,1) = 4;
104  normalizer::QuantileNormalizer qn;
105  qn(m, m);
106  suite.err() << "Testing m(0,0)\n";
107  suite.add(suite.equal(m(0,0), 2));
108  suite.err() << "Testing m(0,1)\n";
109  suite.add(suite.equal(m(0,1), 6));
110  suite.err() << "Testing m(1,0)\n";
111  suite.add(suite.equal(m(1,0), 6));
112  suite.err() << "Testing m(1,1)\n";
113  suite.add(suite.equal(m(1,1), 2));
114}
115
116void test_spearman(test::Suite& suite)
117{
118  suite.err() << "Testing Spearman\n";
119  normalizer::Spearman spearman;
120  std::vector<double> vec;
121  vec.push_back(0);
122  vec.push_back(2);
123  vec.push_back(3);
124  vec.push_back(1);
125  spearman(vec.begin(), vec.end(), vec.begin());
126  std::vector<double> correct;
127  correct.push_back(0);
128  correct.push_back(0.5);
129  correct.push_back(0.75);
130  correct.push_back(0.25);
131  suite.equal_range(vec.begin(), vec.end(), correct.begin());
132  test_spearman_weighted(suite);
133}
134
135
136void test_spearman_weighted(test::Suite& suite)
137{
138  suite.err() << "Testing Weighted Spearman\n";
139  normalizer::Spearman spearman;
140
141  suite.err() << "Testing that unity weights reproduces unweighted case\n";
142  utility::MatrixWeighted m(1,4,0,1);
143  utility::MatrixWeighted res(m.rows(), m.columns(),3.14,0);
144  m(0,0).data()=0;
145  m(0,1).data()=2;
146  m(0,2).data()=3;
147  m(0,3).data()=1;
148  std::vector<double> correct(m.columns());
149  std::vector<double> correct_w(m.columns(), 1.0);
150  std::copy(utility::data_iterator(m.begin_row(0)),
151            utility::data_iterator(m.end_row(0)),
152            correct.begin());
153  spearman(correct.begin(), correct.end(), correct.begin());
154  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
155
156  using utility::data_iterator;
157  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
158                               data_iterator(res.end_row(0)),
159                               correct.begin()));
160  using utility::weight_iterator;
161  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
162                               weight_iterator(res.end_row(0)),
163                               correct_w.begin()));
164
165  suite.err() << "Testing rescaling of weights\n";
166  for (size_t i=0; i<m.columns(); ++i) {
167    m(0,i).weight() *= 2;
168    correct_w[i] *= 2;
169  }   
170  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
171  suite.add(suite.equal_range(data_iterator(res.begin_row(0)),
172                               data_iterator(res.end_row(0)),
173                               correct.begin()));
174  suite.add(suite.equal_range(weight_iterator(res.begin_row(0)),
175                               weight_iterator(res.end_row(0)),
176                               correct_w.begin()));
177
178 
179  suite.err() << "Testing case with a zero weight\n";
180  m(0,1).data() = std::numeric_limits<double>::quiet_NaN();
181  m(0,1).weight() = 0.0;
182  spearman(m.begin_row(0), m.end_row(0), res.begin_row(0));
183  suite.add(suite.equal(res(0,0).data(), 0)); 
184  suite.add(suite.equal(res(0,2).data(), 2.0/3)); 
185  suite.add(suite.equal(res(0,3).data(), 1.0/3)); 
186}
Note: See TracBrowser for help on using the repository browser.