29 classType =
"ClassLabelTimeoutFilter";
30 postProcessingType = classType;
31 postProcessingInputMode = INPUT_MODE_PREDICTED_CLASS_LABEL;
32 postProcessingOutputMode = OUTPUT_MODE_PREDICTED_CLASS_LABEL;
33 debugLog.setProceedingText(
"[DEBUG ClassLabelTimeoutFilter]");
34 errorLog.setProceedingText(
"[ERROR ClassLabelTimeoutFilter]");
35 warningLog.setProceedingText(
"[WARNING ClassLabelTimeoutFilter]");
36 init(timeoutDuration,filterMode);
41 classType =
"ClassLabelTimeoutFilter";
42 postProcessingType = classType;
43 postProcessingInputMode = INPUT_MODE_PREDICTED_CLASS_LABEL;
44 postProcessingOutputMode = OUTPUT_MODE_PREDICTED_CLASS_LABEL;
45 debugLog.setProceedingText(
"[DEBUG ClassLabelTimeoutFilter]");
46 errorLog.setProceedingText(
"[ERROR ClassLabelTimeoutFilter]");
47 warningLog.setProceedingText(
"[WARNING ClassLabelTimeoutFilter]");
50 this->filteredClassLabel = rhs.filteredClassLabel;
51 this->filterMode = rhs.filterMode;
52 this->timeoutDuration = rhs.timeoutDuration;
53 this->classLabelTimers = rhs.classLabelTimers;
67 this->filteredClassLabel = rhs.filteredClassLabel;
68 this->filterMode = rhs.filterMode;
69 this->timeoutDuration = rhs.timeoutDuration;
70 this->classLabelTimers = rhs.classLabelTimers;
80 if( postProcessing == NULL )
return false;
87 this->filteredClassLabel = ptr->filterMode;
88 this->filterMode = ptr->filterMode;
89 this->timeoutDuration = ptr->timeoutDuration;
90 this->classLabelTimers = ptr->classLabelTimers;
100 #ifdef GRT_SAFE_CHECKING
102 errorLog <<
"process(const VectorDouble &inputVector) - Not initialized!" << std::endl;
106 if( inputVector.
getSize() != numInputDimensions ){
107 errorLog <<
"process(const VectorDouble &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
113 processedData[0] =
filter( (UINT)inputVector[0] );
118 filteredClassLabel = 0;
119 classLabelTimers.clear();
120 processedData.clear();
121 processedData.
resize(1,0);
129 if( filterMode != ALL_CLASS_LABELS && filterMode != INDEPENDENT_CLASS_LABELS ){
130 errorLog <<
"init(double timeoutDuration,UINT filterMode) - Unkown filter mode!" << std::endl;
134 this->timeoutDuration = timeoutDuration;
135 this->filterMode = filterMode;
136 numInputDimensions = 1;
137 numOutputDimensions = 1;
138 initialized =
reset();
145 if( predictedClassLabel == 0 && classLabelTimers.size() == 0 ){
146 filteredClassLabel = predictedClassLabel;
147 return filteredClassLabel;
150 bool matchFound =
false;
153 switch( filterMode ){
154 case ALL_CLASS_LABELS:
157 if( classLabelTimers.size() == 0 ){
158 filteredClassLabel = predictedClassLabel;
159 classLabelTimers.push_back(
ClassLabelAndTimer(predictedClassLabel,timeoutDuration) );
162 if( classLabelTimers[0].timerReached() ){
164 classLabelTimers.clear();
168 filteredClassLabel = 0;
169 if( predictedClassLabel ){
170 filteredClassLabel =
filter( predictedClassLabel );
173 }
else filteredClassLabel = 0;
177 case INDEPENDENT_CLASS_LABELS:
180 if( classLabelTimers.size() > 0 ){
181 iter = classLabelTimers.begin();
183 while( iter != classLabelTimers.end() ){
184 if( iter->getClassLabel() == predictedClassLabel ){
186 if( iter->timerReached() ){
188 iter->set(predictedClassLabel,timeoutDuration);
192 filteredClassLabel = predictedClassLabel;
194 }
else filteredClassLabel = 0;
199 if( iter->timerReached() ){
201 iter = classLabelTimers.erase( iter );
210 classLabelTimers.push_back(
ClassLabelAndTimer(predictedClassLabel,timeoutDuration) );
211 filteredClassLabel = predictedClassLabel;
217 return filteredClassLabel;
222 for(UINT i=0; i<classLabelTimers.
getSize(); i++){
223 if( classLabelTimers[i].timerReached() ){
234 errorLog <<
"saveModelToFile(string filename) - The ClassLabelTimeoutFilter has not been initialized" << std::endl;
239 file.open(filename.c_str(), std::ios::out);
253 if( !file.is_open() ){
254 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
258 file <<
"GRT_CLASS_LABEL_TIMEOUT_FILTER_FILE_V1.0" << std::endl;
259 file <<
"NumInputDimensions: " << numInputDimensions << std::endl;
260 file <<
"NumOutputDimensions: " << numOutputDimensions << std::endl;
261 file <<
"FilterMode: " << filterMode << std::endl;
262 file <<
"TimeoutDuration: " << timeoutDuration << std::endl;
270 file.open(filename.c_str(), std::ios::in);
285 if( !file.is_open() ){
286 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
295 if( word !=
"GRT_CLASS_LABEL_TIMEOUT_FILTER_FILE_V1.0" ){
296 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
301 if( word !=
"NumInputDimensions:" ){
302 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
305 file >> numInputDimensions;
309 if( word !=
"NumOutputDimensions:" ){
310 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
313 file >> numOutputDimensions;
317 if( word !=
"FilterMode:" ){
318 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FilterMode header!" << std::endl;
324 if( word !=
"TimeoutDuration:" ){
325 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read TimeoutDuration header!" << std::endl;
328 file >> timeoutDuration;
331 return init(timeoutDuration,filterMode);
335 this->timeoutDuration = timeoutDuration;
343 if( filterMode != ALL_CLASS_LABELS && filterMode != INDEPENDENT_CLASS_LABELS )
return false;
344 this->filterMode = filterMode;
ClassLabelTimeoutFilter(unsigned long timeoutDuration=1000, UINT filterMode=ALL_CLASS_LABELS)
The Class Label Timeout Filter is a useful post-processing module which debounces a gesture (i...
virtual bool process(const VectorDouble &inputVector)
UINT filter(UINT predictedClassLabel)
virtual ~ClassLabelTimeoutFilter()
std::string getPostProcessingType() const
virtual bool resize(const unsigned int size)
unsigned int getSize() const
bool copyBaseVariables(const PostProcessing *postProcessingModule)
bool setTimeoutDuration(unsigned long timeoutDuration)
virtual bool loadModelFromFile(std::string filename)
ClassLabelTimeoutFilter & operator=(const ClassLabelTimeoutFilter &rhs)
virtual bool saveModelToFile(std::string filename) const
bool setFilterMode(UINT filterMode)
virtual bool deepCopyFrom(const PostProcessing *postProcessing)