1 | // $Id: ncc_test.cc 901 2007-09-27 09:00:05Z markus $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Markus Ringnér |
---|
5 | |
---|
6 | This file is part of the yat library, http://trac.thep.lu.se/trac/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 2 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "yat/classifier/IGP.h" |
---|
25 | #include "yat/classifier/MatrixLookup.h" |
---|
26 | #include "yat/classifier/MatrixLookupWeighted.h" |
---|
27 | #include "yat/classifier/NCC.h" |
---|
28 | #include "yat/classifier/Target.h" |
---|
29 | #include "yat/utility/matrix.h" |
---|
30 | #include "yat/statistics/vector_distance_ptr.h" |
---|
31 | #include "yat/statistics/pearson_vector_distance.h" |
---|
32 | #include "yat/utility/utility.h" |
---|
33 | |
---|
34 | #include <cassert> |
---|
35 | #include <fstream> |
---|
36 | #include <iostream> |
---|
37 | #include <sstream> |
---|
38 | #include <string> |
---|
39 | #include <limits> |
---|
40 | #include <cmath> |
---|
41 | |
---|
42 | using namespace theplu::yat; |
---|
43 | |
---|
44 | int main(const int argc,const char* argv[]) |
---|
45 | { |
---|
46 | |
---|
47 | std::ostream* error; |
---|
48 | if (argc>1 && argv[1]==std::string("-v")) |
---|
49 | error = &std::cerr; |
---|
50 | else { |
---|
51 | error = new std::ofstream("/dev/null"); |
---|
52 | if (argc>1) |
---|
53 | std::cout << "ncc_test -v : for printing extra information\n"; |
---|
54 | } |
---|
55 | *error << "testing ncc" << std::endl; |
---|
56 | bool ok = true; |
---|
57 | |
---|
58 | std::ifstream is("data/sorlie_centroid_data.txt"); |
---|
59 | utility::matrix data(is,'\t'); |
---|
60 | is.close(); |
---|
61 | |
---|
62 | is.open("data/sorlie_centroid_classes.txt"); |
---|
63 | classifier::Target targets(is); |
---|
64 | is.close(); |
---|
65 | |
---|
66 | // Generate weight matrix with 0 for missing values and 1 for others. |
---|
67 | utility::matrix weights(data.rows(),data.columns(),0.0); |
---|
68 | utility::nan(data,weights); |
---|
69 | |
---|
70 | classifier::MatrixLookupWeighted dataviewweighted(data,weights); |
---|
71 | statistics::vector_distance_lookup_weighted_ptr distance= |
---|
72 | statistics::vector_distance<statistics::pearson_vector_distance_tag>; |
---|
73 | classifier::NCC ncc(dataviewweighted,targets,distance); |
---|
74 | ncc.train(); |
---|
75 | |
---|
76 | // Comparing the centroids to stored result |
---|
77 | is.open("data/sorlie_centroids.txt"); |
---|
78 | utility::matrix centroids(is); |
---|
79 | is.close(); |
---|
80 | |
---|
81 | if(centroids.rows() != ncc.centroids().rows() || |
---|
82 | centroids.columns() != ncc.centroids().columns()) { |
---|
83 | *error << "Error in the dimensionality of centroids\n"; |
---|
84 | *error << "Nof rows: " << centroids.rows() << " expected: " |
---|
85 | << ncc.centroids().rows() << std::endl; |
---|
86 | *error << "Nof columns: " << centroids.columns() << " expected: " |
---|
87 | << ncc.centroids().columns() << std::endl; |
---|
88 | } |
---|
89 | |
---|
90 | double slack = 0; |
---|
91 | for (size_t i=0; i<centroids.rows(); i++){ |
---|
92 | for (size_t j=0; j<centroids.columns(); j++){ |
---|
93 | slack += fabs(centroids(i,j)-ncc.centroids()(i,j)); |
---|
94 | } |
---|
95 | } |
---|
96 | slack /= (centroids.columns()*centroids.rows()); |
---|
97 | double slack_bound=2e-7; |
---|
98 | if (slack > slack_bound || std::isnan(slack)){ |
---|
99 | *error << "Difference to stored centroids too large\n"; |
---|
100 | *error << "slack: " << slack << std::endl; |
---|
101 | *error << "expected less than " << slack_bound << std::endl; |
---|
102 | ok = false; |
---|
103 | } |
---|
104 | |
---|
105 | utility::matrix prediction; |
---|
106 | ncc.predict(dataviewweighted,prediction); |
---|
107 | |
---|
108 | // Comparing the prediction to stored result |
---|
109 | is.open("data/sorlie_centroid_predictions.txt"); |
---|
110 | utility::matrix result(is,'\t'); |
---|
111 | is.close(); |
---|
112 | |
---|
113 | slack = 0; |
---|
114 | for (size_t i=0; i<result.rows(); i++){ |
---|
115 | for (size_t j=0; j<result.columns(); j++){ |
---|
116 | slack += fabs(result(i,j)-prediction(i,j)); |
---|
117 | } |
---|
118 | } |
---|
119 | slack /= (result.columns()*result.rows()); |
---|
120 | if (slack > slack_bound || std::isnan(slack)){ |
---|
121 | *error << "Difference to stored prediction too large\n"; |
---|
122 | *error << "slack: " << slack << std::endl; |
---|
123 | *error << "expected less than " << slack_bound << std::endl; |
---|
124 | ok = false; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | if(ok) |
---|
129 | *error << "OK" << std::endl; |
---|
130 | |
---|
131 | |
---|
132 | if (error!=&std::cerr) |
---|
133 | delete error; |
---|
134 | |
---|
135 | if(ok) |
---|
136 | return 0; |
---|
137 | return -1; |
---|
138 | |
---|
139 | } |
---|