source: trunk/test/nni_test.cc @ 335

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

Added WeNNI test with binary weights (i.e. kNNI mode).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1// $Id: nni_test.cc 335 2005-06-02 21:46:55Z jari $
2
3#include <c++_tools/utility/FileIO.h>
4#include <c++_tools/gslapi/matrix.h>
5#include <c++_tools/utility/kNNI.h>
6#include <c++_tools/utility/WeNNI.h>
7
8#include <cmath>
9#include <iostream>
10#include <fstream>
11#include <string>
12
13using namespace theplu;
14
15void check_file_access(std::string& str)
16{
17  if (utility::FileIO().access_rights(str,"r")) {
18    std::cerr << "test_nni: Cannot access file " << str << std::endl;
19    exit(-1);
20  }
21}
22
23int main(const int argc,const char* argv[])
24{
25  bool print = (argc>1 && argv[1]==std::string("-p"));
26  bool ok = true;
27  uint neighbours=3;
28  std::string knni_data("data/knni_matrix.data");
29  std::string knni_result("data/knni_result.data");
30  std::string knni_weight("data/knni_weight.data");
31  std::string wenni_data("data/knni_matrix.data");
32  std::string wenni_result("data/wenni_result.data");
33  std::string wenni_weight("data/wenni_weight.data");
34  check_file_access(knni_data);
35  check_file_access(knni_result);
36  check_file_access(knni_weight);
37  check_file_access(wenni_data);
38  check_file_access(wenni_result);
39  check_file_access(wenni_weight);
40
41  // test kNNI
42  std::ifstream data_stream(knni_data.c_str());
43  std::ifstream weight_stream(knni_weight.c_str());
44  gslapi::matrix data(data_stream);
45  gslapi::matrix weight(weight_stream);
46  utility::kNNI knni(data,weight,neighbours);
47  knni.estimate();
48  std::ifstream control_stream(knni_result.c_str());
49  gslapi::matrix control(control_stream);
50  control-=knni.imputed_data();
51  // Jari, should we use GSL defined round off errors? Anyway, the
52  // hardcoded number below should be changed.
53  double error_bound = 5e-13;
54  for (unsigned int i=0; i<control.rows(); i++)
55    for (unsigned int j=0; j<control.columns(); j++)
56      if (fabs(control(i,j))>error_bound) {
57        if (print)
58          std::cerr << "kNNI FAILED, error on row " << i << " and " 
59                    << "column " << j << " is " << fabs(control(i,j))
60                    << ". Expected less than " << error_bound << std::endl;
61        ok=false; // calculation result out of accepted error bounds
62      }
63      else
64        if (!((control(i,j)==control(i,j)))) {
65          if (print)
66            std::cerr << "kNNI FAILED, nan (not a number) " 
67                      << "encountered in test on row " << i
68                      << " and column " << j << std::endl;
69          ok =false; // calucaltion error detected
70        }
71  control_stream.close();
72  data_stream.close();
73  weight_stream.close();
74
75  // test WeNNI
76  data_stream.open(wenni_data.c_str());
77  data=gslapi::matrix(data_stream);
78  weight_stream.open(wenni_weight.c_str());
79  weight=gslapi::matrix(weight_stream);
80  utility::WeNNI wenni(data,weight,neighbours);
81  wenni.estimate();
82  control_stream.open(wenni_result.c_str());
83  control=gslapi::matrix(control_stream);
84  control-=wenni.imputed_data();
85  for (unsigned int i=0; i<control.rows(); i++)
86    for (unsigned int j=0; j<control.columns(); j++)
87      // Jari, should we use GSL defined round off errors? Anyway, the
88      // hardcoded number below should be changed.
89      if (fabs(control(i,j))>error_bound) {
90        if (print)
91          std::cerr << "WeNNI FAILED, error on row " << i << " and " 
92                    << "column " << j << " is " << fabs(control(i,j))
93                    << ". Expected less than " << error_bound << std::endl;
94        ok=false; // calculation result out of accepted error bounds
95      }
96      else
97        if (!((control(i,j)==control(i,j)))) {
98          if (print) 
99            std::cerr << "WeNNI FAILED, nan (not a number) " 
100                      << "encountered in test on row " << i
101                      << " and column " << j << std::endl;
102          ok=false; // calucaltion error detected
103        }
104  control_stream.close();
105  data_stream.close();
106  weight_stream.close();
107
108  // test WeNNI with binary weights
109  data_stream.open(knni_data.c_str());
110  data=gslapi::matrix(data_stream);
111  weight_stream.open(knni_weight.c_str());
112  weight=gslapi::matrix(weight_stream);
113  utility::WeNNI wenni2(data,weight,neighbours);
114  wenni2.estimate();
115  control_stream.open(knni_result.c_str());
116  control=gslapi::matrix(control_stream);
117  control-=wenni2.imputed_data();
118  for (unsigned int i=0; i<control.rows(); i++)
119    for (unsigned int j=0; j<control.columns(); j++)
120      // Jari, should we use GSL defined round off errors? Anyway, the
121      // hardcoded number below should be changed.
122      if (fabs(control(i,j))>error_bound) {
123        if (print)
124          std::cerr << "WeNNI binary weight test FAILED.\nError on row " << i
125                    << " and column " << j << " is " << fabs(control(i,j))
126                    << ". Expected less than " << error_bound << std::endl;
127        ok=false; // calculation result out of accepted error bounds
128      }
129      else
130        if (!((control(i,j)==control(i,j)))) {
131          if (print) 
132            std::cerr << "WeNNI binary wieght test FAILED.\n"
133                      << "nan (not a number) encountered in test on row " << i
134                      << " and column " << j << std::endl;
135          ok=false; // calucaltion error detected
136        }
137  control_stream.close();
138  data_stream.close();
139  weight_stream.close();
140
141  return (ok ? 0 : -1);
142}
Note: See TracBrowser for help on using the repository browser.