test/nni.cc

Code
Comments
Other
Rev Date Author Line
145 05 Sep 04 jari 1 // $Id$
145 05 Sep 04 jari 2
675 10 Oct 06 jari 3 /*
2119 12 Dec 09 peter 4   Copyright (C) 2004 Jari Häkkinen
2119 12 Dec 09 peter 5   Copyright (C) 2005 Jari Häkkinen, Peter Johansson
2119 12 Dec 09 peter 6   Copyright (C) 2006 Jari Häkkinen
2119 12 Dec 09 peter 7   Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
2119 12 Dec 09 peter 8   Copyright (C) 2009 Jari Häkkinen
4207 26 Aug 22 peter 9   Copyright (C) 2012, 2022 Peter Johansson
301 30 Apr 05 peter 10
1437 25 Aug 08 peter 11   This file is part of the yat library, http://dev.thep.lu.se/yat
675 10 Oct 06 jari 12
675 10 Oct 06 jari 13   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 14   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 15   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 16   License, or (at your option) any later version.
675 10 Oct 06 jari 17
675 10 Oct 06 jari 18   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 19   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 21   General Public License for more details.
675 10 Oct 06 jari 22
675 10 Oct 06 jari 23   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 24   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 25 */
675 10 Oct 06 jari 26
2881 18 Nov 12 peter 27 #include <config.h>
2881 18 Nov 12 peter 28
1242 16 Mar 08 peter 29 #include "Suite.h"
1242 16 Mar 08 peter 30
710 20 Dec 06 jari 31 #include "yat/utility/FileUtil.h"
1121 22 Feb 08 peter 32 #include "yat/utility/Matrix.h"
675 10 Oct 06 jari 33 #include "yat/utility/kNNI.h"
675 10 Oct 06 jari 34 #include "yat/utility/WeNNI.h"
675 10 Oct 06 jari 35
196 27 Oct 04 jari 36 #include <cmath>
145 05 Sep 04 jari 37 #include <iostream>
145 05 Sep 04 jari 38 #include <fstream>
145 05 Sep 04 jari 39 #include <string>
145 05 Sep 04 jari 40
680 11 Oct 06 jari 41 using namespace theplu::yat;
145 05 Sep 04 jari 42
146 08 Sep 04 jari 43 void check_file_access(std::string& str)
145 05 Sep 04 jari 44 {
711 21 Dec 06 jari 45   if (utility::FileUtil(str).permissions("r")) {
146 08 Sep 04 jari 46     std::cerr << "test_nni: Cannot access file " << str << std::endl;
146 08 Sep 04 jari 47     exit(-1);
145 05 Sep 04 jari 48   }
145 05 Sep 04 jari 49 }
145 05 Sep 04 jari 50
1242 16 Mar 08 peter 51 int main(int argc, char* argv[])
145 05 Sep 04 jari 52 {
1242 16 Mar 08 peter 53   test::Suite suite(argc, argv);
1727 15 Jan 09 jari 54   suite.err() << "Testing NNI algorithms" << std::endl;
1242 16 Mar 08 peter 55
1727 15 Jan 09 jari 56   suite.err() << "Reading data" << std::endl;
1273 10 Apr 08 jari 57   unsigned int neighbours=3;
1251 03 Apr 08 peter 58   std::string knni_data(test::filename("data/knni_matrix.data"));
1251 03 Apr 08 peter 59   std::string knni_result(test::filename("data/knni_result.data"));
1251 03 Apr 08 peter 60   std::string knni_weight(test::filename("data/knni_weight.data"));
1251 03 Apr 08 peter 61   std::string wenni_data(test::filename("data/knni_matrix.data"));
1251 03 Apr 08 peter 62   std::string wenni_result(test::filename("data/wenni_result.data"));
1251 03 Apr 08 peter 63   std::string wenni_weight(test::filename("data/wenni_weight.data"));
146 08 Sep 04 jari 64   check_file_access(knni_data);
146 08 Sep 04 jari 65   check_file_access(knni_result);
146 08 Sep 04 jari 66   check_file_access(knni_weight);
146 08 Sep 04 jari 67   check_file_access(wenni_data);
146 08 Sep 04 jari 68   check_file_access(wenni_result);
146 08 Sep 04 jari 69   check_file_access(wenni_weight);
145 05 Sep 04 jari 70
146 08 Sep 04 jari 71   // test kNNI
1727 15 Jan 09 jari 72   suite.err() << "Testing kNNI" << std::endl;
146 08 Sep 04 jari 73   std::ifstream data_stream(knni_data.c_str());
146 08 Sep 04 jari 74   std::ifstream weight_stream(knni_weight.c_str());
1121 22 Feb 08 peter 75   utility::Matrix data(data_stream);
1121 22 Feb 08 peter 76   utility::Matrix weight(weight_stream);
301 30 Apr 05 peter 77   utility::kNNI knni(data,weight,neighbours);
1726 15 Jan 09 jari 78   unsigned int nonimputed=knni.estimate();
1726 15 Jan 09 jari 79   if (!suite.equal(nonimputed,15)) {
1726 15 Jan 09 jari 80     suite.err() << "kNNI FAILED, unexpected number of non imputed rows" << std::endl;
1726 15 Jan 09 jari 81     suite.add(false);
1726 15 Jan 09 jari 82   }
146 08 Sep 04 jari 83   std::ifstream control_stream(knni_result.c_str());
1121 22 Feb 08 peter 84   utility::Matrix control(control_stream);
1669 22 Dec 08 peter 85   double error_bound = 1e-11;
145 05 Sep 04 jari 86   for (unsigned int i=0; i<control.rows(); i++)
145 05 Sep 04 jari 87     for (unsigned int j=0; j<control.columns(); j++)
1727 15 Jan 09 jari 88       if ((i==10) && (j==10)) {
1727 15 Jan 09 jari 89         if (!std::isnan(knni.imputed_data()(i,j)-control(i,j))) {
4200 19 Aug 22 peter 90           suite.err() << "kNNI FAILED, error on row " << i << " and "
1727 15 Jan 09 jari 91                       << "column " << j << " expected NaN" << std::endl;
1727 15 Jan 09 jari 92           suite.add(false);
1727 15 Jan 09 jari 93         }
1727 15 Jan 09 jari 94       }
1727 15 Jan 09 jari 95       else if (!suite.equal_fix(knni.imputed_data()(i,j),control(i,j),error_bound)) {
4200 19 Aug 22 peter 96         suite.err() << "kNNI FAILED, error on row " << i << " and "
1727 15 Jan 09 jari 97                     << "column " << j << std::endl;
1242 16 Mar 08 peter 98         suite.add(false); // calculation result out of accepted error bounds
145 05 Sep 04 jari 99       }
146 08 Sep 04 jari 100   control_stream.close();
146 08 Sep 04 jari 101   data_stream.close();
146 08 Sep 04 jari 102   weight_stream.close();
146 08 Sep 04 jari 103
146 08 Sep 04 jari 104   // test WeNNI
1727 15 Jan 09 jari 105   suite.err() << "Testing WeNNI" << std::endl;
146 08 Sep 04 jari 106   data_stream.open(wenni_data.c_str());
1121 22 Feb 08 peter 107   data=utility::Matrix(data_stream);
146 08 Sep 04 jari 108   weight_stream.open(wenni_weight.c_str());
1121 22 Feb 08 peter 109   weight=utility::Matrix(weight_stream);
301 30 Apr 05 peter 110   utility::WeNNI wenni(data,weight,neighbours);
1726 15 Jan 09 jari 111   nonimputed=wenni.estimate();
1726 15 Jan 09 jari 112   if (!suite.equal(nonimputed,15)) {
1726 15 Jan 09 jari 113     suite.err() << "WeNNI FAILED, unexpected number of non imputed rows" << std::endl;
1726 15 Jan 09 jari 114     suite.add(false);
1726 15 Jan 09 jari 115   }
146 08 Sep 04 jari 116   control_stream.open(wenni_result.c_str());
1121 22 Feb 08 peter 117   control=utility::Matrix(control_stream);
146 08 Sep 04 jari 118   for (unsigned int i=0; i<control.rows(); i++)
146 08 Sep 04 jari 119     for (unsigned int j=0; j<control.columns(); j++)
1727 15 Jan 09 jari 120       if ((i==10) && (j==10)) {
1727 15 Jan 09 jari 121         if (!std::isnan(knni.imputed_data()(i,j)-control(i,j))) {
4200 19 Aug 22 peter 122           suite.err() << "WeNNI FAILED, error on row " << i << " and "
1727 15 Jan 09 jari 123                       << "column " << j << " expected NaN" << std::endl;
1727 15 Jan 09 jari 124           suite.add(false);
1727 15 Jan 09 jari 125         }
1727 15 Jan 09 jari 126       }
1727 15 Jan 09 jari 127       else if (!suite.equal_fix(wenni.imputed_data()(i,j),control(i,j),error_bound)){
4200 19 Aug 22 peter 128         suite.err() << "WeNNI FAILED, error on row " << i << " and "
1669 22 Dec 08 peter 129                     << "column " << j << std::endl;
1242 16 Mar 08 peter 130         suite.add(false); // calculation result out of accepted error bounds
146 08 Sep 04 jari 131       }
335 02 Jun 05 jari 132   control_stream.close();
335 02 Jun 05 jari 133   data_stream.close();
335 02 Jun 05 jari 134   weight_stream.close();
335 02 Jun 05 jari 135
335 02 Jun 05 jari 136   // test WeNNI with binary weights
1727 15 Jan 09 jari 137   suite.err() << "Testing WeNNI with binary weights" << std::endl;
335 02 Jun 05 jari 138   data_stream.open(knni_data.c_str());
1121 22 Feb 08 peter 139   data=utility::Matrix(data_stream);
335 02 Jun 05 jari 140   weight_stream.open(knni_weight.c_str());
1121 22 Feb 08 peter 141   weight=utility::Matrix(weight_stream);
335 02 Jun 05 jari 142   utility::WeNNI wenni2(data,weight,neighbours);
1726 15 Jan 09 jari 143   nonimputed=wenni2.estimate();
1726 15 Jan 09 jari 144   if (!suite.equal(nonimputed,15)) {
1726 15 Jan 09 jari 145     suite.err() << "binary WeNNI FAILED, unexpected number of non imputed rows"
1726 15 Jan 09 jari 146                 << std::endl;
1726 15 Jan 09 jari 147     suite.add(false);
1726 15 Jan 09 jari 148   }
335 02 Jun 05 jari 149   control_stream.open(knni_result.c_str());
1121 22 Feb 08 peter 150   control=utility::Matrix(control_stream);
335 02 Jun 05 jari 151   for (unsigned int i=0; i<control.rows(); i++)
335 02 Jun 05 jari 152     for (unsigned int j=0; j<control.columns(); j++)
1727 15 Jan 09 jari 153       if ((i==10) && (j==10)) {
1727 15 Jan 09 jari 154         if (!std::isnan(knni.imputed_data()(i,j)-control(i,j))) {
4200 19 Aug 22 peter 155           suite.err() << "binary WeNNI FAILED, error on row " << i << " and "
1727 15 Jan 09 jari 156                       << "column " << j << " expected NaN" << std::endl;
1727 15 Jan 09 jari 157           suite.add(false);
1727 15 Jan 09 jari 158         }
1727 15 Jan 09 jari 159       }
1727 15 Jan 09 jari 160       else if (!suite.equal_fix(wenni2.imputed_data()(i,j),control(i,j),error_bound)){
1242 16 Mar 08 peter 161         suite.err() << "WeNNI binary weight test FAILED.\nError on row " << i
1669 22 Dec 08 peter 162                     << " and column " << j << std::endl;
1242 16 Mar 08 peter 163         suite.add(false); // calculation result out of accepted error bounds
335 02 Jun 05 jari 164       }
335 02 Jun 05 jari 165   control_stream.close();
335 02 Jun 05 jari 166   data_stream.close();
335 02 Jun 05 jari 167   weight_stream.close();
335 02 Jun 05 jari 168
1242 16 Mar 08 peter 169   return suite.return_value();
145 05 Sep 04 jari 170 }