-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathreader.cpp
76 lines (63 loc) · 1.78 KB
/
reader.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
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <iterator>
#include "reader.hh"
using namespace std;
/**
* @name readMatrix
* @brief read text file into std vector
*
* @param [in] in file containing tab separated data
* @param [in] data holds stuff to cluster
* @param [in] labels holds other stuff
* @param [in] dims which columns should it read
*/
void readMatrix(ifstream &in, vector<vector <float> > &data, vector< vector<string> > &labels, string dims){
if(!in.is_open()){
cout << "Failed to open file." << endl;
exit(1);
}
stringstream iss(dims);
vector<int> dim(2,1);
int d = -1;
//split string on "-"
if(dims != ""){
string item;
int ct = 0;
while( getline(iss, item, '-') && ct < 2){ dim[ct++] = atoi(item.c_str()); }
d = (dim[1] - dim[0]) + 1;
}
string line = "";
while(getline(in,line)) // loop through the file
{
stringstream is(line);
istream_iterator<string> begin(is);
istream_iterator<string> end;
vector<string> tokens(begin, end);
//require all rows to be same width
if( d<0 ){
d = tokens.size();
} else {
if(dims == "" && tokens.size() != (size_t)d){
cerr << "ragged array elements at " << data.size() << endl; exit(1);
}
}
if( (int)tokens.size()-dim[0]+1 < d ){ cerr << "too few array elements at " << data.size() << endl; exit(1); }
//stuff before the data cols
vector<string> tmps;
for(int i=0; i<dim[0]-1; ++i){
tmps.push_back( tokens[i] );
}
//data cols
vector<float> tmp;
for(int i=dim[0]-1; i<dim[0]-1+d; ++i){ tmp.push_back( atof(tokens[i].c_str()) ); }
//stuff after data cols
for(int i=dim[0]-1+d; i<(int)tokens.size(); ++i){
tmps.push_back( tokens[i] );
}
data.push_back(tmp);
labels.push_back(tmps);
}
}