Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_accessor.hpp
Go to the documentation of this file.
1#ifndef FSTDATAACCESSOR_H
2#define FSTDATAACCESSOR_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <vector>
50#include "error.hpp"
51#include "global.hpp"
52#include "clonable.hpp"
53
54/*============== Template parameter type naming conventions ==============
55--------- Numeric types: -------------------------------------------------
56DATATYPE - data sample values - usually real numbers (but may be integers
57 in text processing etc.)
58REALTYPE - must be real numbers - for representing intermediate results of
59 calculations like mean, covariance etc.
60IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
61 extent depends on numbers of samples in data
62DIMTYPE - index values for enumeration of features (dimensions), or classes (not
63 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
64 but be aware of expressions like _classes*_features*_features !
65 in linearized representations of feature matrices for all classes
66BINTYPE - feature selection marker type - represents ca. <10 different feature
67 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
68RETURNTYPE - criterion value: real value, but may be extended in future to support
69 multiple values
70--------- Class types: ---------------------------------------------------
71SUBSET - class of class type Subset
72CLASSIFIER - class implementing interface defined in abstract class Classifier
73EVALUATOR - class implementing interface defined in abstract class Sequential_Step
74DISTANCE - class implementing interface defined in abstract class Distance
75DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
76INTERVALCONTAINER - class of class type Data_Intervaller
77CONTAINER - STL container of class type Data_Interval
78========================================================================== */
79
80namespace FST {
81
90
119template<typename DATATYPE, typename IDXTYPE>
120class Data_Accessor : public Clonable // abstract class
121{
122 public:
123 typedef const DATATYPE* PPattern;
124 Data_Accessor() {notify("Data_Accessor constructor");}
125 virtual ~Data_Accessor() {notify("Data_Accessor destructor");}
126
127 virtual unsigned int getNoOfClasses() const =0;
128 virtual unsigned int getNoOfFeatures() const =0;
129 virtual IDXTYPE getClassSize(const unsigned int c) const =0;
130 virtual IDXTYPE getClassSizeSum() const =0;
131
132 virtual void setClass(const int c) =0;
133 virtual int getClass() const =0;
134
135 virtual unsigned int getNoOfSplits() const =0;
136 virtual bool getFirstSplit()=0;
137 virtual bool getNextSplit()=0;
138 virtual unsigned int getSplitIndex() const =0;
139
142 virtual IDXTYPE getNoOfBlocks(const DataPart ofwhat) const =0;
153 virtual bool getFirstBlock(const DataPart ofwhat, PPattern &firstpattern, IDXTYPE &patterns, const unsigned int loopdepth=0)=0;
158 virtual bool getNextBlock(const DataPart ofwhat, PPattern &firstpattern, IDXTYPE &patterns, const unsigned int loopdepth=0)=0;
159
161 struct Block {
163 IDXTYPE size;
164 };
165
173 public:
175 class iterator {
176 public:
178 iterator(Block_Range *r, const bool done) : _r(r), _done(done) {_cur.data=nullptr; _cur.size=0; if(!_done) _done=!_r->_da->getFirstBlock(_r->_part,_cur.data,_cur.size,_r->_depth);}
179 const Block& operator*() const {return _cur;}
180 iterator& operator++() {_done=!_r->_da->getNextBlock(_r->_part,_cur.data,_cur.size,_r->_depth); return *this;}
181 bool operator!=(const iterator& o) const {return _done!=o._done;}
182 private:
184 bool _done;
186 };
188 Block_Range(Data_Accessor *da, const DataPart part) : _da(da), _part(part), _depth(da->acquire_block_loop_slot()) {}
190 iterator begin() {return iterator(this,false);}
191 iterator end() {return iterator(this,true);}
192 unsigned int loopdepth() const {return _depth;}
193 // neither copyable nor movable: exactly one owner of the loopdepth slot. blocks()
194 // can still return a range by value because C++17 guaranteed copy elision
195 // materializes the return value in place, with no copy or move involved.
196 Block_Range(const Block_Range&) = delete;
197 Block_Range& operator=(const Block_Range&) = delete;
198 private:
199 Data_Accessor *_da;
201 const unsigned int _depth;
202 };
204 Block_Range blocks(const DataPart ofwhat) {return Block_Range(this,ofwhat);}
206 virtual IDXTYPE getBlockIndex(const unsigned int loopdepth=0) const =0;
208 virtual IDXTYPE getNoOfPatterns(const DataPart ofwhat) const =0;
216 virtual void substitute(const DataPart source, const DataPart target)=0;
218 virtual void resubstitute()=0;
219
220 Data_Accessor* clone() const override =0;
221 Data_Accessor* sharing_clone() const override =0;
222 Data_Accessor* stateless_clone() const override =0;
223 private:
228 static const unsigned int _first_acquired_loopdepth = 4;
229 std::vector<bool> _block_slot_used;
231 unsigned int acquire_block_loop_slot() {
232 for(unsigned int i=0;i<(unsigned int)_block_slot_used.size();++i) if(!_block_slot_used[i]) {_block_slot_used[i]=true; return _first_acquired_loopdepth+i;}
233 _block_slot_used.push_back(true);
234 return _first_acquired_loopdepth+(unsigned int)(_block_slot_used.size()-1);
235 }
236
237 void release_block_loop_slot(const unsigned int depth) {
238 assert(depth>=_first_acquired_loopdepth);
239 const unsigned int i=depth-_first_acquired_loopdepth;
240 if(i<(unsigned int)_block_slot_used.size()) _block_slot_used[i]=false;
241 }
242};
243
244
245} // namespace
246#endif // FSTDATAACCESSOR_H ///:~
Forward iterator over Block_Range; drives the wrapped getFirstBlock()/getNextBlock() cursor at the ra...
Definition data_accessor.hpp:175
iterator & operator++()
advances to the next block; becomes an end iterator once the loop is exhausted
Definition data_accessor.hpp:180
bool _done
whether iteration is exhausted
Definition data_accessor.hpp:184
Block_Range * _r
the owning range
Definition data_accessor.hpp:183
bool operator!=(const iterator &o) const
range-for only ever compares against end(); true until exhaustion
Definition data_accessor.hpp:181
iterator(Block_Range *r, const bool done)
Constructs an end iterator when done is true; otherwise fetches the first block via r,...
Definition data_accessor.hpp:178
Block _cur
the current block
Definition data_accessor.hpp:185
const Block & operator*() const
the current block (data pointer + size)
Definition data_accessor.hpp:179
RAII range over consecutive data blocks of one DataPart in the current split.
Definition data_accessor.hpp:172
const DataPart _part
the DataPart selected
Definition data_accessor.hpp:200
const unsigned int _depth
this range's owned loopdepth slot
Definition data_accessor.hpp:201
iterator begin()
an iterator positioned at the first block, or already at end() if there are none
Definition data_accessor.hpp:190
iterator end()
the sentinel end iterator
Definition data_accessor.hpp:191
unsigned int loopdepth() const
the owned slot (e.g. for getBlockIndex())
Definition data_accessor.hpp:192
Data_Accessor * _da
the accessor iterated
Definition data_accessor.hpp:199
Block_Range(Data_Accessor *da, const DataPart part)
Acquires a private loopdepth slot from da for the range's lifetime; part selects which DataPart the r...
Definition data_accessor.hpp:188
abstract class, defines interface for data access implementations
Definition data_accessor.hpp:121
void release_block_loop_slot(const unsigned int depth)
Returns the loopdepth slot acquired by acquire_block_loop_slot() so a future Block_Range can reuse it...
Definition data_accessor.hpp:237
virtual bool getFirstSplit()=0
data access iteration (to support, e.g., loops in cross-validation)
Data_Accessor * sharing_clone() const override=0
virtual bool getFirstBlock(const DataPart ofwhat, PPattern &firstpattern, IDXTYPE &patterns, const unsigned int loopdepth=0)=0
returns pointer to first consecutive block of data of requested DataPart type in the current split (a...
virtual void substitute(const DataPart source, const DataPart target)=0
enables change of meaning of DataPart types, for use in specialized data access scenarios like in bia...
virtual bool getNextBlock(const DataPart ofwhat, PPattern &firstpattern, IDXTYPE &patterns, const unsigned int loopdepth=0)=0
returns pointer to next consecutive block of data of requested DataPart type in the current split (ac...
virtual bool getNextSplit()=0
data access iteration (to support, e.g., loops in cross-validation)
virtual IDXTYPE getClassSize(const unsigned int c) const =0
returns size (number of samples in) of class c
virtual void resubstitute()=0
resets standard DataPart types' meaning, i.e. undoes every substitute() call
virtual unsigned int getNoOfFeatures() const =0
returns data dimensionality
virtual unsigned int getNoOfClasses() const =0
returns number of classes
virtual unsigned int getNoOfSplits() const =0
data access iteration (to support, e.g., loops in cross-validation)
unsigned int acquire_block_loop_slot()
Reserves and returns a free loopdepth (reusing a released one if available, else growing the pool by ...
Definition data_accessor.hpp:231
virtual IDXTYPE getNoOfPatterns(const DataPart ofwhat) const =0
returns number of patterns in all consecutive blocks of data of requested DataPart type in the curren...
virtual int getClass() const =0
returns active class
virtual IDXTYPE getBlockIndex(const unsigned int loopdepth=0) const =0
returns index of the current consecutive block of data of requested DataPart type in the current spli...
Data_Accessor * clone() const override=0
const DATATYPE * PPattern
one data sample: a raw pointer to its (read-only) feature vector, getNoOfFeatures() entries long
Definition data_accessor.hpp:123
Block_Range blocks(const DataPart ofwhat)
RAII block range over the current split - prefer over the deprecated getFirstBlock()/getNextBlock() c...
Definition data_accessor.hpp:204
virtual IDXTYPE getClassSizeSum() const =0
returns summed size (number of samples in) of all classes, i.e., no. of all patterns in data
virtual unsigned int getSplitIndex() const =0
data access iteration (to support, e.g., loops in cross-validation)
Data_Accessor * stateless_clone() const override=0
virtual IDXTYPE getNoOfBlocks(const DataPart ofwhat) const =0
returns the number of consecutive blocks the requested DataPart is made of in the current split
static const unsigned int _first_acquired_loopdepth
Definition data_accessor.hpp:228
virtual void setClass(const int c)=0
sets active class from which get*Block() will fetch data
std::vector< bool > _block_slot_used
Definition data_accessor.hpp:229
Defines interface for classes that may need to get cloned.
FST4 specific error passing class.
Global definitions.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
DataPart
Selects which part of a split's data a block-access call reads.
Definition data_accessor.hpp:83
@ TRAINTEST
both parts concatenated (train followed by test)
Definition data_accessor.hpp:87
@ ALL
every pattern regardless of split (the whole class)
Definition data_accessor.hpp:88
@ NONE
no part (used internally, e.g. for traintestphase state before a phase is chosen)
Definition data_accessor.hpp:84
@ TEST
the split's test part
Definition data_accessor.hpp:86
@ TRAIN
the split's training part
Definition data_accessor.hpp:85
one consecutive block of patterns as yielded by Block_Range/blocks()
Definition data_accessor.hpp:161
PPattern data
pointer to the first pattern of the block
Definition data_accessor.hpp:162
IDXTYPE size
number of patterns in the block
Definition data_accessor.hpp:163
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.