source: trunk/test/nni_test.cc @ 1607

Last change on this file since 1607 was 1554, checked in by Jari Häkkinen, 13 years ago

Fixes #192. Using std::numeric_limits, since impute algorithms are slightly changed template results also changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1// $Id: nni_test.cc 1554 2008-10-09 18:31:34Z jari $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen
5  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "Suite.h"
26
27#include "yat/utility/FileUtil.h"
28#include "yat/utility/Matrix.h"
29#include "yat/utility/kNNI.h"
30#include "yat/utility/WeNNI.h"
31
32#include <cmath>
33#include <iostream>
34#include <fstream>
35#include <string>
36
37using namespace theplu::yat;
38
39void check_file_access(std::string& str)
40{
41  if (utility::FileUtil(str).permissions("r")) {
42    std::cerr << "test_nni: Cannot access file " << str << std::endl;
43    exit(-1);
44  }
45}
46
47int main(int argc, char* argv[])
48{
49  test::Suite suite(argc, argv);
50
51  unsigned int neighbours=3;
52  std::string knni_data(test::filename("data/knni_matrix.data"));
53  std::string knni_result(test::filename("data/knni_result.data"));
54  std::string knni_weight(test::filename("data/knni_weight.data"));
55  std::string wenni_data(test::filename("data/knni_matrix.data"));
56  std::string wenni_result(test::filename("data/wenni_result.data"));
57  std::string wenni_weight(test::filename("data/wenni_weight.data"));
58  check_file_access(knni_data);
59  check_file_access(knni_result);
60  check_file_access(knni_weight);
61  check_file_access(wenni_data);
62  check_file_access(wenni_result);
63  check_file_access(wenni_weight);
64
65  // test kNNI
66  std::ifstream data_stream(knni_data.c_str());
67  std::ifstream weight_stream(knni_weight.c_str());
68  utility::Matrix data(data_stream);
69  utility::Matrix weight(weight_stream);
70  utility::kNNI knni(data,weight,neighbours);
71  knni.estimate();
72  std::ifstream control_stream(knni_result.c_str());
73  utility::Matrix control(control_stream);
74  control-=knni.imputed_data();
75  // error_bound is arbitrarily selected
76  double error_bound = std::numeric_limits<double>::epsilon()*1e4;
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.