def getBestDecisionStump(feature, posSampleList, negSampleList, sumOfPosWeight, sumOfNegWeight):
    sampleImageList = posSampleList + negSampleList
    length = len(sampleImageList)
    sumOfLeftPosWeight = 0.0  # just represent the sum of positive weights on the left of threshold
    sumOfLeftNegWeight = 0.0  # just represent the sum of negative weights on the left of threshold
    threshold = (sampleImageList[0].getFeatureValue() + sampleImageList[1].getFeatureValue()) / 2
    polarity = POS_POLARITY
    error = sumOfPosWeight + sumOfNegWeight

    # calculate feature values for all training samples based on given feature
    for sampleImage in sampleImageList:
        image = sampleImage.getImage()
        integralImage = Util.getIntegralImage(image)
        featureValue = Util.featureExtracion(integralImage, feature)
        sampleImage.setFeatureValue(featureValue)

    # sort feature bundle list (quick sort in util)
    Util.quickSort(sampleImageList, 0, length - 1)

    # (for loop) get the threshold and polarity with lowest error of weight sum
    for i in xrange(length - 1):
        sampleImage = sampleImageList[i]
        sampleType = sampleImage.getSampleType()
        featureValue = sampleImage.getFeatureValue()
        nextFeatureValue = sampleImageList[i + 1].getFeatureValue()
        weight = sampleImage.getWeight()

        newThreshold = (featureValue + nextFeatureValue) / 2.0
        newPolarity = POS_POLARITY
        newError = error

        if sampleType == Util.FACE:
            sumOfLeftPosWeight += weight
        else:
            sumOfLeftNegWeight += weight

        error1 = sumOfLeftPosWeight + (sumOfNegWeight - sumOfLeftNegWeight)
        error2 = (sumOfPosWeight - sumOfLeftPosWeight) + sumOfLeftNegWeight
        if error1 > error2:
            newError = error2
        else:
            newPolarity = NEG_POLARITY
            newError = error1

        if newError < error:
            threshold = newThreshold
            polarity = newPolarity
            error = newError

    return DecisionStump(feature, threshold, polarity, error)
 def isFace(self, integralWindow):
     featureValue = Util.featureExtracion(integralWindow, self.feature)
     if (self.polarity * featureValue) < (self.polarity * self.threshold):
         return self.alpha
     else:
         return 0.0