def evaluate(self, p):
        if self.currSignal is None:
            print "please capture some data firstly."
            exit(1)
        currSignal1 = self.filter(self.currSignal)
        upperIndex = int(p * self.sampleSize)
        lowerIndex = upperIndex - len(self.currSignal)
        lowerIndex = 0 if lowerIndex < 0 else lowerIndex

        if self.locationPrefer == 1:
            totalDist = 0
            totalPos  = 0
            dataCount = len(self.data)
            for line in self.data:
                dist, pos = dtwCalculate(currSignal1, line[lowerIndex: upperIndex])
                totalDist += dist
                totalPos  += pos
            return totalDist / dataCount, totalPos / dataCount + lowerIndex
        else:
            bestDist = 999999999
            bestPos  = 0
            for line in self.data:
                dist, pos = dtwCalculate(currSignal1, line[lowerIndex: upperIndex])
                if dist < bestDist:
                    bestPos = pos
                    bestDist = dist
            return bestDist, 0
    def eval(self, p):
        if self.currSignal is None:
            print "please capture some data firstly."
            exit(1)

        lower = 0 if p - self.lower < 0 else p - self.lower
        upper = 1 if p - self.upper > 1 else p + self.upper
        lowerIndex = int(self.standardLength * lower)
        upperIndex = int(self.standardLength * upper)
        if self.locationPrefer == 1:
            totalDist = 0
            totalPos  = 0
            dataCount = len(self.data)
            for line in self.data:
                dist, pos = dtwCalculate(self.currSignal, line[lowerIndex: upperIndex])
                totalDist += dist
                totalPos  += pos
            return totalDist / dataCount, totalPos / dataCount + lowerIndex
        else:
            bestDist = 999999999
            bestPos  = 0
            for line in self.data:
                dist, pos = dtwCalculate(self.currSignal, line[lowerIndex: upperIndex])
                if dist < bestDist:
                    bestPos = pos
                    bestDist = dist
            return bestDist, float(bestPos + lowerIndex + len(self.lastSignal)) / self.standardLength
from config.Config import config
from data.DataLoader import DataLoader
from algorithm.Dtw import dtwCalculate

testdatafilename = config.data['datafile']['test']
dl = DataLoader()
line1 = dl.read(testdatafilename[0])

sub = line1[100:105]

dist, position= dtwCalculate(sub, line1)