509 |
18 Feb 06 |
peter |
// $Id$ |
509 |
18 Feb 06 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
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) 2012 Peter Johansson |
509 |
18 Feb 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 |
|
1248 |
19 Mar 08 |
peter |
27 |
#include "Suite.h" |
1248 |
19 Mar 08 |
peter |
28 |
|
675 |
10 Oct 06 |
jari |
29 |
#include "yat/classifier/Target.h" |
1134 |
23 Feb 08 |
peter |
30 |
#include "yat/utility/Index.h" |
675 |
10 Oct 06 |
jari |
31 |
|
509 |
18 Feb 06 |
peter |
32 |
#include <fstream> |
509 |
18 Feb 06 |
peter |
33 |
#include <iostream> |
509 |
18 Feb 06 |
peter |
34 |
#include <string> |
509 |
18 Feb 06 |
peter |
35 |
#include <vector> |
509 |
18 Feb 06 |
peter |
36 |
|
680 |
11 Oct 06 |
jari |
37 |
using namespace theplu::yat; |
509 |
18 Feb 06 |
peter |
38 |
|
1248 |
19 Mar 08 |
peter |
39 |
int main( int argc, char* argv[]) |
4200 |
19 Aug 22 |
peter |
40 |
{ |
1248 |
19 Mar 08 |
peter |
41 |
test::Suite suite(argc, argv); |
1248 |
19 Mar 08 |
peter |
42 |
suite.err() << "testing target" << std::endl; |
509 |
18 Feb 06 |
peter |
43 |
|
509 |
18 Feb 06 |
peter |
// This test assumes classes are introduced in order of appearence |
509 |
18 Feb 06 |
peter |
45 |
std::vector<std::string> color(10,"default"); |
509 |
18 Feb 06 |
peter |
46 |
color[2]=color[7]="white"; |
509 |
18 Feb 06 |
peter |
47 |
color[4]=color[5]="black"; |
509 |
18 Feb 06 |
peter |
48 |
color[6]=color[3]="green"; |
509 |
18 Feb 06 |
peter |
49 |
color[8]=color[9]="red"; |
509 |
18 Feb 06 |
peter |
50 |
classifier::Target target3(color); |
509 |
18 Feb 06 |
peter |
51 |
std::vector<size_t> facit(10); |
509 |
18 Feb 06 |
peter |
52 |
facit[2]=facit[7]=1; |
509 |
18 Feb 06 |
peter |
53 |
facit[4]=facit[5]=3; |
509 |
18 Feb 06 |
peter |
54 |
facit[6]=facit[3]=2; |
509 |
18 Feb 06 |
peter |
55 |
facit[8]=facit[9]=4; |
4200 |
19 Aug 22 |
peter |
56 |
|
509 |
18 Feb 06 |
peter |
57 |
for (size_t i=0; i<facit.size(); i++){ |
509 |
18 Feb 06 |
peter |
58 |
if (target3(i)!=facit[i]){ |
1248 |
19 Mar 08 |
peter |
59 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
60 |
suite.err() << "target(" << i << ") is " << target3(0) << " expected " |
509 |
18 Feb 06 |
peter |
61 |
<< facit[i] << std::endl; |
509 |
18 Feb 06 |
peter |
62 |
} |
509 |
18 Feb 06 |
peter |
63 |
} |
521 |
22 Feb 06 |
peter |
64 |
std::vector<size_t> sub; |
521 |
22 Feb 06 |
peter |
65 |
sub.push_back(2); |
521 |
22 Feb 06 |
peter |
66 |
sub.push_back(1); |
1134 |
23 Feb 08 |
peter |
67 |
classifier::Target sub_target(target3,utility::Index(sub)); |
521 |
22 Feb 06 |
peter |
68 |
if (target3(sub[0])!=sub_target(0) || target3(sub[1])!=sub_target(1)){ |
1248 |
19 Mar 08 |
peter |
69 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
70 |
suite.err() << "error in constructor Target(const Target&, std::vector<size_t>&)" |
521 |
22 Feb 06 |
peter |
71 |
<< "\nclasses are not preserved" << std::endl; |
521 |
22 Feb 06 |
peter |
72 |
} |
4200 |
19 Aug 22 |
peter |
73 |
if (target3.binary(sub[0])!=sub_target.binary(0) || |
521 |
22 Feb 06 |
peter |
74 |
target3.binary(sub[1])!=sub_target.binary(1)){ |
1248 |
19 Mar 08 |
peter |
75 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
76 |
suite.err() << "error in constructor Target(const Target&, std::vector<size_t>&)" |
521 |
22 Feb 06 |
peter |
77 |
<< "\nbinary classes are not preserved" << std::endl; |
521 |
22 Feb 06 |
peter |
78 |
} |
521 |
22 Feb 06 |
peter |
79 |
|
521 |
22 Feb 06 |
peter |
80 |
|
509 |
18 Feb 06 |
peter |
81 |
std::vector<std::string> label(31,"negative"); |
4200 |
19 Aug 22 |
peter |
82 |
for (size_t i=0; i<16; i++) |
509 |
18 Feb 06 |
peter |
83 |
label[i] = "positive"; |
509 |
18 Feb 06 |
peter |
84 |
label[20]="neither"; |
509 |
18 Feb 06 |
peter |
85 |
classifier::Target target(label); |
509 |
18 Feb 06 |
peter |
86 |
if (target.nof_classes()!=3){ |
1248 |
19 Mar 08 |
peter |
87 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
88 |
suite.err() << "Expected number of classes to be 3" << std::endl; |
509 |
18 Feb 06 |
peter |
89 |
} |
509 |
18 Feb 06 |
peter |
90 |
if (target(0)!=0){ |
1248 |
19 Mar 08 |
peter |
91 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
92 |
suite.err() << "Error: target(0)!=0" << std::endl; |
509 |
18 Feb 06 |
peter |
93 |
} |
509 |
18 Feb 06 |
peter |
94 |
if (target(20)!=2){ |
1248 |
19 Mar 08 |
peter |
95 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
96 |
suite.err() << "Error: target(20)!=2" << std::endl; |
509 |
18 Feb 06 |
peter |
97 |
} |
514 |
20 Feb 06 |
peter |
98 |
if (!target.binary(0)){ |
1248 |
19 Mar 08 |
peter |
99 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
100 |
suite.err() << "Error: target.binary(0) not true" << std::endl; |
4200 |
19 Aug 22 |
peter |
101 |
} |
514 |
20 Feb 06 |
peter |
102 |
if (target.binary(20)){ |
1248 |
19 Mar 08 |
peter |
103 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
104 |
suite.err() << "Error: target.binary(20) not false" << std::endl; |
4200 |
19 Aug 22 |
peter |
105 |
} |
514 |
20 Feb 06 |
peter |
106 |
target.set_binary(2,true); |
514 |
20 Feb 06 |
peter |
107 |
if (!target.binary(20)){ |
1248 |
19 Mar 08 |
peter |
108 |
suite.add(false); |
1248 |
19 Mar 08 |
peter |
109 |
suite.err() << "Error: target.binary(20) not true" << std::endl; |
4200 |
19 Aug 22 |
peter |
110 |
} |
1251 |
03 Apr 08 |
peter |
111 |
std::ifstream is(test::filename("data/rank_target.txt").c_str()); |
509 |
18 Feb 06 |
peter |
112 |
classifier::Target target2(is); |
509 |
18 Feb 06 |
peter |
113 |
is.close(); |
509 |
18 Feb 06 |
peter |
114 |
if (target2.size()!=6){ |
1248 |
19 Mar 08 |
peter |
115 |
suite.add(false); |
4200 |
19 Aug 22 |
peter |
116 |
suite.err() << "ERROR: size of target is " << target2.size() |
1248 |
19 Mar 08 |
peter |
117 |
<< " expected 6." << std::endl; |
4200 |
19 Aug 22 |
peter |
118 |
} |
509 |
18 Feb 06 |
peter |
119 |
|
1248 |
19 Mar 08 |
peter |
120 |
suite.return_value(); |
509 |
18 Feb 06 |
peter |
121 |
} |