def evalParams(m1=MEAN_COEFF, m2=STD_COEFF,  epsilon=EPSILON_FACTOR,gap=GAP_FACTOR, overlap=OVERLAP_FACTOR):       
    sum = 0
    for stitch in xrange(stitchesNum):
        data = []
        tags = []
        for subject in subjects:
            for index in xrange(8):
                try:
                    input = getAMCInput(joint, subject, index)
                except:
                    continue
                parts = st.createParts(input, partsAmount)
                stitched = st.stitch(parts, m1, m2, epsilon,gap, overlap)
                #plt.figure()
                #plt.plot(stitched)
                periods = pr.breakToPeriods(stitched)
                periods = ut.alignByMaxMany(periods)
                periods = inter.getUniformSampledVecs(periods, 100)
                data = data + periods
                tags = tags + [subject]*len(periods)
                #st.plotParts(periods)
        
        cl = KNeighborsClassifier()
        cl.n_neighbors = 5
        cl.weights = 'distance' 
        testSize = 1
        score = crossValidate(cl, data, tags, testSize, testAmount)
        #print str(m2)+' '+ str(score)
        sum+=score
    score = float(sum)/stitchesNum
    scores[m1, m2] = score
    return score
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