source: plugins/base1/se.lu.thep.wenni/trunk/test/wenni_test.cc @ 836

Last change on this file since 836 was 836, checked in by Peter Johansson, 13 years ago

refs #63. Fixed vpath issues in test so make distcheck no works in stand-alone and base1.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1// $Id: wenni_test.cc 836 2008-11-28 17:27:49Z peter $
2
3/*
4  Copyright (C) 2005, 2006 Jari Häkkinen
5  Copyright (C) 2007 2008 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 <cassert>
27#include <cmath>
28#include <cstdlib>
29#include <iostream>
30#include <fstream>
31#include <sstream>
32#include <string>
33#include <unistd.h>
34
35bool check_default_results(void);
36bool check_knni_results(void);
37bool check_raw_results(void);
38bool check_wenni_snr_results(void);
39bool cleanup(void);
40bool compare(const std::string&, const std::string&, double error_bound=1e-20);
41bool diff(const std::string&, const std::string&);
42bool rm(const std::string& filename);
43
44
45int main(const int argc,const char* argv[])
46{ 
47  bool teststatus=false;
48
49  teststatus|=check_raw_results();
50  teststatus|=check_default_results();
51
52  char* buf=std::getenv("srcdir");
53  assert(buf);
54  std::string srcdir(buf);
55  std::stringstream system_call;
56  system_call << "./nni -data default_results/wenni_logratio.data"
57              << " -neighbours 10 -weight " << srcdir
58              << "/../data/wenni_snr.data -weight_is_snr"
59              << " > wenni_imputed.data";
60  if (system(system_call.str().c_str())) {
61    teststatus|=true;
62    std::cerr << "ERROR: system call failed:\n\t" << system_call << std::endl;
63  }
64  teststatus|=check_wenni_snr_results();
65
66  system_call.str("");
67  system_call << "./nni -data default_results/wenni_logratio.data -neighbours 10"
68              << " -nni_algorithm kNNI -weight default_results/wenni_weight.data"
69              << " -weight_cutoff 0.5 > knni_imputed.data";
70  if (system(system_call.str().c_str())) {
71    teststatus|=true;
72    std::cerr << "ERROR: system call failed:\n\t" << system_call << std::endl;
73  }
74  teststatus|=check_knni_results();
75
76  //teststatus|=cleanup();
77
78  return (teststatus ? -1 : 0);
79}
80
81
82bool check_default_results(void)
83{
84  char* buf=std::getenv("srcdir");
85  assert(buf);
86  std::string srcdir(buf);
87
88  bool teststatus=false;
89  teststatus|=diff("default_results/BASEfile.data",
90                   srcdir+"/../data/basefile_in.data");
91  teststatus|=diff("default_results/basefile_out.data",
92                   srcdir+"/../data/basefile_out.data");
93  teststatus|=compare("default_results/wenni_BCh1SD.data",
94                      srcdir+"/../data/wenni_BCh1SD.data");
95  teststatus|=compare("default_results/wenni_BCh2SD.data",
96                      srcdir+"/../data/wenni_BCh2SD.data");
97  teststatus|=compare("default_results/wenni_imputed.data",
98                      srcdir+"/../data/wenni_imputed.data");
99  teststatus|=compare("default_results/wenni_intensity1.data",
100                      srcdir+"/../data/wenni_intensity1.data");
101  teststatus|=compare("default_results/wenni_intensity2.data",
102                      srcdir+"/../data/wenni_intensity2.data");
103  teststatus|=compare("default_results/wenni_logratio.data",
104                      srcdir+"/../data/wenni_logratio.data");
105  teststatus|=compare("default_results/wenni_weight.data",
106                      srcdir+"/../data/wenni_weight.data");
107  if (teststatus)
108    std::cerr << "ERROR: check_default_results failed." << std::endl;
109  return teststatus;
110}
111
112
113bool check_knni_results(void)
114{
115  char* buf=std::getenv("srcdir");
116  assert(buf);
117  std::string srcdir(buf);
118  bool teststatus=false;
119  teststatus|=diff("knni_imputed.data",srcdir+"/../data/knni_imputed.data");
120  if (teststatus)
121    std::cerr << "ERROR: check_knni_results failed." << std::endl;
122  return teststatus;
123}
124
125
126bool check_raw_results(void)
127{
128  char* buf=std::getenv("srcdir");
129  assert(buf);
130  std::string srcdir(buf);
131
132  bool teststatus=false;
133  teststatus|=diff("raw_results/BASEfile.data",
134                   srcdir+"/../data/basefile_in.data");
135  teststatus|=diff("raw_results/basefile_out.data","../data/basefile_out.data");
136  teststatus|=compare("raw_results/wenni_BCh1Mean.data",
137                      srcdir+"/../data/wenni_BCh1Mean.data");
138  teststatus|=compare("raw_results/wenni_BCh1SD.data",
139                      srcdir+"/../data/wenni_BCh1SD.data");
140  teststatus|=compare("raw_results/wenni_BCh2Mean.data",
141                      srcdir+"/../data/wenni_BCh2Mean.data");
142  teststatus|=compare("raw_results/wenni_BCh2SD.data",
143                      srcdir+"/../data/wenni_BCh2SD.data");
144  teststatus|=compare("raw_results/wenni_FCh1Mean.data",
145                      srcdir+"/../data/wenni_FCh1Mean.data");
146  teststatus|=compare("raw_results/wenni_FCh2Mean.data",
147                      srcdir+"/../data/wenni_FCh2Mean.data");
148  teststatus|=compare("raw_results/wenni_imputed.data",
149                      srcdir+"/../data/wenni_imputed.data");
150  teststatus|=compare("raw_results/wenni_logratio.data",
151                      srcdir+"/../data/wenni_logratio.data");
152  teststatus|=compare("raw_results/wenni_weight.data",
153                      srcdir+"/../data/wenni_weight.data");
154  if (teststatus)
155    std::cerr << "ERROR: check_raw_results failed." << std::endl;
156  return teststatus;
157}
158
159
160bool check_wenni_snr_results(void)
161{
162  bool teststatus=false;
163  char* buf=std::getenv("srcdir");
164  assert(buf);
165  std::string srcdir(buf);
166  teststatus|=compare("wenni_imputed.data",srcdir+"/../data/wenni_imputed.data",
167                      1.1e-11);
168  if (teststatus)
169    std::cerr << "ERROR: check_wenni_results failed." << std::endl;
170  return teststatus;
171}
172
173
174bool cleanup(void)
175{
176  bool teststatus=false;
177  teststatus|=rm("BASEfile.data");
178  teststatus|=rm("basefile_out.data");
179  teststatus|=rm("knni_imputed.data");
180  teststatus|=rm("wenni_BCh1Mean.data");
181  teststatus|=rm("wenni_BCh1SD.data");
182  teststatus|=rm("wenni_BCh2Mean.data");
183  teststatus|=rm("wenni_BCh2SD.data");
184  teststatus|=rm("wenni_FCh1Mean.data");
185  teststatus|=rm("wenni_FCh2Mean.data");
186  teststatus|=rm("wenni_imputed.data");
187  teststatus|=rm("wenni_intensity1.data");
188  teststatus|=rm("wenni_intensity2.data");
189  teststatus|=rm("wenni_logratio.data");
190  teststatus|=rm("wenni_weight.data");
191  return teststatus;
192}
193
194
195bool compare(const std::string& a,const std::string& b, double error_bound)
196{
197  bool teststatus=false;
198  std::ifstream astream(a.c_str());
199  assert(astream.good());
200  std::ifstream bstream(b.c_str());
201  assert(bstream.good());
202  theplu::gslapi::matrix adata(astream);
203  theplu::gslapi::matrix bdata(bstream);
204  adata-=bdata;
205  for (size_t i=0; i<adata.rows(); i++)
206    for (size_t j=0; j<adata.columns(); j++)
207      if ((fabs(adata(i,j))>error_bound) || // result outside acceptable bounds
208          (!((adata(i,j)==adata(i,j)))))    // calucaltion error detected
209        teststatus=true;
210  astream.close();
211  bstream.close();
212  if (teststatus)
213    std::cerr << "ERROR: compare " << a << " " << b << std::endl;
214  return teststatus;
215}
216
217
218bool diff(const std::string& a,const std::string& b)
219{
220  std::ifstream is1(a.c_str());
221  assert(is1.good());
222  std::ifstream is2(a.c_str());
223  assert(is2.good());
224  std::string str1;
225  std::string str2;
226  while (getline(is1,str1)){
227    getline(is2,str2);
228    if (str1!=str2) {
229      std::cerr << "ERROR: diff " << a << " " << b << std::endl;
230      return true;
231    }
232  }
233  return false;
234}
235
236
237bool rm(const std::string& filename) {
238  if (unlink(filename.c_str())) {
239    std::cerr << "ERROR: unlink call failed for: " << filename << std::endl;
240    return true;
241  }
242  return false;
243}
Note: See TracBrowser for help on using the repository browser.