69 |
11 Feb 06 |
jari |
// $Id$ |
69 |
11 Feb 06 |
jari |
2 |
|
95 |
05 Apr 06 |
jari |
3 |
/* |
95 |
05 Apr 06 |
jari |
Copyright (C) 2005, 2006 Jari Häkkinen |
314 |
28 May 07 |
peter |
Copyright (C) 2007 Peter Johansson |
95 |
05 Apr 06 |
jari |
6 |
|
95 |
05 Apr 06 |
jari |
This file is part of WeNNI, |
825 |
26 Nov 08 |
jari |
http://baseplugins.thep.lu.se/wiki/se.lu.thep.WeNNI |
95 |
05 Apr 06 |
jari |
9 |
|
95 |
05 Apr 06 |
jari |
WeNNI is free software; you can redistribute it and/or modify it |
95 |
05 Apr 06 |
jari |
under the terms of the GNU General Public License as published by |
824 |
26 Nov 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
95 |
05 Apr 06 |
jari |
(at your option) any later version. |
95 |
05 Apr 06 |
jari |
14 |
|
95 |
05 Apr 06 |
jari |
WeNNI is distributed in the hope that it will be useful, but WITHOUT |
95 |
05 Apr 06 |
jari |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
95 |
05 Apr 06 |
jari |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
95 |
05 Apr 06 |
jari |
License for more details. |
95 |
05 Apr 06 |
jari |
19 |
|
95 |
05 Apr 06 |
jari |
You should have received a copy of the GNU General Public License |
824 |
26 Nov 08 |
jari |
along with WeNNI. If not, see <http://www.gnu.org/licenses/>. |
95 |
05 Apr 06 |
jari |
22 |
*/ |
95 |
05 Apr 06 |
jari |
23 |
|
69 |
11 Feb 06 |
jari |
24 |
#include "Parameter.h" |
69 |
11 Feb 06 |
jari |
25 |
#include "nni.h" |
88 |
04 Apr 06 |
jari |
26 |
#include <config.h> // this header file is created by configure |
69 |
11 Feb 06 |
jari |
27 |
|
69 |
11 Feb 06 |
jari |
28 |
#include <iostream> |
69 |
11 Feb 06 |
jari |
29 |
#include <string> |
69 |
11 Feb 06 |
jari |
30 |
|
69 |
11 Feb 06 |
jari |
31 |
#include <c++_tools/utility/FileIO.h> |
69 |
11 Feb 06 |
jari |
32 |
|
69 |
11 Feb 06 |
jari |
33 |
|
69 |
11 Feb 06 |
jari |
34 |
namespace theplu { |
69 |
11 Feb 06 |
jari |
35 |
namespace wenni { |
69 |
11 Feb 06 |
jari |
36 |
|
69 |
11 Feb 06 |
jari |
37 |
Parameter::Parameter(const int argc,const char *argv[]) |
69 |
11 Feb 06 |
jari |
38 |
{ |
69 |
11 Feb 06 |
jari |
39 |
using namespace std; |
69 |
11 Feb 06 |
jari |
// check if help requested |
69 |
11 Feb 06 |
jari |
41 |
for (int i=1; i<argc; i++) |
69 |
11 Feb 06 |
jari |
42 |
if (string(argv[i])==string("-help") || string(argv[i])==string("-h")) { |
313 |
28 May 07 |
peter |
43 |
std::cout << "\n"; |
313 |
28 May 07 |
peter |
44 |
std::cout << "Perform missing value imputation on a data matrix with\n"; |
313 |
28 May 07 |
peter |
45 |
std::cout << "associated weights. Weights may be SNR, see options.\n"; |
313 |
28 May 07 |
peter |
46 |
std::cout << "\n"; |
313 |
28 May 07 |
peter |
47 |
std::cout << "The input matrices are read from files and the imputed data\n"; |
313 |
28 May 07 |
peter |
48 |
std::cout << "is written to stdout\n"; |
313 |
28 May 07 |
peter |
49 |
std::cout << "\n"; |
313 |
28 May 07 |
peter |
50 |
std::cout << "Seven options are available (default value within parenthesis):\n"; |
313 |
28 May 07 |
peter |
51 |
std::cout << " -beta: set the beta value for weight calculation (0.6)\n"; |
313 |
28 May 07 |
peter |
52 |
std::cout << " See the WeNNI paper for details on this parameter.\n"; |
313 |
28 May 07 |
peter |
53 |
std::cout << " -data: set the data file name (data.data)\n"; |
313 |
28 May 07 |
peter |
54 |
std::cout << " -neighbours: set the number of nearest neighbours (3)\n"; |
313 |
28 May 07 |
peter |
55 |
std::cout << " This sets the number of contributions to use in the\n"; |
313 |
28 May 07 |
peter |
56 |
std::cout << " imputation. For binary weights this corresponds\n"; |
313 |
28 May 07 |
peter |
57 |
std::cout << " directly to closest neighbours, whereas For non-binary\n"; |
313 |
28 May 07 |
peter |
58 |
std::cout << " weights the accumulated weights are compared against\n"; |
313 |
28 May 07 |
peter |
59 |
std::cout << " the cutoff.\n"; |
313 |
28 May 07 |
peter |
60 |
std::cout << " -nni_algorithm: set the algorithm to use (WeNNI)\n"; |
313 |
28 May 07 |
peter |
61 |
std::cout << " Available algorithms are kNNI and WeNNI and a string\n"; |
313 |
28 May 07 |
peter |
62 |
std::cout << " is expected as the option\n"; |
313 |
28 May 07 |
peter |
63 |
std::cout << " --version print version information and exit\n"; |
313 |
28 May 07 |
peter |
64 |
std::cout << " -weight: set the weight (or SNR) file name (weight.data)\n"; |
313 |
28 May 07 |
peter |
65 |
std::cout << " Weights must be within [0,1].\n"; |
313 |
28 May 07 |
peter |
66 |
std::cout << " See -weight_cutoff parameter if kNNI algo is used.\n"; |
313 |
28 May 07 |
peter |
67 |
std::cout << " -weight_cutoff: set the cutoff value for weights (0.0)\n"; |
313 |
28 May 07 |
peter |
68 |
std::cout << " kNNI requires binary weights. All values larger than\n"; |
313 |
28 May 07 |
peter |
69 |
std::cout << " the cutoff will be treated as 1s, and 0s otherwise.\n"; |
313 |
28 May 07 |
peter |
70 |
std::cout << " -weight_is_snr: weight file contains snr values rather than\n"; |
313 |
28 May 07 |
peter |
71 |
std::cout << " weights (false).\n"; |
313 |
28 May 07 |
peter |
72 |
std::cout << " Use -beta to tune the weights.\n"; |
313 |
28 May 07 |
peter |
73 |
std::cout << "\n"; |
69 |
11 Feb 06 |
jari |
74 |
exit(0); // always exit after printing help |
69 |
11 Feb 06 |
jari |
75 |
} |
69 |
11 Feb 06 |
jari |
76 |
|
69 |
11 Feb 06 |
jari |
77 |
defaults(); |
69 |
11 Feb 06 |
jari |
78 |
|
69 |
11 Feb 06 |
jari |
79 |
for (int i=1; i<argc; i++) { |
69 |
11 Feb 06 |
jari |
80 |
bool ok=false; |
69 |
11 Feb 06 |
jari |
81 |
string myargv(argv[i]); |
69 |
11 Feb 06 |
jari |
82 |
if (myargv==string("-beta")) |
69 |
11 Feb 06 |
jari |
83 |
if (++i<argc) { |
69 |
11 Feb 06 |
jari |
84 |
beta_=atof(argv[i]); |
69 |
11 Feb 06 |
jari |
85 |
ok=true; |
69 |
11 Feb 06 |
jari |
86 |
} |
69 |
11 Feb 06 |
jari |
87 |
if (myargv==string("-data")) |
69 |
11 Feb 06 |
jari |
88 |
if (++i<argc) { |
69 |
11 Feb 06 |
jari |
89 |
data_file_=argv[i]; |
69 |
11 Feb 06 |
jari |
90 |
ok=true; |
69 |
11 Feb 06 |
jari |
91 |
} |
69 |
11 Feb 06 |
jari |
92 |
if (myargv==string("-neighbours")) |
69 |
11 Feb 06 |
jari |
93 |
if (++i<argc) { |
69 |
11 Feb 06 |
jari |
94 |
neighbours_=atoi(argv[i]); |
69 |
11 Feb 06 |
jari |
95 |
ok=true; |
69 |
11 Feb 06 |
jari |
96 |
} |
69 |
11 Feb 06 |
jari |
97 |
if (myargv==string("-nni_algorithm")) |
69 |
11 Feb 06 |
jari |
98 |
if (++i<argc) { |
69 |
11 Feb 06 |
jari |
99 |
ok=true; |
69 |
11 Feb 06 |
jari |
100 |
if (argv[i]==string("kNNI")) |
69 |
11 Feb 06 |
jari |
101 |
nni_algorithm_=kNNI; |
69 |
11 Feb 06 |
jari |
102 |
else |
69 |
11 Feb 06 |
jari |
103 |
if (argv[i]==string("WeNNI")) |
69 |
11 Feb 06 |
jari |
104 |
nni_algorithm_=WeNNI; |
69 |
11 Feb 06 |
jari |
105 |
else |
69 |
11 Feb 06 |
jari |
106 |
ok=false; |
69 |
11 Feb 06 |
jari |
107 |
} |
88 |
04 Apr 06 |
jari |
108 |
if (myargv=="--version") { |
88 |
04 Apr 06 |
jari |
109 |
version(); |
88 |
04 Apr 06 |
jari |
110 |
exit(0); |
88 |
04 Apr 06 |
jari |
111 |
} |
69 |
11 Feb 06 |
jari |
112 |
if (myargv==string("-weight")) |
69 |
11 Feb 06 |
jari |
113 |
if (++i<argc) { |
69 |
11 Feb 06 |
jari |
114 |
weight_file_=argv[i]; |
69 |
11 Feb 06 |
jari |
115 |
ok=true; |
69 |
11 Feb 06 |
jari |
116 |
} |
69 |
11 Feb 06 |
jari |
117 |
if (myargv==string("-weight_cutoff")) |
69 |
11 Feb 06 |
jari |
118 |
if (++i<argc) { |
69 |
11 Feb 06 |
jari |
119 |
weight_cutoff_=atof(argv[i]); |
69 |
11 Feb 06 |
jari |
120 |
ok=true; |
69 |
11 Feb 06 |
jari |
121 |
} |
69 |
11 Feb 06 |
jari |
122 |
if (myargv==string("-weight_is_snr")) { |
69 |
11 Feb 06 |
jari |
123 |
weight_is_snr_=!weight_is_snr_; |
69 |
11 Feb 06 |
jari |
124 |
ok=true; |
69 |
11 Feb 06 |
jari |
125 |
} |
69 |
11 Feb 06 |
jari |
126 |
|
69 |
11 Feb 06 |
jari |
127 |
if (!ok) |
69 |
11 Feb 06 |
jari |
128 |
std::cerr << "# Parameter::Parameter Invalid option:" |
69 |
11 Feb 06 |
jari |
129 |
<< argv[i] << std::endl; |
69 |
11 Feb 06 |
jari |
130 |
} |
69 |
11 Feb 06 |
jari |
131 |
|
69 |
11 Feb 06 |
jari |
132 |
analyse(); |
69 |
11 Feb 06 |
jari |
133 |
} |
69 |
11 Feb 06 |
jari |
134 |
|
69 |
11 Feb 06 |
jari |
135 |
|
69 |
11 Feb 06 |
jari |
136 |
|
69 |
11 Feb 06 |
jari |
137 |
void Parameter::analyse(void) |
69 |
11 Feb 06 |
jari |
138 |
{ |
69 |
11 Feb 06 |
jari |
139 |
using namespace theplu::utility; |
69 |
11 Feb 06 |
jari |
140 |
bool ok=true; |
69 |
11 Feb 06 |
jari |
141 |
if (FileIO().access_rights(data_file(),"r")) { |
69 |
11 Feb 06 |
jari |
142 |
std::cerr << "Cannot access data file " << data_file() << std::endl; |
69 |
11 Feb 06 |
jari |
143 |
ok=false; |
69 |
11 Feb 06 |
jari |
144 |
} |
69 |
11 Feb 06 |
jari |
145 |
if (FileIO().access_rights(weight_file(),"r")) { |
69 |
11 Feb 06 |
jari |
146 |
std::cerr << "Cannot access flag file " << weight_file() << std::endl; |
69 |
11 Feb 06 |
jari |
147 |
ok=false; |
69 |
11 Feb 06 |
jari |
148 |
} |
69 |
11 Feb 06 |
jari |
149 |
if (beta_<0) { |
69 |
11 Feb 06 |
jari |
150 |
std::cerr << "Negative beta (" << beta_ << ") is out of bounds." |
69 |
11 Feb 06 |
jari |
151 |
<< std::endl; |
69 |
11 Feb 06 |
jari |
152 |
ok=false; |
69 |
11 Feb 06 |
jari |
153 |
} |
69 |
11 Feb 06 |
jari |
154 |
if (weight_cutoff_ && (nni_algorithm()==WeNNI)) |
69 |
11 Feb 06 |
jari |
155 |
std::cerr << "Algo WeNNI set, non-zero weight cutoff (" << weight_cutoff_ |
69 |
11 Feb 06 |
jari |
156 |
<< ") ignorded." << std::endl; |
69 |
11 Feb 06 |
jari |
157 |
if ((nni_algorithm()<kNNI) || |
69 |
11 Feb 06 |
jari |
158 |
(nni_algorithm()>WeNNI)) { |
69 |
11 Feb 06 |
jari |
159 |
std::cerr << "Algo out of bounds " << nni_algorithm() << std::endl; |
69 |
11 Feb 06 |
jari |
160 |
ok=false; |
69 |
11 Feb 06 |
jari |
161 |
} |
69 |
11 Feb 06 |
jari |
162 |
if (!ok) |
69 |
11 Feb 06 |
jari |
163 |
std::exit(-1); |
69 |
11 Feb 06 |
jari |
164 |
} |
69 |
11 Feb 06 |
jari |
165 |
|
69 |
11 Feb 06 |
jari |
166 |
|
69 |
11 Feb 06 |
jari |
167 |
|
69 |
11 Feb 06 |
jari |
168 |
void Parameter::defaults(void) |
69 |
11 Feb 06 |
jari |
169 |
{ |
69 |
11 Feb 06 |
jari |
170 |
beta_=0.6; |
69 |
11 Feb 06 |
jari |
171 |
data_file_="data.data"; |
69 |
11 Feb 06 |
jari |
172 |
neighbours_=3; |
69 |
11 Feb 06 |
jari |
173 |
nni_algorithm_=WeNNI; |
69 |
11 Feb 06 |
jari |
174 |
weight_cutoff_=0; |
69 |
11 Feb 06 |
jari |
175 |
weight_file_="weight.data"; |
69 |
11 Feb 06 |
jari |
176 |
weight_is_snr_=false; |
69 |
11 Feb 06 |
jari |
177 |
} |
69 |
11 Feb 06 |
jari |
178 |
|
88 |
04 Apr 06 |
jari |
179 |
|
88 |
04 Apr 06 |
jari |
180 |
|
88 |
04 Apr 06 |
jari |
181 |
void Parameter::version(void) const |
88 |
04 Apr 06 |
jari |
182 |
{ |
88 |
04 Apr 06 |
jari |
183 |
using namespace std; |
88 |
04 Apr 06 |
jari |
184 |
cout << PACKAGE_STRING |
111 |
14 Jun 06 |
jari |
185 |
<< "\nCopyright (C) 2005-2006 Jari Häkkinen and Peter Johansson.\n\n" |
88 |
04 Apr 06 |
jari |
186 |
<< "This is free software; see the source for copying conditions.\n" |
88 |
04 Apr 06 |
jari |
187 |
<< "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n" |
88 |
04 Apr 06 |
jari |
188 |
<< "A PARTICULAR PURPOSE." << endl; |
88 |
04 Apr 06 |
jari |
189 |
} |
88 |
04 Apr 06 |
jari |
190 |
|
69 |
11 Feb 06 |
jari |
191 |
}} // of namespace wenni and namespace theplu |