29 const unsigned int hysteresisThreshold,
const unsigned int swipeDirection,
bool useScaling)
31 this->swipeIndex = swipeIndex;
32 this->swipeThreshold = swipeThreshold;
33 this->hysteresisThreshold = hysteresisThreshold;
34 this->swipeDirection = swipeDirection;
35 this->useScaling = useScaling;
37 supportsNullRejection =
false;
39 classType =
"SwipeDetector";
40 classifierType = classType;
41 classifierMode = STANDARD_CLASSIFIER_MODE;
42 debugLog.setProceedingText(
"[DEBUG SwipeDetector]");
43 errorLog.setProceedingText(
"[ERROR SwipeDetector]");
44 trainingLog.setProceedingText(
"[TRAINING SwipeDetector]");
45 warningLog.setProceedingText(
"[WARNING SwipeDetector]");
47 swipeIntegrationCoeff = 0.92;
48 movementIntegrationCoeff = 0.90;
49 movementThreshold = 3000;
50 contextFilterSize = 5;
56 supportsNullRejection =
false;
57 classType =
"SwipeDetector";
58 classifierType = classType;
59 classifierMode = STANDARD_CLASSIFIER_MODE;
60 debugLog.setProceedingText(
"[DEBUG SwipeDetector]");
61 errorLog.setProceedingText(
"[ERROR SwipeDetector]");
62 trainingLog.setProceedingText(
"[TRAINING SwipeDetector]");
63 warningLog.setProceedingText(
"[WARNING SwipeDetector]");
65 swipeIntegrationCoeff = 0.92;
66 movementIntegrationCoeff = 0.90;
67 movementThreshold = 3000;
68 contextFilterSize = 5;
80 this->firstSample = rhs.firstSample;
81 this->swipeDetected = rhs.swipeDetected;
82 this->contextInput = rhs.contextInput;
83 this->swipeIndex = rhs.swipeIndex;
84 this->swipeDirection = rhs.swipeDirection;
85 this->contextFilterSize = rhs.contextFilterSize;
86 this->swipeIntegrationCoeff = rhs.swipeIntegrationCoeff;
87 this->movementIntegrationCoeff = rhs.movementIntegrationCoeff;
88 this->swipeThreshold = rhs.swipeThreshold;
89 this->hysteresisThreshold = rhs.hysteresisThreshold;
90 this->swipeVelocity = rhs.swipeVelocity;
91 this->movementVelocity = rhs.movementVelocity;
92 this->movementThreshold = rhs.movementThreshold;
93 this->contextFilteredValue = rhs.contextFilteredValue;
94 this->lastX = rhs.lastX;
95 this->thresholdDetector = rhs.thresholdDetector;
96 this->contextFilter = rhs.contextFilter;
106 if( classifier == NULL )
return false;
112 this->firstSample = ptr->firstSample;
113 this->swipeDetected = ptr->swipeDetected;
114 this->contextInput = ptr->contextInput;
115 this->swipeIndex = ptr->swipeIndex;
116 this->swipeDirection = ptr->swipeDirection;
117 this->contextFilterSize = ptr->contextFilterSize;
118 this->swipeIntegrationCoeff = ptr->swipeIntegrationCoeff;
119 this->movementIntegrationCoeff = ptr->movementIntegrationCoeff;
120 this->swipeThreshold = ptr->swipeThreshold;
121 this->hysteresisThreshold = ptr->hysteresisThreshold;
122 this->swipeVelocity = ptr->swipeVelocity;
123 this->movementVelocity = ptr->movementVelocity;
124 this->movementThreshold = ptr->movementThreshold;
125 this->contextFilteredValue = ptr->contextFilteredValue;
126 this->lastX = ptr->lastX;
127 this->thresholdDetector = ptr->thresholdDetector;
128 this->contextFilter = ptr->contextFilter;
137 bool SwipeDetector::init(
const unsigned int numInputDimensions){
142 this->numInputDimensions = numInputDimensions;
147 nullRejectionThresholds.
resize(2,0);
164 errorLog <<
"train_(trainingData &labelledTrainingData) - Training data has zero samples!" << std::endl;
168 numInputDimensions = N;
173 nullRejectionThresholds.
resize(2,0);
179 trainingData.
scale(0, 1);
191 predictedClassLabel = 0;
193 swipeDetected =
false;
196 errorLog <<
"predict_(VectorDouble &inputVector) - SwipeDetector Model Not Trained!" << std::endl;
200 if( inputVector.size() != numInputDimensions ){
201 errorLog <<
"predict_(VectorDouble &inputVector) - The size of the input vector (" << inputVector.size() <<
") does not match the num features in the model (" << numInputDimensions <<
")" << std::endl;
206 for(UINT n=0; n<numInputDimensions; n++){
207 inputVector[n] =
scale(inputVector[n], ranges[n].minValue, ranges[n].maxValue, 0, 1);
211 if( classLikelihoods.size() != numClasses ) classLikelihoods.
resize(numClasses,0);
212 if( classDistances.size() != numClasses ) classDistances.
resize(numClasses,0);
216 movementVelocity *= movementIntegrationCoeff;
217 for(
unsigned int i=0; i<numInputDimensions; i++){
218 if( i != swipeIndex )
219 movementVelocity += GRT::SQR(inputVector[i]-lastX[i]);
221 }
else firstSample =
false;
225 swipeVelocity = (swipeVelocity*swipeIntegrationCoeff) + inputVector[swipeIndex];
226 thresholdDetector.update( swipeVelocity );
229 contextFilteredValue = contextFilter.filter( contextInput ? 1 : 0 );
232 swipeDetected = thresholdDetector.getThresholdCrossingDetected() && movementVelocity < movementThreshold && contextFilteredValue == 1;
235 predictedClassLabel = 1;
236 classLikelihoods[0] = 1.0;
237 classDistances[1] = 0;
239 predictedClassLabel = 2;
240 classLikelihoods[0] = 0.0;
241 classDistances[1] = 1;
259 movementVelocity = 0;
263 swipeDetected =
false;
266 if( swipeDirection == POSITIVE_SWIPE ){
267 thresholdDetector.setThresholdCrossingMode( GRT::ThresholdCrossingDetector::UPPER_THRESHOLD_CROSSING );
268 thresholdDetector.setUpperThreshold( swipeThreshold );
270 if( swipeDirection == NEGATIVE_SWIPE ){
271 thresholdDetector.setThresholdCrossingMode( GRT::ThresholdCrossingDetector::LOWER_THRESHOLD_CROSSING );
272 thresholdDetector.setLowerThreshold( swipeThreshold );
275 thresholdDetector.setAnalysisMode( GRT::ThresholdCrossingDetector::DERIVATIVE_ANALYSIS_MODE );
276 thresholdDetector.setDetectionTimeoutMode( GRT::ThresholdCrossingDetector::HYSTERESIS_THRESHOLD );
277 thresholdDetector.setHysteresisThreshold( hysteresisThreshold );
279 contextFilter.init( contextFilterSize, 1 );
288 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
293 file<<
"GRT_SWIPE_DETECTION_MODEL_FILE_V1.0\n";
297 errorLog <<
"saveModelToFile(fstream &file) - Failed to save classifier base settings to file!" << std::endl;
303 file <<
"SwipeIndex: " << swipeIndex << std::endl;
304 file <<
"ContextFilterSize: " << contextFilterSize << std::endl;
305 file <<
"SwipeIntegrationCoeff: " << swipeIntegrationCoeff << std::endl;
306 file <<
"MovementIntegrationCoeff: " << movementIntegrationCoeff << std::endl;
307 file <<
"SwipeThreshold: " << swipeThreshold << std::endl;
309 file <<
"HysteresisThreshold: " << hysteresisThreshold << std::endl;
310 file <<
"SwipeThreshold: " << swipeThreshold << std::endl;
311 file <<
"MovementThreshold: " << movementThreshold << std::endl;
312 file <<
"SwipeThreshold: " << swipeThreshold << std::endl;
324 numInputDimensions = 0;
330 errorLog <<
"loadModelFromFile(string filename) - Could not open file to load model" << std::endl;
339 if(word !=
"GRT_SWIPE_DETECTION_MODEL_FILE_V1.0"){
340 errorLog <<
"loadModelFromFile(string filename) - Could not find Model File Header" << std::endl;
346 errorLog <<
"loadModelFromFile(string filename) - Failed to load base settings from file!" << std::endl;
353 if( word !=
"SwipeIndex:" ){
354 errorLog <<
"loadModelFromFile(string filename) - Could not load the SwipeIndex!" << std::endl;
360 if( word !=
"ContextFilterSize:" ){
361 errorLog <<
"loadModelFromFile(string filename) - Could not load the ContextFilterSize!" << std::endl;
364 file >> contextFilterSize;
367 if( word !=
"SwipeIntegrationCoeff:" ){
368 errorLog <<
"loadModelFromFile(string filename) - Could not load the SwipeIntegrationCoeff!" << std::endl;
371 file >> swipeIntegrationCoeff;
374 if( word !=
"MovementIntegrationCoeff:" ){
375 errorLog <<
"loadModelFromFile(string filename) - Could not load the MovementIntegrationCoeff!" << std::endl;
378 file >> movementIntegrationCoeff;
381 if( word !=
"SwipeThreshold:" ){
382 errorLog <<
"loadModelFromFile(string filename) - Could not load the SwipeThreshold!" << std::endl;
385 file >> swipeThreshold;
388 if( word !=
"HysteresisThreshold:" ){
389 errorLog <<
"loadModelFromFile(string filename) - Could not load the HysteresisThreshold!" << std::endl;
392 file >> hysteresisThreshold;
395 if( word !=
"SwipeThreshold:" ){
396 errorLog <<
"loadModelFromFile(string filename) - Could not load the SwipeThreshold!" << std::endl;
399 file >> swipeThreshold;
402 if( word !=
"MovementThreshold:" ){
403 errorLog <<
"loadModelFromFile(string filename) - Could not load the MovementThreshold!" << std::endl;
406 file >> movementThreshold;
410 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
412 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
422 return swipeDetected;
426 return thresholdDetector.getAnalysisValue();
430 return swipeThreshold;
434 return hysteresisThreshold;
438 return movementVelocity;
442 return movementThreshold;
446 return contextFilteredValue;
450 return swipeIntegrationCoeff;
454 this->contextInput = context;
459 this->swipeIndex = swipeIndex;
466 if( swipeDirection != POSITIVE_SWIPE && swipeDirection != NEGATIVE_SWIPE ){
467 errorLog <<
"setSwipeDirection(const unsigned int swipeDirection) - Unknown swipeDirection!" << std::endl;
471 this->swipeDirection = swipeDirection;
478 this->swipeThreshold = swipeThreshold;
484 this->hysteresisThreshold = hysteresisThreshold;
490 this->movementThreshold = movementThreshold;
496 this->swipeIntegrationCoeff = swipeIntegrationCoeff;
bool saveBaseSettingsToFile(std::fstream &file) const
virtual bool predict_(VectorDouble &inputVector)
#define DEFAULT_NULL_LIKELIHOOD_VALUE
Float scale(const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)
bool setMovementThreshold(const Float movementThreshold)
bool setSwipeIntegrationCoeff(const Float swipeIntegrationCoeff)
std::string getClassifierType() const
Float getHysteresisThreshold() const
virtual bool resize(const unsigned int size)
SwipeDetector(const unsigned int swipeIndex=0, const unsigned int swipeThreshold=100, const unsigned int hysteresisThreshold=0, const unsigned int swipeDirection=POSITIVE_SWIPE, bool useScaling=false)
virtual ~SwipeDetector(void)
bool setContext(const bool context)
bool setSwipeThreshold(const Float swipeThreshold)
Float getContextValue() const
Float getMovementVelocity() const
virtual bool loadModelFromFile(std::fstream &file)
bool setSwipeDirection(const unsigned int swipeDirection)
UINT getNumSamples() const
Float getMovementThreshold() const
SwipeDetector & operator=(const SwipeDetector &rhs)
This class implements a basic swipe detection classification algorithm.
bool getSwipeDetected() const
bool copyBaseVariables(const Classifier *classifier)
bool loadBaseSettingsFromFile(std::fstream &file)
bool setHysteresisThreshold(const Float hysteresisThreshold)
UINT getNumDimensions() const
virtual bool deepCopyFrom(const Classifier *classifier)
virtual bool saveModelToFile(std::fstream &file) const
Vector< MinMax > getRanges() const
Float getSwipeIntegrationCoeff() const
virtual bool train_(ClassificationData &trainingData)
Float getSwipeThreshold() const
bool scale(const Float minTarget, const Float maxTarget)
Float getSwipeValue() const
bool setSwipeIndex(const unsigned int swipeIndex)