-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathARFF.cpp
More file actions
377 lines (324 loc) · 12.8 KB
/
ARFF.cpp
File metadata and controls
377 lines (324 loc) · 12.8 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "ARFF.h"
#include "AdditionalMethods.h"
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
//#include "DataObject.h"
/*! \class ARFF
* \brief A class for reading data from or writing data to an arff file.
*/
ARFF::ARFF()
{
classFound = false;
data.reserve(0);
attributes.reserve(0);
attributeIntClass.reserve(0);
attributeStringClass.reserve(0);
attributesTypes.reserve(0);
objIntClass.reserve(0);
}
ARFF::ARFF(const char* path)
{
classFound = false;
data.reserve(0);
attributes.reserve(0);
attributeIntClass.reserve(0);
attributeStringClass.reserve(0);
attributesTypes.reserve(0);
objIntClass.reserve(0);
this->fileToRead = path;
this->readArffFile();
}
ARFF::~ARFF()
{
}
std::vector<int> ARFF::getAttributeClasses()
{
return this->attributeIntClass;
}
std::vector<std::string> ARFF::getAttributeStringClasses()
{
return this->attributeStringClass;
}
void ARFF::readArffFile()
{
//LOG(INFO) << "Initiating arff file " << DamisFile::getFilePath() << " read";
std::stringstream s;
std::ifstream file (this->fileToRead);
std::string line_from_file;
std::string tmp1, tmp2, tmp3;
std::vector<std::string> tmp, tmp4;
std::vector<std::string> stringVector; stringVector.reserve(0);
std::vector<double> doubleVector; doubleVector.reserve(0);
int line_no = 1;
if (file.is_open() != false)
{
readSuccess = true;
while (std::getline(file, line_from_file))
{
if (line_from_file.length() == 0)
{
line_no++;
continue;
}
std::istringstream iss(line_from_file);
std::string sub;
iss >> sub;
if (sub == "%")
{
line_no++;
continue;
}
else
{
std::transform(sub.begin(), sub.end(), sub.begin(), ::toupper);
if (sub == "@ATTRIBUTE")
{
iss >> tmp1;
iss >> tmp2;
std::transform(tmp1.begin(), tmp1.end(), tmp1.begin(), ::toupper);
std::transform(tmp2.begin(), tmp2.end(), tmp2.begin(), ::toupper);
if (tmp1 == "CLASS" || tmp1 == "'CLASS'")
{
ARFF::classFound = true;
ARFF::classAttributeIndex = ARFF::attributes.size();
// LOG(INFO) << "Found CLASS attribute at index " << ArffFile::classAttributeIndex;
int startClassList = line_from_file.find("{");
int endClassList = line_from_file.find("}");
if (startClassList != std::string::npos && endClassList != std::string::npos) //if both brackets found
{
tmp3.assign(line_from_file, startClassList + 1, endClassList - startClassList - 1);
// LOG(INFO) << "Classes are " << tmp3;
tmp4 = AdditionalMethods::split(tmp3, ','); //split resulting string
std::string className;
for (unsigned int i = 0; i < tmp4.size(); i++) //push back each class label
{
//trim spaces from start
tmp4[i].erase(tmp4[i].begin(), std::find_if(tmp4[i].begin(), tmp4[i].end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
//trim spaces from end
tmp4[i].erase(std::find_if(tmp4[i].rbegin(), tmp4[i].rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), tmp4[i].end());
className.assign(tmp4[i]);
std::transform(className.begin(), className.end(), className.begin(), ::toupper);
ARFF::attributeStringClass.push_back(className);
ARFF::attributeIntClass.push_back(i);
}
}
else
{
// std::cout << "Class atribute is ill-defined, cannot find {} brackets";
readSuccess = false;
}
}
else if (tmp2 == "REAL" || tmp2 == "INTEGER" || tmp2 == "NUMERIC")
{
ARFF::attributes.push_back(tmp1);
ARFF::attributesTypes.push_back(tmp2);
}
}
else if (sub == "@DATA" || sub == "@RELATION")
{
line_no++;
continue;
}
else
{
tmp = AdditionalMethods::split(line_from_file, ','); //split data section by comma
int noOfAttr = (ARFF::classFound) ? ARFF::attributes.size() + 1 : ARFF::attributes.size(); //if there are class add 1 to atrributes
if (tmp.size() == noOfAttr)
{
bool badClass;
if (ARFF::classFound)
badClass = true;
else
badClass = false;
bool badAtrrSection = false;
std::string className;
int classInt;
for (unsigned int i = 0; i < tmp.size(); i++) // for each attribute in each data line
{
if (ARFF::classFound && (i == ARFF::classAttributeIndex))
{
//check if the class attribute in data section is valid
//trim spaces from start
tmp[i].erase(tmp[i].begin(), std::find_if(tmp[i].begin(), tmp[i].end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
//trim spaces from end
tmp[i].erase(std::find_if(tmp[i].rbegin(), tmp[i].rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), tmp[i].end());
className.assign(tmp[i]);
std::transform(className.begin(), className.end(), className.begin(), ::toupper);
if (className !="?")
{
for (int cA = 0; cA < ARFF::attributeStringClass.size(); cA++)
{
if (ARFF::attributeStringClass.at(cA) == className)
{
badClass = false;
classInt = ARFF::attributeIntClass.at(cA);
break;
}
}
}
else
{
badClass = false;
}
if (badClass)
{
// std::cout << "Found invalid class at line - " << line_no;
readSuccess = false;
//continue;
}
}
else
{
//try to convert to double
char *err;
double x = strtod(tmp[i].c_str(), &err);
if (*err == 0 && tmp[i] !="")
doubleVector.push_back(x);
else
{
// std::cout << "File is not valid (skipping object), found non numeric value \\ " << tmp[i] << " \\ at line " <<line_no <<" at position " <<i;
badAtrrSection = true;
readSuccess = false;
}
}
} //end for tmp.size();
if (badAtrrSection == false && badClass == false)
{
if (ARFF::classFound)
{
if(className == "?")
ARFF::objIntClass.push_back(-1);
else
ARFF::objIntClass.push_back(ARFF::attributeIntClass.at(classInt));
// std::cout << ARFF::attributeIntClass.at(classInt) << std::endl;
}
ARFF::data.push_back(doubleVector); //add to matrix
}
doubleVector.clear();
}
else
{
// std::cout << "Data section line " << line_no << " does not have required quantity features ";
readSuccess = false;
}
}
}
line_no++;
}
file.close();
}
else
{
// std::cout << "Cannot open file " << this->fileToRead << " for reading into ARFF object";
readSuccess = false;
}
if (ARFF::data.empty() || ARFF::attributes.empty())
{
// std::cout << "Data file " << this->fileToRead << " does not contain either attribute or correct data section ";
readSuccess = false;
}
}
int ARFF::getObjectClass(int i)
{
return objIntClass.at(i);
}
/**
* Returns indicates is class attribute found
*/
bool ARFF::isClassFound()
{
return ARFF::classFound;
}
/**
* Returns objects class list <int> (data section)
*/
std::vector<int> ARFF::getIntClass()
{
return ARFF::objIntClass;
}
void ARFF::writeStatData(std::string statFile, double err, double calcTime)
{
std::scientific;
std::ofstream file(statFile.c_str());
file << "%"<<std::endl;
file<<"@ATTRIBUTE algError REAL"<<std::endl;
file<<"@ATTRIBUTE calcTime REAL"<<std::endl;
file <<"%"<<std::endl;
file<<"@DATA"<<std::endl;
time_t endTime;
time(&endTime);
file << err << ", "<<difftime(endTime, AdditionalMethods::startTime);
file.close();
// AdditionalMethods::deleteFile();
}
std::vector<std::string> ARFF::getAttributes()
{
return attributes;
}
std::vector<std::vector<double> > ARFF::getData()
{
return data;
}
void ARFF::writeData(const char* path, std::vector<std::string> comments, std::vector<DataObject> data, std::vector <std::string> classLabels, std::vector<std::string> attributeLabels)
{
std::ofstream file (path);
int n = data.size();
int k = 0;
std::vector<std::string> tmp;
file << "%"<<std::endl;
if (!comments.empty())
{
for (int i = 0; i < comments.size(); i++)
{
//tmp = AdditionalMethods::split(comments.at(i), '\n');
file <<comments.at(i);
/*for (int j = 0; j < tmp.size(); j++)
{
file <<"% " << tmp[j] << std::endl;
}*/
}
file << "%"<<std::endl;
}
for (int i = 0; i < attributeLabels.size(); i++)
file<<"@ATTRIBUTE "<<attributeLabels.at(i)<<std::endl;
if (!classLabels.empty())
{
file <<"@ATTRIBUTE Class {";
for (int i = 0; i < classLabels.size() - 1; i++)
{
file << classLabels.at(i) << ", ";
}
file << classLabels.at(classLabels.size() - 1) << "}"<< std::endl;
}
file << "%"<<std::endl;
file<<"@DATA"<<std::endl;
if (!data.empty())
{
// int featureCount = data.at(0).getFeatureCount();
for (int i = 0; i < n; i++)
{
k = data.at(i).getFeatureCount();
for (int j = 0; j < k - 1; j++)
file<<data.at(i).getFeatureAt(j)<<", ";
if (!classLabels.empty() && data.at(i).getClassLabel() != -1)
file<<data.at(i).getFeatureAt(k - 1)<< ", " << classLabels.at(data.at(i).getClassLabel()) << std::endl;
else if (!classLabels.empty() && data.at(i).getClassLabel() == -1)
file<<data.at(i).getFeatureAt(k - 1)<< ", ?" << std::endl;
else
file<<data.at(i).getFeatureAt(k - 1) << std::endl;
}
}
file.close();
}
std::string ARFF::getFailReason()
{
return failReason;
}
bool ARFF::isSuccessfullyRead()
{
return readSuccess;
}