def test_ngrams_1(self):
     """Integration test for ngrams."""
     expected = AllVoiceIntervalNGrams.series_maker(
         AllVoiceIntervalNGrams.two_grams)
     expected = pandas.DataFrame({
         ('ngram.NGramIndexer', 'Soprano,Bass Alto,Bass Tenor,Bass : Bass'):
         expected
     })
     v_setts = {
         'quality': False,
         'simple or compound': 'compound',
         'directed': True
     }
     h_setts = {
         'quality': False,
         'horiz_attach_later': True,
         'simple or compound': False,
         'directed': True
     }
     n_setts = {
         'n': 2,
         'horizontal': [('Bass', )],
         'brackets': True,
         'continuer': '1',
         'vertical': [('Soprano,Bass', 'Alto,Bass', 'Tenor,Bass')]
     }
     ind_piece = Importer(
         os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv603.xml'))
     vt = ind_piece.get_data('vertical_interval', settings=v_setts)
     hz = ind_piece.get_data('horizontal_interval', settings=h_setts)
     actual = ind_piece.get_data('ngram', data=(vt, hz), settings=n_setts)
     self.assertTrue(actual.equals(expected))
Beispiel #2
0
def melodic_pattern_rest_finder(music_score, ngrams=3):
    imported_score = Importer(music_score)
    pattern_dict = {}
    horizontal_settings = {
        'quality': 'diatonic with quality',
        'simple or compound': 'compound',
        'directed': True,
        'horiz_attach_later': True,
        'mp': False
    }
    ngram_settings = {'n': ngrams, 'vertical': 'all', 'brackets': False}

    horizontal_intervals = imported_score.get_data(
        'horizontal_interval', settings=horizontal_settings)
    n_grams = imported_score.get_data('ngram',
                                      data=[horizontal_intervals],
                                      settings=ngram_settings)
    ngrams_occurrences = imported_score.get_data('frequency', data=n_grams)

    for occurrence in ngrams_occurrences:
        for i, y in enumerate(occurrence.values):
            pattern = occurrence.index[i]
            num_pattern_occurrences = occurrences_sum(occurrence, i)
            pattern_dict[pattern] = num_pattern_occurrences
        for w in sorted(pattern_dict, key=pattern_dict.get, reverse=True):
            print(w, pattern_dict[w])
Beispiel #3
0
def dissonance_count(
        music_score):  # Returns a dictionary of the most common dissonances
    """
    Dissonances are described as follows:
    ‘Q’: Dissonant third quarter (special type of accented passing
    tone)
    ‘D’: Descending passing tone
    ‘R’: Ascending (“rising”) passing tone
    ‘L’: Lower neighbour
    ‘U’: Upper neighbour
    ‘S’: Suspension
    ‘F’: Fake suspension
    ‘f’: Diminished fake suspension
    ‘A’: Anticipation
    ‘C’: Nota cambiata
    ‘H’: Chanson idiom
    ‘E’: Echappée (escape tone)
    ‘-‘: Either no dissonance, or the part in question is not
    considered to be the dissonant note of the dissonance it’s in.
    :param music_score: a music score in the following formats: MIDI, Humdrum, xml.
    :return: dictionary of most common dissonances in the music piece
    """
    imported_score = Importer(music_score)
    dissonance = imported_score.get_data('dissonance')
    offset_dissonance = imported_score.get_data('dissonance')
    print(offset_dissonance)
    dissonance_dict = {}
    for dissonance_list in dissonance.values:
        for dissonance in dissonance_list:
            if dissonance is not "-":
                if dissonance not in dissonance_dict.keys():
                    dissonance_dict[dissonance] = 1
                else:
                    dissonance_dict[dissonance] += 1
    print(dissonance_dict)
 def test_dynamic_offset_method(self):
     # integration test for dynamic offset method call and execution
     expected = os.path.join(VIS_PATH, 'tests', 'expecteds', 'bwv77', 'dynamic_offset_method_test')
     expected = pandas.read_pickle(expected)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl'))
     nr = ip.get_data('noterest')
     actual = ip.get_data('offset', data=nr, settings={'quarterLength': 'dynamic'})
     self.assertTrue(actual.equals(expected))
 def test_duration_indexer_5(self):
     # Alto part of bwv603.mxl which is a part with ties. Also test that data argument is passed 
     # correctly. Since the data argument is passed, these results should not be cached.
     expected = TestDurationIndexer.make_series(TestDurationIndexer.bwv603_alto)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus/bwv603.xml'))
     actual = ip.get_data('duration', data=(ip.get_data('noterest'), ip._analyses['part_streams'])).iloc[:, 1].dropna()
     self.assertTrue(actual.equals(expected))
     self.assertTrue('duration' not in ip._analyses.keys())
Beispiel #6
0
 def test_dynamic_offset_method(self):
     # integration test for dynamic offset method call and execution
     expected = os.path.join(VIS_PATH, 'tests', 'expecteds', 'bwv77',
                             'dynamic_offset_method_test')
     expected = pandas.read_pickle(expected)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv77.mxl'))
     nr = ip.get_data('noterest')
     actual = ip.get_data('offset',
                          data=nr,
                          settings={'quarterLength': 'dynamic'})
     self.assertTrue(actual.equals(expected))
 def test_ngrams_1(self):
     """Ngram integration test."""
     expected = AllVoiceIntervalNGrams.series_maker(AllVoiceIntervalNGrams.two_grams)
     expected = pandas.DataFrame({('ngram.NGramIndexer', 'Soprano,Bass Alto,Bass Tenor,Bass : Bass'): expected})
     ind_piece = Importer(os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv2.xml'))
     setts = {'quality': False, 'simple': False, 'horiz_attach_later': True}
     horiz_ints = ind_piece.get_data('horizontal_interval', settings=setts)
     vert_ints = ind_piece.get_data('vertical_interval', settings=setts)
     setts = {'n': 2, 'continuer': '1', 'horizontal': 'lowest',
              'vertical': [('Soprano,Bass', 'Alto,Bass', 'Tenor,Bass')], 'brackets': True,}
     actual = ind_piece.get_data('ngram', data=(vert_ints, horiz_ints), settings=setts)
     self.assertTrue(actual.equals(expected))
Beispiel #8
0
 def test_ngrams_3(self):
     """Integration test for ngrams."""
     expected = AllVoiceIntervalNGrams.series_maker(AllVoiceIntervalNGrams.two_grams)
     expected = pandas.DataFrame({('ngram.NGramIndexer', '0,3 1,3 2,3 : 3'): expected})
     v_setts = {'quality': False, 'simple or compound': 'compound', 'directed': True}
     h_setts = {'quality': False, 'horiz_attach_later': True, 'simple or compound': False, 'directed': True}
     n_setts = {'n': 2, 'horizontal': [('3',)], 'vertical': [('0,3', '1,3', '2,3')], 'brackets': True, 'continuer': '1'}
     ind_piece = Importer(os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv603.xml'))
     vt = ind_piece.get_data('vertical_interval', settings=v_setts)
     hz = ind_piece.get_data('horizontal_interval', settings=h_setts)
     actual = ind_piece.get_data('ngram', data=(vt, hz), settings=n_setts)
     self.assertTrue(actual.equals(expected))
Beispiel #9
0
 def test_duration_indexer_5(self):
     # Alto part of bwv603.mxl which is a part with ties. Also test that data argument is passed
     # correctly. Since the data argument is passed, these results should not be cached.
     expected = TestDurationIndexer.make_series(
         TestDurationIndexer.bwv603_alto)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus/bwv603.xml'))
     actual = ip.get_data(
         'duration',
         data=(ip.get_data('noterest'),
               ip._analyses['part_streams'])).iloc[:, 1].dropna()
     self.assertTrue(actual.equals(expected))
     self.assertTrue('duration' not in ip._analyses.keys())
Beispiel #10
0
def run_analysis(filename):
    s = Importer(filename)
    v = s.get_data('vertical_interval', vert_setts)
    h = s.get_data('horizontal_interval', horiz_setts)
    parts = [x for x in s._metadata['parts']]
    # Assuming parts come from higher to lower and reversing
    parts = list(reversed(parts))
    combinations = get_part_combinations(parts)
    for vertical, horizontal in combinations:
        ngram_setts['vertical'] = vertical
        ngram_setts['horizontal'] = horizontal
        ngram = s.get_data('ngram', data=[v, h], settings=ngram_setts)
        for k1, v1 in ngram.to_dict().items():
            for k2, v2 in v1.items():
                print('{}: {}'.format(k2, v2))
Beispiel #11
0
def intervals_counter(music_score):
    imported_score = Importer(music_score)
    intervals_dict = {}
    hor_int_settings = {
        'quality': 'diatonic with quality',
        'simple or compound': 'compound',
        'directed': False
    }
    horizontal_intervals = imported_score.get_data('horizontal_interval',
                                                   settings=hor_int_settings)
    freqs = imported_score.get_data('frequency', data=horizontal_intervals)
    for interval_type in freqs:
        for i, num_occurrences in enumerate(interval_type.index):
            intervals_dict[num_occurrences] = occurrences_sum(interval_type, i)
        for w in sorted(intervals_dict, key=intervals_dict.get, reverse=True):
            print(w, intervals_dict[w])
 def test_duration_indexer_4(self):
     # Soprano part of bwv77.mxl which is a part with no ties
     expected = TestDurationIndexer.make_series(TestDurationIndexer.bwv77_soprano)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus/bwv77.mxl'))
     ip._analyses['part_streams'] = ip._get_part_streams()
     actual = ip.get_data('duration').iloc[:, 0].dropna()
     self.assertTrue(actual.equals(expected))
 def test_measure_indexer_5(self):
     # A two-part test piece with no pick-up measure originally written to test fermata indexer.
     measure_data = pandas.Series([1, 2], index=[0.0, 4.0])
     expected = pandas.concat([measure_data]*2, axis=1)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus/test_fermata_rest.xml'))
     actual = ip.get_data('measure')
     expected.columns = actual.columns
     self.assertTrue(actual.equals(expected))
Beispiel #14
0
def rhythmic_pattern(music_score, ngrams=5):
    imported_score = Importer(music_score)
    pattern_dict = {}
    ngram_settings = {'n': ngrams, 'vertical': 'all', 'brackets': False}

    duration = imported_score.get_data('duration')
    n_grams = imported_score.get_data('ngram',
                                      data=[duration],
                                      settings=ngram_settings)
    ngrams_occurrences = imported_score.get_data('frequency', data=n_grams)
    for occurrence in ngrams_occurrences:
        for i, y in enumerate(occurrence.values):
            pattern = occurrence.index[i]
            num_pattern_occurrences = occurrences_sum(occurrence, i)
            pattern_dict[pattern] = num_pattern_occurrences
        for w in sorted(pattern_dict, key=pattern_dict.get, reverse=True):
            print(w, pattern_dict[w])
Beispiel #15
0
 def test_duration_indexer_4(self):
     # Soprano part of bwv77.mxl which is a part with no ties
     expected = TestDurationIndexer.make_series(
         TestDurationIndexer.bwv77_soprano)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus/bwv77.mxl'))
     ip._analyses['part_streams'] = ip._get_part_streams()
     actual = ip.get_data('duration').iloc[:, 0].dropna()
     self.assertTrue(actual.equals(expected))
 def test_duration_indexer_6(self):
     # Soprano and bass parts of bwv603.xml
     # We won't verify all the parts, but we'll submit them all for analysis.
     expected = pandas.concat([TestDurationIndexer.make_series(TestDurationIndexer.bwv603_soprano),
                               TestDurationIndexer.make_series(TestDurationIndexer.bwv603_bass)], axis=1)
     expected.columns = pandas.MultiIndex.from_product([('meter.DurationIndexer',), ('Soprano', 'Bass')])
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus/bwv603.xml'))
     actual = ip.get_data('duration').iloc[:, [0, 3]].dropna(how='all')
     self.assertTrue(actual.equals(expected))
def run_analysis(filename):
    s = Importer(filename)
    nr = s.get_data('noterest')
    lines = [' '.join(l.split()) for l in nr.to_string().split('\n')]
    for line in lines[2:]:
        ind, b, t, a, s = line.split(' ')
        print('{}:{}, {}, {}, {}, {}'.format(
            int(float(ind) / 3) + 1,
            float(ind) % 3, b, t, a, s))
Beispiel #18
0
def count_intervals():
    def get_ints(row, num_voices):
        return tuple(sorted(set(n for n in tuple((tuple(row))[1]) if n != 'Rest'), key = lambda n: abs(int(n)))[:num_voices-1])
    prog_counter = Counter()
    i = 0

    vert_settings = {
        'quality': 'chromatic',
        'simple or compound': 'simple',
        'directed': True
    }

    for m in [p for p in os.listdir('music_files/corpus/Palestrina/') if p[-3:] == 'xml']:
        print("processing " + m)
        num_voices = m.split('_')[-1][0]
        if num_voices.isdigit():
            num_voices = int(num_voices)
        else:
            print("skipping " + m)
            continue
        ip = Importer('music_files/corpus/Palestrina/' + m)
        df = ip.get_data('vertical_interval', settings=vert_settings).fillna(method="ffill")

        note_df = ip.get_data('noterest').fillna(method="ffill")

        #int_df = df['interval.IntervalIndexer']

        it = zip(df.iterrows(), islice(df.iterrows(), 1, None))
        for row in it:
            ints = tuple(get_ints(r, num_voices) for r in row)
            prog_counter.update((ints, ))

        if i % 100 == 0:
            with open('scripts/prog.pckl', 'wb') as f:
                pickle.dump(prog_counter, f)

    with open('scripts/prog.pckl', 'wb') as f:
        pickle.dump(prog_counter, f)

    return prog_counter, df
Beispiel #19
0
 def test_ngrams_1(self):
     """Ngram integration test."""
     expected = AllVoiceIntervalNGrams.series_maker(
         AllVoiceIntervalNGrams.two_grams)
     expected = pandas.DataFrame({
         ('ngram.NGramIndexer', 'Soprano,Bass Alto,Bass Tenor,Bass : Bass'):
         expected
     })
     ind_piece = Importer(
         os.path.join(VIS_PATH, 'tests', 'corpus', 'bwv2.xml'))
     setts = {'quality': False, 'simple': False, 'horiz_attach_later': True}
     horiz_ints = ind_piece.get_data('horizontal_interval', settings=setts)
     vert_ints = ind_piece.get_data('vertical_interval', settings=setts)
     setts = {
         'n': 2,
         'continuer': '1',
         'horizontal': 'lowest',
         'vertical': [('Soprano,Bass', 'Alto,Bass', 'Tenor,Bass')],
         'brackets': True,
     }
     actual = ind_piece.get_data('ngram',
                                 data=(vert_ints, horiz_ints),
                                 settings=setts)
     self.assertTrue(actual.equals(expected))
Beispiel #20
0
 def test_duration_indexer_6(self):
     # Soprano and bass parts of bwv603.xml
     # We won't verify all the parts, but we'll submit them all for analysis.
     expected = pandas.concat([
         TestDurationIndexer.make_series(
             TestDurationIndexer.bwv603_soprano),
         TestDurationIndexer.make_series(TestDurationIndexer.bwv603_bass)
     ],
                              axis=1)
     expected.columns = pandas.MultiIndex.from_product([
         ('meter.DurationIndexer', ), ('Soprano', 'Bass')
     ])
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus/bwv603.xml'))
     actual = ip.get_data('duration').iloc[:, [0, 3]].dropna(how='all')
     self.assertTrue(actual.equals(expected))
Beispiel #21
0
 def test_diss_indexer_run_2(self):
     """
     Test the dissonance indexer on an entire real piece that has most of the dissonance types 
     and covers almost all of the logic, namely the "Kyrie" in the test corpus. NB: perhaps 
     this test should get moved to the integration tests file.
     """
     expected = pd.read_pickle(os.path.join(VIS_PATH, 'tests', 'expecteds', 'test_dissonance_thorough.pickle'))
     # Detection of Z's and O's was removed and so this old ground-truth is updated in this test with the two 
     # following lines. If the dissonance indexer gets re-written, it would be good to go back to this old 
     # ground truth without needing these two replace() calls.
     expected.replace('Z', '-', inplace=True)
     expected.replace('O', '-', inplace=True)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus', 'Kyrie.krn'))
     actual = ip.get_data('dissonance')
     assert_frame_equal(actual, expected)
     self.assertTrue(actual.equals(expected))
Beispiel #22
0
 def test_diss_indexer_run_2(self):
     """
     Test the dissonance indexer on an entire real piece that has most of the dissonance types 
     and covers almost all of the logic, namely the "Kyrie" in the test corpus. NB: perhaps 
     this test should get moved to the integration tests file.
     """
     expected = pd.read_pickle(
         os.path.join(VIS_PATH, 'tests', 'expecteds',
                      'test_dissonance_thorough.pickle'))
     # Detection of Z's and O's was removed and so this old ground-truth is updated in this test with the two
     # following lines. If the dissonance indexer gets re-written, it would be good to go back to this old
     # ground truth without needing these two replace() calls.
     expected.replace('Z', '-', inplace=True)
     expected.replace('O', '-', inplace=True)
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus', 'Kyrie.krn'))
     actual = ip.get_data('dissonance')
     expected.columns = actual.columns  # the pickle file has old-style column names.
     assert_frame_equal(actual, expected)
Beispiel #23
0
def count_notes():
    def get_notes(row):
            return tuple(sorted(set([n[0] if n[1].isdigit() else n[0:2] for n in tuple((tuple(row))[1]) if n != 'Rest'])))

    prog_counter = Counter()
    i = 0

    for m in [p for p in os.listdir('music_files/corpus/Palestrina/') if p[-3:] == 'xml']:
        print("processing " + m)
        ip = Importer('music_files/corpus/Palestrina/' + m)
        df = ip.get_data('noterest').fillna(method="ffill")
        it = zip(df.iterrows(), islice(df.iterrows(), 1, None))
        for row in it:
            notes = tuple(map(get_notes, row))
            prog_counter.update((notes, ))

        if i % 100 == 0:
            with open('scripts/prog.pckl', 'wb') as f:
                pickle.dump(prog_counter, f)

    with open('scripts/prog.pckl', 'wb') as f:
        pickle.dump(prog_counter, f)
Beispiel #24
0
 def test_multistop_indexer_3(self):
     # Integration test on a piece with multiple voices in each part
     ip = Importer(os.path.join(VIS_PATH, 'tests', 'corpus', 'prelude28-20.mid'))
     actual = ip.get_data('multistop')
     self.assertTrue(8 == len(actual.columns))
Beispiel #25
0
# coding: utf-8
from vis.models.indexed_piece import Importer
import matplotlib.pyplot as plt
import numpy as np

lower_limit = 0
upper_limit = -1

if __name__ == '__main__':
    parts = ['Violin I', 'Violin II', 'Viola', 'Violoncello']
    a = Importer('encodings/a.musicxml')
    b = Importer('encodings/b.musicxml')
    c = Importer('encodings/c.musicxml')
    an = a.get_data('noterest')
    bn = b.get_data('noterest')
    cn = c.get_data('noterest')
    index = an.index.union(bn.index.union(cn.index))
    an = an.reindex(index).fillna('tie')
    bn = bn.reindex(index).fillna('tie')
    cn = cn.reindex(index).fillna('tie')
    ab = an == bn
    bc = bn == cn
    ca = cn == an
    abm = ab.values[lower_limit:upper_limit].T
    bcm = bc.values[lower_limit:upper_limit].T
    cam = ca.values[lower_limit:upper_limit].T
    index = index[:upper_limit]
    measureoffsets = np.arange(0, len(index[lower_limit:upper_limit]), 3.0)
    fig, ax = plt.subplots(3, sharex=True)
    ax[0].imshow(abm, cmap='gray_r', aspect='auto')
    ax[0].set_title('Encoding A vs Encoding B', loc='right')
from vis.models.indexed_piece import Importer
import vis
VIS_path = vis.__path__[0]

folder = VIS_path + '/tests/corpus/elvisdownload2'
dendro_setts = {'graph_settings': {'filename_and_type': 'Dendrogram_output.png'}}
ap = Importer(folder)
intervals = ap.get_data('vertical_interval')
ap.get_data(combined_experimenter='dendrogram', data=[intervals], settings=dendro_setts)
Beispiel #27
0
from vis.models.indexed_piece import Importer
import vis
VIS_path = vis.__path__[0]

folder = VIS_path + '/tests/corpus/elvisdownload2'
dendro_setts = {
    'graph_settings': {
        'filename_and_type': 'Dendrogram_output.png'
    }
}
ap = Importer(folder)
intervals = ap.get_data('vertical_interval')
ap.get_data(combined_experimenter='dendrogram',
            data=[intervals],
            settings=dendro_setts)