source: trunk/test/nni_test.cc @ 865

Last change on this file since 865 was 865, checked in by Peter, 16 years ago

changing URL to http://trac.thep.lu.se/trac/yat

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1// $Id: nni_test.cc 865 2007-09-10 19:41:04Z peter $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen
5  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2006, 2007 Jari Häkkinen
7
8  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "yat/utility/FileUtil.h"
27#include "yat/utility/matrix.h"
28#include "yat/utility/kNNI.h"
29#include "yat/utility/WeNNI.h"
30
31#include <cmath>
32#include <iostream>
33#include <fstream>
34#include <string>
35
36using namespace theplu::yat;
37
38void check_file_access(std::string& str)
39{
40  if (utility::FileUtil(str).permissions("r")) {
41    std::cerr << "test_nni: Cannot access file " << str << std::endl;
42    exit(-1);
43  }
44}
45
46int main(const int argc,const char* argv[])
47{
48  bool print = (argc>1 && argv[1]==std::string("-p"));
49  bool ok = true;
50  uint neighbours=3;
51  std::string knni_data("data/knni_matrix.data");
52  std::string knni_result("data/knni_result.data");
53  std::string knni_weight("data/knni_weight.data");
54  std::string wenni_data("data/knni_matrix.data");
55  std::string wenni_result("data/wenni_result.data");
56  std::string wenni_weight("data/wenni_weight.data");
57  check_file_access(knni_data);
58  check_file_access(knni_result);
59  check_file_access(knni_weight);
60  check_file_access(wenni_data);
61  check_file_access(wenni_result);
62  check_file_access(wenni_weight);
63
64  // test kNNI
65  std::ifstream data_stream(knni_data.c_str());
66  std::ifstream weight_stream(knni_weight.c_str());
67  utility::matrix data(data_stream);
68  utility::matrix weight(weight_stream);
69  utility::kNNI knni(data,weight,neighbours);
70  knni.estimate();
71  std::ifstream control_stream(knni_result.c_str());
72  utility::matrix control(control_stream);
73  control-=knni.imputed_data();
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      if (fabs(control(i,j))>error_bound) {
109        if (print)
110          std::cerr << "WeNNI FAILED, error on row " << i << " and " 
111                    << "column " << j << " is " << fabs(control(i,j))
112                    << ". Expected less than " << error_bound << std::endl;
113        ok=false; // calculation result out of accepted error bounds
114      }
115      else
116        if (!((control(i,j)==control(i,j)))) {
117          if (print) 
118            std::cerr << "WeNNI FAILED, nan (not a number) " 
119                      << "encountered in test on row " << i
120                      << " and column " << j << std::endl;
121          ok=false; // calucaltion error detected
122        }
123  control_stream.close();
124  data_stream.close();
125  weight_stream.close();
126
127  // test WeNNI with binary weights
128  data_stream.open(knni_data.c_str());
129  data=utility::matrix(data_stream);
130  weight_stream.open(knni_weight.c_str());
131  weight=utility::matrix(weight_stream);
132  utility::WeNNI wenni2(data,weight,neighbours);
133  wenni2.estimate();
134  control_stream.open(knni_result.c_str());
135  control=utility::matrix(control_stream);
136  control-=wenni2.imputed_data();
137  for (unsigned int i=0; i<control.rows(); i++)
138    for (unsigned int j=0; j<control.columns(); j++)
139      if (fabs(control(i,j))>error_bound) {
140        if (print)
141          std::cerr << "WeNNI binary weight test FAILED.\nError on row " << i
142                    << " and column " << j << " is " << fabs(control(i,j))
143                    << ". Expected less than " << error_bound << std::endl;
144        ok=false; // calculation result out of accepted error bounds
145      }
146      else
147        if (!((control(i,j)==control(i,j)))) {
148          if (print) 
149            std::cerr << "WeNNI binary wieght test FAILED.\n"
150                      << "nan (not a number) encountered in test on row " << i
151                      << " and column " << j << std::endl;
152          ok=false; // calucaltion error detected
153        }
154  control_stream.close();
155  data_stream.close();
156  weight_stream.close();
157
158  return (ok ? 0 : -1);
159}
Note: See TracBrowser for help on using the repository browser.