source: trunk/test/nni_test.cc @ 1669

Last change on this file since 1669 was 1669, checked in by Peter, 13 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
RevLine 
[145]1// $Id: nni_test.cc 1669 2008-12-22 03:00:06Z peter $
2
[675]3/*
[831]4  Copyright (C) 2004 Jari Häkkinen
5  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
[1275]6  Copyright (C) 2006 Jari Häkkinen
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
[301]8
[1437]9  This file is part of the yat library, http://dev.thep.lu.se/yat
[675]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
[1486]13  published by the Free Software Foundation; either version 3 of the
[675]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
[1487]22  along with yat. If not, see <http://www.gnu.org/licenses/>.
[675]23*/
24
[1242]25#include "Suite.h"
26
[710]27#include "yat/utility/FileUtil.h"
[1121]28#include "yat/utility/Matrix.h"
[675]29#include "yat/utility/kNNI.h"
30#include "yat/utility/WeNNI.h"
31
[196]32#include <cmath>
[145]33#include <iostream>
34#include <fstream>
35#include <string>
36
[680]37using namespace theplu::yat;
[145]38
[146]39void check_file_access(std::string& str)
[145]40{
[711]41  if (utility::FileUtil(str).permissions("r")) {
[146]42    std::cerr << "test_nni: Cannot access file " << str << std::endl;
43    exit(-1);
[145]44  }
45}
46
[1242]47int main(int argc, char* argv[])
[145]48{
[1242]49  test::Suite suite(argc, argv);
50
[1273]51  unsigned int neighbours=3;
[1251]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"));
[146]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);
[145]64
[146]65  // test kNNI
66  std::ifstream data_stream(knni_data.c_str());
67  std::ifstream weight_stream(knni_weight.c_str());
[1121]68  utility::Matrix data(data_stream);
69  utility::Matrix weight(weight_stream);
[301]70  utility::kNNI knni(data,weight,neighbours);
[299]71  knni.estimate();
[146]72  std::ifstream control_stream(knni_result.c_str());
[1121]73  utility::Matrix control(control_stream);
[1669]74  double error_bound = 1e-11;
[145]75  for (unsigned int i=0; i<control.rows(); i++)
76    for (unsigned int j=0; j<control.columns(); j++)
[1669]77      if (!suite.equal_fix(knni.imputed_data()(i,j),control(i,j),error_bound)) {
[1242]78        suite.err() << "kNNI FAILED, error on row " << i << " and " 
[1669]79                    << "column " << j << " is" << std::endl;
[1242]80        suite.add(false); // calculation result out of accepted error bounds
[145]81      }
[146]82  control_stream.close();
83  data_stream.close();
84  weight_stream.close();
85
86  // test WeNNI
87  data_stream.open(wenni_data.c_str());
[1121]88  data=utility::Matrix(data_stream);
[146]89  weight_stream.open(wenni_weight.c_str());
[1121]90  weight=utility::Matrix(weight_stream);
[301]91  utility::WeNNI wenni(data,weight,neighbours);
[299]92  wenni.estimate();
[146]93  control_stream.open(wenni_result.c_str());
[1121]94  control=utility::Matrix(control_stream);
[146]95  for (unsigned int i=0; i<control.rows(); i++)
96    for (unsigned int j=0; j<control.columns(); j++)
[1669]97      if (!suite.equal_fix(wenni.imputed_data()(i,j),control(i,j),error_bound)){
[1242]98        suite.err() << "WeNNI FAILED, error on row " << i << " and " 
[1669]99                    << "column " << j << std::endl;
[1242]100        suite.add(false); // calculation result out of accepted error bounds
[146]101      }
[335]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());
[1121]108  data=utility::Matrix(data_stream);
[335]109  weight_stream.open(knni_weight.c_str());
[1121]110  weight=utility::Matrix(weight_stream);
[335]111  utility::WeNNI wenni2(data,weight,neighbours);
112  wenni2.estimate();
113  control_stream.open(knni_result.c_str());
[1121]114  control=utility::Matrix(control_stream);
[335]115  for (unsigned int i=0; i<control.rows(); i++)
116    for (unsigned int j=0; j<control.columns(); j++)
[1669]117      if (!suite.equal_fix(wenni2.imputed_data()(i,j),control(i,j),error_bound)){
[1242]118        suite.err() << "WeNNI binary weight test FAILED.\nError on row " << i
[1669]119                    << " and column " << j << std::endl;
[1242]120        suite.add(false); // calculation result out of accepted error bounds
[335]121      }
122  control_stream.close();
123  data_stream.close();
124  weight_stream.close();
125
[1242]126  return suite.return_value();
[145]127}
Note: See TracBrowser for help on using the repository browser.