Debian Codegraph @

src/fileclasses/FileReaderAscii.h

raw symbols SCIP
1 /**********************************************************************
2 FileReaderAscii.h: class to read ASCII files using (colum based)
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #ifndef _IMGREADERASCII_H_
22 #define _IMGREADERASCII_H_
23
24 #include <string>
25 #include <vector>
26 #include <fstream>
27 #include "base/Optionjl.h"
28 //#include <armadillo>
29
30 //--------------------------------------------------------------------------
31 class FileReaderAscii
32 {
33 public:
34 FileReaderAscii();
35 FileReaderAscii(const std::string& filename);
36 FileReaderAscii(const std::string& filename, const char& fieldseparator);
37 ~FileReaderAscii();
38 void reset(){m_ifstream.clear();m_ifstream.seekg(0,std::ios::beg);};
39 void open(const std::string& filename);
40 void close(void);
41 void setFieldSeparator(const char& fieldseparator){m_fs=fieldseparator;};
42 void setMinRow(int minRow){m_minRow=minRow;};
43 void setMaxRow(int maxRow){m_maxRow=maxRow;};
44 void setComment(char comment){m_comment=comment;};
45 unsigned int nrOfCol(bool checkCols=false, bool verbose=false);
46 unsigned int nrOfRow(bool checkCols=false, bool verbose=false);
47 template<class T> unsigned int readData(std::vector<std::vector<T> > &dataVector, const std::vector<int> &cols, double scale=1.0, double offset=0.0, bool transpose=false, bool verbose=false);
48 template<class T> unsigned int readData(std::vector<T> &dataVector, int col, double scale=1.0, double offset=0, bool verbose=false);
49
50 protected:
51 std::string m_filename;
52 std::ifstream m_ifstream;
53 char m_fs;
54 char m_comment;
55 double m_min;
56 double m_max;
57 int m_minRow;
58 int m_maxRow;
59 };
60
61 template<class T> unsigned int FileReaderAscii::readData(std::vector<T> &dataVector, int col, double scale, double offset, bool verbose){
62 reset();
63 dataVector.clear();
64 int nrow=0;
65 bool withinRange=true;
66 if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
67 if(verbose)
68 std::cout << "reading csv file " << m_filename << std::endl;
69 std::string csvRecord;
70 while(getline(m_ifstream,csvRecord)){//read a line
71 withinRange=true;
72 if(nrow<m_minRow)
73 withinRange=false;
74 if(m_maxRow>m_minRow)
75 if(nrow>m_maxRow)
76 withinRange=false;
77 if(withinRange){
78 std::istringstream csvstream(csvRecord);
79 std::string item;
80 int ncol=0;
81 bool isComment=false;
82 while(getline(csvstream,item,m_fs)){//read a column
83 if(verbose)
84 std::cout << item << " ";
85 size_t pos=item.find(m_comment);
86 if(pos!=std::string::npos){
87 isComment=true;
88 if(pos>0)
89 item=item.substr(0,pos-1);
90 else
91 break;
92 if(verbose)
93 std::cout << "comment found, string is " << item << std::endl;
94 }
95 if(ncol==col){
96 T value=scale*string2type<T>(item)+offset;
97 if((value>=m_min&&value<=m_max)||m_max<=m_min)
98 dataVector.push_back(value);
99 }
100 ++ncol;
101 if(isComment)
102 break;
103 }
104 if(verbose)
105 std::cout << std::endl;
106 if(dataVector.size()&&ncol<=col){
107 std::ostringstream ess;
108 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << ")" << std::endl;
109 throw(ess.str());
110 }
111 }
112 ++nrow;
113 }
114 assert(dataVector.size());
115 }
116 else{//space or tab delimited fields
117 if(verbose)
118 std::cout << "space or tab delimited fields" << std::endl;
119 std::string spaceRecord;
120 while(!getline(m_ifstream, spaceRecord).eof()){
121 withinRange=true;
122 if(nrow<m_minRow)
123 withinRange=false;
124 if(m_maxRow>m_minRow)
125 if(nrow>m_maxRow)
126 withinRange=false;
127 if(withinRange){
128 if(verbose>1)
129 std::cout << spaceRecord << std::endl;
130 std::istringstream lineStream(spaceRecord);
131 std::string item;
132 int ncol=0;
133 bool isComment=false;
134 while(lineStream >> item){
135 if(verbose)
136 std::cout << item << " ";
137 // std::istringstream itemStream(item);
138 size_t pos=item.find(m_comment);
139 if(pos!=std::string::npos){
140 isComment=true;
141 if(pos>0)
142 item=item.substr(0,pos-1);
143 else
144 break;
145 if(verbose)
146 std::cout << "comment found, string is " << item << std::endl;
147 }
148 T value=scale*string2type<T>(item)+offset;
149 // T value=string2type<T>(item);
150 if(ncol==col){
151 if((value>=m_min&&value<=m_max)||m_max<=m_min)
152 dataVector.push_back(value);
153 }
154 ++ncol;
155 if(isComment)
156 break;
157 }
158 if(verbose>1)
159 std::cout << std::endl;
160 if(verbose)
161 std::cout << "number of columns: " << ncol << std::endl;
162 if(dataVector.size()&&ncol<=col){
163 std::ostringstream ess;
164 ess << "Error: different number of cols found in line " << nrow << " (" << ncol << ")" << std::endl;
165 throw(ess.str());
166 }
167 }
168 ++nrow;
169 }
170 }
171 return dataVector.size();
172 }
173
174 template<class T> unsigned int FileReaderAscii::readData(std::vector<std::vector<T> > &dataVector, const std::vector<int> &cols, double scale, double offset, bool transpose, bool verbose){
175 reset();
176 dataVector.clear();
177 if(!transpose)
178 dataVector.resize(cols.size());
179 int nrow=0;
180 bool withinRange=true;
181 if(m_fs>' '&&m_fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
182 if(verbose)
183 std::cout << "reading csv file " << m_filename << std::endl;
184 std::string csvRecord;
185 while(getline(m_ifstream,csvRecord)){//read a line
186 std::vector<T> sampleVector;
187 withinRange=true;
188 if(nrow<m_minRow)
189 withinRange=false;
190 if(m_maxRow>m_minRow)
191 if(nrow>m_maxRow)
192 withinRange=false;
193 if(withinRange){
194 std::istringstream csvstream(csvRecord);
195 std::string item;
196 int ncol=0;
197 bool isComment=false;
198 while(getline(csvstream,item,m_fs)){//read a column
199 if(verbose)
200 std::cout << item << " ";
201 size_t pos=item.find(m_comment);
202 if(pos!=std::string::npos){
203 isComment=true;
204 if(pos>0)
205 item=item.substr(0,pos-1);
206 else
207 break;
208 if(verbose)
209 std::cout << "comment found, string is " << item << std::endl;
210 }
211 for(int icol=0;icol<cols.size();++icol){
212 if(ncol==cols[icol]){
213 T value=scale*string2type<T>(item)+offset;
214 // T value=string2type<T>(item);
215 if((value>=m_min&&value<=m_max)||m_max<=m_min){
216 if(transpose)
217 sampleVector.push_back(value);
218 else
219 dataVector[icol].push_back(value);
220 }
221 }
222 }
223 ++ncol;
224 if(isComment)
225 break;
226 }
227 if(verbose)
228 std::cout << std::endl;
229 // if(dataVector.back().size())
230 // assert(ncol>=cols[0]);
231 }
232 if(sampleVector.size()&&transpose)
233 dataVector.push_back(sampleVector);
234 ++nrow;
235 }
236 assert(dataVector.size());
237 }
238 else{//space or tab delimited fields
239 if(verbose)
240 std::cout << "space or tab delimited fields" << std::endl;
241 std::string spaceRecord;
242 while(!getline(m_ifstream, spaceRecord).eof()){
243 std::vector<T> sampleVector;
244 withinRange=true;
245 if(nrow<m_minRow)
246 withinRange=false;
247 if(m_maxRow>m_minRow)
248 if(nrow>m_maxRow)
249 withinRange=false;
250 if(withinRange){
251 if(verbose>1)
252 std::cout << spaceRecord << std::endl;
253 std::istringstream lineStream(spaceRecord);
254 std::string item;
255 int ncol=0;
256 bool isComment=false;
257 while(lineStream >> item){
258 if(verbose)
259 std::cout << item << " ";
260 // std::istringstream itemStream(item);
261 size_t pos=item.find(m_comment);
262 if(pos!=std::string::npos){
263 isComment=true;
264 if(pos>0)
265 item=item.substr(0,pos-1);
266 else
267 break;
268 if(verbose)
269 std::cout << "comment found, string is " << item << std::endl;
270 }
271 T value=scale*string2type<T>(item)+offset;
272 // T value=string2type<T>(item);
273 for(int icol=0;icol<cols.size();++icol){
274 if(ncol==cols[icol]){
275 if((value>=m_min&&value<=m_max)||m_max<=m_min){
276 if(transpose)
277 sampleVector.push_back(value);
278 else
279 dataVector[icol].push_back(value);
280 }
281 }
282 }
283 ++ncol;
284 if(isComment)
285 break;
286 }
287 if(verbose>1)
288 std::cout << std::endl;
289 if(verbose)
290 std::cout << "number of columns: " << ncol << std::endl;
291 // if(dataVector.back().size())
292 // assert(ncol>=cols[0]);
293 }
294 if(sampleVector.size()&&transpose)
295 dataVector.push_back(sampleVector);
296 ++nrow;
297 }
298 }
299 return dataVector.size();
300 }
301
302 #endif // _IMGREADERASCII_H_