def test(f_discretization=10, e_discretization=30, indep=False, selection=None, subset=None, corpus=None, smoothing=None, sensitivity=5, segmentation='reasonable'):
    if not selection:
        selection = (db.select())
    score = db.getScore1(selection)
    if not corpus:
        (f, e, m) = tools.chooseFeatures()
    else:
        (f, e, m) = tools.loadFeatures(corpus)
    if m['version'] != sf.version:
        print("Scorefeatures versions don't match! Corpus version: {0} Scorefeatures version: {1}".format(m['version'], sf.version))
        exit(0)
    if not subset:
        # Select a subset by hand
        subset = selectSubset(m['featureset'])
    print('\n\tPerforming {0}'.format(selection))
    print('\tFeatures version: {0}'.format(m['version']))
    print('\tFeatureset used: [', end=' ')
    print(', '.join([m['featureset'][i] for i in subset]), end=' ')
    print(']')
    print('\tScorefeatures discretization: {0}\n\tExpression discretization: {1}'.format(f_discretization, e_discretization))
    print('\tSensitivity: {0}'.format(sensitivity))
    print('\tSmoothing: {0}'.format(smoothing))
    print('\tCorpus: {0}\n'.format(corpus))
    if indep:
        hmm = HMM_indep(2, smoothing)
    else:
        hmm = HMM(2, smoothing)

    trainHMM(hmm, f, e, f_discretization, e_discretization, subset=subset, ignore=selection, sensitivity=sensitivity)
    #trainHMM(hmm, f, e, f_discretization, e_discretization, subset=subset)
    hmm.storeInfo('hmm2.txt')
    print("Loading score")
    melodyscore = Score(score).melody()
    melody = tools.parseScore(melodyscore)
    # Segmentate the the score
    print("Analysing score")
    if segmentation == 'reasonable':
        print('Using reasonable segmentation')
        onset = structure.reasonableSegmentation(melody)
    elif segmentation == 'new':
        print('Using new segmentation')
        onset = structure.newSegmentation(melody)
    elif segmentation == 'notelevel':
        print('Using notelevel segmentation')
        onset = structure.noteLevel(melody)

    #onset = structure.groupings(structure.list_to_tree(structure.first_order_tree(structure.onset, melody, 0.1)), 1)
    #namelist = []
    #for group in onset:
    #  namelist.append([leaf.name() for leaf in group])
    #print namelist

    (p, expression) = render(melodyscore, onset, hmm, f_discretization, e_discretization, subset, sensitivity=sensitivity)

    print("Done, resulting expression(with a probability of {1}): {0}".format(expression, p))

    performance = perform.perform(score, melodyscore, onset, expression, converter=melody)
    playPerformance(selection, performance, expression, onset, '{0}_{1}_on_{2}_discret_{3}-{4}_smoothing_{5}'.format(\
        selection[0], selection[1], corpus, f_discretization, e_discretization, smoothing))
def loadperformance():
    (selection, expression) = tools.loadPerformance()
    score = db.getScore1(selection)
    melodyscore = Score(score).melody()
    melody = tools.parseScore(melodyscore)
    segmentation = structure.reasonableSegmentation(melody)
    performance = perform.perform(score, Score(score).melody(), segmentation, expression, converter=melody)
    seq = Sequencer()
    seq.play(performance)
def plotCorpus():
    (f, e, m) = tools.chooseFeatures()
    d = input('Discretization? ')
    if not d == '':
        s = input('Sensitivity? [5]')
        if s == '':
            s = 5.0
        d = float(d)
        s = float(s)
    else: d = None

    for work in f:
        print('Plotting and saving {0}'.format(work))
        expression = e[work]
        if d:
            expression = [undiscretize_expression(discretize_expression(x, d, sensitivity=s), d, sensitivity=s) for x in expression]
        score = db.getScore1(work)
        segmentation = structure.reasonableSegmentation(tools.parseScore(Score(score).melody()))
        visualize(segmentation, expression, [0, 1, 2], '{0}-{1}-{2}'.format(work[0], work[1], work[2]), '{0}-{1} by {2}'.format(work[0], work[1], work[2]))
 elif a[1] == 'align':
     import train
     set = [db.select()]
     train.train(set)
     exit(0)
 elif a[1] == 'plotcorpus':
     plotCorpus()
     exit(0)
 elif a[1] == 'plot':
     (selection, expression) = tools.loadPerformance()
     if selection == None:
         selection = db.select()
     score = db.getScore1(selection)
     melodyscore = Score(score).melody()
     melody = tools.parseScore(melodyscore)
     segmentation = structure.reasonableSegmentation(melody)
     visualize(segmentation, expression)
     exit(0)
 elif a[1] == 'corpora':
     for x in tools.datasets():
         print('Name:\t[{0}]\tInfo:\t{1}'.format(x, tools.corpusInfo(x)))
     exit(0)
 elif a[1] == 'corpusinfo':
     choice = util.menu("Select corpus", tools.datasets())
     print(tools.corpusInfo(tools.datasets()[choice]))
     tools.extendedCorpusInfo(tools.datasets()[choice])
     exit(0)
 elif a[1] == 'features':
     s = db.select()
     score = Score(db.getScore1(s))
     melodyscore = score.melody()