537 |
05 Mar 06 |
peter |
// $Id$ |
537 |
05 Mar 06 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2007 Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2012, 2013 Peter Johansson |
537 |
05 Mar 06 |
peter |
8 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
10 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
15 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
20 |
|
675 |
10 Oct 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
675 |
10 Oct 06 |
jari |
23 |
*/ |
675 |
10 Oct 06 |
jari |
24 |
|
2881 |
18 Nov 12 |
peter |
25 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
26 |
|
1233 |
15 Mar 08 |
peter |
27 |
#include "Suite.h" |
1233 |
15 Mar 08 |
peter |
28 |
|
1121 |
22 Feb 08 |
peter |
29 |
#include "yat/utility/Matrix.h" |
675 |
10 Oct 06 |
jari |
30 |
#include "yat/classifier/DataLookup1D.h" |
675 |
10 Oct 06 |
jari |
31 |
#include "yat/classifier/MatrixLookup.h" |
820 |
17 Mar 07 |
peter |
32 |
#include "yat/classifier/utility.h" |
675 |
10 Oct 06 |
jari |
33 |
|
679 |
11 Oct 06 |
markus |
34 |
|
679 |
11 Oct 06 |
markus |
35 |
#include <cstdio> |
537 |
05 Mar 06 |
peter |
36 |
#include <fstream> |
537 |
05 Mar 06 |
peter |
37 |
#include <iostream> |
2055 |
08 Sep 09 |
peter |
38 |
#include <limits> |
537 |
05 Mar 06 |
peter |
39 |
#include <vector> |
593 |
25 Aug 06 |
markus |
40 |
#include <cmath> |
537 |
05 Mar 06 |
peter |
41 |
|
680 |
11 Oct 06 |
jari |
42 |
using namespace theplu::yat; |
537 |
05 Mar 06 |
peter |
43 |
|
1121 |
22 Feb 08 |
peter |
44 |
utility::Matrix matrix(size_t n); |
537 |
05 Mar 06 |
peter |
45 |
|
1233 |
15 Mar 08 |
peter |
46 |
int main(int argc, char* argv[]) |
537 |
05 Mar 06 |
peter |
47 |
{ |
1233 |
15 Mar 08 |
peter |
48 |
theplu::yat::test::Suite suite(argc, argv); |
680 |
11 Oct 06 |
jari |
49 |
using namespace theplu::yat::classifier; |
537 |
05 Mar 06 |
peter |
50 |
|
1233 |
15 Mar 08 |
peter |
51 |
suite.err() << "Testing Lookup Classes" << std::endl; |
1233 |
15 Mar 08 |
peter |
52 |
suite.err() << "Testing DataLookup1D" << std::endl; |
1121 |
22 Feb 08 |
peter |
53 |
utility::Matrix gsl_m1(matrix(5)); |
537 |
05 Mar 06 |
peter |
54 |
std::vector<size_t> index_odd; |
537 |
05 Mar 06 |
peter |
55 |
index_odd.push_back(1); |
537 |
05 Mar 06 |
peter |
56 |
index_odd.push_back(3); |
537 |
05 Mar 06 |
peter |
57 |
std::vector<size_t> index_even; |
537 |
05 Mar 06 |
peter |
58 |
index_even.push_back(2); |
537 |
05 Mar 06 |
peter |
59 |
index_even.push_back(0); |
537 |
05 Mar 06 |
peter |
60 |
index_even.push_back(4); |
1134 |
23 Feb 08 |
peter |
61 |
classifier::MatrixLookup m1(gsl_m1,utility::Index(index_odd), |
1134 |
23 Feb 08 |
peter |
62 |
utility::Index(index_even)); |
1233 |
15 Mar 08 |
peter |
63 |
suite.err() << gsl_m1 << std::endl << '\n'; |
1233 |
15 Mar 08 |
peter |
64 |
suite.err() << m1 << std::endl; |
1233 |
15 Mar 08 |
peter |
65 |
suite.err() << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" |
537 |
05 Mar 06 |
peter |
66 |
<< " const size_t, const bool)..."; |
565 |
16 Mar 06 |
peter |
67 |
classifier::DataLookup1D v1(m1,1, true); |
4200 |
19 Aug 22 |
peter |
68 |
if (v1.size()!=m1.columns() || v1(0)!=m1(1,0) || |
537 |
05 Mar 06 |
peter |
69 |
v1(1)!=m1(1,1) ) { |
3103 |
02 Nov 13 |
peter |
70 |
suite.add(false); |
1233 |
15 Mar 08 |
peter |
71 |
suite.err() << "\nERROR" << std::endl; |
4200 |
19 Aug 22 |
peter |
72 |
suite.err() << "size: " << v1.size() << " expected " << m1.columns() << "\n" |
537 |
05 Mar 06 |
peter |
73 |
<< "v1(0): " << v1(0) << " expected " << m1(1,0) << "\n" |
4200 |
19 Aug 22 |
peter |
74 |
<< "v1(1): " << v1(1) << " expected " << m1(1,1) |
537 |
05 Mar 06 |
peter |
75 |
<< std::endl; |
537 |
05 Mar 06 |
peter |
76 |
} |
537 |
05 Mar 06 |
peter |
77 |
else |
1233 |
15 Mar 08 |
peter |
78 |
suite.err() << "Ok" << std::endl; |
537 |
05 Mar 06 |
peter |
79 |
|
1233 |
15 Mar 08 |
peter |
80 |
suite.err() << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" |
537 |
05 Mar 06 |
peter |
81 |
<< " const size_t, const bool false)..."; |
537 |
05 Mar 06 |
peter |
82 |
classifier::DataLookup1D v2(m1,1,false); |
537 |
05 Mar 06 |
peter |
83 |
if (v2.size()!=m1.rows() || v2(0)!=m1(0,1) || v2(1)!=m1(1,1) ) { |
3103 |
02 Nov 13 |
peter |
84 |
suite.add(false); |
1233 |
15 Mar 08 |
peter |
85 |
suite.err() << "\nERROR\n" |
4200 |
19 Aug 22 |
peter |
86 |
<< "size: " << v2.size() << " expected " << m1.rows() << "\n" |
537 |
05 Mar 06 |
peter |
87 |
<< "v2(0): " << v2(0) << " expected " << m1(0,1) << "\n" |
4200 |
19 Aug 22 |
peter |
88 |
<< "v2(1): " << v2(1) << " expected " << m1(1,1) |
537 |
05 Mar 06 |
peter |
89 |
<< std::endl; |
537 |
05 Mar 06 |
peter |
90 |
} |
537 |
05 Mar 06 |
peter |
91 |
else |
1233 |
15 Mar 08 |
peter |
92 |
suite.err() << "Ok" << std::endl; |
537 |
05 Mar 06 |
peter |
93 |
|
1233 |
15 Mar 08 |
peter |
94 |
suite.err() << "DataLookup1D::DataLookup1D(const DataLookup1D&)..."; |
537 |
05 Mar 06 |
peter |
95 |
classifier::DataLookup1D v3(v2); |
537 |
05 Mar 06 |
peter |
96 |
if (v3.size()!=v2.size() || v3(0)!=v2(0) || v3(1)!=v2(1) ) { |
3103 |
02 Nov 13 |
peter |
97 |
suite.add(false); |
1233 |
15 Mar 08 |
peter |
98 |
suite.err() << "\nERROR\n" |
4200 |
19 Aug 22 |
peter |
99 |
<< "size: " << v3.size() << " expected " << v2.size() << "\n" |
537 |
05 Mar 06 |
peter |
100 |
<< "v2(0): " << v3(0) << " expected " << v2(0) << "\n" |
537 |
05 Mar 06 |
peter |
101 |
<< "v2(1): " << v3(1) << " expected " << v2(1) |
537 |
05 Mar 06 |
peter |
102 |
<< std::endl; |
537 |
05 Mar 06 |
peter |
103 |
} |
537 |
05 Mar 06 |
peter |
104 |
else |
1233 |
15 Mar 08 |
peter |
105 |
suite.err() << "Ok" << std::endl; |
537 |
05 Mar 06 |
peter |
106 |
|
1233 |
15 Mar 08 |
peter |
107 |
suite.err() << "DataLookup1D::DataLookup1D(const size_t, const double)..."; |
537 |
05 Mar 06 |
peter |
108 |
classifier::DataLookup1D v4(12,3); |
537 |
05 Mar 06 |
peter |
109 |
if (v4.size()!=12 || v4(10)!=3 ) { |
3103 |
02 Nov 13 |
peter |
110 |
suite.add(false); |
1233 |
15 Mar 08 |
peter |
111 |
suite.err() << "\nERROR\n" |
4200 |
19 Aug 22 |
peter |
112 |
<< "size: " << v4.size() << " expected " << 12 << "\n" |
4200 |
19 Aug 22 |
peter |
113 |
<< "v2(0): " << v4(10) << " expected " << 3 |
537 |
05 Mar 06 |
peter |
114 |
<< std::endl; |
537 |
05 Mar 06 |
peter |
115 |
} |
537 |
05 Mar 06 |
peter |
116 |
else |
1233 |
15 Mar 08 |
peter |
117 |
suite.err() << "Ok" << std::endl; |
537 |
05 Mar 06 |
peter |
118 |
|
1233 |
15 Mar 08 |
peter |
119 |
suite.err() << "Testing that output from ostream operator for DataLookup1D" |
616 |
31 Aug 06 |
jari |
120 |
<< " can be used by the utility::vector istream constructor..."; |
593 |
25 Aug 06 |
markus |
121 |
|
593 |
25 Aug 06 |
markus |
// First with a vector with no missing values separated by ' '. |
1251 |
03 Apr 08 |
peter |
123 |
std::ofstream my_out("/tmp/yat_test_datalookup1D.txt"); |
593 |
25 Aug 06 |
markus |
124 |
my_out << v1; |
593 |
25 Aug 06 |
markus |
125 |
my_out.close(); |
1251 |
03 Apr 08 |
peter |
126 |
std::ifstream is("/tmp/yat_test_datalookup1D.txt"); |
1120 |
21 Feb 08 |
peter |
127 |
utility::Vector v5(is); |
593 |
25 Aug 06 |
markus |
128 |
is.close(); |
593 |
25 Aug 06 |
markus |
129 |
if (v5.size()!=v1.size() || v5(0)!=v1(0) || v5(1)!=v1(1) || |
593 |
25 Aug 06 |
markus |
130 |
v5(2)!=v1(2)) { |
3103 |
02 Nov 13 |
peter |
131 |
suite.add(false); |
1233 |
15 Mar 08 |
peter |
132 |
suite.err() << "\nERROR\n" << std::endl; |
593 |
25 Aug 06 |
markus |
133 |
} |
1251 |
03 Apr 08 |
peter |
134 |
std::remove("/tmp/yat_test_datalookup1D.txt"); |
4200 |
19 Aug 22 |
peter |
135 |
|
593 |
25 Aug 06 |
markus |
// Second with a vector with a missing value separated by '\t'. |
593 |
25 Aug 06 |
markus |
137 |
gsl_m1(3,0)=std::numeric_limits<double>::quiet_NaN(); |
4200 |
19 Aug 22 |
peter |
138 |
classifier::DataLookup1D v6(m1,1, true); |
1251 |
03 Apr 08 |
peter |
139 |
my_out.open("/tmp/yat_test_datalookup1D.txt"); |
593 |
25 Aug 06 |
markus |
140 |
char prev=my_out.fill('\t'); |
593 |
25 Aug 06 |
markus |
141 |
my_out << v1; |
593 |
25 Aug 06 |
markus |
142 |
my_out.fill(prev); |
593 |
25 Aug 06 |
markus |
143 |
my_out.close(); |
1251 |
03 Apr 08 |
peter |
144 |
is.open("/tmp/yat_test_datalookup1D.txt"); |
1120 |
21 Feb 08 |
peter |
145 |
utility::Vector v7(is,'\t'); |
593 |
25 Aug 06 |
markus |
146 |
is.close(); |
593 |
25 Aug 06 |
markus |
147 |
if (v7.size()!=v6.size() || !std::isnan(v7(1))) { |
3103 |
02 Nov 13 |
peter |
148 |
suite.add(false); |
1233 |
15 Mar 08 |
peter |
149 |
suite.err() << "\nERROR\n" |
4200 |
19 Aug 22 |
peter |
150 |
<< "size: " << v7.size() << " expected " << v6.size() << "\n" |
4200 |
19 Aug 22 |
peter |
151 |
<< "v7(1): " << v7(1) << " expected nan\n" |
593 |
25 Aug 06 |
markus |
152 |
<< std::endl; |
593 |
25 Aug 06 |
markus |
153 |
} |
1251 |
03 Apr 08 |
peter |
154 |
std::remove("data/yat_test_datalookup1D.txt"); |
1233 |
15 Mar 08 |
peter |
155 |
suite.err() << "\n"; |
820 |
17 Mar 07 |
peter |
156 |
|
820 |
17 Mar 07 |
peter |
157 |
DataLookup1D dl(v5); |
1120 |
21 Feb 08 |
peter |
158 |
utility::Vector v8; |
820 |
17 Mar 07 |
peter |
159 |
classifier::convert(dl, v8); |
820 |
17 Mar 07 |
peter |
160 |
if (!v5.equal(v8,0.0)) { |
3103 |
02 Nov 13 |
peter |
161 |
suite.add(false); |
1233 |
15 Mar 08 |
peter |
162 |
suite.err() << "Error: Creating a DataLookup1D(utility::vector)\n" |
820 |
17 Mar 07 |
peter |
163 |
<< "and classifier::convert(DataLookup, utility::vector)\n" |
820 |
17 Mar 07 |
peter |
164 |
<< "does not give back original vector\n" |
820 |
17 Mar 07 |
peter |
165 |
<< "orginal: " << v5 << "\n" |
820 |
17 Mar 07 |
peter |
166 |
<< "DataLookup1D: " << dl << "\n" |
820 |
17 Mar 07 |
peter |
167 |
<< "final result: " << v8 << "\n" |
820 |
17 Mar 07 |
peter |
168 |
<< std::endl; |
820 |
17 Mar 07 |
peter |
169 |
} |
820 |
17 Mar 07 |
peter |
170 |
|
1233 |
15 Mar 08 |
peter |
171 |
return suite.return_value(); |
537 |
05 Mar 06 |
peter |
172 |
} |
537 |
05 Mar 06 |
peter |
173 |
|
1121 |
22 Feb 08 |
peter |
174 |
utility::Matrix matrix(size_t n) |
537 |
05 Mar 06 |
peter |
175 |
{ |
1121 |
22 Feb 08 |
peter |
176 |
utility::Matrix res(n,n); |
537 |
05 Mar 06 |
peter |
177 |
for (size_t i=0;i<n;i++) |
537 |
05 Mar 06 |
peter |
178 |
for (size_t j=0;j<n;j++) |
537 |
05 Mar 06 |
peter |
179 |
res(i,j)=10*i+j; |
537 |
05 Mar 06 |
peter |
180 |
return res; |
537 |
05 Mar 06 |
peter |
181 |
} |