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