1 | // $Id: nni_test.cc 759 2007-02-19 19:41:25Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) The authors contributing to this file. |
---|
5 | |
---|
6 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "yat/utility/FileUtil.h" |
---|
25 | #include "yat/utility/matrix.h" |
---|
26 | #include "yat/utility/kNNI.h" |
---|
27 | #include "yat/utility/WeNNI.h" |
---|
28 | |
---|
29 | #include <cmath> |
---|
30 | #include <iostream> |
---|
31 | #include <fstream> |
---|
32 | #include <string> |
---|
33 | |
---|
34 | using namespace theplu::yat; |
---|
35 | |
---|
36 | void check_file_access(std::string& str) |
---|
37 | { |
---|
38 | if (utility::FileUtil(str).permissions("r")) { |
---|
39 | std::cerr << "test_nni: Cannot access file " << str << std::endl; |
---|
40 | exit(-1); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | int main(const int argc,const char* argv[]) |
---|
45 | { |
---|
46 | bool print = (argc>1 && argv[1]==std::string("-p")); |
---|
47 | bool ok = true; |
---|
48 | uint neighbours=3; |
---|
49 | std::string knni_data("data/knni_matrix.data"); |
---|
50 | std::string knni_result("data/knni_result.data"); |
---|
51 | std::string knni_weight("data/knni_weight.data"); |
---|
52 | std::string wenni_data("data/knni_matrix.data"); |
---|
53 | std::string wenni_result("data/wenni_result.data"); |
---|
54 | std::string wenni_weight("data/wenni_weight.data"); |
---|
55 | check_file_access(knni_data); |
---|
56 | check_file_access(knni_result); |
---|
57 | check_file_access(knni_weight); |
---|
58 | check_file_access(wenni_data); |
---|
59 | check_file_access(wenni_result); |
---|
60 | check_file_access(wenni_weight); |
---|
61 | |
---|
62 | // test kNNI |
---|
63 | std::ifstream data_stream(knni_data.c_str()); |
---|
64 | std::ifstream weight_stream(knni_weight.c_str()); |
---|
65 | utility::matrix data(data_stream); |
---|
66 | utility::matrix weight(weight_stream); |
---|
67 | utility::kNNI knni(data,weight,neighbours); |
---|
68 | knni.estimate(); |
---|
69 | std::ifstream control_stream(knni_result.c_str()); |
---|
70 | utility::matrix control(control_stream); |
---|
71 | control-=knni.imputed_data(); |
---|
72 | double error_bound = 5e-13; |
---|
73 | for (unsigned int i=0; i<control.rows(); i++) |
---|
74 | for (unsigned int j=0; j<control.columns(); j++) |
---|
75 | if (fabs(control(i,j))>error_bound) { |
---|
76 | if (print) |
---|
77 | std::cerr << "kNNI FAILED, error on row " << i << " and " |
---|
78 | << "column " << j << " is " << fabs(control(i,j)) |
---|
79 | << ". Expected less than " << error_bound << std::endl; |
---|
80 | ok=false; // calculation result out of accepted error bounds |
---|
81 | } |
---|
82 | else |
---|
83 | if (!((control(i,j)==control(i,j)))) { |
---|
84 | if (print) |
---|
85 | std::cerr << "kNNI FAILED, nan (not a number) " |
---|
86 | << "encountered in test on row " << i |
---|
87 | << " and column " << j << std::endl; |
---|
88 | ok =false; // calucaltion error detected |
---|
89 | } |
---|
90 | control_stream.close(); |
---|
91 | data_stream.close(); |
---|
92 | weight_stream.close(); |
---|
93 | |
---|
94 | // test WeNNI |
---|
95 | data_stream.open(wenni_data.c_str()); |
---|
96 | data=utility::matrix(data_stream); |
---|
97 | weight_stream.open(wenni_weight.c_str()); |
---|
98 | weight=utility::matrix(weight_stream); |
---|
99 | utility::WeNNI wenni(data,weight,neighbours); |
---|
100 | wenni.estimate(); |
---|
101 | control_stream.open(wenni_result.c_str()); |
---|
102 | control=utility::matrix(control_stream); |
---|
103 | control-=wenni.imputed_data(); |
---|
104 | for (unsigned int i=0; i<control.rows(); i++) |
---|
105 | for (unsigned int j=0; j<control.columns(); j++) |
---|
106 | if (fabs(control(i,j))>error_bound) { |
---|
107 | if (print) |
---|
108 | std::cerr << "WeNNI FAILED, error on row " << i << " and " |
---|
109 | << "column " << j << " is " << fabs(control(i,j)) |
---|
110 | << ". Expected less than " << error_bound << std::endl; |
---|
111 | ok=false; // calculation result out of accepted error bounds |
---|
112 | } |
---|
113 | else |
---|
114 | if (!((control(i,j)==control(i,j)))) { |
---|
115 | if (print) |
---|
116 | std::cerr << "WeNNI FAILED, nan (not a number) " |
---|
117 | << "encountered in test on row " << i |
---|
118 | << " and column " << j << std::endl; |
---|
119 | ok=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 (fabs(control(i,j))>error_bound) { |
---|
138 | if (print) |
---|
139 | std::cerr << "WeNNI binary weight test FAILED.\nError on row " << i |
---|
140 | << " and column " << j << " is " << fabs(control(i,j)) |
---|
141 | << ". Expected less than " << error_bound << std::endl; |
---|
142 | ok=false; // calculation result out of accepted error bounds |
---|
143 | } |
---|
144 | else |
---|
145 | if (!((control(i,j)==control(i,j)))) { |
---|
146 | if (print) |
---|
147 | std::cerr << "WeNNI binary wieght test FAILED.\n" |
---|
148 | << "nan (not a number) encountered in test on row " << i |
---|
149 | << " and column " << j << std::endl; |
---|
150 | ok=false; // calucaltion error detected |
---|
151 | } |
---|
152 | control_stream.close(); |
---|
153 | data_stream.close(); |
---|
154 | weight_stream.close(); |
---|
155 | |
---|
156 | return (ok ? 0 : -1); |
---|
157 | } |
---|