source: trunk/test/nni_test.cc @ 711

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

Fixes #1 and #80. Interface changed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1// $Id: nni_test.cc 711 2006-12-21 08:10:30Z jari $
2
3/*
4  Copyright (C) The authors contributing to this file.
5
6  This file is part of the yat library, http://lev.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "yat/utility/FileUtil.h"
25#include "yat/utility/matrix.h"
26#include "yat/utility/kNNI.h"
27#include "yat/utility/WeNNI.h"
28
29#include <cmath>
30#include <iostream>
31#include <fstream>
32#include <string>
33
34using namespace theplu::yat;
35
36void check_file_access(std::string& str)
37{
38  if (utility::FileUtil(str).permissions("r")) {
39    std::cerr << "test_nni: Cannot access file " << str << std::endl;
40    exit(-1);
41  }
42}
43
44int main(const int argc,const char* argv[])
45{
46  bool print = (argc>1 && argv[1]==std::string("-p"));
47  bool ok = true;
48  uint neighbours=3;
49  std::string knni_data("data/knni_matrix.data");
50  std::string knni_result("data/knni_result.data");
51  std::string knni_weight("data/knni_weight.data");
52  std::string wenni_data("data/knni_matrix.data");
53  std::string wenni_result("data/wenni_result.data");
54  std::string wenni_weight("data/wenni_weight.data");
55  check_file_access(knni_data);
56  check_file_access(knni_result);
57  check_file_access(knni_weight);
58  check_file_access(wenni_data);
59  check_file_access(wenni_result);
60  check_file_access(wenni_weight);
61
62  // test kNNI
63  std::ifstream data_stream(knni_data.c_str());
64  std::ifstream weight_stream(knni_weight.c_str());
65  utility::matrix data(data_stream);
66  utility::matrix weight(weight_stream);
67  utility::kNNI knni(data,weight,neighbours);
68  knni.estimate();
69  std::ifstream control_stream(knni_result.c_str());
70  utility::matrix control(control_stream);
71  control-=knni.imputed_data();
72  // Jari, should we use GSL defined round off errors? Anyway, the
73  // hardcoded number below should be changed.
74  double error_bound = 5e-13;
75  for (unsigned int i=0; i<control.rows(); i++)
76    for (unsigned int j=0; j<control.columns(); j++)
77      if (fabs(control(i,j))>error_bound) {
78        if (print)
79          std::cerr << "kNNI FAILED, error on row " << i << " and " 
80                    << "column " << j << " is " << fabs(control(i,j))
81                    << ". Expected less than " << error_bound << std::endl;
82        ok=false; // calculation result out of accepted error bounds
83      }
84      else
85        if (!((control(i,j)==control(i,j)))) {
86          if (print)
87            std::cerr << "kNNI FAILED, nan (not a number) " 
88                      << "encountered in test on row " << i
89                      << " and column " << j << std::endl;
90          ok =false; // calucaltion error detected
91        }
92  control_stream.close();
93  data_stream.close();
94  weight_stream.close();
95
96  // test WeNNI
97  data_stream.open(wenni_data.c_str());
98  data=utility::matrix(data_stream);
99  weight_stream.open(wenni_weight.c_str());
100  weight=utility::matrix(weight_stream);
101  utility::WeNNI wenni(data,weight,neighbours);
102  wenni.estimate();
103  control_stream.open(wenni_result.c_str());
104  control=utility::matrix(control_stream);
105  control-=wenni.imputed_data();
106  for (unsigned int i=0; i<control.rows(); i++)
107    for (unsigned int j=0; j<control.columns(); j++)
108      // Jari, should we use GSL defined round off errors? Anyway, the
109      // hardcoded number below should be changed.
110      if (fabs(control(i,j))>error_bound) {
111        if (print)
112          std::cerr << "WeNNI FAILED, error on row " << i << " and " 
113                    << "column " << j << " is " << fabs(control(i,j))
114                    << ". Expected less than " << error_bound << std::endl;
115        ok=false; // calculation result out of accepted error bounds
116      }
117      else
118        if (!((control(i,j)==control(i,j)))) {
119          if (print) 
120            std::cerr << "WeNNI FAILED, nan (not a number) " 
121                      << "encountered in test on row " << i
122                      << " and column " << j << std::endl;
123          ok=false; // calucaltion error detected
124        }
125  control_stream.close();
126  data_stream.close();
127  weight_stream.close();
128
129  // test WeNNI with binary weights
130  data_stream.open(knni_data.c_str());
131  data=utility::matrix(data_stream);
132  weight_stream.open(knni_weight.c_str());
133  weight=utility::matrix(weight_stream);
134  utility::WeNNI wenni2(data,weight,neighbours);
135  wenni2.estimate();
136  control_stream.open(knni_result.c_str());
137  control=utility::matrix(control_stream);
138  control-=wenni2.imputed_data();
139  for (unsigned int i=0; i<control.rows(); i++)
140    for (unsigned int j=0; j<control.columns(); j++)
141      // Jari, should we use GSL defined round off errors? Anyway, the
142      // hardcoded number below should be changed.
143      if (fabs(control(i,j))>error_bound) {
144        if (print)
145          std::cerr << "WeNNI binary weight test FAILED.\nError on row " << i
146                    << " and column " << j << " is " << fabs(control(i,j))
147                    << ". Expected less than " << error_bound << std::endl;
148        ok=false; // calculation result out of accepted error bounds
149      }
150      else
151        if (!((control(i,j)==control(i,j)))) {
152          if (print) 
153            std::cerr << "WeNNI binary wieght test FAILED.\n"
154                      << "nan (not a number) encountered in test on row " << i
155                      << " and column " << j << std::endl;
156          ok=false; // calucaltion error detected
157        }
158  control_stream.close();
159  data_stream.close();
160  weight_stream.close();
161
162  return (ok ? 0 : -1);
163}
Note: See TracBrowser for help on using the repository browser.