Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_accessor_splitting.hpp
Go to the documentation of this file.
1#ifndef FSTDATAACCESSORSPLITTING_H
2#define FSTDATAACCESSORSPLITTING_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 <vector>
53#include "error.hpp"
54#include "global.hpp"
55#include "data_accessor.hpp"
56#include "data_splitter.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
143template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
144class Data_Accessor_Splitting : public Data_Accessor<DATATYPE,IDXTYPE>
145{
146 // NOTE: the calling convention is the same for every nested data access loop (as in cross-validation):
147 // all loops call the same methods (just get*Split, get*Block, get*Pattern), and the loop level
148 // they refer to is selected beforehand with setSplittingDepth()
149 // Access to data is defined using indexes, assuming data is accessible by random access
150 // Support for arbitrary data splitting (train/test, cross-validation, hold-out, etc., by means
151 // of arbitrary Data_Splitter objects) is implemented here
152 public:
153 typedef std::shared_ptr<Data_Splitter<INTERVALCONTAINER,IDXTYPE> > PSplitter;
154 typedef std::shared_ptr<std::vector<PSplitter> > PSplitters;
155 typedef const DATATYPE* PPattern;
158 virtual ~Data_Accessor_Splitting(){notify("Data_Accessor_Splitting destructor.");}
159
160 virtual unsigned int getNoOfClasses() const override;
161 virtual unsigned int getNoOfFeatures() const override;
162
163 virtual IDXTYPE getClassSize(const unsigned int c) const override;
164 virtual IDXTYPE getClassSizeSum() const override;
165
166 virtual void setClass(const int c) override {assert(c>=0); active_class = c;}
167 virtual int getClass() const override {return active_class;}
168
171 void setSplittingDepth(const unsigned int depth);
172 unsigned int getSplittingDepth() const {return splitting_depth;}
173
174 virtual unsigned int getNoOfSplits() const override;
175 virtual bool getFirstSplit() override;
176 virtual bool getNextSplit() override;
177 virtual unsigned int getSplitIndex() const override;
178
179 // NOTE: in the block accessing methods below, use a different loopdepth whenever two or more loops
180 // (of any DataPart type) overlap in time, otherwise their cursor state collides
181
183 virtual IDXTYPE getNoOfBlocks(const DataPart ofwhat) const override;
185 virtual bool getFirstBlock(const DataPart ofwhat, PPattern &firstpattern, IDXTYPE &patterns, const unsigned int loopdepth=0) override=0;
187 virtual bool getNextBlock(const DataPart ofwhat, PPattern &firstpattern, IDXTYPE &patterns, const unsigned int loopdepth=0) override=0;
189 virtual IDXTYPE getBlockIndex(const unsigned int loopdepth=0) const override;
191 virtual IDXTYPE getNoOfPatterns(const DataPart ofwhat) const override;
193 virtual void substitute(const DataPart source, const DataPart target) override;
195 virtual void resubstitute() override;
196
197 virtual std::ostream& print(std::ostream& os) const override;
198 protected:
201 Data_Accessor_Splitting(const Data_Accessor_Splitting &da); // to be used in clone() in derived classes
202 protected:
203 typedef std::vector<unsigned int> CLASSSIZES;
207 void initialize(const unsigned int _features, const CLASSSIZES &_classes);
209 unsigned int features;
210
211 // DataPart mapping
216 DataPart mappedDataPart(const DataPart ofwhat) const;
217
218 // splitting support
222 virtual bool getFirstBlock(const DataPart ofwhat, DATAINTERVAL &tmp, const unsigned int loopdepth=0);
224 virtual bool getNextBlock(const DataPart ofwhat, DATAINTERVAL &tmp, const unsigned int loopdepth=0);
225
226 typedef std::shared_ptr<INTERVALCONTAINER> PIntervaller;
228 class DataSplit {
229 public:
230 DataSplit() {_train.reset(); _test.reset(); _reduced_train.reset(); _reduced_test.reset(); notify("DataSplit empty constructor.");}
231 DataSplit(const DataSplit& cs);
232 DataSplit(const DataSplit& cs, int);
233 ~DataSplit() {notify("DataSplit destructor.");}
239 };
240 std::vector<std::vector<DataSplit> > splits;
241
242 std::vector<IDXTYPE> enum_split;
243 std::vector<std::vector<IDXTYPE> > enum_block;
244 std::vector<std::vector<DataPart> > tt_phase;
245
246 unsigned int splitting_depth;
247
249
250 // for debugging
252 bool is_initialized() const {return _initialize_called;}
255 void assert_splits(const int splitting_check=-1) const;
256};
257
258
259// ============================================ PUBLIC ===============================================
260
261template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
264{
265 notify("Data_Accessor_Splitting constructor.");
266 _initialize_called=false;
267 features=0;
268 assert(_dsp->size()>0);
269 dsp=_dsp;
270 active_class=-1;
272 splits.assign(_dsp->size(),std::vector<DataSplit>());
273 enum_split.assign(_dsp->size(),0);
274 enum_block.assign(_dsp->size(),std::vector<IDXTYPE>());
275 tt_phase.assign(_dsp->size(),std::vector<DataPart>());
276}
277
278template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
283
284template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
289
290template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
292{
293 assert(c<classes.size());
294 return classes[c];
295}
296
297template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
299{
300 IDXTYPE patterns=0;
301 for(unsigned int c=0;c<classes.size();c++) patterns+=classes[c];
302 return patterns;
303}
304
305template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
307{
308#ifdef DEBUG
309 assert(depth<dsp->size());
310 assert(enum_split.size()==dsp->size());
311 for(unsigned int n=0;n<depth;n++) assert(enum_split[n]>0);
312#endif
313 splitting_depth=depth;
314}
315
316// NOTE: precondition: initialize() must have completed before this is meaningful — it assigns
317// the per-class interval containers and calls set_n() on the depth-0 splitters (deeper
318// splitters get set_n() in the course of split enumeration). The asserts below make a
319// violation fail fast in debug builds.
320template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
322{
323 assert(dsp); // consider also checking the vector contents
324 assert(splitting_depth<dsp->size());
325 assert(splitting_depth<splits.size() && !splits[splitting_depth].empty()); // initialize() has run
326 return (*dsp)[splitting_depth]->getNoOfSplits();
327}
328
329template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
331{
332#ifdef DEBUG
333 assert(dsp); // consider also checking the vector contents
334 assert(is_initialized());
335 assert(splitting_depth<(unsigned int)dsp->size());
336 for(unsigned int n=0;n<splitting_depth;n++) assert(enum_split[n]>0);
337 assert_splits(/*n not set in splitters yet*/);
338#endif
339 bool result=true;
340 if(splitting_depth>0)
341 for(unsigned int c=0; c<classes.size(); c++)
342 {splits[splitting_depth][c]._all.startidx=0; (*dsp)[splitting_depth]->set_n(c, splits[splitting_depth][c]._all.count=splits[splitting_depth-1][c]._train->sum() );}
343 else
344 for(unsigned int c=0; c<classes.size(); c++)
345 {splits[splitting_depth][c]._all.startidx=0; (*dsp)[splitting_depth]->set_n(c, splits[splitting_depth][c]._all.count=classes[c] );}
346
347 if(!(*dsp)[splitting_depth]->makeFirstSplit()) result=false;
348 else
349 if(splitting_depth>0) for(unsigned int c=0; c<classes.size(); c++) {
350 // translate the new level's intervals (relative to the enclosing level's TRAIN part) into
351 // absolute indexes through the enclosing level's ABSOLUTE train list (_reduced_train): at
352 // depth 1 that list is the depth-0 _train itself, at depth 2 and deeper _train would be
353 // relative to depth-2's train part and the translation would land on the wrong samples
354 splits[splitting_depth-1][c]._reduced_train->reduce(splits[splitting_depth][c]._train,splits[splitting_depth][c]._reduced_train);
355 splits[splitting_depth-1][c]._reduced_train->reduce(splits[splitting_depth][c]._test,splits[splitting_depth][c]._reduced_test);
356 }
358 for(unsigned int n=splitting_depth+1;n<dsp->size();n++) enum_split[n]=0; // reset/cancel all deeper splits
359
360 return result;
361}
362
363template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
365{
366#ifdef DEBUG
367 assert(dsp); // consider also checking the vector contents
368 assert(is_initialized());
369 assert(splitting_depth<dsp->size());
370 for(unsigned int n=0;n<=splitting_depth;n++) assert(enum_split[n]>0);
372#endif
373 bool result=true;
374 if(!(*dsp)[splitting_depth]->makeNextSplit()) result=false;
375 else
376 if(splitting_depth>0) for(unsigned int c=0; c<classes.size(); c++) {
377 splits[splitting_depth-1][c]._reduced_train->reduce(splits[splitting_depth][c]._train,splits[splitting_depth][c]._reduced_train);
378 splits[splitting_depth-1][c]._reduced_train->reduce(splits[splitting_depth][c]._test,splits[splitting_depth][c]._reduced_test);
379 }
381 for(unsigned int n=splitting_depth+1;n<dsp->size();n++) enum_split[n]=0; // reset/cancel all deeper splits
382
383 return result;
384}
385
386template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
392
393template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
395{
396 assert(dsp); // consider also checking the vector contents
397 assert(is_initialized());
398 assert(active_class>=0 && active_class<(int)classes.size());
399 assert(splitting_depth<dsp->size());
401 switch(mappedDataPart(ofwhat)) {
402 case TRAIN: {return splits[splitting_depth][active_class]._reduced_train->size();}
403 case TEST: {return splits[splitting_depth][active_class]._reduced_test->size();}
404 case TRAINTEST: {return splits[splitting_depth][active_class]._reduced_train->size()+splits[splitting_depth][active_class]._reduced_test->size();}
405 case ALL: {if(splitting_depth>0) return splits[splitting_depth-1][active_class]._reduced_train->size(); else return 1;}
406 case NONE: {return 0;}
407 }
408 return 0;
409}
410
411template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
413{
414 assert(dsp); // consider also checking the vector contents
415 assert(splitting_depth<dsp->size());
416 assert(loopdepth<enum_block[splitting_depth].size());
417 return enum_block[splitting_depth][loopdepth];
418}
419
420template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
422{
423 assert(is_initialized());
424 assert(splitting_depth<dsp->size());
425 assert(active_class>=0 && active_class<(int)classes.size());
427 switch(mappedDataPart(ofwhat)) {
428 case TRAIN: {return splits[splitting_depth][active_class]._reduced_train->sum();}
429 case TEST: {return splits[splitting_depth][active_class]._reduced_test->sum();}
430 case TRAINTEST: {return splits[splitting_depth][active_class]._reduced_train->sum()+splits[splitting_depth][active_class]._reduced_test->sum();}
431 case ALL: {if(splitting_depth>0) return splits[splitting_depth-1][active_class]._reduced_train->sum();
432 else return classes[active_class];}
433 case NONE: {return 0;}
434 }
435 return 0;
436}
437
438template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
440{
441 assert(source==TRAIN || source==TEST || source==TRAINTEST || source==ALL);
442 assert(target==TRAIN || target==TEST || target==TRAINTEST || target==ALL);
443 switch(source){
444 case TRAIN: mappedTRAIN=target; break;
445 case TEST: mappedTEST=target; break;
446 case TRAINTEST: mappedTRAINTEST=target; break;
447 case ALL: mappedALL=target; break;
448 case NONE: break;
449 }
450}
451
452template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
460
461
462// ============================================ PROTECTED ===============================================
463
464// strong copy-constructor, creates 1:1 copy of all structures
465template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
467 classes(da.classes),
468 features(da.features),
473 splits(da.splits),
476 tt_phase(da.tt_phase),
480{
481 notify("Data_Accessor_Splitting() copy constructor.");
482 notify("DAS::copy-constructor, before da.assert_splits()");
483 da.assert_splits();
484 notify("DAS::copy-constructor, after da.assert_splits()");
485 assert(da.dsp);
486 assert(da.dsp->size()>0);
487 dsp.reset(new std::vector<PSplitter>);
488 for(unsigned int n=0; n<da.dsp->size();n++)
489 {
490
491 PSplitter ps(dynamic_cast<Data_Splitter<INTERVALCONTAINER,IDXTYPE>* >((*da.dsp)[n]->stateless_clone()));
492 for(unsigned int c=0; c<classes.size(); c++)
493 ps->assign(c,splits[n][c]._train,splits[n][c]._test);
494 dsp->push_back(ps);
495 }
496 notify("DAS::copy-constructor, before assert_splits()");
498 notify("DAS::copy-constructor, after assert_splits()");
499}
500
501
502template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
504{
505 notify("Data_Accessor_Splitting()::initialize.");
506 if(is_initialized()) notify("Re-initializing Data_Accessor_Splitting.");
507 classes.resize( _classes.size() );
508 copy(_classes.begin(), _classes.end(), classes.begin() );
509 features=_features;
510
511 for(unsigned int n=0;n<dsp->size();n++) // number of nested splitting levels
512 {
513 splits[n].clear();
514 notify("Data_Accessor_Splitting()::initialize to resize splits.");
515 splits[n].resize(classes.size());
516 notify("Data_Accessor_Splitting()::initialize splits resized.");
517 for(unsigned int c=0;c<classes.size();c++)
518 {
519 DataSplit &cs=splits[n][c];
520 cs._train.reset(new INTERVALCONTAINER());
521 cs._test.reset(new INTERVALCONTAINER());
522 (*dsp)[n]->assign(c,cs._train,cs._test);
523 if(n==0) {
524 (*dsp)[0]->set_n(c,classes[c]);
526 cs._reduced_test=cs._test;
527 } else {
528 cs._reduced_train.reset(new INTERVALCONTAINER());
529 cs._reduced_test.reset(new INTERVALCONTAINER());
530 }
531 // NOTE: set_n() is done here for depth 0 only, where the sample count per class is
532 // simply the class size. At deeper levels the count depends on the enclosing
533 // level's current TRAIN part, so getFirstSplit() re-issues set_n() on the
534 // splitter of that level before every split enumeration.
535 }
536 }
538 notify("Data_Accessor_Splitting()::initialize finished.");
539}
540
541template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
543{
544 switch(ofwhat){
545 case TRAIN: return mappedTRAIN;
546 case TEST: return mappedTEST;
547 case TRAINTEST: return mappedTRAINTEST;
548 case ALL: return mappedALL;
549 case NONE: return NONE;
550 }
551 return ofwhat;
552}
553
554template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
556{
557#ifdef DEBUG
558 assert(dsp); // consider also checking the vector contents
559 assert(is_initialized());
560 assert(splitting_depth<dsp->size());
561 assert(active_class>=0 && active_class<(int)classes.size());
562 for(unsigned int n=0;n<=splitting_depth;n++) assert(enum_split[n]>0);
564#endif
565 if(enum_block[splitting_depth].size()<=loopdepth) enum_block[splitting_depth].resize(loopdepth+1);
566 switch(mappedDataPart(ofwhat)) {
567 case TRAIN: {tmp=splits[splitting_depth][active_class]._reduced_train->getFirstBlock(loopdepth); break;}
568 case TEST: {tmp=splits[splitting_depth][active_class]._reduced_test->getFirstBlock(loopdepth); break;}
569 case TRAINTEST: {
570 if(tt_phase[splitting_depth].size()<=loopdepth) tt_phase[splitting_depth].resize(loopdepth+1);
571 tt_phase[splitting_depth][loopdepth]=TRAIN;
572 tmp=splits[splitting_depth][active_class]._reduced_train->getFirstBlock(loopdepth);
573 if(tmp==nullptr) {
574 tt_phase[splitting_depth][loopdepth]=TEST;
575 tmp=splits[splitting_depth][active_class]._reduced_test->getFirstBlock(loopdepth);
576 }
577 if(tmp==nullptr) tt_phase[splitting_depth][loopdepth]=NONE;
578 break;}
579 case ALL: {
580 if(splitting_depth>0) tmp=splits[splitting_depth-1][active_class]._reduced_train->getFirstBlock(loopdepth);
581 else tmp=&(splits[0][active_class]._all);
582 break;}
583 case NONE: {tmp=nullptr;}
584 }
585 if(tmp!=nullptr) enum_block[splitting_depth][loopdepth]=1; else {enum_block[splitting_depth][loopdepth]=0; return false;}
586 return true;
587}
588
589template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
591{
592#ifdef DEBUG
593 assert(dsp); // consider also checking the vector contents
594 assert(is_initialized());
595 assert(splitting_depth<dsp->size());
596 assert(active_class>=0 && active_class<(int)classes.size());
597 assert(loopdepth<enum_block[splitting_depth].size());
598 for(unsigned int n=0;n<=splitting_depth;n++) assert(enum_split[n]>0);
599 assert(enum_block[splitting_depth][loopdepth]>0);
601#endif
602 switch(mappedDataPart(ofwhat)) {
603 case TRAIN: {tmp=splits[splitting_depth][active_class]._reduced_train->getNextBlock(loopdepth); break;}
604 case TEST: {tmp=splits[splitting_depth][active_class]._reduced_test->getNextBlock(loopdepth); break;}
605 case TRAINTEST: {
606 assert(loopdepth<tt_phase[splitting_depth].size());
607 assert(tt_phase[splitting_depth][loopdepth]==TRAIN || tt_phase[splitting_depth][loopdepth]==TEST);
608 if(tt_phase[splitting_depth][loopdepth]==TRAIN) {
609 tmp=splits[splitting_depth][active_class]._reduced_train->getNextBlock(loopdepth);
610 if(tmp==nullptr) {
611 tt_phase[splitting_depth][loopdepth]=TEST;
612 tmp=splits[splitting_depth][active_class]._reduced_test->getFirstBlock(loopdepth);
613 }
614 } else {
615 tmp=splits[splitting_depth][active_class]._reduced_test->getNextBlock(loopdepth);
616 }
617 if(tmp==nullptr) tt_phase[splitting_depth][loopdepth]=NONE;
618 break;}
619 case ALL: {if(splitting_depth>0) tmp=splits[splitting_depth-1][active_class]._reduced_train->getNextBlock(loopdepth); else tmp=nullptr; break;}
620 case NONE: {tmp=nullptr;}
621 }
622 if(tmp!=nullptr) enum_block[splitting_depth][loopdepth]++; else {enum_block[splitting_depth][loopdepth]=0; return false;}
623 return true;
624}
625
628template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
630{
631 notify("DataSplit strong copy constructor.");
632 if(cs._train) _train.reset(new INTERVALCONTAINER(*cs._train));
633 if(cs._test) _test.reset(new INTERVALCONTAINER(*cs._test));
634 if(cs._reduced_train == cs._train) // && cs._reduced_test == cs._test
635 {
636 notify("DataSplit _reduced_* to be linked");
639 } else {
640 notify("DataSplit _reduced_* to be re-allocated");
641 if(cs._reduced_train) _reduced_train.reset(new INTERVALCONTAINER(*cs._reduced_train));
642 if(cs._reduced_test) _reduced_test.reset(new INTERVALCONTAINER(*cs._reduced_test));
643 }
644 _all.startidx=cs._all.startidx; _all.count=cs._all.count;
645}
646
649template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
651{
652 notify("DataSplit weak copy constructor.");
653 _train=cs._train;
654 _test=cs._test;
657 _all.startidx=cs._all.startidx; _all.count=cs._all.count;
658}
659
660template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
662{
663 if(dsp) {
664 if(dsp->size()>0) {
665 os << "Data_Accessor_Splitting() splitters: ";
666 typename std::vector<PSplitter>::const_iterator iter;
667 for(iter=dsp->begin();iter!=dsp->end();iter++) {
668 if(*iter) os << **iter << " "; else os << "nullptr ";
669 }
670 } else os << "Data_Accessor_Splitting()";
671 }
672 return os;
673}
674
675template<typename DATATYPE, typename IDXTYPE, class INTERVALCONTAINER>
677{
678 // assumes Data_Accessor_Splitting() has been initialized; all data structures are checked to be valid and non-empty
679#ifdef DEBUG
680 assert(features>0); // No. of features
681 assert(classes.size()>0); // No. of classes
682 assert(dsp); // consider also checking the vector contents
683 assert(dsp->size()>0); // No. of splitting levels
684 // the following not to be checked here
685
686 assert(splitting_check<(const int)dsp->size());
687 // splitting_check == -1 by default
688 // splitting_check > -1 checks that in each level 0..splitting_check the number of samples is set > 0
689
690 // check correctness of data strucutre sizes
691 assert(enum_split.size()==dsp->size());
692 assert(splits.size()==dsp->size());
693 assert(enum_block.size()==dsp->size());
694 assert(tt_phase.size()==dsp->size());
695 for(unsigned int n=0;n<dsp->size();n++) assert(splits[n].size()==classes.size());
696
697 for(unsigned int n=0;n<dsp->size();n++)
698 {
699 for(unsigned int c=0;c<classes.size();c++)
700 {
701 if((int)n<=splitting_check) assert((*dsp)[n]->get_n(c)>0);
702 assert(splits[n][c]._train);
703 assert(splits[n][c]._test);
704 assert(splits[n][c]._reduced_train);
705 assert(splits[n][c]._reduced_test);
706 if(n==0) {
707 assert(splits[n][c]._reduced_train==splits[n][c]._train);
708 assert(splits[n][c]._reduced_test==splits[n][c]._test);
709 } else {
710 assert(splits[n][c]._reduced_train!=splits[n][c]._train);
711 assert(splits[n][c]._reduced_test!=splits[n][c]._test);
712 }
713 }
714 }
715#endif
716}
717
718
719} // namespace
720#endif // FSTDATAACCESSORSPLITTING_H ///:~
Data splitting support structure; holds one set of intervals (train, test) per each splitting depth a...
Definition data_accessor_splitting.hpp:228
PIntervaller _test
this depth/class's base (splitter-produced) test intervals
Definition data_accessor_splitting.hpp:235
DataSplit()
all interval lists start unset
Definition data_accessor_splitting.hpp:230
PIntervaller _reduced_train
depth 0: same list as _train; deeper: _train's indexes reduced (translated) into the enclosing depth'...
Definition data_accessor_splitting.hpp:236
PIntervaller _train
this depth/class's base (splitter-produced) train intervals
Definition data_accessor_splitting.hpp:234
PIntervaller _reduced_test
depth 0: same list as _test; deeper: _test's indexes reduced (translated) into the enclosing depth's ...
Definition data_accessor_splitting.hpp:237
Data_Interval< IDXTYPE > _all
to enable passing of block reference when DataPart ofwhat==ALL
Definition data_accessor_splitting.hpp:238
DataPart mappedALL
current remapping target of ALL (see substitute()/resubstitute()); defaults to ALL
Definition data_accessor_splitting.hpp:215
PSplitters dsp
the splitter stack this accessor was constructed with, one Data_Splitter per nested splitting depth
Definition data_accessor_splitting.hpp:219
std::shared_ptr< std::vector< PSplitter > > PSplitters
shared pointer to the ordered list of splitters, one per nested splitting depth (depth 0 = outermost)
Definition data_accessor_splitting.hpp:154
DataPart mappedTRAINTEST
current remapping target of TRAINTEST (see substitute()/resubstitute()); defaults to TRAINTEST
Definition data_accessor_splitting.hpp:214
std::vector< std::vector< DataSplit > > splits
one set of splitters per each splitting depth and class
Definition data_accessor_splitting.hpp:240
virtual unsigned int getSplitIndex() const override
data access iteration (to support, e.g., loops in cross-validation)
Definition data_accessor_splitting.hpp:387
virtual int getClass() const override
returns active class
Definition data_accessor_splitting.hpp:167
virtual IDXTYPE getClassSize(const unsigned int c) const override
returns size (number of samples in) of class c
Definition data_accessor_splitting.hpp:291
std::vector< unsigned int > CLASSSIZES
per-class pattern counts, indexed by class index; the format initialize() takes
Definition data_accessor_splitting.hpp:203
virtual unsigned int getNoOfClasses() const override
returns number of classes
Definition data_accessor_splitting.hpp:279
virtual bool getNextSplit() override
data access iteration (to support, e.g., loops in cross-validation)
Definition data_accessor_splitting.hpp:364
virtual std::ostream & print(std::ostream &os) const override
writes the splitter stack's print() output (or "Data_Accessor_Splitting()" if the stack is empty) to ...
Definition data_accessor_splitting.hpp:661
virtual bool getFirstSplit() override
data access iteration (to support, e.g., loops in cross-validation)
Definition data_accessor_splitting.hpp:330
virtual void resubstitute() override
resets standard DataPart types' meaning
Definition data_accessor_splitting.hpp:453
DataPart mappedTRAIN
current remapping target of TRAIN (see substitute()/resubstitute()); defaults to TRAIN
Definition data_accessor_splitting.hpp:212
void setSplittingDepth(const unsigned int depth)
Selects the nested splitting level (0 = outermost/depth-0 splitter, 1 = the next-nested level,...
Definition data_accessor_splitting.hpp:306
const Data_Interval< IDXTYPE > * DATAINTERVAL
Definition data_accessor_splitting.hpp:220
virtual void setClass(const int c) override
sets active class -> from now on only data from class c will be considered
Definition data_accessor_splitting.hpp:166
virtual bool getFirstBlock(const DataPart ofwhat, PPattern &firstpattern, IDXTYPE &patterns, const unsigned int loopdepth=0) override=0
returns pointer to first consecutive block of data of requested DataPart type in the current split (a...
bool is_initialized() const
Definition data_accessor_splitting.hpp:252
std::vector< std::vector< DataPart > > tt_phase
in current block loop.. for DataPart==TRAINTEST indicates which half is being delivered: TRAIN,...
Definition data_accessor_splitting.hpp:244
std::shared_ptr< INTERVALCONTAINER > PIntervaller
Definition data_accessor_splitting.hpp:226
DataPart mappedDataPart(const DataPart ofwhat) const
looks up ofwhat's current remapping target (mappedTRAIN/mappedTEST/mappedTRAINTEST/mappedALL); NONE m...
Definition data_accessor_splitting.hpp:542
virtual IDXTYPE getNoOfPatterns(const DataPart ofwhat) const override
returns number of patterns in all consecutive blocks of data of requested DataPart type in the curren...
Definition data_accessor_splitting.hpp:421
const DATATYPE * PPattern
Definition data_accessor_splitting.hpp:155
virtual unsigned int getNoOfFeatures() const override
returns data dimensionality
Definition data_accessor_splitting.hpp:285
int active_class
denotes from which class the get*Train* get*Validate* get*Test* methods return patterns,...
Definition data_accessor_splitting.hpp:248
void assert_splits(const int splitting_check=-1) const
DEBUG-only: asserts the internal structures (dsp, splits, enum_split, enum_block, tt_phase,...
Definition data_accessor_splitting.hpp:676
virtual IDXTYPE getClassSizeSum() const override
returns summed size (number of samples in) of all classes, i.e., no. of all patterns in data
Definition data_accessor_splitting.hpp:298
Data_Accessor_Splitting(const PSplitters _dsp)
Constructs an (uninitialized) accessor bound to the splitter stack _dsp; concrete subclasses must sti...
Definition data_accessor_splitting.hpp:262
virtual IDXTYPE getBlockIndex(const unsigned int loopdepth=0) const override
returns index of the current consecutive block of data of requested DataPart type in the current spli...
Definition data_accessor_splitting.hpp:412
virtual bool getNextBlock(const DataPart ofwhat, PPattern &firstpattern, IDXTYPE &patterns, const unsigned int loopdepth=0) override=0
returns pointer to next consecutive block of data of requested DataPart type in the current split (ac...
std::vector< std::vector< IDXTYPE > > enum_block
current block loop.. 0~none
Definition data_accessor_splitting.hpp:243
virtual IDXTYPE getNoOfBlocks(const DataPart ofwhat) const override
Definition data_accessor_splitting.hpp:394
unsigned int features
data dimensionality, as passed to initialize()
Definition data_accessor_splitting.hpp:209
unsigned int getSplittingDepth() const
the nested splitting level last set via setSplittingDepth()
Definition data_accessor_splitting.hpp:172
virtual unsigned int getNoOfSplits() const override
data access iteration (to support, e.g., loops in cross-validation)
Definition data_accessor_splitting.hpp:321
unsigned int splitting_depth
the nested splitting level the get*Split()/get*Block() calls currently refer to; set via setSplitting...
Definition data_accessor_splitting.hpp:246
virtual void substitute(const DataPart source, const DataPart target) override
enables change of meaning of DataPart types, for use in specialized data access scenarios like in bia...
Definition data_accessor_splitting.hpp:439
DataPart mappedTEST
current remapping target of TEST (see substitute()/resubstitute()); defaults to TEST
Definition data_accessor_splitting.hpp:213
bool _initialize_called
set true by initialize(); read by is_initialized()
Definition data_accessor_splitting.hpp:251
CLASSSIZES classes
pattern count per class, as passed to initialize()
Definition data_accessor_splitting.hpp:208
std::vector< IDXTYPE > enum_split
current split.. 0~none
Definition data_accessor_splitting.hpp:242
std::shared_ptr< Data_Splitter< INTERVALCONTAINER, IDXTYPE > > PSplitter
shared pointer to one Data_Splitter (one nested splitting level)
Definition data_accessor_splitting.hpp:153
void initialize(const unsigned int _features, const CLASSSIZES &_classes)
Definition data_accessor_splitting.hpp:503
Abstract class, defines interface for data splitting implementations (for use in data accessors).
Definition data_splitter.hpp:124
Defines interface for data access implementations.
Defines interface for data splitting implementations (for use in data accessors).
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
Support structure to hold data interval (referring into data held by data accessor).
Definition data_intervaller.hpp:88
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.