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))
Exemple #2
0
def train(trainset):

    expression = {}
    features = {}
    const = 0
    Max = 0
    Min = None
    count = 0
    print(">>> Loading scores and deviations, this will take hours and may eat all you memory")

    for query in trainset:
        print(">>> Loading: {0}".format(query))
        score = db.getScore1(query)
        deviations = db.getDeviation1(query)
        alignment = Alignment(score, deviations)
        melody = alignment.melody()
        #segments = structure.newSegmentation(tools.parseScore(melody))
        segments = structure.noteLevel(tools.parseScore(melody))
        const += len(segments)
        lengths = sum([len(s) for s in segments])
        m = max([len(s) for s in segments])
        mi = min([len(s) for s in segments])
        if m > Max:
            Max = m
        if not Min:
            Min = mi
        if mi < Min:
            Min = mi
        print('>>> Extracting features')
        expression[query] = performancefeatures.vanDerWeijExpression(alignment, segments)
        features[query] = scorefeatures.vanDerWeijFeatures(melody, segments)
        count += 1
        print('{0}/{1} done'.format(count, len(trainset)))

    print("Done, {0} segments found with an average length of: {1} (min: {2} max: {3})".format(const, lengths / float(const), Min, Max))
    tools.saveFeatures(features, expression)