1 | // $Id: ncc_test.cc 925 2007-10-02 14:02:08Z 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/pearson_vector_distance.h" |
---|
31 | #include "yat/utility/utility.h" |
---|
32 | |
---|
33 | #include <cassert> |
---|
34 | #include <fstream> |
---|
35 | #include <iostream> |
---|
36 | #include <sstream> |
---|
37 | #include <string> |
---|
38 | #include <limits> |
---|
39 | #include <cmath> |
---|
40 | |
---|
41 | using namespace theplu::yat; |
---|
42 | |
---|
43 | int main(const int argc,const char* argv[]) |
---|
44 | { |
---|
45 | |
---|
46 | std::ostream* error; |
---|
47 | if (argc>1 && argv[1]==std::string("-v")) |
---|
48 | error = &std::cerr; |
---|
49 | else { |
---|
50 | error = new std::ofstream("/dev/null"); |
---|
51 | if (argc>1) |
---|
52 | std::cout << "ncc_test -v : for printing extra information\n"; |
---|
53 | } |
---|
54 | *error << "testing ncc" << std::endl; |
---|
55 | bool ok = true; |
---|
56 | |
---|
57 | classifier::MatrixLookupWeighted ml(4,4); |
---|
58 | std::vector<std::string> vec(4, "pos"); |
---|
59 | vec[3]="bjds"; |
---|
60 | classifier::Target target(vec); |
---|
61 | classifier::NCC<statistics::pearson_vector_distance_tag> ncctmp(ml,target); |
---|
62 | *error << "training...\n"; |
---|
63 | ncctmp.train(); |
---|
64 | |
---|
65 | std::ifstream is("data/sorlie_centroid_data.txt"); |
---|
66 | utility::matrix data(is,'\t'); |
---|
67 | is.close(); |
---|
68 | |
---|
69 | is.open("data/sorlie_centroid_classes.txt"); |
---|
70 | classifier::Target targets(is); |
---|
71 | is.close(); |
---|
72 | |
---|
73 | // Generate weight matrix with 0 for missing values and 1 for others. |
---|
74 | utility::matrix weights(data.rows(),data.columns(),0.0); |
---|
75 | utility::nan(data,weights); |
---|
76 | |
---|
77 | classifier::MatrixLookupWeighted dataviewweighted(data,weights); |
---|
78 | classifier::NCC<statistics::pearson_vector_distance_tag> ncc(dataviewweighted,targets); |
---|
79 | *error << "training...\n"; |
---|
80 | ncc.train(); |
---|
81 | |
---|
82 | // Comparing the centroids to stored result |
---|
83 | is.open("data/sorlie_centroids.txt"); |
---|
84 | utility::matrix centroids(is); |
---|
85 | is.close(); |
---|
86 | |
---|
87 | if(centroids.rows() != ncc.centroids().rows() || |
---|
88 | centroids.columns() != ncc.centroids().columns()) { |
---|
89 | *error << "Error in the dimensionality of centroids\n"; |
---|
90 | *error << "Nof rows: " << centroids.rows() << " expected: " |
---|
91 | << ncc.centroids().rows() << std::endl; |
---|
92 | *error << "Nof columns: " << centroids.columns() << " expected: " |
---|
93 | << ncc.centroids().columns() << std::endl; |
---|
94 | } |
---|
95 | |
---|
96 | double slack = 0; |
---|
97 | for (size_t i=0; i<centroids.rows(); i++){ |
---|
98 | for (size_t j=0; j<centroids.columns(); j++){ |
---|
99 | slack += fabs(centroids(i,j)-ncc.centroids()(i,j)); |
---|
100 | } |
---|
101 | } |
---|
102 | slack /= (centroids.columns()*centroids.rows()); |
---|
103 | double slack_bound=2e-7; |
---|
104 | if (slack > slack_bound || std::isnan(slack)){ |
---|
105 | *error << "Difference to stored centroids too large\n"; |
---|
106 | *error << "slack: " << slack << std::endl; |
---|
107 | *error << "expected less than " << slack_bound << std::endl; |
---|
108 | ok = false; |
---|
109 | } |
---|
110 | |
---|
111 | *error << "prediction...\n"; |
---|
112 | utility::matrix prediction; |
---|
113 | ncc.predict(dataviewweighted,prediction); |
---|
114 | |
---|
115 | // Comparing the prediction to stored result |
---|
116 | is.open("data/sorlie_centroid_predictions.txt"); |
---|
117 | utility::matrix result(is,'\t'); |
---|
118 | is.close(); |
---|
119 | |
---|
120 | slack = 0; |
---|
121 | for (size_t i=0; i<result.rows(); i++){ |
---|
122 | for (size_t j=0; j<result.columns(); j++){ |
---|
123 | slack += fabs(result(i,j)-prediction(i,j)); |
---|
124 | } |
---|
125 | } |
---|
126 | slack /= (result.columns()*result.rows()); |
---|
127 | if (slack > slack_bound || std::isnan(slack)){ |
---|
128 | *error << "Difference to stored prediction too large\n"; |
---|
129 | *error << "slack: " << slack << std::endl; |
---|
130 | *error << "expected less than " << slack_bound << std::endl; |
---|
131 | ok = false; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | if(ok) |
---|
136 | *error << "OK" << std::endl; |
---|
137 | |
---|
138 | |
---|
139 | if (error!=&std::cerr) |
---|
140 | delete error; |
---|
141 | |
---|
142 | if(ok) |
---|
143 | return 0; |
---|
144 | return -1; |
---|
145 | } |
---|