def getFalseNegativesInTranscription(trans, notFound, origOnsets, transOnsets):
    '''
    This function takes in the transcribed syllables, the one not found, the onset positions and returns
    the list of the tuples where each tuple is of the format (<position>, <pattern>)
    '''

    falseNegs = []

    for tup in notFound:
        # The start position of the ground truth pattern..
        sOrig = tup[0] - 1
            
        # Getting the number of syllables in the original score pattern 
        #ptrOrig = tup[1].strip() # Have spaces at the end
        #ptrOrigLen = len(ptrOrig.split(' '))
        ptrOrigLen = len(tup[1])

        # Getting the start and end time of the transcription
        sTime = origOnsets[sOrig] 
        eTime = origOnsets[sOrig + ptrOrigLen - 1]

        if sTime >= eTime:
            print 'analysis.getFalseNegativesInTranscription:: sTime greater or equal than the eTime:' + str(sTime) + ' and ' + str(eTime)
            sys.exit()

        # Getting the closest indices for the start and end time in the 
        sIndex = ut.findClosestIndex(sTime, transOnsets, 0, len(transOnsets) - 1)
        eIndex = ut.findClosestIndex(eTime, transOnsets, 0, len(transOnsets) - 1)

        falseNegs.append((sIndex + 1, trans[sIndex : eIndex + 1]))

    return falseNegs
def getIndicesInTrans(origOnset, transOnset, dictPtrIndices):
    """
    Inputs the onset locations of ground truth (origOnset), transcribed score (transOnset) and the start and end locations
    of the pattern in the ground truth and returns the list of indices for the closest onset positions in the transcription
    """
    result = {}
    retRes = []

    print 'The number of patterns in the GT are:' + str(len(dictPtrIndices))

    for i, ons in enumerate(transOnset):
        closestInOrig = ut.findClosestIndex(ons, origOnset, 0, len(origOnset)-1)
        for key, val in dictPtrIndices.iteritems():
            if closestInOrig in val:
                if key in result:
                    result[key].append(i)
                else:
                    result[key] = [i]
    for key, val in result.iteritems():
        if max(val) - min(val) + 1 != len(val):
            print 'Something wrong with the positions found'
            print 'key:' + str(key)
            print 'val:' + str(val)
        retRes.append(val)
    return retRes
def getPatternsInTransInGTPos(masterData, queryList):
    '''
    This function takes in the masterData and queryList and returns pattern wise list of patterns in transcribed data for 
    each composition for the positions of the where there is a pattern in the GT. This is to analyze the errors in the transcription
    '''
    res = []
    for query in queryList:
        qLen = len(query)
        qRes = []
        transQ = []
        for compData in masterData:
            uniqMatchesGT = rlcs.getGTPatterns(compData[1][0],query)
            #print 'In GT for composition ' + str(compData[2] + ':' + str(uniqMatchesGT))

            # getting the onset times of the pattern boundaries
            GTOnsets = compData[1][1] # array of onsets in the ground truth of the composition
            transOnsets = compData[0][1] # array of the onsets in the transcribed data of the composition
            for match in uniqMatchesGT:

                #print 'Working for:' + str(match)

                GTStartIndex = match[0] - 1 # start index of the pattern in the GT
                GTEndIndex = GTStartIndex + qLen - 1 # end index of the pattern in the GT

                #print 'Starting index in GT:' + str(GTStartIndex)
                #print 'Ending index in GT:' + str(GTEndIndex)

                transStartIndex = ut.findClosestIndex(GTOnsets[GTStartIndex], transOnsets, 0, len(transOnsets)-1)
                transEndIndex = ut.findClosestIndex(GTOnsets[GTEndIndex], transOnsets, 0, len(transOnsets)-1)

                #print 'Starting index in Trans:' + str(transStartIndex)
                #print 'Ending index in iTrans:' + str(transEndIndex)
                if compData[0][0][transStartIndex] == 'NA' and  compData[0][0][transStartIndex+1] == 'KI' and compData[0][0][transStartIndex+2] == 'TA' and compData[0][0][transStartIndex+3] == 'TA' and transEndIndex-transStartIndex+1 == 4:
                    print compData[2]
                qRes.append(transEndIndex - transStartIndex + 1)
                transQ.append(compData[0][0][transStartIndex:transEndIndex + 1])
        res.append((query, qRes, transQ))
    return res