Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_splitter.hpp
Go to the documentation of this file.
1#ifndef FSTDATASPLITTER_H
2#define FSTDATASPLITTER_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 <memory>
50#include <cassert>
51#include <iostream>
52#include <cstdlib>
53#include <ctime>
54#include <vector>
55#include <string>
56#include "error.hpp"
57#include "global.hpp"
58#include "clonable.hpp"
59
60/*============== Template parameter type naming conventions ==============
61--------- Numeric types: -------------------------------------------------
62DATATYPE - data sample values - usually real numbers (but may be integers
63 in text processing etc.)
64REALTYPE - must be real numbers - for representing intermediate results of
65 calculations like mean, covariance etc.
66IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
67 extent depends on numbers of samples in data
68DIMTYPE - index values for enumeration of features (dimensions), or classes (not
69 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
70 but be aware of expressions like _classes*_features*_features !
71 in linearized representations of feature matrices for all classes
72BINTYPE - feature selection marker type - represents ca. <10 different feature
73 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
74RETURNTYPE - criterion value: real value, but may be extended in future to support
75 multiple values
76--------- Class types: ---------------------------------------------------
77SUBSET - class of class type Subset
78CLASSIFIER - class implementing interface defined in abstract class Classifier
79EVALUATOR - class implementing interface defined in abstract class Sequential_Step
80DISTANCE - class implementing interface defined in abstract class Distance
81DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
82INTERVALCONTAINER - class of class type Data_Intervaller
83CONTAINER - STL container of class type Data_Interval
84========================================================================== */
85
86namespace FST {
87
88template<class INTERVALCONTAINER, typename IDXTYPE>
89class Data_Splitter;
90
91template<class INTERVALCONTAINER, typename IDXTYPE>
92std::ostream& operator<<(std::ostream& os, const Data_Splitter<INTERVALCONTAINER,IDXTYPE>& ds);
93
123template<class INTERVALCONTAINER, typename IDXTYPE>
124class Data_Splitter : public Clonable {
126 friend std::ostream& operator<< <>(std::ostream& os, const Data_Splitter<INTERVALCONTAINER,IDXTYPE>& ds);
127public:
128 typedef std::shared_ptr<INTERVALCONTAINER> PIntervaller;
129 Data_Splitter() {csplit.clear(); notify("Data_Splitter constructor.");}
131 Data_Splitter(const Data_Splitter& dsp);
132 virtual ~Data_Splitter() {notify("Data_Splitter destructor.");}
133
135 void assign(const unsigned int _clsidx, const PIntervaller _train, const PIntervaller _test);
137 void set_n(const unsigned int _clsidx, const IDXTYPE _n);
138 IDXTYPE get_n(const unsigned int _clsidx) const;
139
140 virtual IDXTYPE getNoOfSplits() const =0;
144 bool makeFirstSplit();
147 bool makeNextSplit();
148
149 Data_Splitter* clone() const override {throw fst_error("Data_Splitter::clone() not supported, use Data_Splitter::stateless_clone() instead.");}
150 Data_Splitter* sharing_clone() const override {throw fst_error("Data_Splitter::sharing_clone() not supported, use Data_Splitter::stateless_clone() instead.");}
151 Data_Splitter* stateless_clone() const override =0;
152
153 virtual std::ostream& print(std::ostream& os) const override {return os;}
154
155protected:
157 class TClassSplitter {
158 public:
159 TClassSplitter() {n=0; train.reset(); test.reset(); notify("TClassSplitter empty constructor.");}
161 TClassSplitter(const TClassSplitter& cs) : n(cs.n) {
162 notify("TClassSplitter strong copy constructor begin.");
163 if(cs.train) train.reset(new INTERVALCONTAINER(*cs.train));
164 if(cs.test) test.reset(new INTERVALCONTAINER(*cs.test));
165 notify("TClassSplitter strong copy constructor end.");
166 }
167 ~TClassSplitter() {notify("TClassSplitter destructor.");}
168 IDXTYPE n;
171 };
172 typedef std::shared_ptr<TClassSplitter> PClassSplitter;
173 std::vector<PClassSplitter> csplit;
174 typedef typename std::vector<PClassSplitter>::const_iterator ClassIterator;
175
176 void assert_csplit() const;
177
178 // hooks a concrete splitter implements; see the class description for the call order and
179 // for the contract the written intervals must satisfy
180 virtual const char* splitter_name() const =0;
181 virtual bool prepareFirstSplit() {return true;}
182 virtual bool prepareNextSplit() =0;
183 virtual void splitClassFirst(ClassIterator c, const unsigned int cidx) =0;
184 virtual void splitClassNext(ClassIterator c, const unsigned int cidx) {}
185 virtual void finishSplit() {}
186};
187
188template<class INTERVALCONTAINER, typename IDXTYPE>
190{
192 try {
193 if(!prepareFirstSplit()) return false;
194 unsigned int cidx=0;
195 for(ClassIterator c=csplit.begin();c!=csplit.end();c++,cidx++) splitClassFirst(c,cidx);
196 finishSplit();
197 } catch(...) {
198 throw fst_error(std::string(splitter_name())+"::makeFirstSplit() error.");
199 }
200 return true;
201}
202
203template<class INTERVALCONTAINER, typename IDXTYPE>
205{
206 try {
207 if(!prepareNextSplit()) return false;
208 unsigned int cidx=0;
209 for(ClassIterator c=csplit.begin();c!=csplit.end();c++,cidx++) splitClassNext(c,cidx);
210 finishSplit();
211 } catch(...) {
212 throw fst_error(std::string(splitter_name())+"::makeNextSplit() error.");
213 }
214 return true;
215}
216
217
218template<class INTERVALCONTAINER, typename IDXTYPE>
219Data_Splitter<INTERVALCONTAINER,IDXTYPE>::Data_Splitter(const Data_Splitter& dsp)
220{
221 notify("Data_Splitter strong copy constructor begin.");
222 csplit.clear();
223 for(typename std::vector<PClassSplitter>::const_iterator c=dsp.csplit.begin();c!=dsp.csplit.end();c++)
224 {PClassSplitter pcs(new TClassSplitter(*(*c))); this->csplit.push_back(pcs);}
225 notify("Data_Splitter strong copy constructor end.");
226}
227
228template<class INTERVALCONTAINER, typename IDXTYPE>
229void Data_Splitter<INTERVALCONTAINER,IDXTYPE>::assign(const unsigned int _clsidx, const PIntervaller _train, const PIntervaller _test)
230{
231 assert(_train);
232 assert(_test);
233 try {
234 while(csplit.size()<=_clsidx) {PClassSplitter pcs(new TClassSplitter); csplit.push_back(pcs);}
235 csplit[_clsidx]->train=_train;
236 csplit[_clsidx]->test=_test;
237 } catch(...) {
238 throw fst_error("Data_Splitter::set_n() out-of-index? error.");
239 }
240}
241
242template<class INTERVALCONTAINER, typename IDXTYPE>
243void Data_Splitter<INTERVALCONTAINER,IDXTYPE>::set_n(const unsigned int _clsidx, const IDXTYPE _n)
244{
245 assert(_n>0);
246 try {
247 while(csplit.size()<=_clsidx) {PClassSplitter pcs(new TClassSplitter); csplit.push_back(pcs);}
248 csplit[_clsidx]->n=_n;
249 } catch(...) {
250 throw fst_error("Data_Splitter::set_n() out-of-index? error.");
251 }
252}
253
254template<class INTERVALCONTAINER, typename IDXTYPE>
255IDXTYPE Data_Splitter<INTERVALCONTAINER,IDXTYPE>::get_n(const unsigned int _clsidx) const
256{
257 assert(_clsidx<csplit.size());
258 try {
259 return csplit[_clsidx]->n;
260 } catch(...) {
261 throw fst_error("Data_Splitter::get_n() out-of-index? error.");
262 }
263}
264
266template<class INTERVALCONTAINER, typename IDXTYPE>
267std::ostream& operator<<(std::ostream& os, const Data_Splitter<INTERVALCONTAINER,IDXTYPE>& ds) {
268 return ds.print(os);
269}
270
271template<class INTERVALCONTAINER, typename IDXTYPE>
273{
274#ifdef DEBUG
275 assert(csplit.size()>0);
276 for(ClassIterator c=csplit.begin();c!=csplit.end();c++) {
277 assert((*c)->n>0);
278 assert((*c)->train);
279 assert((*c)->test);
280 }
281#endif
282}
283
284
285} // namespace
286#endif // FSTDATASPLITTER_H ///:~
Data splitting support structure; holds one set of intervals (train, test) per each (data)class.
Definition data_splitter.hpp:157
PIntervaller test
pointer to external list of intervals that will be overwritten from here
Definition data_splitter.hpp:170
TClassSplitter(const TClassSplitter &cs)
Deep-copies cs's train/test interval lists (own INTERVALCONTAINER instances, not shared).
Definition data_splitter.hpp:161
PIntervaller train
pointer to external list of intervals that will be overwritten from here
Definition data_splitter.hpp:169
IDXTYPE n
n=total no. of patterns
Definition data_splitter.hpp:168
Abstract class, defines interface for data splitting implementations (for use in data accessors).
Definition data_splitter.hpp:124
virtual bool prepareNextSplit()=0
advance split-sequence state; false = no more splits
virtual bool prepareFirstSplit()
set up split-sequence state; false = no split can be made
Definition data_splitter.hpp:181
std::vector< PClassSplitter > csplit
one TClassSplitter per data class, indexed by class index; grown on demand by assign()/set_n()
Definition data_splitter.hpp:173
Data_Splitter * sharing_clone() const override
Definition data_splitter.hpp:150
bool makeNextSplit()
Produces the next split: prepareNextSplit(), then splitClassNext() per class, then finishSplit().
Definition data_splitter.hpp:204
std::shared_ptr< INTERVALCONTAINER > PIntervaller
shared pointer to one class's list of train/test data intervals
Definition data_splitter.hpp:128
void set_n(const unsigned int _clsidx, const IDXTYPE _n)
Sets class _clsidx's pattern count, which getNoOfSplits() and the concrete prepare*‍/splitClass* hook...
Definition data_splitter.hpp:243
virtual void splitClassFirst(ClassIterator c, const unsigned int cidx)=0
write first-split train/test intervals of class cidx (c is that class's entry in csplit)
IDXTYPE get_n(const unsigned int _clsidx) const
the pattern count previously set for class _clsidx via set_n()
Definition data_splitter.hpp:255
std::shared_ptr< TClassSplitter > PClassSplitter
shared pointer to one class's TClassSplitter
Definition data_splitter.hpp:172
virtual const char * splitter_name() const =0
class name, used in error messages
void assign(const unsigned int _clsidx, const PIntervaller _train, const PIntervaller _test)
Registers class _clsidx's train/test interval lists (growing csplit as needed); the caller retains ow...
Definition data_splitter.hpp:229
void assert_csplit() const
DEBUG-only: asserts csplit is non-empty and every class has n>0 and non-null train/test lists.
Definition data_splitter.hpp:272
Data_Splitter * stateless_clone() const override=0
virtual void finishSplit()
post-loop state update shared by first/next split
Definition data_splitter.hpp:185
virtual void splitClassNext(ClassIterator c, const unsigned int cidx)
write next-split train/test intervals of class cidx (a single-split splitter never gets here,...
Definition data_splitter.hpp:184
virtual IDXTYPE getNoOfSplits() const =0
Data_Splitter * clone() const override
Definition data_splitter.hpp:149
std::vector< PClassSplitter >::const_iterator ClassIterator
iterator over csplit, as passed to the per-class splitClassFirst()/splitClassNext() hooks
Definition data_splitter.hpp:174
bool makeFirstSplit()
Produces the first split: prepareFirstSplit(), then splitClassFirst() per class, then finishSplit().
Definition data_splitter.hpp:189
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the splitter and its configuration; base default writes not...
Definition data_splitter.hpp:153
FST4 specific error passing class.
Definition error.hpp:61
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
std::ostream & operator<<(std::ostream &os, const Data_Intervaller< CONTAINER, IDXTYPE > &ia)
Streams every contained interval as "si:<startidx>, c:<count>" lines to os.
Definition data_intervaller.hpp:152
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.