21 #define GRT_DLL_EXPORTS 27 const std::string SwipeDetector::id =
"SwipeDetector";
36 this->swipeIndex = swipeIndex;
37 this->swipeThreshold = swipeThreshold;
38 this->hysteresisThreshold = hysteresisThreshold;
39 this->swipeDirection = swipeDirection;
40 this->useScaling = useScaling;
42 supportsNullRejection =
false;
44 classifierMode = STANDARD_CLASSIFIER_MODE;
46 swipeIntegrationCoeff = 0.92;
47 movementIntegrationCoeff = 0.90;
48 movementThreshold = 3000;
49 contextFilterSize = 5;
56 supportsNullRejection =
false;
57 classifierMode = STANDARD_CLASSIFIER_MODE;
59 swipeIntegrationCoeff = 0.92;
60 movementIntegrationCoeff = 0.90;
61 movementThreshold = 3000;
62 contextFilterSize = 5;
74 this->firstSample = rhs.firstSample;
75 this->swipeDetected = rhs.swipeDetected;
76 this->contextInput = rhs.contextInput;
77 this->swipeIndex = rhs.swipeIndex;
78 this->swipeDirection = rhs.swipeDirection;
79 this->contextFilterSize = rhs.contextFilterSize;
80 this->swipeIntegrationCoeff = rhs.swipeIntegrationCoeff;
81 this->movementIntegrationCoeff = rhs.movementIntegrationCoeff;
82 this->swipeThreshold = rhs.swipeThreshold;
83 this->hysteresisThreshold = rhs.hysteresisThreshold;
84 this->swipeVelocity = rhs.swipeVelocity;
85 this->movementVelocity = rhs.movementVelocity;
86 this->movementThreshold = rhs.movementThreshold;
87 this->contextFilteredValue = rhs.contextFilteredValue;
88 this->lastX = rhs.lastX;
89 this->thresholdDetector = rhs.thresholdDetector;
90 this->contextFilter = rhs.contextFilter;
100 if( classifier == NULL )
return false;
106 this->firstSample = ptr->firstSample;
107 this->swipeDetected = ptr->swipeDetected;
108 this->contextInput = ptr->contextInput;
109 this->swipeIndex = ptr->swipeIndex;
110 this->swipeDirection = ptr->swipeDirection;
111 this->contextFilterSize = ptr->contextFilterSize;
112 this->swipeIntegrationCoeff = ptr->swipeIntegrationCoeff;
113 this->movementIntegrationCoeff = ptr->movementIntegrationCoeff;
114 this->swipeThreshold = ptr->swipeThreshold;
115 this->hysteresisThreshold = ptr->hysteresisThreshold;
116 this->swipeVelocity = ptr->swipeVelocity;
117 this->movementVelocity = ptr->movementVelocity;
118 this->movementThreshold = ptr->movementThreshold;
119 this->contextFilteredValue = ptr->contextFilteredValue;
120 this->lastX = ptr->lastX;
121 this->thresholdDetector = ptr->thresholdDetector;
122 this->contextFilter = ptr->contextFilter;
131 bool SwipeDetector::init(
const unsigned int numInputDimensions){
136 this->numInputDimensions = numInputDimensions;
141 nullRejectionThresholds.
resize(2,0);
158 errorLog << __GRT_LOG__ <<
" Training data has zero samples!" << std::endl;
162 numInputDimensions = N;
167 nullRejectionThresholds.
resize(2,0);
173 trainingData.
scale(0, 1);
185 predictedClassLabel = 0;
187 swipeDetected =
false;
190 errorLog << __GRT_LOG__ <<
" SwipeDetector Model Not Trained!" << std::endl;
194 if( inputVector.
getSize() != numInputDimensions ){
195 errorLog << __GRT_LOG__ <<
" The size of the input vector (" << inputVector.
getSize() <<
") does not match the num features in the model (" << numInputDimensions <<
")" << std::endl;
200 for(UINT n=0; n<numInputDimensions; n++){
201 inputVector[n] =
scale(inputVector[n], ranges[n].minValue, ranges[n].maxValue, 0, 1);
205 if( classLikelihoods.size() != numClasses ) classLikelihoods.
resize(numClasses,0);
206 if( classDistances.size() != numClasses ) classDistances.
resize(numClasses,0);
210 movementVelocity *= movementIntegrationCoeff;
211 for(
unsigned int i=0; i<numInputDimensions; i++){
212 if( i != swipeIndex )
213 movementVelocity += GRT::SQR(inputVector[i]-lastX[i]);
215 }
else firstSample =
false;
219 swipeVelocity = (swipeVelocity*swipeIntegrationCoeff) + inputVector[swipeIndex];
220 thresholdDetector.update( swipeVelocity );
223 contextFilteredValue = contextFilter.filter( contextInput ? 1 : 0 );
226 swipeDetected = thresholdDetector.getThresholdCrossingDetected() && movementVelocity < movementThreshold && contextFilteredValue == 1;
229 predictedClassLabel = 1;
230 classLikelihoods[0] = 1.0;
231 classDistances[1] = 0;
233 predictedClassLabel = 2;
234 classLikelihoods[0] = 0.0;
235 classDistances[1] = 1;
253 movementVelocity = 0;
257 swipeDetected =
false;
260 if( swipeDirection == POSITIVE_SWIPE ){
261 thresholdDetector.setThresholdCrossingMode( GRT::ThresholdCrossingDetector::UPPER_THRESHOLD_CROSSING );
262 thresholdDetector.setUpperThreshold( swipeThreshold );
264 if( swipeDirection == NEGATIVE_SWIPE ){
265 thresholdDetector.setThresholdCrossingMode( GRT::ThresholdCrossingDetector::LOWER_THRESHOLD_CROSSING );
266 thresholdDetector.setLowerThreshold( swipeThreshold );
269 thresholdDetector.setAnalysisMode( GRT::ThresholdCrossingDetector::DERIVATIVE_ANALYSIS_MODE );
270 thresholdDetector.setDetectionTimeoutMode( GRT::ThresholdCrossingDetector::HYSTERESIS_THRESHOLD );
271 thresholdDetector.setHysteresisThreshold( hysteresisThreshold );
273 contextFilter.init( contextFilterSize, 1 );
282 errorLog << __GRT_LOG__ <<
" The file is not open!" << std::endl;
287 file<<
"GRT_SWIPE_DETECTION_MODEL_FILE_V1.0\n";
291 errorLog << __GRT_LOG__ <<
" Failed to save classifier base settings to file!" << std::endl;
297 file <<
"SwipeIndex: " << swipeIndex << std::endl;
298 file <<
"ContextFilterSize: " << contextFilterSize << std::endl;
299 file <<
"SwipeIntegrationCoeff: " << swipeIntegrationCoeff << std::endl;
300 file <<
"MovementIntegrationCoeff: " << movementIntegrationCoeff << std::endl;
301 file <<
"SwipeThreshold: " << swipeThreshold << std::endl;
303 file <<
"HysteresisThreshold: " << hysteresisThreshold << std::endl;
304 file <<
"SwipeThreshold: " << swipeThreshold << std::endl;
305 file <<
"MovementThreshold: " << movementThreshold << std::endl;
306 file <<
"SwipeThreshold: " << swipeThreshold << std::endl;
318 numInputDimensions = 0;
324 errorLog << __GRT_LOG__ <<
" Could not open file to load model" << std::endl;
333 if(word !=
"GRT_SWIPE_DETECTION_MODEL_FILE_V1.0"){
334 errorLog << __GRT_LOG__ <<
" Could not find Model File Header" << std::endl;
340 errorLog << __GRT_LOG__ <<
" Failed to load base settings from file!" << std::endl;
347 if( word !=
"SwipeIndex:" ){
348 errorLog << __GRT_LOG__ <<
" Could not load the SwipeIndex!" << std::endl;
354 if( word !=
"ContextFilterSize:" ){
355 errorLog << __GRT_LOG__ <<
" Could not load the ContextFilterSize!" << std::endl;
358 file >> contextFilterSize;
361 if( word !=
"SwipeIntegrationCoeff:" ){
362 errorLog << __GRT_LOG__ <<
" Could not load the SwipeIntegrationCoeff!" << std::endl;
365 file >> swipeIntegrationCoeff;
368 if( word !=
"MovementIntegrationCoeff:" ){
369 errorLog << __GRT_LOG__ <<
" Could not load the MovementIntegrationCoeff!" << std::endl;
372 file >> movementIntegrationCoeff;
375 if( word !=
"SwipeThreshold:" ){
376 errorLog << __GRT_LOG__ <<
" Could not load the SwipeThreshold!" << std::endl;
379 file >> swipeThreshold;
382 if( word !=
"HysteresisThreshold:" ){
383 errorLog << __GRT_LOG__ <<
" Could not load the HysteresisThreshold!" << std::endl;
386 file >> hysteresisThreshold;
389 if( word !=
"SwipeThreshold:" ){
390 errorLog << __GRT_LOG__ <<
" Could not load the SwipeThreshold!" << std::endl;
393 file >> swipeThreshold;
396 if( word !=
"MovementThreshold:" ){
397 errorLog << __GRT_LOG__ <<
" Could not load the MovementThreshold!" << std::endl;
400 file >> movementThreshold;
404 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
406 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
416 return swipeDetected;
420 return thresholdDetector.getAnalysisValue();
424 return swipeThreshold;
428 return hysteresisThreshold;
432 return movementVelocity;
436 return movementThreshold;
440 return contextFilteredValue;
444 return swipeIntegrationCoeff;
448 this->contextInput = context;
453 this->swipeIndex = swipeIndex;
460 if( swipeDirection != POSITIVE_SWIPE && swipeDirection != NEGATIVE_SWIPE ){
461 errorLog << __GRT_LOG__ <<
" Unknown swipeDirection!" << std::endl;
465 this->swipeDirection = swipeDirection;
472 this->swipeThreshold = swipeThreshold;
478 this->hysteresisThreshold = hysteresisThreshold;
484 this->movementThreshold = movementThreshold;
490 this->swipeIntegrationCoeff = swipeIntegrationCoeff;
bool saveBaseSettingsToFile(std::fstream &file) const
virtual bool predict_(VectorDouble &inputVector)
std::string getId() const
#define DEFAULT_NULL_LIKELIHOOD_VALUE
bool setMovementThreshold(const Float movementThreshold)
bool setSwipeIntegrationCoeff(const Float swipeIntegrationCoeff)
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)
static std::string getId()
virtual ~SwipeDetector(void)
bool setContext(const bool context)
bool setSwipeThreshold(const Float swipeThreshold)
Float getContextValue() const
Float getMovementVelocity() const
virtual bool save(std::fstream &file) const
bool setSwipeDirection(const unsigned int swipeDirection)
UINT getNumSamples() const
Float getMovementThreshold() const
SwipeDetector & operator=(const SwipeDetector &rhs)
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 load(std::fstream &file)
Vector< MinMax > getRanges() const
Float getSwipeIntegrationCoeff() const
virtual bool train_(ClassificationData &trainingData)
Float getSwipeThreshold() const
This class implements a basic swipe detection classification algorithm.
bool scale(const Float minTarget, const Float maxTarget)
Float getSwipeValue() const
This is the main base class that all GRT Classification algorithms should inherit from...
bool setSwipeIndex(const unsigned int swipeIndex)
Float scale(const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)