def runWithParameters(argv):
    
    if len(argv) != 6:
            print ("Tool to get alignment accuracy of of one jingju aria with different parameters ")
            print ("usage: {}   <pathToRecordings> <withScores> <deviation_INSeconds> <recordingName_noExtension> <withVocalPrediciton> ".format(argv[0]) )
            sys.exit()
    
    rootURI = argv[1]
#     rootURI = '/Users/joro/Documents/Phd/UPF/arias/'
    
#     URIrecordingNoExt =  rootURI + 'laosheng-erhuang_04'
#     URIrecordingNoExt =  rootURI + 'laosheng-xipi_02'
#     URIrecordingNoExt =  rootURI + 'dan-xipi_01'
#     URIrecordingNoExt =  rootURI + 'dan-xipi_02'

    URIrecordingNoExt = rootURI + argv[4]
    lyricsTextGrid = URIrecordingNoExt + '.TextGrid'

    # load total # different sentences + their rspective ts
#         fromTss, toTss = loadSectionTimeStamps(sectionAnnoURI)
    listSentences = divideIntoSentencesFromAnno(lyricsTextGrid) #uses TextGrid annotation to derive structure. TODO: instead of annotation, uses score
    
    withScores  = int(argv[2])
    ParametersAlgo.DEVIATION_IN_SEC = float(argv[3])
    musicXMLParser = None
    
    if withScores:
        musicXmlURI = URIrecordingNoExt + '_score.xml'
        musicXMLParser = MusicXMLParser(musicXmlURI, lyricsTextGrid)
    
    correctDuration = 0
    totalDuration = 0
    accuracyList = []
    
    withVocalPrediction = int(argv[5])
#     for whichSentence, currSentence in  reversed(list(enumerate(listSentences))):
    for whichSentence, currSentence in  enumerate(listSentences):
        currCorrectDuration, currTotalDuration = doitOneChunkAlign(URIrecordingNoExt, musicXMLParser,  whichSentence, currSentence, withScores, withVocalPrediction)  
        
        currAcc = currCorrectDuration / currTotalDuration
        accuracyList.append(currAcc)
        print "sentence {}: acc ={:.2f}".format(whichSentence, currAcc)
        
        correctDuration += currCorrectDuration
        totalDuration += currTotalDuration

##### TRICK: take only first three sentences:
#         if whichSentence == 2:
#             break
    print "final: {:.2f}".format(correctDuration / totalDuration * 100)     
    import matplotlib.pyplot as plt
    plt.plot(accuracyList, 'ro')
    plt.show()  
def test_oracle_jingju(URIrecordingNoExt,  whichSentence, fromPhonemeIdx, toPhonemeIdx):
    '''
    read phoneme-level ground truth and test with dan-xipi_02
    '''
    
    ANNOTATION_EXT = '.TextGrid'
    listSentences = divideIntoSentencesFromAnno(URIrecordingNoExt + ANNOTATION_EXT) #uses TextGrid annotation to derive structure. TODO: instead of annotation, uses score
    
    withSynthesis = False
    currSentence = listSentences[whichSentence]
    
    # consider only part of audio
  
    fromTs = currSentence[0]
    toTs = currSentence[1]

    
    lyrics = loadLyricsFromTextGridSentence(currSentence)
    
    tokenLevelAlignedSuffix = '.syllables_oracle'
    detectedAlignedfileName = URIrecordingNoExt + '_' + str(fromTs) + '_' + str(toTs) + '_'  + tokenLevelAlignedSuffix
    
    if os.path.isfile(detectedAlignedfileName):
        print "{} already exists. No decoding".format(detectedAlignedfileName)
        
        from Utilz import readListOfListTextFile
        detectedTokenList  = readListOfListTextFile(detectedAlignedfileName)
        
    else:
        detectedTokenList = decodeWithOracle(lyrics, URIrecordingNoExt, fromTs, toTs, fromPhonemeIdx, toPhonemeIdx)
          
        if not os.path.isfile(detectedAlignedfileName):
            from PraatVisualiser import tokenList2TabFile
            detectedAlignedfileName =  tokenList2TabFile(detectedTokenList, URIrecordingNoExt, tokenLevelAlignedSuffix)
          
    # eval on phrase level
    evalLevel = 2
    
    fromSyllable = currSentence[2]
    toSyllable = currSentence[3]
    

    correctDuration, totalDuration = _evalAccuracy(URIrecordingNoExt + ANNOTATION_EXT, detectedTokenList, evalLevel, fromSyllable, toSyllable )
    print "accuracy= {}".format(correctDuration / totalDuration)
    
    return detectedTokenList
def doitOneChunkTest():
    '''
    meant to be run for withScores = 0
    '''
    URIrecordingNoExt = os.path.abspath('dan-xipi_01')
    URIrecordingNoExt = '/Users/joro/Documents/Phd/UPF/arias_dev_01_t_70/dan-xipi_02'
     
    lyricsTextGrid = URIrecordingNoExt + '.TextGrid'
    whichSentence = 0
         
    listSentences = divideIntoSentencesFromAnno(lyricsTextGrid) #uses TextGrid annotation to derive structure. TODO: instead of annotation, uses score
    musicXMLParser = 'dummy'
    sentence = listSentences[whichSentence]

#   meant to be run for withScores = 0
    withScores = 0
    withVocalPrediction = 0
    currCorrectDuration, currTotalDuration = doitOneChunkAlign(URIrecordingNoExt, musicXMLParser,  whichSentence, sentence, withScores, withVocalPrediction)  
 
    currAcc = currCorrectDuration / currTotalDuration
    print "sentence {}: acc ={:.2f}".format(whichSentence, currAcc)
def readPinYinTest():
    URIrecordingNoExt = '/Users/joro/Documents/Phd/UPF/ariasAnnotated/2-16_伴奏:听兄言不由我花容惊变.TextGrid'
    allSentences = divideIntoSentencesFromAnno(URIrecordingNoExt)
    print allSentences