1 | // $Id: wenni_test.cc 825 2008-11-26 22:42:17Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006 Jari Häkkinen |
---|
5 | Copyright (C) 2007 Peter Johansson |
---|
6 | |
---|
7 | This file is part of WeNNI, |
---|
8 | http://baseplugins.thep.lu.se/wiki/se.lu.thep.WeNNI |
---|
9 | |
---|
10 | WeNNI is free software; you can redistribute it and/or modify it |
---|
11 | under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 3 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | WeNNI is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
17 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
---|
18 | License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with WeNNI. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "c++_tools/gslapi/matrix.h" |
---|
25 | |
---|
26 | #include <cmath> |
---|
27 | #include <cstdlib> |
---|
28 | #include <iostream> |
---|
29 | #include <fstream> |
---|
30 | #include <string> |
---|
31 | #include <unistd.h> |
---|
32 | |
---|
33 | bool check_default_results(void); |
---|
34 | bool check_knni_results(void); |
---|
35 | bool check_raw_results(void); |
---|
36 | bool check_wenni_snr_results(void); |
---|
37 | bool cleanup(void); |
---|
38 | bool compare(const std::string&, const std::string&, double error_bound=1e-20); |
---|
39 | bool diff(const std::string&, const std::string&); |
---|
40 | bool rm(const std::string& filename); |
---|
41 | |
---|
42 | |
---|
43 | int main(const int argc,const char* argv[]) |
---|
44 | { |
---|
45 | bool teststatus=false; |
---|
46 | |
---|
47 | teststatus|=check_raw_results(); |
---|
48 | teststatus|=check_default_results(); |
---|
49 | |
---|
50 | std::string system_call= "./nni -data default_results/wenni_logratio.data"; |
---|
51 | system_call+=" -neighbours 10 -weight ../data/wenni_snr.data -weight_is_snr"; |
---|
52 | system_call+=" > wenni_imputed.data"; |
---|
53 | if (system(system_call.c_str())) { |
---|
54 | teststatus|=true; |
---|
55 | std::cerr << "ERROR: system call failed:\n\t" << system_call << std::endl; |
---|
56 | } |
---|
57 | teststatus|=check_wenni_snr_results(); |
---|
58 | |
---|
59 | system_call= "./nni -data default_results/wenni_logratio.data -neighbours 10"; |
---|
60 | system_call+=" -nni_algorithm kNNI -weight default_results/wenni_weight.data"; |
---|
61 | system_call+=" -weight_cutoff 0.5 > knni_imputed.data"; |
---|
62 | if (system(system_call.c_str())) { |
---|
63 | teststatus|=true; |
---|
64 | std::cerr << "ERROR: system call failed:\n\t" << system_call << std::endl; |
---|
65 | } |
---|
66 | teststatus|=check_knni_results(); |
---|
67 | |
---|
68 | //teststatus|=cleanup(); |
---|
69 | |
---|
70 | return (teststatus ? -1 : 0); |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | bool check_default_results(void) |
---|
75 | { |
---|
76 | bool teststatus=false; |
---|
77 | teststatus|=diff("default_results/BASEfile.data","../data/basefile_in.data"); |
---|
78 | teststatus|=diff("default_results/basefile_out.data", |
---|
79 | "../data/basefile_out.data"); |
---|
80 | teststatus|=compare("default_results/wenni_BCh1SD.data", |
---|
81 | "../data/wenni_BCh1SD.data"); |
---|
82 | teststatus|=compare("default_results/wenni_BCh2SD.data", |
---|
83 | "../data/wenni_BCh2SD.data"); |
---|
84 | teststatus|=compare("default_results/wenni_imputed.data", |
---|
85 | "../data/wenni_imputed.data"); |
---|
86 | teststatus|=compare("default_results/wenni_intensity1.data", |
---|
87 | "../data/wenni_intensity1.data"); |
---|
88 | teststatus|=compare("default_results/wenni_intensity2.data", |
---|
89 | "../data/wenni_intensity2.data"); |
---|
90 | teststatus|=compare("default_results/wenni_logratio.data", |
---|
91 | "../data/wenni_logratio.data"); |
---|
92 | teststatus|=compare("default_results/wenni_weight.data", |
---|
93 | "../data/wenni_weight.data"); |
---|
94 | if (teststatus) |
---|
95 | std::cerr << "ERROR: check_default_results failed." << std::endl; |
---|
96 | return teststatus; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | bool check_knni_results(void) |
---|
101 | { |
---|
102 | bool teststatus=false; |
---|
103 | teststatus|=diff("knni_imputed.data","../data/knni_imputed.data"); |
---|
104 | if (teststatus) |
---|
105 | std::cerr << "ERROR: check_knni_results failed." << std::endl; |
---|
106 | return teststatus; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | bool check_raw_results(void) |
---|
111 | { |
---|
112 | bool teststatus=false; |
---|
113 | teststatus|=diff("raw_results/BASEfile.data","../data/basefile_in.data"); |
---|
114 | teststatus|=diff("raw_results/basefile_out.data","../data/basefile_out.data"); |
---|
115 | teststatus|=compare("raw_results/wenni_BCh1Mean.data", |
---|
116 | "../data/wenni_BCh1Mean.data"); |
---|
117 | teststatus|=compare("raw_results/wenni_BCh1SD.data", |
---|
118 | "../data/wenni_BCh1SD.data"); |
---|
119 | teststatus|=compare("raw_results/wenni_BCh2Mean.data", |
---|
120 | "../data/wenni_BCh2Mean.data"); |
---|
121 | teststatus|=compare("raw_results/wenni_BCh2SD.data", |
---|
122 | "../data/wenni_BCh2SD.data"); |
---|
123 | teststatus|=compare("raw_results/wenni_FCh1Mean.data", |
---|
124 | "../data/wenni_FCh1Mean.data"); |
---|
125 | teststatus|=compare("raw_results/wenni_FCh2Mean.data", |
---|
126 | "../data/wenni_FCh2Mean.data"); |
---|
127 | teststatus|=compare("raw_results/wenni_imputed.data", |
---|
128 | "../data/wenni_imputed.data"); |
---|
129 | teststatus|=compare("raw_results/wenni_logratio.data", |
---|
130 | "../data/wenni_logratio.data"); |
---|
131 | teststatus|=compare("raw_results/wenni_weight.data", |
---|
132 | "../data/wenni_weight.data"); |
---|
133 | if (teststatus) |
---|
134 | std::cerr << "ERROR: check_raw_results failed." << std::endl; |
---|
135 | return teststatus; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | bool check_wenni_snr_results(void) |
---|
140 | { |
---|
141 | bool teststatus=false; |
---|
142 | teststatus|=compare("wenni_imputed.data","../data/wenni_imputed.data", |
---|
143 | 1.1e-11); |
---|
144 | if (teststatus) |
---|
145 | std::cerr << "ERROR: check_wenni_results failed." << std::endl; |
---|
146 | return teststatus; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | bool cleanup(void) |
---|
151 | { |
---|
152 | bool teststatus=false; |
---|
153 | teststatus|=rm("BASEfile.data"); |
---|
154 | teststatus|=rm("basefile_out.data"); |
---|
155 | teststatus|=rm("knni_imputed.data"); |
---|
156 | teststatus|=rm("wenni_BCh1Mean.data"); |
---|
157 | teststatus|=rm("wenni_BCh1SD.data"); |
---|
158 | teststatus|=rm("wenni_BCh2Mean.data"); |
---|
159 | teststatus|=rm("wenni_BCh2SD.data"); |
---|
160 | teststatus|=rm("wenni_FCh1Mean.data"); |
---|
161 | teststatus|=rm("wenni_FCh2Mean.data"); |
---|
162 | teststatus|=rm("wenni_imputed.data"); |
---|
163 | teststatus|=rm("wenni_intensity1.data"); |
---|
164 | teststatus|=rm("wenni_intensity2.data"); |
---|
165 | teststatus|=rm("wenni_logratio.data"); |
---|
166 | teststatus|=rm("wenni_weight.data"); |
---|
167 | return teststatus; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | bool compare(const std::string& a,const std::string& b, double error_bound) |
---|
172 | { |
---|
173 | bool teststatus=false; |
---|
174 | std::ifstream astream(a.c_str()); |
---|
175 | std::ifstream bstream(b.c_str()); |
---|
176 | theplu::gslapi::matrix adata(astream); |
---|
177 | theplu::gslapi::matrix bdata(bstream); |
---|
178 | adata-=bdata; |
---|
179 | for (size_t i=0; i<adata.rows(); i++) |
---|
180 | for (size_t j=0; j<adata.columns(); j++) |
---|
181 | if ((fabs(adata(i,j))>error_bound) || // result outside acceptable bounds |
---|
182 | (!((adata(i,j)==adata(i,j))))) // calucaltion error detected |
---|
183 | teststatus=true; |
---|
184 | astream.close(); |
---|
185 | bstream.close(); |
---|
186 | if (teststatus) |
---|
187 | std::cerr << "ERROR: compare " << a << " " << b << std::endl; |
---|
188 | return teststatus; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | bool diff(const std::string& a,const std::string& b) |
---|
193 | { |
---|
194 | std::ifstream is1(a.c_str()); |
---|
195 | std::ifstream is2(a.c_str()); |
---|
196 | std::string str1; |
---|
197 | std::string str2; |
---|
198 | while (getline(is1,str1)){ |
---|
199 | getline(is2,str2); |
---|
200 | if (str1!=str2) { |
---|
201 | std::cerr << "ERROR: diff " << a << " " << b << std::endl; |
---|
202 | return true; |
---|
203 | } |
---|
204 | } |
---|
205 | return false; |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | bool rm(const std::string& filename) { |
---|
210 | if (unlink(filename.c_str())) { |
---|
211 | std::cerr << "ERROR: unlink call failed for: " << filename << std::endl; |
---|
212 | return true; |
---|
213 | } |
---|
214 | return false; |
---|
215 | } |
---|