Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches

ARFF data format filter. More...

#include <data_file_ARFF.hpp>

Classes

struct  SkippedColumn
 One @ATTRIBUTE column that was recognized but could not be loaded (STRING/DATE/unknown type), for the load-summary report. More...

Public Member Functions

 Data_File_ARFF (std::string fileName)
 constructs an (unloaded) reader for fileName; call prepareFile() to actually read it
bool prepareFile ()
 Reads and parses the whole .arff file: the header (@RELATION/@ATTRIBUTE lines) then every @DATA row. Columns of an unsupported type (STRING, DATE, or any other non-numeric/non-nominal type) are skipped gracefully with a loud per-column message; a malformed @ATTRIBUTE definition is still a hard failure. Loading only hard-fails on unsupported columns if NONE of them turn out loadable (every column skipped).
void sortRecordsByClass ()
 Reorders data in place, grouping records by class id (bucket sort over classCount buckets, then concatenated in class order).
unsigned int getNoOfClasses ()
 Number of classes, i.e. the CLASS attribute's value count.
unsigned int getNoOfFeatures ()
 number of non-class loadable features (features, i.e. attributes minus the CLASS attribute)
unsigned int getNoOfAttributes ()
unsigned int getTotalSize ()
 Number of loaded records (data's size).
unsigned int getClassSize (int clsId)
 Number of records belonging to class clsId.
std::string getClassName (int clsId)
 Name (nominal value text) of class clsId.
Data_File_ARFF_Record * getRecordAt (long i)
 the loaded record at index i, or nullptr if i is out of range
float getFeatureValue (long record, int feature)
 The value of features[feature] in record record, read out of that record's raw data buffer.
void printInfo ()
 prints a full debug dump (every record's feature values, the attribute list with nominal value tables, and per-class sizes) to stdout; just a "File not loaded" message if loaded is false

Private Member Functions

Data_File_ARFF_Feature * createAttribute (char *info)
bool readNextRow (char *row)
 Reads and stores one data row (dense or sparse, dispatched on a leading '{'); increments the row's class's classSize entry if the row has a class id.
void prepareAttributes ()
 Post-@DATA setup: assigns each loaded attribute's Data_File_ARFF_Feature::offset within a record buffer, splits off the CLASS attribute into classAttribute/classCount (allocating classSize), and sets recordSize.
Data_File_ARFF_Record * readNormalRow (char *row)
 Parses one dense (comma-separated) data row into a new record, walking file columns through columnMap (skipped columns are consumed and discarded).
Data_File_ARFF_Record * readSparseRow (char *row)
 Parses one sparse ({idx value, idx value, ...}) data row into a new record (unset attributes default to 0; an omitted class attribute defaults to class 0), remapping each file column index through columnMap.
bool readAttrValue (Data_File_ARFF_Feature *feature, Data_File_ARFF_Record *rec, char *value)
 Parses value into feature's slot of rec's data buffer (float for TYPE_NUMERIC, a Data_File_ARFF_Feature::values index for TYPE_NOMINAL); also sets rec's class id if feature isClass.
bool readLine (FILE *f, std::vector< char > &buff)
char * trim (char *szSource)
 trims a string (removes spaces, tabs, and cr / lf
char * mystrupr (char *szSource)
 replaces nonstandard strupr

Private Attributes

std::string _fileName
 input .arff file name, as passed to the constructor
std::vector< Data_File_ARFF_Record * > data
 loaded records, owned (deleted in the destructor); reordered in place by sortRecordsByClass()
std::vector< Data_File_ARFF_Feature * > attributes
 LOADED attribute columns only (unsupported types are skipped), INCLUDING the CLASS attribute if present; owned (deleted in the destructor).
std::vector< Data_File_ARFF_Feature * > features
 the subset of attributes that are actual features (i.e. excluding the CLASS attribute); populated by prepareAttributes()
std::vector< SkippedColumn > skippedColumns
 every column skipped due to an unsupported type, in file order; see columnMap
std::vector< int > columnMap
 one entry per @ATTRIBUTE line in file order: the index into attributes for a loaded column, or -1 for a skipped one (see skippedColumns)
int * classSize
 per-class record counts, indexed by class id (size classCount); owned, allocated in prepareAttributes(), freed in the destructor
std::string relationName
 the @RELATION name
int attributesCount
 number of loaded (non-skipped) attribute columns, i.e. attributes.size()
int classAttribute
 index into attributes of the nominal attribute named CLASS/'CLASS', or -1 if none was found
int classCount
 number of classes (the CLASS attribute's value count), or -1 if classAttribute is -1
int recordSize
 size in bytes of one record's raw data buffer (sum of all loaded attributes' Data_File_ARFF_Feature::size); set by prepareAttributes()
bool loaded
 whether prepareFile() completed without error; getters that need loaded data throw fst_error while false

Detailed Description

ARFF data format filter.

Reads the subset of Weka's ARFF format that FST4 can use.

Supported. @RELATION, @ATTRIBUTE and @DATA sections in the usual order; lines starting with '' are comments and blank lines are ignored; keywords are matched case-insensitively; data rows may be dense (comma-separated, values optionally single- or double-quoted) or sparse ({index value, index value, ...}, where an omitted attribute means 0 and an omitted class means class 0). Lines may be of any length.

Attribute types. NUMERIC, REAL and INTEGER load as floating-point features. A nominal type ({v1,v2,...}) loads as its 0-based index into the declared value list; a row's value must match one of the declared names exactly (after trimming), or the row fails.

Attribute types that are skipped. STRING, DATE, RELATIONAL and any unrecognized type cannot be represented as a feature. Such a column is reported on standard output and then skipped: its tokens are parsed and discarded, and the file continues loading.

Warning
A skipped column does NOT keep an index. Feature indices are assigned to the REMAINING columns in file order, so every column after a skipped one shifts down by one, and the mapping between FST4 feature numbers and columns in the file is no longer the identity. The load summary states how many columns were skipped. If every column is skipped, loading fails.

The class attribute. The class is the nominal attribute named CLASS (case-insensitive, with or without surrounding single quotes). It is excluded from the features, and its value list defines the classes, in declaration order. A file without such an attribute loads, but reports zero classes and cannot be split by class – so the FST4 accessors require one.

Errors. A malformed @ATTRIBUTE definition, an unparseable data value, and a sparse row with an out-of-range column index all abort the load; the reader reports the reason on standard output and prepareFile() returns false. An unopenable file throws fst_error.

Member Function Documentation

◆ createAttribute()

Data_File_ARFF_Feature * FST::Data_File_ARFF::createAttribute ( char * info)
inlineprivate

parses one @ATTRIBUTE definition's name type text (info) into a new Data_File_ARFF_Feature (TYPE_STRING/TYPE_UNSUPPORTED for unloadable types); returns nullptr on a malformed definition

Nominal type

◆ getClassName()

std::string FST::Data_File_ARFF::getClassName ( int clsId)
inline

Name (nominal value text) of class clsId.

Returns
"-n/a-" if classSize is null or clsId is out of [0,classCount); the CLASS attribute's value name otherwise.
Exceptions
fst_errorif the file was not loaded.

◆ getClassSize()

unsigned int FST::Data_File_ARFF::getClassSize ( int clsId)
inline

Number of records belonging to class clsId.

Returns
the count from classSize.
Exceptions
fst_errorif the file was not loaded, if classSize is null (no class attribute), or if clsId is out of [0,classCount).

◆ getFeatureValue()

float FST::Data_File_ARFF::getFeatureValue ( long record,
int feature )
inline

The value of features[feature] in record record, read out of that record's raw data buffer.

Note
A nominal feature's value is returned as its (small, exactly float-representable) Data_File_ARFF_Feature::values index, cast from the short it is stored as.
Warning
Unchecked: record must be in [0,getTotalSize()) and feature in [0,getNoOfFeatures()), and the file must be loaded. Out-of-range arguments read out of bounds rather than failing.

◆ getNoOfAttributes()

unsigned int FST::Data_File_ARFF::getNoOfAttributes ( )
inline

number of loaded attribute columns (attributes), INCLUDING the CLASS attribute if present

◆ getNoOfClasses()

unsigned int FST::Data_File_ARFF::getNoOfClasses ( )
inline

Number of classes, i.e. the CLASS attribute's value count.

Returns
0 if no CLASS attribute was found (logged, not an error); the CLASS attribute's Data_File_ARFF_Feature::values size otherwise.
Exceptions
fst_errorif the file was not loaded, or (defensively – prepareAttributes() makes this unreachable) if the CLASS attribute is not nominal.

◆ getTotalSize()

unsigned int FST::Data_File_ARFF::getTotalSize ( )
inline

Number of loaded records (data's size).

Exceptions
fst_errorif the file was not loaded.

◆ prepareFile()

bool FST::Data_File_ARFF::prepareFile ( )
inline

Reads and parses the whole .arff file: the header (@RELATION/@ATTRIBUTE lines) then every @DATA row. Columns of an unsupported type (STRING, DATE, or any other non-numeric/non-nominal type) are skipped gracefully with a loud per-column message; a malformed @ATTRIBUTE definition is still a hard failure. Loading only hard-fails on unsupported columns if NONE of them turn out loadable (every column skipped).

Returns
whether loading succeeded (also stored in loaded); throws fst_error if the file cannot be opened.

◆ readAttrValue()

bool FST::Data_File_ARFF::readAttrValue ( Data_File_ARFF_Feature * feature,
Data_File_ARFF_Record * rec,
char * value )
inlineprivate

Parses value into feature's slot of rec's data buffer (float for TYPE_NUMERIC, a Data_File_ARFF_Feature::values index for TYPE_NOMINAL); also sets rec's class id if feature isClass.

Returns
false (logging a message) if value doesn't parse as the feature's type, or isn't among a nominal feature's Data_File_ARFF_Feature::values.

◆ readLine()

bool FST::Data_File_ARFF::readLine ( FILE * f,
std::vector< char > & buff )
inlineprivate

reads one full line of arbitrary length, growing the buffer as needed; returns false at EOF with nothing read. There is no line-length cap: a wide dense row (thousands of attributes easily exceed any fixed buffer) is read whole rather than being split into fragments that would parse as garbage rows.

◆ readNextRow()

bool FST::Data_File_ARFF::readNextRow ( char * row)
inlineprivate

Reads and stores one data row (dense or sparse, dispatched on a leading '{'); increments the row's class's classSize entry if the row has a class id.

Returns
false if the row is empty or fails to parse (no record is stored).

◆ readNormalRow()

Data_File_ARFF_Record * FST::Data_File_ARFF::readNormalRow ( char * row)
inlineprivate

Parses one dense (comma-separated) data row into a new record, walking file columns through columnMap (skipped columns are consumed and discarded).

Returns
the parsed record, or nullptr (freeing the partial record) if a token fails to parse.

◆ readSparseRow()

Data_File_ARFF_Record * FST::Data_File_ARFF::readSparseRow ( char * row)
inlineprivate

Parses one sparse ({idx value, idx value, ...}) data row into a new record (unset attributes default to 0; an omitted class attribute defaults to class 0), remapping each file column index through columnMap.

Returns
the parsed record, or nullptr (freeing the partial record) if a token fails to parse.

◆ sortRecordsByClass()

void FST::Data_File_ARFF::sortRecordsByClass ( )
inline

Reorders data in place, grouping records by class id (bucket sort over classCount buckets, then concatenated in class order).

Exceptions
fst_errorif the file was not loaded or classCount<1 (no class attribute was found).

The documentation for this class was generated from the following file:
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.