1 | // $Id: nni_test.cc 1726 2009-01-15 21:15:26Z 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 | |
---|
37 | using namespace theplu::yat; |
---|
38 | |
---|
39 | void 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 | |
---|
47 | int 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 | unsigned int nonimputed=knni.estimate(); |
---|
72 | if (!suite.equal(nonimputed,15)) { |
---|
73 | suite.err() << "kNNI FAILED, unexpected number of non imputed rows" << std::endl; |
---|
74 | suite.add(false); |
---|
75 | } |
---|
76 | std::ifstream control_stream(knni_result.c_str()); |
---|
77 | utility::Matrix control(control_stream); |
---|
78 | double error_bound = 1e-11; |
---|
79 | for (unsigned int i=0; i<control.rows(); i++) |
---|
80 | for (unsigned int j=0; j<control.columns(); j++) |
---|
81 | if (!suite.equal_fix(knni.imputed_data()(i,j),control(i,j),error_bound)) { |
---|
82 | suite.err() << "kNNI FAILED, error on row " << i << " and " |
---|
83 | << "column " << j << " is" << std::endl; |
---|
84 | suite.add(false); // calculation result out of accepted error bounds |
---|
85 | } |
---|
86 | control_stream.close(); |
---|
87 | data_stream.close(); |
---|
88 | weight_stream.close(); |
---|
89 | |
---|
90 | // test WeNNI |
---|
91 | data_stream.open(wenni_data.c_str()); |
---|
92 | data=utility::Matrix(data_stream); |
---|
93 | weight_stream.open(wenni_weight.c_str()); |
---|
94 | weight=utility::Matrix(weight_stream); |
---|
95 | utility::WeNNI wenni(data,weight,neighbours); |
---|
96 | nonimputed=wenni.estimate(); |
---|
97 | if (!suite.equal(nonimputed,15)) { |
---|
98 | suite.err() << "WeNNI FAILED, unexpected number of non imputed rows" << std::endl; |
---|
99 | suite.add(false); |
---|
100 | } |
---|
101 | control_stream.open(wenni_result.c_str()); |
---|
102 | control=utility::Matrix(control_stream); |
---|
103 | for (unsigned int i=0; i<control.rows(); i++) |
---|
104 | for (unsigned int j=0; j<control.columns(); j++) |
---|
105 | if (!suite.equal_fix(wenni.imputed_data()(i,j),control(i,j),error_bound)){ |
---|
106 | suite.err() << "WeNNI FAILED, error on row " << i << " and " |
---|
107 | << "column " << j << std::endl; |
---|
108 | suite.add(false); // calculation result out of accepted error bounds |
---|
109 | } |
---|
110 | control_stream.close(); |
---|
111 | data_stream.close(); |
---|
112 | weight_stream.close(); |
---|
113 | |
---|
114 | // test WeNNI with binary weights |
---|
115 | data_stream.open(knni_data.c_str()); |
---|
116 | data=utility::Matrix(data_stream); |
---|
117 | weight_stream.open(knni_weight.c_str()); |
---|
118 | weight=utility::Matrix(weight_stream); |
---|
119 | utility::WeNNI wenni2(data,weight,neighbours); |
---|
120 | nonimputed=wenni2.estimate(); |
---|
121 | if (!suite.equal(nonimputed,15)) { |
---|
122 | suite.err() << "binary WeNNI FAILED, unexpected number of non imputed rows" |
---|
123 | << std::endl; |
---|
124 | suite.add(false); |
---|
125 | } |
---|
126 | control_stream.open(knni_result.c_str()); |
---|
127 | control=utility::Matrix(control_stream); |
---|
128 | for (unsigned int i=0; i<control.rows(); i++) |
---|
129 | for (unsigned int j=0; j<control.columns(); j++) |
---|
130 | if (!suite.equal_fix(wenni2.imputed_data()(i,j),control(i,j),error_bound)){ |
---|
131 | suite.err() << "WeNNI binary weight test FAILED.\nError on row " << i |
---|
132 | << " and column " << j << std::endl; |
---|
133 | suite.add(false); // calculation result out of accepted error bounds |
---|
134 | } |
---|
135 | control_stream.close(); |
---|
136 | data_stream.close(); |
---|
137 | weight_stream.close(); |
---|
138 | |
---|
139 | return suite.return_value(); |
---|
140 | } |
---|