source: trunk/test/nni_test.cc @ 1242

Last change on this file since 1242 was 1242, checked in by Peter, 14 years ago

working on #223

  • 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 1242 2008-03-16 18:45:37Z 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/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 "Suite.h"
27
28#include "yat/utility/FileUtil.h"
29#include "yat/utility/Matrix.h"
30#include "yat/utility/kNNI.h"
31#include "yat/utility/WeNNI.h"
32
33#include <cmath>
34#include <iostream>
35#include <fstream>
36#include <string>
37
38using namespace theplu::yat;
39
40void check_file_access(std::string& str)
41{
42  if (utility::FileUtil(str).permissions("r")) {
43    std::cerr << "test_nni: Cannot access file " << str << std::endl;
44    exit(-1);
45  }
46}
47
48int main(int argc, char* argv[])
49{
50  test::Suite suite(argc, argv);
51
52  uint neighbours=3;
53  std::string knni_data("data/knni_matrix.data");
54  std::string knni_result("data/knni_result.data");
55  std::string knni_weight("data/knni_weight.data");
56  std::string wenni_data("data/knni_matrix.data");
57  std::string wenni_result("data/wenni_result.data");
58  std::string wenni_weight("data/wenni_weight.data");
59  check_file_access(knni_data);
60  check_file_access(knni_result);
61  check_file_access(knni_weight);
62  check_file_access(wenni_data);
63  check_file_access(wenni_result);
64  check_file_access(wenni_weight);
65
66  // test kNNI
67  std::ifstream data_stream(knni_data.c_str());
68  std::ifstream weight_stream(knni_weight.c_str());
69  utility::Matrix data(data_stream);
70  utility::Matrix weight(weight_stream);
71  utility::kNNI knni(data,weight,neighbours);
72  knni.estimate();
73  std::ifstream control_stream(knni_result.c_str());
74  utility::Matrix control(control_stream);
75  control-=knni.imputed_data();
76  double error_bound = 5e-13;
77  for (unsigned int i=0; i<control.rows(); i++)
78    for (unsigned int j=0; j<control.columns(); j++)
79      if (std::abs(control(i,j))>error_bound) {
80        suite.err() << "kNNI FAILED, error on row " << i << " and " 
81                    << "column " << j << " is " << std::abs(control(i,j))
82                    << ". Expected less than " << error_bound << std::endl;
83        suite.add(false); // calculation result out of accepted error bounds
84      }
85      else
86        if (!((control(i,j)==control(i,j)))) {
87          suite.err() << "kNNI FAILED, nan (not a number) " 
88                      << "encountered in test on row " << i
89                      << " and column " << j << std::endl;
90          suite.add(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 (std::abs(control(i,j))>error_bound) {
109        suite.err() << "WeNNI FAILED, error on row " << i << " and " 
110                    << "column " << j << " is " << std::abs(control(i,j))
111                    << ". Expected less than " << error_bound << std::endl;
112        suite.add(false); // calculation result out of accepted error bounds
113      }
114      else
115        if (!((control(i,j)==control(i,j)))) {
116          suite.err() << "WeNNI FAILED, nan (not a number) " 
117                      << "encountered in test on row " << i
118                      << " and column " << j << std::endl;
119          suite.add(false); // calucaltion error detected
120        }
121  control_stream.close();
122  data_stream.close();
123  weight_stream.close();
124
125  // test WeNNI with binary weights
126  data_stream.open(knni_data.c_str());
127  data=utility::Matrix(data_stream);
128  weight_stream.open(knni_weight.c_str());
129  weight=utility::Matrix(weight_stream);
130  utility::WeNNI wenni2(data,weight,neighbours);
131  wenni2.estimate();
132  control_stream.open(knni_result.c_str());
133  control=utility::Matrix(control_stream);
134  control-=wenni2.imputed_data();
135  for (unsigned int i=0; i<control.rows(); i++)
136    for (unsigned int j=0; j<control.columns(); j++)
137      if (std::abs(control(i,j))>error_bound) {
138        suite.err() << "WeNNI binary weight test FAILED.\nError on row " << i
139                    << " and column " << j << " is " << std::abs(control(i,j))
140                    << ". Expected less than " << error_bound << std::endl;
141        suite.add(false); // calculation result out of accepted error bounds
142      }
143      else
144        if (!((control(i,j)==control(i,j)))) {
145          suite.err() << "WeNNI binary wieght test FAILED.\n"
146                      << "nan (not a number) encountered in test on row " << i
147                      << " and column " << j << std::endl;
148          suite.add(false); // calucaltion error detected
149        }
150  control_stream.close();
151  data_stream.close();
152  weight_stream.close();
153
154  return suite.return_value();
155}
Note: See TracBrowser for help on using the repository browser.