21 #define GRT_DLL_EXPORTS
30 classType =
"ClassLabelTimeoutFilter";
31 postProcessingType = classType;
32 postProcessingInputMode = INPUT_MODE_PREDICTED_CLASS_LABEL;
33 postProcessingOutputMode = OUTPUT_MODE_PREDICTED_CLASS_LABEL;
34 debugLog.setProceedingText(
"[DEBUG ClassLabelTimeoutFilter]");
35 errorLog.setProceedingText(
"[ERROR ClassLabelTimeoutFilter]");
36 warningLog.setProceedingText(
"[WARNING ClassLabelTimeoutFilter]");
37 init(timeoutDuration,filterMode);
42 classType =
"ClassLabelTimeoutFilter";
43 postProcessingType = classType;
44 postProcessingInputMode = INPUT_MODE_PREDICTED_CLASS_LABEL;
45 postProcessingOutputMode = OUTPUT_MODE_PREDICTED_CLASS_LABEL;
46 debugLog.setProceedingText(
"[DEBUG ClassLabelTimeoutFilter]");
47 errorLog.setProceedingText(
"[ERROR ClassLabelTimeoutFilter]");
48 warningLog.setProceedingText(
"[WARNING ClassLabelTimeoutFilter]");
51 this->filteredClassLabel = rhs.filteredClassLabel;
52 this->filterMode = rhs.filterMode;
53 this->timeoutDuration = rhs.timeoutDuration;
54 this->classLabelTimers = rhs.classLabelTimers;
68 this->filteredClassLabel = rhs.filteredClassLabel;
69 this->filterMode = rhs.filterMode;
70 this->timeoutDuration = rhs.timeoutDuration;
71 this->classLabelTimers = rhs.classLabelTimers;
81 if( postProcessing == NULL )
return false;
88 this->filteredClassLabel = ptr->filterMode;
89 this->filterMode = ptr->filterMode;
90 this->timeoutDuration = ptr->timeoutDuration;
91 this->classLabelTimers = ptr->classLabelTimers;
101 #ifdef GRT_SAFE_CHECKING
103 errorLog <<
"process(const VectorDouble &inputVector) - Not initialized!" << std::endl;
107 if( inputVector.
getSize() != numInputDimensions ){
108 errorLog <<
"process(const VectorDouble &inputVector) - The size of the inputVector (" << inputVector.
getSize() <<
") does not match that of the filter (" << numInputDimensions <<
")!" << std::endl;
114 processedData[0] =
filter( (UINT)inputVector[0] );
119 filteredClassLabel = 0;
120 classLabelTimers.clear();
121 processedData.clear();
122 processedData.
resize(1,0);
130 if( filterMode != ALL_CLASS_LABELS && filterMode != INDEPENDENT_CLASS_LABELS ){
131 errorLog <<
"init(double timeoutDuration,UINT filterMode) - Unkown filter mode!" << std::endl;
135 this->timeoutDuration = timeoutDuration;
136 this->filterMode = filterMode;
137 numInputDimensions = 1;
138 numOutputDimensions = 1;
139 initialized =
reset();
146 if( predictedClassLabel == 0 && classLabelTimers.size() == 0 ){
147 filteredClassLabel = predictedClassLabel;
148 return filteredClassLabel;
151 bool matchFound =
false;
154 switch( filterMode ){
155 case ALL_CLASS_LABELS:
158 if( classLabelTimers.size() == 0 ){
159 filteredClassLabel = predictedClassLabel;
160 classLabelTimers.push_back(
ClassLabelAndTimer(predictedClassLabel,timeoutDuration) );
163 if( classLabelTimers[0].timerReached() ){
165 classLabelTimers.clear();
169 filteredClassLabel = 0;
170 if( predictedClassLabel ){
171 filteredClassLabel =
filter( predictedClassLabel );
174 }
else filteredClassLabel = 0;
178 case INDEPENDENT_CLASS_LABELS:
181 if( classLabelTimers.size() > 0 ){
182 iter = classLabelTimers.begin();
184 while( iter != classLabelTimers.end() ){
185 if( iter->getClassLabel() == predictedClassLabel ){
187 if( iter->timerReached() ){
189 iter->set(predictedClassLabel,timeoutDuration);
193 filteredClassLabel = predictedClassLabel;
195 }
else filteredClassLabel = 0;
200 if( iter->timerReached() ){
202 iter = classLabelTimers.erase( iter );
211 classLabelTimers.push_back(
ClassLabelAndTimer(predictedClassLabel,timeoutDuration) );
212 filteredClassLabel = predictedClassLabel;
218 return filteredClassLabel;
223 for(UINT i=0; i<classLabelTimers.
getSize(); i++){
224 if( classLabelTimers[i].timerReached() ){
235 errorLog <<
"saveModelToFile(string filename) - The ClassLabelTimeoutFilter has not been initialized" << std::endl;
240 file.open(filename.c_str(), std::ios::out);
254 if( !file.is_open() ){
255 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
259 file <<
"GRT_CLASS_LABEL_TIMEOUT_FILTER_FILE_V1.0" << std::endl;
260 file <<
"NumInputDimensions: " << numInputDimensions << std::endl;
261 file <<
"NumOutputDimensions: " << numOutputDimensions << std::endl;
262 file <<
"FilterMode: " << filterMode << std::endl;
263 file <<
"TimeoutDuration: " << timeoutDuration << std::endl;
271 file.open(filename.c_str(), std::ios::in);
286 if( !file.is_open() ){
287 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
296 if( word !=
"GRT_CLASS_LABEL_TIMEOUT_FILTER_FILE_V1.0" ){
297 errorLog <<
"loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
302 if( word !=
"NumInputDimensions:" ){
303 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
306 file >> numInputDimensions;
310 if( word !=
"NumOutputDimensions:" ){
311 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
314 file >> numOutputDimensions;
318 if( word !=
"FilterMode:" ){
319 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read FilterMode header!" << std::endl;
325 if( word !=
"TimeoutDuration:" ){
326 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read TimeoutDuration header!" << std::endl;
329 file >> timeoutDuration;
332 return init(timeoutDuration,filterMode);
336 this->timeoutDuration = timeoutDuration;
344 if( filterMode != ALL_CLASS_LABELS && filterMode != INDEPENDENT_CLASS_LABELS )
return false;
345 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)
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)