TRN data format filter. More...
#include <data_file_TRN.hpp>
Public Member Functions | |
| void | open (const std::string &filename) |
| opens TRN file and reads header | |
| void | close () |
| closes TRN file if it is open | |
| const unsigned int | getNoOfFeatures () const |
| data dimensionality, as read from the header's #features line | |
| const unsigned int | getNoOfClasses () const |
| number of classes, as read from the header's #classes line | |
| const IDXTYPE | getClassSize (const unsigned int cidx) const |
| bool | read_val (DATATYPE &target) |
Reads the next value from the data section into target; returns false (without throwing) on EOF/parse failure, or if the header was never successfully read (open() failed or was not called). | |
| DATATYPE | read_val () |
| Reads and returns the next value from the data section; throws fst_error on EOF/parse failure, or if the header was never successfully read. | |
Protected Member Functions | |
| void | GetFileParams () |
| parses the TRN header (opens _filename, reads #datafile/#title/#features/#classes keywords, leaves the file positioned at the start of the data section in _datastart); throws fst_error on any malformed header | |
Protected Attributes | |
| std::string | _filename |
| input data file name, as passed to open() | |
| std::string | _title |
| optional data title (part of the data file); defaults to "no title" if the header omits #title | |
| unsigned int | _no_of_features |
| number of features (read from the header) | |
| unsigned int | _no_of_classes |
| number of classes (read from the header) | |
| std::vector< IDXTYPE > | _class_sizes |
| per-class sample counts, indexed by class index | |
| IDXTYPE | _datastart |
| file offset of the start of data past the header | |
| int | _valid |
| indicates the state, when header is successfully read and file open and ready to be read for actual data. | |
| FILE * | _file |
| the open TRN file handle, or nullptr if none is open | |
Private Member Functions | |
| Data_File_TRN (const Data_File_TRN &) | |
declared, not defined -> prevents pass-by-value/copy (predates the C++11 =delete idiom used elsewhere in FST4) | |
TRN data format filter.
Reads FST's own plain-text data format. A TRN file is a header followed by a data section.
Header. One keyword per line, the keyword being the line's first word:
#datafile – file-type marker, takes no argument. REQUIRED, and must precede every other keyword. #title <text> – optional; the rest of the line is the data set's title. Longer than 499 characters it is truncated. Omitted, the title reads "no title". #features <N> – REQUIRED; N is the data dimensionality, at least 1. #classes <K> <n1>,<n2>,... – REQUIRED; K is the number of classes, followed by exactly K per-class sample counts. Commas and whitespace separate the counts interchangeably. Every count must be at least 1. #data – optional; marks the end of the header. If it is absent, the header ends at the first line that is neither blank, a comment, nor a keyword, and that line is read as the first row of data.A line whose first non-blank character is a semicolon is a comment, and blank lines are ignored; both may appear anywhere in the header. No keyword may appear twice. Keyword order is otherwise free, except that #datafile comes first.
Data section. Whitespace-separated decimal numbers, read strictly in order: all samples of class 1, then all samples of class 2, and so on; within a class, one sample after another, each sample being #features consecutive values. Line breaks carry no meaning – only the sequence of numbers matters, although one sample per line is the usual layout. There is NO class-label column: class membership is positional and comes entirely from the counts on the #classes line, so the file must hold at least features * (n1+n2+...+nK) values.
A minimal example (2 features, 2 classes of 3 and 2 samples):
#datafile #title A tiny example ; comments start with a semicolon #features 2 #classes 2 3,2 #data 0.1 0.4 0.2 0.5 0.3 0.6 1.1 1.4 1.2 1.5
int, so a class larger than INT_MAX is not representable. Neither limit is reachable with realistic data. Malformed headers throw fst_error; see GetFileParams() for what each error code means.
|
inline |
size of class cidx, as read from the header's #classes line; 0 if cidx is out of range
|
protected |
parses the TRN header (opens _filename, reads #datafile/#title/#features/#classes keywords, leaves the file positioned at the start of the data section in _datastart); throws fst_error on any malformed header
Every failure throws fst_error with the same message text and a distinguishing code: 1 the file cannot be opened; 5 a keyword occurs twice; 6 a keyword or a data row appears before #datafile and #features have both been seen; 7 the #features argument is not a number; 8 the #classes argument is not a number, or the #classes line is missing altogether; 9 one of the per-class counts is not a number; 10 an unrecognized line appears before the header is complete; 11 the file holds nothing but blank and comment lines; 12 the file ends inside the header (neither a data row nor a #data line follows it); 88 the number of per-class counts differs from the declared class count; 100 fewer than one class; 101 fewer than one feature; 102 a class with fewer than one sample.