1#ifndef FSTDATAFILETRN_H
2#define FSTDATAFILETRN_H
135template<
typename DATATYPE,
typename IDXTYPE>
139 ~Data_File_TRN() {
close();}
140 void open(
const std::string& filename);
174template<
typename DATATYPE,
typename IDXTYPE>
183template<
typename DATATYPE,
typename IDXTYPE>
186 const unsigned int ONUMCOM=5;
187 const unsigned int OHEADSTRLEN=1000;
188 const unsigned int OFILENAMELENGTH=500;
189 const std::string ONOTITLE=
"no title";
190 const char *command[ONUMCOM]={
"#datafile",
196 char word[OHEADSTRLEN];
221 while((!feof(
_file))&&(!header_done))
224 if(fgets(s,OHEADSTRLEN,
_file)==
nullptr)
break;
226 scanned=sscanf(s,
"%s",word);
227 if((scanned)&&(scanned!=EOF)&&(word[0]!=
';')&&(word[0]!=0))
233 for(i=0;(i<ONUMCOM)&&(!keyword);i++)
235 s2=strstr(word,command[i]);
239 s3=strstr(s,word)+strlen(command[i]);
248 if(!((seen&1)&&(seen&4)))
249 {
throw fst_error(
"Data_File_TRN file read problem.",10);}
251 else for(i=0;s3[i]!=0;i++)
if(s3[i]==
',')s3[i]=
' ';
254 if((keyword)&&(seen&keyword)) {
throw fst_error(
"Data_File_TRN file read problem.",5);}
259 case 0:
if((seen&1)&&(seen&4))
267 else {
throw fst_error(
"Data_File_TRN file read problem.",6);}
270 case 2:
if(!(seen&1)) {
throw fst_error(
"Data_File_TRN file read problem.",6);}
272 s2=strstr(s3,
"\n");
if(s2!=
nullptr) s2[0]=0;
273 while((s3[0]!=0)&&((s3[0]==
' ')||(s3[0]==
'\t'))) s3++;
275 if(strlen(s3)>=OFILENAMELENGTH)
276 s3[OFILENAMELENGTH-1]=0;
281 case 4:
if(!(seen&1)) {
throw fst_error(
"Data_File_TRN file read problem.",6);}
286 if((!scanned)||(scanned==EOF))
289 throw fst_error(
"Data_File_TRN file read problem.",7);
294 case 8:
if(!(seen&1)) {
throw fst_error(
"Data_File_TRN file read problem.",6);}
299 if((!scanned)||(scanned==EOF))
302 throw fst_error(
"Data_File_TRN file read problem.",8);
310 sscanf(s3,
"%s",word);
311 s3=strstr(s3,word)+strlen(word);
316 scanned=sscanf(s3,
"%d",&tmp_scan); temp=(IDXTYPE)tmp_scan;
318 if((!scanned)||(scanned==EOF))
320 throw fst_error(
"Data_File_TRN file read problem.",9);
325 sscanf(s3,
"%s",word);
326 s3=strstr(s3,word)+strlen(word);
333 case 16:
if((seen&1)&&(seen&4))
338 else {
throw fst_error(
"Data_File_TRN file read problem.",6);}
347 if(empty) {
throw fst_error(
"Data_File_TRN file read problem.",11);}
349 if(!header_done) {
throw fst_error(
"Data_File_TRN file read problem.",12);}
354 throw fst_error(
"Data_File_TRN file read problem.",8);
364template<
typename DATATYPE,
typename IDXTYPE>
368 double dbltmp;
int err;
369 err=fscanf(
_file,
"%lf",&dbltmp);
378template<
typename DATATYPE,
typename IDXTYPE>
382 double dbltmp;
int err;
383 err=fscanf(
_file,
"%lf",&dbltmp);
384 if(err>0)
return DATATYPE(dbltmp);
else throw fst_error(
"Data_File_TRN::read_val() problem.",200);
386 throw fst_error(
"Data_File_TRN::read_val() problem.",201);
int _valid
indicates the state, when header is successfully read and file open and ready to be read for actual d...
Definition data_file_TRN.hpp:170
const unsigned int getNoOfFeatures() const
data dimensionality, as read from the header's #features line
Definition data_file_TRN.hpp:142
void close()
closes TRN file if it is open
Definition data_file_TRN.hpp:141
const unsigned int getNoOfClasses() const
number of classes, as read from the header's #classes line
Definition data_file_TRN.hpp:143
const IDXTYPE getClassSize(const unsigned int cidx) const
Definition data_file_TRN.hpp:144
Data_File_TRN(const Data_File_TRN &)
declared, not defined -> prevents pass-by-value/copy (predates the C++11 =delete idiom used elsewhere...
std::vector< IDXTYPE > _class_sizes
per-class sample counts, indexed by class index
Definition data_file_TRN.hpp:168
FILE * _file
the open TRN file handle, or nullptr if none is open
Definition data_file_TRN.hpp:171
IDXTYPE _datastart
file offset of the start of data past the header
Definition data_file_TRN.hpp:169
void GetFileParams()
parses the TRN header (opens _filename, reads #datafile/#title/#features/#classes keywords,...
Definition data_file_TRN.hpp:184
std::string _filename
input data file name, as passed to open()
Definition data_file_TRN.hpp:164
DATATYPE read_val()
Reads and returns the next value from the data section; throws fst_error on EOF/parse failure,...
Definition data_file_TRN.hpp:379
unsigned int _no_of_features
number of features (read from the header)
Definition data_file_TRN.hpp:166
std::string _title
optional data title (part of the data file); defaults to "no title" if the header omits #title
Definition data_file_TRN.hpp:165
void open(const std::string &filename)
opens TRN file and reads header
Definition data_file_TRN.hpp:175
unsigned int _no_of_classes
number of classes (read from the header)
Definition data_file_TRN.hpp:167
FST4 specific error passing class.
Definition error.hpp:61
FST4 specific error passing class.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80