def extract(self, fileName):
     f = open(fileName, 'r')
     headers = f.readline().split()
     headers = jm.getFileHeader(headers)
     jointsIndices = self.extractor.getJointsIndices(headers)
     frontDirections = []
     centers = []
     sholderVec = []
     for line in f:
         lineInFloats=[float(v) for v in line.split()]
         centers.append(ae.calcJointsAverage(lineInFloats, jointsIndices))
         shouldersVecOnXZPlan = ae.getVecBetweenJoints(headers, lineInFloats,\
                                'ShoulderRight_X', 'ShoulderLeft_X')
         #sholderVec.append(shouldersVecOnXZPlan)
         frontDirections.append([shouldersVecOnXZPlan[2], 0 , -shouldersVecOnXZPlan[0]])
         #frontDirections.append([1,0,0])
     frontDirections = zip(*ma.partsmovingAverage(zip(*frontDirections), 50, 1))
     movingDirections = np.diff(centers, axis=0)
     movingDirections = zip(*ma.partsmovingAverage(zip(*movingDirections), 50, 1))
     advancements = []
     for front, move in zip(frontDirections[:-1], movingDirections):
         if np.abs(front[0])/np.abs(front[2]) > 2:
             front = [ae.length(front),0,0]
         product = np.dot(front, move)
         advancements.append(product)
     return advancements
     """
Esempio n. 2
0
import utils.utils as ut
import utils.MovingAverage as ma

file = 'AMCs/598.amc'
joint = 'rtibia'
list = getAMCperiod(joint, file)
stride = list[112:251] 
list = ut.alignByMax(stride)
#list = ut.alignByMax(list)
noiseStdFactor = 0.04
amplitude = np.max(list) - np.min(list)
var = (amplitude*noiseStdFactor)**2
print var
partsAmount = 16
noisy_parts = createParts(list, True, partsAmount, var)
parts = ma.partsmovingAverage(noisy_parts)

frameSize = math.ceil(np.sqrt(len(parts)))
fig = plt.figure()
for i,part in enumerate(parts):
    curr = fig.add_subplot(frameSize,  frameSize, i+1)
    curr.plot(part)
        
merged, mergedDes = loop.stitch(parts)
print(len(merged))
bestFeatures = merged[-1]
des = mergedDes[-1]
outOff, listOff = ut.alignByBig(bestFeatures, list)
plt.figure()
plt.plot(xrange(listOff, listOff+len(list)), list, color='b')
plt.plot(xrange(outOff, outOff+len(bestFeatures)), bestFeatures, color='r')
def evalParams(partsAmount):
    noiseStdFactor = 0.02       
    stances = {}
    swings = {}
    for subject in subjects:
        for index in xrange(8):
            try:
                input = getAMCInput(joint, subject, index)
            except:
                continue
            amplitude = np.max(input) - np.min(input)
            var = (amplitude*noiseStdFactor)**2
            for stitch in xrange(stitchesNum):
                try:
                    noisy_parts = st.createParts(input, False, partsAmount, var)
                except:
                    continue
                parts = ma.partsmovingAverage(noisy_parts)
                stitched_parts, des = loop.stitch(parts)
                if(len(stitched_parts) == len(parts)):
                    continue
                stitched = stitched_parts[-1]
                input = stitched
                tmpSwings, tmpStances = getSwingsAndStances(input)
                key = (stitch, subject) 
                for stanceLen, swingLen in zip(tmpStances, tmpSwings):
                    if(stanceLen > 55):
                        stances[key] = stances.get(key, []) + [stanceLen]
                    if(swingLen > 35):
                        swings[key] = swings.get(key, []) + [swingLen]
    stancesCV = []
    swingsCV = []
    strideCV = []
    strideLengths = {}
    stancesLengths = {}
    swingsLengths = {}
    for stitch in xrange(stitchesNum):
        for subject in subjects:
            key = (stitch, subject)
            stance = None
            if not(key in stances and key in swings):
                continue
            stance = stances[key]
            cv = getCV(stance)
            if not math.isnan(cv) and 0 < cv :
                stancesCV.append(cv)
            stancesLengths[subject] = stancesLengths.get(subject, []) + stance
            
            swing = swings[key]
            cv = getCV(swing)
            if not math.isnan(cv) and 0 < cv :
                swingsCV.append(cv)
            swingsLengths[subject] = swingsLengths.get(subject, []) + swing
            
            strideLength = [x+y for x,y in zip(stance, swing)]
            cv = getCV(strideLength)
            if not math.isnan(cv) and 0 < cv :
                strideCV.append(cv)
            strideLengths[subject] = strideLengths.get(subject, []) + strideLength
    
    stancesLengths = [stancesLengths[subject] for subject in stancesLengths]
    swingsLengths = [swingsLengths[subject] for subject in swingsLengths]
    strideLengths = [strideLengths[subject] for subject in strideLengths]
    strideReference = [115.61538461538461, 113.66666666666667, 116.11111111111111, 126.375]
    strideMeans = [np.mean(seq) for seq in strideLengths]
    finalGrade = np.mean([np.abs(y-x)/x for x,y in zip(strideReference, strideMeans)])   
    stanceCVmean = np.mean(stancesCV)
    swingCVmean = np.mean(swingsCV)
    strideCVsMean = np.mean(strideCV)
    titles = ['Subject: ' + str(x) for x in subjects]
    #plotParts(stancesLengths, 'Stance number', 'Stance time(in frames)', titles)
    #plotParts(swingsLengths, 'Swing number', 'Swing time(in frames)', titles)
    #plotParts(strideLengths, 'Stride number', 'Stride time(in frames)', titles)
    print 'partsAmount', partsAmount
    print stanceCVmean, [np.mean(seq) for seq in stancesLengths]
    print  swingCVmean, [np.mean(seq) for seq in swingsLengths]
    print strideCVsMean, strideMeans
    return finalGrade