9 #ifndef REDUKT_DATAREADER_H
10 #define REDUKT_DATAREADER_H
12 #include <boost/numeric/ublas/matrix.hpp>
34 DataReader( C& data ): m_data(data), m_rows(-1),m_columns(-1) {}
36 bool loadCSV(
const char* filename );
42 inline bool insertValue(
int row,
int column,
double value );
46 size_t pos = s.find(
',');
47 while( pos != std::string::npos )
53 pos = s.find(
',', pos );
59 size_t pos = s.find(
'#');
60 if( pos != std::string::npos )
70 size_t pos = s.find_first_not_of(
' ');
73 if( pos == std::string::npos )
return true;
76 if( s[pos] ==
'#' )
return true;
83 int m_rows, m_columns;
100 f.open( filename, ifstream::in );
104 cout <<
"ERROR opening file " << filename << endl;
108 int rows=0, columns=-1;
115 cout <<
"Read " << cur_line <<
" lines\r" << std::flush;
117 getline( f, line ); cur_line++;
118 if( line.empty() )
continue;
121 if( is_comment_or_empty( line ) )
continue;
124 remove_commas( line );
127 remove_comments( line );
129 stringstream ss( line );
134 bool first_value_in_row=
true;
135 while( (ss >> value) && !ss.fail() )
138 if( cur_col >= columns )
140 cout << endl <<
"WARNING skipped additional column in line " << cur_line << endl;
144 insertValue( rows, cur_col, value );
147 first_value_in_row =
false;
152 if( ss.rdstate() & ifstream::failbit )
154 if( first_value_in_row )
156 cout << endl <<
"WARNING skipped line " << cur_line <<
" assuming it contains some kind of comment" << endl;
176 if( cur_col =! columns )
178 cout << endl <<
"ERROR in line " << cur_line <<
", too few columns!" << endl;
210 m_data( row, column ) = (float)value;
220 m_data( row, column ) = value;
228 m_data.push_back( (
float)value );
236 m_data.push_back( (
double)value );
241 #endif // REDUKT_DATAREADER_H
Load real valued matrix type data from disk (currently only CSV is supported).
Definition: DataReader.h:31
int numRows() const
Definition: DataReader.h:38
int numColumns() const
Definition: DataReader.h:39
DataReader(C &data)
Definition: DataReader.h:34
void remove_comments(std::string &s)
Definition: DataReader.h:57
bool insertValue(int row, int column, double value)
bool is_comment_or_empty(const std::string &s)
Definition: DataReader.h:67
bool loadCSV(const char *filename)
Definition: DataReader.h:94
void remove_commas(std::string &s)
Definition: DataReader.h:44