Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_intervaller.hpp
Go to the documentation of this file.
1#ifndef FSTDATAINTERVALLER_H
2#define FSTDATAINTERVALLER_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 <algorithm>
52#include <iostream>
53#include <sstream>
54#include <vector> // for blockiter - array of iterators indexed by loopdepth
55#include "error.hpp"
56#include "global.hpp"
57
58/*============== Template parameter type naming conventions ==============
59--------- Numeric types: -------------------------------------------------
60DATATYPE - data sample values - usually real numbers (but may be integers
61 in text processing etc.)
62REALTYPE - must be real numbers - for representing intermediate results of
63 calculations like mean, covariance etc.
64IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
65 extent depends on numbers of samples in data
66DIMTYPE - index values for enumeration of features (dimensions), or classes (not
67 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
68 but be aware of expressions like _classes*_features*_features !
69 in linearized representations of feature matrices for all classes
70BINTYPE - feature selection marker type - represents ca. <10 different feature
71 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
72RETURNTYPE - criterion value: real value, but may be extended in future to support
73 multiple values
74--------- Class types: ---------------------------------------------------
75SUBSET - class of class type Subset
76CLASSIFIER - class implementing interface defined in abstract class Classifier
77EVALUATOR - class implementing interface defined in abstract class Sequential_Step
78DISTANCE - class implementing interface defined in abstract class Distance
79DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
80INTERVALCONTAINER - class of class type Data_Intervaller
81CONTAINER - STL container of class type Data_Interval
82========================================================================== */
83
84namespace FST {
85
87template<typename IDXTYPE>
89 IDXTYPE startidx;
90 IDXTYPE count;
91};
92
93template <class CONTAINER, typename IDXTYPE>
95
96template <class CONTAINER, typename IDXTYPE>
97std::ostream& operator<<(std::ostream& os, const Data_Intervaller<CONTAINER,IDXTYPE>& ia);
98
100template<class CONTAINER, typename IDXTYPE>
102class Data_Intervaller : public CONTAINER {
103public:
104 Data_Intervaller() {notify("Data_Intervaller constructor.");};
108 Data_Intervaller(const Data_Intervaller& tin);
109 virtual ~Data_Intervaller() {notify("Data_Intervaller destructor.");};
110 Data_Interval<IDXTYPE>* getFirstBlock(const unsigned int loopdepth=0);
111 Data_Interval<IDXTYPE>* getNextBlock(const unsigned int loopdepth=0);
114 bool check_validity(const IDXTYPE maxidx);
115 IDXTYPE sum();
116 IDXTYPE max_idx();
131 void reduce(const std::shared_ptr<Data_Intervaller<CONTAINER,IDXTYPE> > reducer, std::shared_ptr<Data_Intervaller<CONTAINER,IDXTYPE> > target);
133 friend std::ostream& operator<< <>(std::ostream& os, const Data_Intervaller<CONTAINER,IDXTYPE>& ia);
134private:
135 typedef typename Data_Intervaller<CONTAINER,IDXTYPE>::iterator TCI;
136 std::vector<TCI> blockiter;
137};
138
139template<class CONTAINER, typename IDXTYPE>
140Data_Intervaller<CONTAINER,IDXTYPE>::Data_Intervaller(const Data_Intervaller& tin)
141{
142 notify("Data_Intervaller copy constructor.");
143 this->clear();
144 for(typename Data_Intervaller<CONTAINER,IDXTYPE>::const_iterator iter=tin.begin();iter!=tin.end();iter++) this->push_back(*iter);
145 // blockiter deliberately NOT copied: it holds iterators into the SOURCE container, which
146 // would be invalid here — the copy starts with no active block loops; getFirstBlock()
147 // (re)initializes the cursor of whichever loopdepth is used next
148}
149
151template<class CONTAINER, typename IDXTYPE>
152std::ostream& operator<<(std::ostream& os, const Data_Intervaller<CONTAINER,IDXTYPE>& ia) {
153 for(typename Data_Intervaller<CONTAINER,IDXTYPE>::const_iterator iter=ia.begin();iter!=ia.end();iter++) os << "si:" << iter->startidx << ", c:" << iter->count << std::endl;
154 return os;
155}
156
157template<class CONTAINER, typename IDXTYPE>
159{
160 if(blockiter.size()<=loopdepth) blockiter.resize(loopdepth+1);
161 blockiter[loopdepth]=this->begin();
162 if(blockiter[loopdepth]!=this->end()) return &(*blockiter[loopdepth]);
163 else return nullptr;
164}
165
166template<class CONTAINER, typename IDXTYPE>
168{
169 // NOTE: getNextBlock() must be preceded by getFirstBlock() with the same loopdepth value, otherwise the behaviour is undefined
170 assert(loopdepth<blockiter.size());
171 ++blockiter[loopdepth];
172 if(blockiter[loopdepth]!=this->end()) return &(*blockiter[loopdepth]);
173 else return nullptr;
174}
175
176template<class CONTAINER, typename IDXTYPE>
178{
179 if(this->empty()) {{std::ostringstream sos; sos << "Data_Intervaller is empty" << std::endl; syncout::print(std::cerr,sos);} return false;}
180 TCI iter=this->begin();
181 IDXTYPE pivot=iter->startidx+iter->count;
182 if(iter->startidx<0 || (maxidx>0 && (pivot>maxidx)) ) {{std::ostringstream sos; sos <<"first is wrong"<<std::endl; syncout::print(std::cerr,sos);} return false;} // the first interval reaches outside <0,maxidx>
183 iter++;
184 while(iter!=this->end())
185 {
186 if(iter->startidx<pivot) {{std::ostringstream sos; sos <<"iter->startidx="<<iter->startidx<<", pivot="<<pivot<<std::endl; syncout::print(std::cerr,sos);} return false;}
187 pivot=iter->startidx+iter->count;
188 if(maxidx>0 && (pivot>maxidx)) {{std::ostringstream sos; sos <<"startidx+count>maxidx"<<std::endl; syncout::print(std::cerr,sos);} return false;}
189 ++iter;
190 }
191 return true;
192}
193
194template<class CONTAINER, typename IDXTYPE>
196{
197 IDXTYPE cnt=0;
198 for(TCI iter=this->begin();iter!=this->end();iter++) cnt+=iter->count;
199 return cnt;
200}
201
202template<class CONTAINER, typename IDXTYPE>
204{
205 IDXTYPE mx=0;
206 for(TCI iter=this->begin();iter!=this->end();iter++) if(iter->count>0 && iter->startidx+iter->count-1>mx) mx=iter->startidx+iter->count-1; // count==0: startidx+count-1 underflows unsigned IDXTYPE
207 return mx;
208}
209
210template<class CONTAINER, typename IDXTYPE>
211void Data_Intervaller<CONTAINER,IDXTYPE>::reduce(const std::shared_ptr<Data_Intervaller<CONTAINER,IDXTYPE> > reducer, std::shared_ptr<Data_Intervaller<CONTAINER,IDXTYPE> > target)
212{
213 try{
214 assert(sum()>reducer->max_idx()); // the reducer indexes INTO this list: its largest index must be below this list's sample count
215 target->clear();
216 typename Data_Intervaller<CONTAINER,IDXTYPE>::const_iterator rediter=reducer->begin();
217 IDXTYPE Mbottom=0;
218 for(TCI iter=this->begin();iter!=this->end();iter++) {
219 if((*iter).count==0) continue; // empty interval: Mtop below would underflow unsigned IDXTYPE
220 bool stop=false;
221 while((!stop) && (rediter!=reducer->end())) {
222 if((*rediter).count==0) {rediter++; continue;} // empty interval: Rtop below would underflow unsigned IDXTYPE
223 IDXTYPE Rbottom=(*rediter).startidx;
224 IDXTYPE Rtop=Rbottom+(*rediter).count-1;
225 IDXTYPE MTRUEbottom=(*iter).startidx;
226 IDXTYPE Mtop=Mbottom+(*iter).count-1;
227 if(!(Rtop<Mbottom || Rbottom>Mtop)) { //write intersection to target
228 IDXTYPE ISCbottom=Rbottom; if(Rbottom<Mbottom) ISCbottom=Mbottom;
229 IDXTYPE ISCtop=Rtop; if(Rtop>Mtop) ISCtop=Mtop;
230 Data_Interval<IDXTYPE> I={MTRUEbottom+(ISCbottom-Mbottom), ISCtop-ISCbottom+1};
231 target->push_back(I);
232 }
233 if(Rtop>=Mtop) stop=true;
234 if(Rtop<=Mtop) rediter++;
235 }
236 Mbottom+=(*iter).count;
237 }
238
239 } catch(...) {
240 throw fst_error("Data_Intervaller::reduce() error.");
241 }
242}
243
244
245} // namespace
246#endif // FSTDATAINTERVALLER_H ///:~
Container to hold list of Data_Interval; implements nested interval reduction.
Definition data_intervaller.hpp:102
Data_Interval< IDXTYPE > * getNextBlock(const unsigned int loopdepth=0)
Definition data_intervaller.hpp:167
IDXTYPE max_idx()
returns the largest sample index any interval covers, i.e. the maximum of startidx+count-1 over non-e...
Definition data_intervaller.hpp:203
Data_Interval< IDXTYPE > * getFirstBlock(const unsigned int loopdepth=0)
positions loop loopdepth at the first interval and returns it, or nullptr if there are none
Definition data_intervaller.hpp:158
Data_Intervaller(const Data_Intervaller &tin)
Definition data_intervaller.hpp:140
std::vector< TCI > blockiter
Definition data_intervaller.hpp:136
bool check_validity(const IDXTYPE maxidx)
checks whether all intervals are non-overlapping, ordered ascending, and all fit to <0,...
Definition data_intervaller.hpp:177
void reduce(const std::shared_ptr< Data_Intervaller< CONTAINER, IDXTYPE > > reducer, std::shared_ptr< Data_Intervaller< CONTAINER, IDXTYPE > > target)
Translates reducer's RELATIVE index intervals into absolute ones and stores them in target.
Definition data_intervaller.hpp:211
Data_Intervaller< std::vector< FST::Data_Interval< IDXTYPE > >, IDXTYPE >::iterator TCI
Definition data_intervaller.hpp:135
IDXTYPE sum()
sums the sizes of all contained intervals, i.e. the total number of samples they cover
Definition data_intervaller.hpp:195
FST4 specific error passing class.
Definition error.hpp:61
static void print(std::ostream &os, std::ostringstream &sos)
Writes sos's buffered content to os, serializing concurrent writers under FST_THREADED via a shared m...
Definition global.hpp:86
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
Support structure to hold data interval (referring into data held by data accessor).
Definition data_intervaller.hpp:88
IDXTYPE startidx
index of the interval's first sample
Definition data_intervaller.hpp:89
IDXTYPE count
number of samples in the interval
Definition data_intervaller.hpp:90
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.