source: trunk/test/nni_test.cc @ 1669

Last change on this file since 1669 was 1669, checked in by Peter, 12 years ago

refs #470: nni_test

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1// $Id: nni_test.cc 1669 2008-12-22 03:00:06Z peter $
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  double error_bound = 1e-11;
75  for (unsigned int i=0; i<control.rows(); i++)
76    for (unsigned int j=0; j<control.columns(); j++)
77      if (!suite.equal_fix(knni.imputed_data()(i,j),control(i,j),error_bound)) {
78        suite.err() << "kNNI FAILED, error on row " << i << " and " 
79                    << "column " << j << " is" << std::endl;
80        suite.add(false); // calculation result out of accepted error bounds
81      }
82  control_stream.close();
83  data_stream.close();
84  weight_stream.close();
85
86  // test WeNNI
87  data_stream.open(wenni_data.c_str());
88  data=utility::Matrix(data_stream);
89  weight_stream.open(wenni_weight.c_str());
90  weight=utility::Matrix(weight_stream);
91  utility::WeNNI wenni(data,weight,neighbours);
92  wenni.estimate();
93  control_stream.open(wenni_result.c_str());
94  control=utility::Matrix(control_stream);
95  for (unsigned int i=0; i<control.rows(); i++)
96    for (unsigned int j=0; j<control.columns(); j++)
97      if (!suite.equal_fix(wenni.imputed_data()(i,j),control(i,j),error_bound)){
98        suite.err() << "WeNNI FAILED, error on row " << i << " and " 
99                    << "column " << j << std::endl;
100        suite.add(false); // calculation result out of accepted error bounds
101      }
102  control_stream.close();
103  data_stream.close();
104  weight_stream.close();
105
106  // test WeNNI with binary weights
107  data_stream.open(knni_data.c_str());
108  data=utility::Matrix(data_stream);
109  weight_stream.open(knni_weight.c_str());
110  weight=utility::Matrix(weight_stream);
111  utility::WeNNI wenni2(data,weight,neighbours);
112  wenni2.estimate();
113  control_stream.open(knni_result.c_str());
114  control=utility::Matrix(control_stream);
115  for (unsigned int i=0; i<control.rows(); i++)
116    for (unsigned int j=0; j<control.columns(); j++)
117      if (!suite.equal_fix(wenni2.imputed_data()(i,j),control(i,j),error_bound)){
118        suite.err() << "WeNNI binary weight test FAILED.\nError on row " << i
119                    << " and column " << j << std::endl;
120        suite.add(false); // calculation result out of accepted error bounds
121      }
122  control_stream.close();
123  data_stream.close();
124  weight_stream.close();
125
126  return suite.return_value();
127}
Note: See TracBrowser for help on using the repository browser.