Ejemplo n.º 1
0
def benchmark(net, benchmark_dir):
    r"""
    Run a benchmark to gauge the quality of the ANN approach.

    This function takes a fully trained ANN as well as a directory
    containing only benchmark files as parameters.
    """
    tracks = read_standard_tracks
    hits = 0
    misses = 0
    # **note that there is one *chord context* per chord**
    contexts = [context for piece in os.listdir(benchmark_dir)
                        for track in tracks(os.path.join(benchmark_dir, piece))
                        for context in chord_contexts(track)]
    for context in contexts:
        preceding = context[0]
        theoretical_chord = physical2theoretical_chord(context[1])
        following = context[2]
        recent_notes = context[3]
        chord_index = context[4]
        limiting = context[5]
        best_config = _best_sane_chord(preceding, theoretical_chord, following,
                                       recent_notes, chord_index, limiting,
                                       net)
        LOG.info('Best config: {best_config}.'.format(**locals()))
        LOG.info('Truth: {0}'.format(context[1]))
        if best_config == context[1]:
            hits += 1
        else:
            misses += 1

    print('Hits: {hits}.\nMisses: {misses}'.format(**locals()))
    print('Accuracy: {ratio}'.format(ratio = float(hits) / (hits + misses)))
Ejemplo n.º 2
0
def dataset(data_dir):
    r"""
    Given a directory filled with data files, create a corresponding
    dataset.
    """
    data = SupervisedDataSet(NUMBER_INPUTS, SUPPORTED_FRETS)
    for piece in os.listdir(data_dir):
        LOG.info('Adding samples from piece: {p}'.format(p=piece))
        full_path = os.path.join(data_dir, piece)
        tracks = read_standard_tracks(full_path)
        for track in tracks:
            limiting_notes_generator = limiting_notes(track)
            try:
                next_limitation = limiting_notes_generator.next()
            except StopIteration:
                next_limitation = None
            for context in chord_contexts(track):
                context = (context[0],
                           context[1],
                           context[2],  # **physical** successors!
                           context[3],
                           context[4],
                           next_limitation)
                LOG.debug('Chord context: {c}'.format(c=context))
                chord_entry = context[1]
                # sample data yields once for each note in chord!
                for (inputs, outputs) in _sample_data(*context):
                    data.addSample(inputs, outputs)
                while next_limitation and next_limitation[1] <= chord_entry:
                    try:
                        next_limitation = limiting_notes_generator.next()
                    except StopIteration:
                        next_limitation = None
    return data
 def test_first_result(self):
     preceding = []
     current = {3 : 9, 4 : 9, 5: 7}
     following = [{3 : 6, 4 : 6, 5 : 4}]
     recent_notes = {}
     chord_context = chord_contexts(self.tab).next()
     self.assertEqual(chord_context,
                      (preceding, current, following, {}, 0,
                       (('G#', 2), 1)))
 def test_empty_block(self):
     block = ['E|---|',
              'B|---|',
              'G|---|',
              'D|---|',
              'A|---|',
              'E|---|']
     expected = []
     self.assertEqual(list(chord_contexts(block)),
                      expected)
 def test_second_result(self):
     preceding = [{3 : 9, 4 : 9, 5 : 7}]
     current = {3 : 6, 4 : 6, 5 : 4}
     following = [{3: 4, 4: 4, 5: 2}]
     recent_notes = {('B', 2): (7, 5),
                     ('F#', 3): (9, 4),
                     ('B', 3): (9, 3)}
     generator = chord_contexts(self.tab)
     for _ in range(2):
         chord_context = generator.next()
     self.assertEqual(chord_context, (preceding, current, following, recent_notes, 1,
                                      (('F#', 2), 2)))
 def test_third_result(self):
     preceding = [{3: 6, 4: 6, 5: 4}]
     current = {3: 4, 4: 4, 5: 2}
     following = []
     recent_notes = {('G#', 2): (4, 5),
                     ('D#', 3): (6, 4),
                     ('G#', 3): (6, 3),
                     ('B', 2): (7, 5),
                     ('F#', 3): (9, 4),
                     ('B', 3): (9, 3)}
     for context in chord_contexts(self.tab):
         chord_context = context
     self.assertEqual(chord_context, (preceding, current, following, recent_notes, 2, None))
 def test_iteration_stopped(self):
     generator = chord_contexts(self.tab)
     for _ in range(3):
         generator.next()
     with self.assertRaises(StopIteration):
         generator.next()