26 RegisterClassifierModule< SwipeDetector > SwipeDetector::registerModule(
"SwipeDetector");
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;
144 classLabels.resize( 2 );
147 nullRejectionThresholds.resize(2,0);
164 errorLog <<
"train_(trainingData &labelledTrainingData) - Training data has zero samples!" << endl;
168 numInputDimensions = N;
170 classLabels.resize( 2 );
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!" << 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 <<
")" << 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 );
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;
261 lastX.resize( 3, 0 );
263 swipeDetected =
false;
266 if( swipeDirection == POSITIVE_SWIPE ){
270 if( swipeDirection == NEGATIVE_SWIPE ){
275 thresholdDetector.
setAnalysisMode( GRT::ThresholdCrossingDetector::DERIVATIVE_ANALYSIS_MODE );
279 contextFilter.
init( contextFilterSize, 1 );
288 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << endl;
293 file<<
"GRT_SWIPE_DETECTION_MODEL_FILE_V1.0\n";
297 errorLog <<
"saveModelToFile(fstream &file) - Failed to save classifier base settings to file!" << endl;
303 file <<
"SwipeIndex: " << swipeIndex << endl;
304 file <<
"ContextFilterSize: " << contextFilterSize << endl;
305 file <<
"SwipeIntegrationCoeff: " << swipeIntegrationCoeff << endl;
306 file <<
"MovementIntegrationCoeff: " << movementIntegrationCoeff << endl;
307 file <<
"SwipeThreshold: " << swipeThreshold << endl;
309 file <<
"HysteresisThreshold: " << hysteresisThreshold << endl;
310 file <<
"SwipeThreshold: " << swipeThreshold << endl;
311 file <<
"MovementThreshold: " << movementThreshold << endl;
312 file <<
"SwipeThreshold: " << swipeThreshold << endl;
324 numInputDimensions = 0;
330 errorLog <<
"loadModelFromFile(string filename) - Could not open file to load model" << endl;
339 if(word !=
"GRT_SWIPE_DETECTION_MODEL_FILE_V1.0"){
340 errorLog <<
"loadModelFromFile(string filename) - Could not find Model File Header" << endl;
346 errorLog <<
"loadModelFromFile(string filename) - Failed to load base settings from file!" << endl;
353 if( word !=
"SwipeIndex:" ){
354 errorLog <<
"loadModelFromFile(string filename) - Could not load the SwipeIndex!" << endl;
360 if( word !=
"ContextFilterSize:" ){
361 errorLog <<
"loadModelFromFile(string filename) - Could not load the ContextFilterSize!" << endl;
364 file >> contextFilterSize;
367 if( word !=
"SwipeIntegrationCoeff:" ){
368 errorLog <<
"loadModelFromFile(string filename) - Could not load the SwipeIntegrationCoeff!" << endl;
371 file >> swipeIntegrationCoeff;
374 if( word !=
"MovementIntegrationCoeff:" ){
375 errorLog <<
"loadModelFromFile(string filename) - Could not load the MovementIntegrationCoeff!" << endl;
378 file >> movementIntegrationCoeff;
381 if( word !=
"SwipeThreshold:" ){
382 errorLog <<
"loadModelFromFile(string filename) - Could not load the SwipeThreshold!" << endl;
385 file >> swipeThreshold;
388 if( word !=
"HysteresisThreshold:" ){
389 errorLog <<
"loadModelFromFile(string filename) - Could not load the HysteresisThreshold!" << endl;
392 file >> hysteresisThreshold;
395 if( word !=
"SwipeThreshold:" ){
396 errorLog <<
"loadModelFromFile(string filename) - Could not load the SwipeThreshold!" << endl;
399 file >> swipeThreshold;
402 if( word !=
"MovementThreshold:" ){
403 errorLog <<
"loadModelFromFile(string filename) - Could not load the MovementThreshold!" << endl;
406 file >> movementThreshold;
409 maxLikelihood = DEFAULT_NULL_LIKELIHOOD_VALUE;
410 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
411 classLikelihoods.resize(numClasses,DEFAULT_NULL_LIKELIHOOD_VALUE);
412 classDistances.resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
422 return swipeDetected;
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!" << endl;
471 this->swipeDirection = swipeDirection;
478 this->swipeThreshold = swipeThreshold;
484 this->hysteresisThreshold = hysteresisThreshold;
490 this->movementThreshold = movementThreshold;
496 this->swipeIntegrationCoeff = swipeIntegrationCoeff;
virtual bool deepCopyFrom(const Classifier *classifier)
virtual bool train_(ClassificationData &trainingData)
double getContextValue() const
bool setThresholdCrossingMode(const UINT thresholdCrossingMode)
bool setDetectionTimeoutMode(const UINT detectionTimeoutMode)
bool getThresholdCrossingDetected() const
bool copyBaseVariables(const Classifier *classifier)
double getMovementThreshold() const
UINT getNumDimensions() const
bool loadBaseSettingsFromFile(fstream &file)
UINT getNumSamples() const
bool setAnalysisMode(const UINT analysisMode)
bool setLowerThreshold(const double lowerThreshold)
bool setSwipeDirection(const unsigned int swipeDirection)
bool update(const double x)
SwipeDetector & operator=(const SwipeDetector &rhs)
bool setMovementThreshold(const double movementThreshold)
virtual bool saveModelToFile(fstream &file) const
bool saveBaseSettingsToFile(fstream &file) const
double scale(const double &x, const double &minSource, const double &maxSource, const double &minTarget, const double &maxTarget, const bool constrain=false)
double getSwipeIntegrationCoeff() const
bool setUpperThreshold(const double upperThreshold)
bool scale(const double minTarget, const double maxTarget)
bool getSwipeDetected() const
double getMovementVelocity() const
virtual bool predict_(VectorDouble &inputVector)
bool setSwipeThreshold(const double swipeThreshold)
This class implements a basic swipe detection classification algorithm.
virtual ~SwipeDetector(void)
double getSwipeValue() const
double getSwipeThreshold() const
bool setSwipeIndex(const unsigned int swipeIndex)
bool setHysteresisThreshold(const double hysteresisThreshold)
vector< MinMax > getRanges() const
double getAnalysisValue() const
virtual bool loadModelFromFile(fstream &file)
string getClassifierType() const
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)
bool setSwipeIntegrationCoeff(const double swipeIntegrationCoeff)
double getHysteresisThreshold() const
bool setContext(const bool context)
bool setHysteresisThreshold(const double hysteresisThreshold)