def flat_prior(p=None): model = train(annotations.corpus()) for key in model: model[key] = p if not p: model[key] = 1/float(len(model.keys())) return model
def cross_validate(folds=5): corpus = annotations.corpus() n = len(corpus) results = [] for i in range(folds): print 'Preparing fold {0} out of {1}'.format(i+1, folds) trainset = [] testset = [] n_test = int(n/float(folds)) for j in range(n_test): index = int(random.random() * (n-j)) testset += [corpus[index]] del corpus[index] trainset = corpus print 'Training model' model = train(trainset) print 'Done. Model: {0}'.format(model) print 'Evaluating model.' cross_ent = 0 for (a, parse) in testset: logP, N = loglikelihood(parse, model) cross_ent += -logP cross_ent /= float(n_test) print 'Done. Average cross-entropy: {0}'.format(cross_ent) results.append(cross_ent) corpus = trainset + testset result = sum(results)/float(len(results)) print 'All done. Resulting cross-entropy: {0}'.format(result)
def corpusAndParser(n=15): from jazzr.models import pcfg, treeconstraints from jazzr.corpus import annotations corpus = annotations.corpus() allowed = treeconstraints.train(corpus) allowed.append(['tie', 'tie', 'on']) model = pcfg.train(corpus) #model = None return annotations.loadAnnotations(), SimpleParser(n=n, model=model, corpus=True, allowed=allowed, tolerance=0.001, maxdepth=5)
def test2(): from jazzr.corpus import annotations corpus = annotations.corpus('explicitswing') for song, parse in corpus: print 'Name: {0}. Length {1}.'.format(song.name, len(song)) try: test(parse) except: print "Failed"
def run(): corpus = acorpus.corpus() for (a, p) in corpus: print a.name newA = convert(a) normalname = '-'.join(newA.name.split('-')[:-1]) part = newA.name.split('-')[-1:][0] newA.metadata['name'] = normalname newA.name = normalname acorpus.save_annotation('explicitswing', newA, midifile=acorpus.load_midifile('annotations', normalname), part=part)
def __init__(self, collection='explicitswing', n=15, beam=0.8, std=0.1, expected_logratio=0.0, model=None, allowed=None): if model == None: corpus = annotations.corpus(collection=collection) model = pcfg.train(corpus) self.allowed = treeconstraints.train(corpus) else: self.model = model self.allowed = allowed # Standard deviation expressed in proportion of beatlength self.std = std self.expected_logratio = expected_logratio super(StochasticParser, self).__init__(beam=beam, model=model, n=n)
from jazzr.corpus import midi, annotations corpus = annotations.corpus() names = [] for annotation, tree in corpus: name = '-'.join(annotation.name.split('-')[0:2]) if not name in names: names.append(name) for name in names: song, version, track, singletrack = midi.parsename(name) mf = midi.load(song, version, track, singletrack, collection='original') print mf.name for track in sorted(mf.keys()): print '\t{0}'.format(mf[track].name)