-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCClctrl.cpp
217 lines (206 loc) · 4.85 KB
/
CClctrl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "CClctrl.h"
CClctrl::CClctrl(int arc, char** arv) : argc(arc), argv(arv)
{
argNo=1;
fileFormat = 0; // svmlight format
verbosity = 2; // second highest level (highest is 3)
time_t seconds;
time(&seconds);
setSeed((unsigned long) seconds);
}
bool CClctrl::isCurrentArg(string shortName, string commandName)
{
return (getCurrentArgument()==shortName || getCurrentArgument()==commandName);
}
void CClctrl::confirmCurrentArg(string commandName)
{
if(getCurrentArgument()!=commandName)
exitError("Unrecognised argument " + getCurrentArgument() + " for `" + getMode() + "' command.");
}
void CClctrl::unrecognisedFlag()
{
exitError("Unrecognised flag: " + getCurrentArgument() + " under " + getMode() + " command.");
}
void CClctrl::helpUsage(const string description, const int width, const int padding)
{
ndlstrutil::wrapOutputText(cout, "Usage: " + description, width, padding);
cout << endl;
cout << endl;
}
void CClctrl::helpDescriptor(const string description, const int width, const int padding)
{
ndlstrutil::wrapOutputText(cout, description, width, padding);
cout << endl;
cout << endl;
}
void CClctrl::helpArgument(const string flags, const string explanation, const int width, const int padding)
{
string padStr="";
for(int i=0; i<padding; i++)
padStr += " ";
cout << padStr << flags << endl;
ndlstrutil::wrapOutputText(cout, explanation, width, padding+3);
cout << endl;
cout << endl;
}
void CClctrl::waitForSpace()
{
cout << "Press enter for more." << endl;
char ch;
cin.get(ch);
}
int CClctrl::readSvmlDataFile(CMatrix& X, CMatrix& y, const string fileName)
{
ifstream in(fileName.c_str());
if(!in.is_open()) throw ndlexceptions::FileReadError(fileName);
string line;
string token;
bool featureRead=false;
int numData=0;
int maxFeat=0;
while(getline(in, line))
{
featureRead=false;
if(line[line.size()-1]=='\r')
line.erase(line.size()-1);
if(line[0]=='#')
continue;
numData++;
int pos=0;
while(pos<line.size())
{
token.erase();
while(pos<line.size() && line[pos]!=' ')
{
token+=line[pos];
pos++;
}
pos++;
if(token.size()>0)
{
// deal with token.
if(featureRead)
{
int ind = token.find(':');
if(ind==std::string::npos || ind < 0)
{
ndlexceptions::StreamFormatError err("");
throw ndlexceptions::FileFormatError(fileName, err);
}
string featStr=token.substr(0, ind);
int featNum = atoi(featStr.c_str());
if(featNum>maxFeat)
maxFeat=featNum;
}
else
{
featureRead=true;
}
}
}
}
if(verbosity>1)
{
cout << "Data number of features: " << maxFeat << endl;
cout << "Number of data: " << numData << endl;
}
X.resize(numData, maxFeat);
X.zeros();
y.resize(numData, 1);
y.zeros();
in.close();
ifstream inToo(fileName.c_str());
int pointNo=0;
while(getline(inToo, line))
{
if(line[line.size()-1]=='\r')
line.erase(line.size()-1);
featureRead=false;
if(line[0]=='#')
continue;
else
{
int pos=0;
while(pos<line.size())
{
token.erase();
while(pos<line.size() && line[pos]!=' ')
{
token+=line[pos];
pos++;
}
pos++;
if(token.size()>0)
{
// deal with token.
if(featureRead)
{
int ind = token.find(':');
// TODO Check that : is in the string.
string featStr=token.substr(0, ind);
string featValStr=token.substr(ind+1, token.size()-ind);
int featNum = atoi(featStr.c_str());
if(featNum<1 || featNum>maxFeat || pointNo<0 || pointNo>=numData)
{
ndlexceptions::StreamFormatError err("");
throw ndlexceptions::FileFormatError(fileName, err);
}
double featVal = atof(featValStr.c_str());
X.setVal(featVal, pointNo, featNum-1);
}
else
{
y.setVal(atof(token.c_str()), pointNo);
featureRead=true;
}
}
}
}
pointNo++;
}
// WVB ADDED
return -1;
}
void CClctrl::readData(CMatrix& X, CMatrix& y, const string fileName)
{
string m = getMode();
setMode("file");
if(verbosity>1)
cout << "Loading data." << endl;
switch(fileFormat)
{
case 0: /// svmlight file format.
readSvmlDataFile(X, y, fileName);
break;
case 1: /// Matlab file format.
#ifdef _NDLMATLAB
X.readMatlabFile(fileName, "X");
y.readMatlabFile(fileName, "y");
#else
throw ndlexceptions::MatlabInterfaceError("MATLAB not incorporated at compile time");
#endif
break;
default:
exitError("Unrecognised file format number.");
}
if(verbosity>1)
cout << "Data set loaded." << endl;
setMode(m);
}
void CClctrl::exitNormal()
{
#ifdef _DEBUG
#ifdef _MSC_VER
// For debugging under visual studio, to prevent window disappearing.
waitForSpace();
#endif
#endif
exit(0);
}
void CClctrl::exitError(const string error)
{
cerr << error << endl << endl;
waitForSpace();
helpInfo();
exit(1);
}