source: trunk/test/nni_test.cc @ 710

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

Addresses #1 and #80. Changed FileIO to FileUtil? and added tests on FileUtil?.

  • 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 710 2006-12-20 22:08:25Z 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  utility::FileUtil file(str);
39  if (file.access_rights(str,"r")) {
40    std::cerr << "test_nni: Cannot access file " << str << std::endl;
41    exit(-1);
42  }
43}
44
45int main(const int argc,const char* argv[])
46{
47  bool print = (argc>1 && argv[1]==std::string("-p"));
48  bool ok = true;
49  uint neighbours=3;
50  std::string knni_data("data/knni_matrix.data");
51  std::string knni_result("data/knni_result.data");
52  std::string knni_weight("data/knni_weight.data");
53  std::string wenni_data("data/knni_matrix.data");
54  std::string wenni_result("data/wenni_result.data");
55  std::string wenni_weight("data/wenni_weight.data");
56  check_file_access(knni_data);
57  check_file_access(knni_result);
58  check_file_access(knni_weight);
59  check_file_access(wenni_data);
60  check_file_access(wenni_result);
61  check_file_access(wenni_weight);
62
63  // test kNNI
64  std::ifstream data_stream(knni_data.c_str());
65  std::ifstream weight_stream(knni_weight.c_str());
66  utility::matrix data(data_stream);
67  utility::matrix weight(weight_stream);
68  utility::kNNI knni(data,weight,neighbours);
69  knni.estimate();
70  std::ifstream control_stream(knni_result.c_str());
71  utility::matrix control(control_stream);
72  control-=knni.imputed_data();
73  // Jari, should we use GSL defined round off errors? Anyway, the
74  // hardcoded number below should be changed.
75  double error_bound = 5e-13;
76  for (unsigned int i=0; i<control.rows(); i++)
77    for (unsigned int j=0; j<control.columns(); j++)
78      if (fabs(control(i,j))>error_bound) {
79        if (print)
80          std::cerr << "kNNI FAILED, error on row " << i << " and " 
81                    << "column " << j << " is " << fabs(control(i,j))
82                    << ". Expected less than " << error_bound << std::endl;
83        ok=false; // calculation result out of accepted error bounds
84      }
85      else
86        if (!((control(i,j)==control(i,j)))) {
87          if (print)
88            std::cerr << "kNNI FAILED, nan (not a number) " 
89                      << "encountered in test on row " << i
90                      << " and column " << j << std::endl;
91          ok =false; // calucaltion error detected
92        }
93  control_stream.close();
94  data_stream.close();
95  weight_stream.close();
96
97  // test WeNNI
98  data_stream.open(wenni_data.c_str());
99  data=utility::matrix(data_stream);
100  weight_stream.open(wenni_weight.c_str());
101  weight=utility::matrix(weight_stream);
102  utility::WeNNI wenni(data,weight,neighbours);
103  wenni.estimate();
104  control_stream.open(wenni_result.c_str());
105  control=utility::matrix(control_stream);
106  control-=wenni.imputed_data();
107  for (unsigned int i=0; i<control.rows(); i++)
108    for (unsigned int j=0; j<control.columns(); j++)
109      // Jari, should we use GSL defined round off errors? Anyway, the
110      // hardcoded number below should be changed.
111      if (fabs(control(i,j))>error_bound) {
112        if (print)
113          std::cerr << "WeNNI FAILED, error on row " << i << " and " 
114                    << "column " << j << " is " << fabs(control(i,j))
115                    << ". Expected less than " << error_bound << std::endl;
116        ok=false; // calculation result out of accepted error bounds
117      }
118      else
119        if (!((control(i,j)==control(i,j)))) {
120          if (print) 
121            std::cerr << "WeNNI FAILED, nan (not a number) " 
122                      << "encountered in test on row " << i
123                      << " and column " << j << std::endl;
124          ok=false; // calucaltion error detected
125        }
126  control_stream.close();
127  data_stream.close();
128  weight_stream.close();
129
130  // test WeNNI with binary weights
131  data_stream.open(knni_data.c_str());
132  data=utility::matrix(data_stream);
133  weight_stream.open(knni_weight.c_str());
134  weight=utility::matrix(weight_stream);
135  utility::WeNNI wenni2(data,weight,neighbours);
136  wenni2.estimate();
137  control_stream.open(knni_result.c_str());
138  control=utility::matrix(control_stream);
139  control-=wenni2.imputed_data();
140  for (unsigned int i=0; i<control.rows(); i++)
141    for (unsigned int j=0; j<control.columns(); j++)
142      // Jari, should we use GSL defined round off errors? Anyway, the
143      // hardcoded number below should be changed.
144      if (fabs(control(i,j))>error_bound) {
145        if (print)
146          std::cerr << "WeNNI binary weight test FAILED.\nError on row " << i
147                    << " and column " << j << " is " << fabs(control(i,j))
148                    << ". Expected less than " << error_bound << std::endl;
149        ok=false; // calculation result out of accepted error bounds
150      }
151      else
152        if (!((control(i,j)==control(i,j)))) {
153          if (print) 
154            std::cerr << "WeNNI binary wieght test FAILED.\n"
155                      << "nan (not a number) encountered in test on row " << i
156                      << " and column " << j << std::endl;
157          ok=false; // calucaltion error detected
158        }
159  control_stream.close();
160  data_stream.close();
161  weight_stream.close();
162
163  return (ok ? 0 : -1);
164}
Note: See TracBrowser for help on using the repository browser.