source: trunk/test/ncc_test.cc @ 593

Last change on this file since 593 was 593, checked in by Markus Ringnér, 15 years ago

Fixed std includes to compile with g++ 4.1.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1// $Id: ncc_test.cc 593 2006-08-25 12:59:21Z markus $
2
3#include <c++_tools/classifier/IGP.h>
4#include <c++_tools/classifier/MatrixLookup.h>
5#include <c++_tools/classifier/NCC.h>
6#include <c++_tools/classifier/Target.h>
7#include <c++_tools/gslapi/matrix.h>
8#include <c++_tools/gslapi/vector.h>
9#include <c++_tools/statistics/PearsonDistance.h>
10#include <c++_tools/utility/utility.h>
11
12#include <cassert>
13#include <fstream>
14#include <iostream>
15#include <sstream>
16#include <string>
17#include <limits>
18#include <cmath>
19
20using namespace theplu;
21
22int main(const int argc,const char* argv[])
23{ 
24
25  std::ostream* error;
26  if (argc>1 && argv[1]==std::string("-v"))
27    error = &std::cerr;
28  else {
29    error = new std::ofstream("/dev/null");
30    if (argc>1)
31      std::cout << "ncc_test -v : for printing extra information\n";
32  }
33  *error << "testing ncc" << std::endl;
34  bool ok = true;
35
36  std::ifstream is("data/sorlie_centroids.txt");
37  gslapi::matrix data(is,'\t');
38  is.close();
39
40  is.open("data/sorlie_centroid_classes.txt");
41  classifier::Target targets(is);
42  is.close();
43
44  // Generate weight matrix with 0 for missing values and 1 for others.
45  gslapi::matrix weights(data.rows(),data.columns(),0.0);
46  for(size_t i=0;i<data.rows();++i)
47    for(size_t j=0;j<data.columns();++j)
48      if(!std::isnan(data(i,j)))
49        weights(i,j)=1.0;
50     
51  classifier::MatrixLookup dataview(data);
52  classifier::MatrixLookup weightview(weights);
53  statistics::PearsonDistance pearson; 
54  classifier::NCC ncc(dataview,targets,pearson,weightview);
55  ncc.train();
56
57  gslapi::matrix prediction;
58  ncc.predict(dataview,prediction);
59 
60  is.open("data/sorlie_centroid_predictions.txt");
61  gslapi::matrix result(is,'\t');
62  is.close();
63
64  // Comparing the prediction to stored result
65  double slack = 0;
66  for (size_t i=0; i<result.rows(); i++){
67    for (size_t j=0; j<result.columns(); j++){
68        slack += fabs(result(i,j)-prediction(i,j));
69    }
70  }
71  slack /= (result.columns()*result.rows());
72  double slack_bound=2e-7;
73  if (slack > slack_bound){
74    *error << "Difference to stored prediction too large\n";
75    *error << "slack: " << slack << std::endl;
76    *error << "expected less than " << slack_bound << std::endl;
77    ok = false;
78  }
79 
80
81  // Testing IGP
82  *error << "testing igp" << std::endl;
83  classifier::IGP igp(dataview,targets,pearson);
84  *error << igp.score() << std::endl;
85
86  if(ok)
87    *error << "OK" << std::endl;
88
89
90  if (error!=&std::cerr)
91    delete error;
92
93  if(ok) 
94    return 0;
95  return -1;
96 
97}
Note: See TracBrowser for help on using the repository browser.