source: trunk/test/nni_test.cc @ 1727

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

Fixes #464. Final tests.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1// $Id: nni_test.cc 1727 2009-01-15 21:49:39Z jari $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen
5  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "Suite.h"
26
27#include "yat/utility/FileUtil.h"
28#include "yat/utility/Matrix.h"
29#include "yat/utility/kNNI.h"
30#include "yat/utility/WeNNI.h"
31
32#include <cmath>
33#include <iostream>
34#include <fstream>
35#include <string>
36
37using namespace theplu::yat;
38
39void check_file_access(std::string& str)
40{
41  if (utility::FileUtil(str).permissions("r")) {
42    std::cerr << "test_nni: Cannot access file " << str << std::endl;
43    exit(-1);
44  }
45}
46
47int main(int argc, char* argv[])
48{
49  test::Suite suite(argc, argv);
50  suite.err() << "Testing NNI algorithms" << std::endl;
51
52  suite.err() << "Reading data" << std::endl;
53  unsigned int neighbours=3;
54  std::string knni_data(test::filename("data/knni_matrix.data"));
55  std::string knni_result(test::filename("data/knni_result.data"));
56  std::string knni_weight(test::filename("data/knni_weight.data"));
57  std::string wenni_data(test::filename("data/knni_matrix.data"));
58  std::string wenni_result(test::filename("data/wenni_result.data"));
59  std::string wenni_weight(test::filename("data/wenni_weight.data"));
60  check_file_access(knni_data);
61  check_file_access(knni_result);
62  check_file_access(knni_weight);
63  check_file_access(wenni_data);
64  check_file_access(wenni_result);
65  check_file_access(wenni_weight);
66
67  // test kNNI
68  suite.err() << "Testing kNNI" << std::endl;
69  std::ifstream data_stream(knni_data.c_str());
70  std::ifstream weight_stream(knni_weight.c_str());
71  utility::Matrix data(data_stream);
72  utility::Matrix weight(weight_stream);
73  utility::kNNI knni(data,weight,neighbours);
74  unsigned int nonimputed=knni.estimate();
75  if (!suite.equal(nonimputed,15)) {
76    suite.err() << "kNNI FAILED, unexpected number of non imputed rows" << std::endl;
77    suite.add(false);
78  }
79  std::ifstream control_stream(knni_result.c_str());
80  utility::Matrix control(control_stream);
81  double error_bound = 1e-11;
82  for (unsigned int i=0; i<control.rows(); i++)
83    for (unsigned int j=0; j<control.columns(); j++)
84      if ((i==10) && (j==10)) {
85        if (!std::isnan(knni.imputed_data()(i,j)-control(i,j))) {
86          suite.err() << "kNNI FAILED, error on row " << i << " and " 
87                      << "column " << j << " expected NaN" << std::endl;
88          suite.add(false);
89        }
90      }
91      else if (!suite.equal_fix(knni.imputed_data()(i,j),control(i,j),error_bound)) {
92        suite.err() << "kNNI FAILED, error on row " << i << " and " 
93                    << "column " << j << std::endl;
94        suite.add(false); // calculation result out of accepted error bounds
95      }
96  control_stream.close();
97  data_stream.close();
98  weight_stream.close();
99
100  // test WeNNI
101  suite.err() << "Testing WeNNI" << std::endl;
102  data_stream.open(wenni_data.c_str());
103  data=utility::Matrix(data_stream);
104  weight_stream.open(wenni_weight.c_str());
105  weight=utility::Matrix(weight_stream);
106  utility::WeNNI wenni(data,weight,neighbours);
107  nonimputed=wenni.estimate();
108  if (!suite.equal(nonimputed,15)) {
109    suite.err() << "WeNNI FAILED, unexpected number of non imputed rows" << std::endl;
110    suite.add(false);
111  }
112  control_stream.open(wenni_result.c_str());
113  control=utility::Matrix(control_stream);
114  for (unsigned int i=0; i<control.rows(); i++)
115    for (unsigned int j=0; j<control.columns(); j++)
116      if ((i==10) && (j==10)) {
117        if (!std::isnan(knni.imputed_data()(i,j)-control(i,j))) {
118          suite.err() << "WeNNI FAILED, error on row " << i << " and " 
119                      << "column " << j << " expected NaN" << std::endl;
120          suite.add(false);
121        }
122      }
123      else if (!suite.equal_fix(wenni.imputed_data()(i,j),control(i,j),error_bound)){
124        suite.err() << "WeNNI FAILED, error on row " << i << " and " 
125                    << "column " << j << std::endl;
126        suite.add(false); // calculation result out of accepted error bounds
127      }
128  control_stream.close();
129  data_stream.close();
130  weight_stream.close();
131
132  // test WeNNI with binary weights
133  suite.err() << "Testing WeNNI with binary weights" << std::endl;
134  data_stream.open(knni_data.c_str());
135  data=utility::Matrix(data_stream);
136  weight_stream.open(knni_weight.c_str());
137  weight=utility::Matrix(weight_stream);
138  utility::WeNNI wenni2(data,weight,neighbours);
139  nonimputed=wenni2.estimate();
140  if (!suite.equal(nonimputed,15)) {
141    suite.err() << "binary WeNNI FAILED, unexpected number of non imputed rows"
142                << std::endl;
143    suite.add(false);
144  }
145  control_stream.open(knni_result.c_str());
146  control=utility::Matrix(control_stream);
147  for (unsigned int i=0; i<control.rows(); i++)
148    for (unsigned int j=0; j<control.columns(); j++)
149      if ((i==10) && (j==10)) {
150        if (!std::isnan(knni.imputed_data()(i,j)-control(i,j))) {
151          suite.err() << "binary WeNNI FAILED, error on row " << i << " and " 
152                      << "column " << j << " expected NaN" << std::endl;
153          suite.add(false);
154        }
155      }
156      else if (!suite.equal_fix(wenni2.imputed_data()(i,j),control(i,j),error_bound)){
157        suite.err() << "WeNNI binary weight test FAILED.\nError on row " << i
158                    << " and column " << j << std::endl;
159        suite.add(false); // calculation result out of accepted error bounds
160      }
161  control_stream.close();
162  data_stream.close();
163  weight_stream.close();
164
165  return suite.return_value();
166}
Note: See TracBrowser for help on using the repository browser.