def __create_empty_dict(self): d = dict() for transition in make_chord_transitions(): if self.__size is None: d.update({transition: NoteTransitionDictionary()}) else: d.update({transition: NoteTransitionDictionary(self.__size)}) return d
def test_init_no_filename(self): self.nd = None self.nd = NoteTransitionDictionary(self.size) empty_dict = { x: {y: 0 for y in range(self.size)} for x in range(self.size) } self.assertEqual(self.nd.get_dict(), empty_dict)
def test_init_no_filename(self): self.td = None self.td = ChordTransitionDictionary(size=self.size) chords = make_chord_transitions() chord_dict = {} for chord in chords: from datastructures.base_structures import NoteTransitionDictionary chord_dict.update({chord: NoteTransitionDictionary(self.size)}) self.assertTrue(chord_dict, self.td.get_dict())
class TestNoteTransitionDictionary(TestCase): def setUp(self): self.size = 10 self.nd = NoteTransitionDictionary(self.size) def test_init_invalid_size(self): from exceptions.Exceptions import InvalidSizeException self.assertRaises(InvalidSizeException, NoteTransitionDictionary, 0) def test_init_no_filename(self): self.nd = None self.nd = NoteTransitionDictionary(self.size) empty_dict = { x: {y: 0 for y in range(self.size)} for x in range(self.size) } self.assertEqual(self.nd.get_dict(), empty_dict) def test_init_with_filename(self): self.nd = None self.nd = NoteTransitionDictionary( self.size, filename="TestNoteTransitionDictionary.txt") self.assertEqual(self.nd.read("TestNoteTransitionDictionary.txt"), self.nd.get_dict()) def test_add_note_transition(self): import random as rand transitions = [] for x in range(self.size**2): start = rand.randint(0, self.size - 1) end = rand.randint(0, self.size - 1) transitions.append(NoteTransition(start, end)) self.nd.add_note_transitions(transitions) for transition in transitions: amount = transitions.count(transition) start = transition.get_start() end = transition.get_end() self.assertEqual(self.nd.get_dict()[start][end], amount) def test_write_and_read(self): import pickle before = dict(self.nd.get_dict()) with open("TestNoteTransitionDictionary.txt", "wb") as file_b: pickle.dump(self.nd.get_dict(), file_b) with open("TestNoteTransitionDictionary.txt", "rb") as file_a: after = pickle.load(file_a) self.assertEqual(before, after)
def test_update_not_empty(self): from datastructures.base_structures import NoteTransitionDictionary import random as rand nd = NoteTransitionDictionary(self.size) transitions = [] for x in range(self.size**2): start = rand.randint(0, self.size-1) end = rand.randint(0, self.size-1) transitions.append(NoteTransition(start,end)) nd.add_note_transitions(transitions) self.pm.update(nd) for i in range(self.size): for j in range(self.size): total = sum(nd.get_dict()[i].values()) if total > 0: self.assertEqual(self.pm.get_matrix()[i][j], nd.get_dict()[i][j]/total)
def test_update_empty(self): from datastructures.base_structures import NoteTransitionDictionary empty = NoteTransitionDictionary(self.size) before = self.pm.get_matrix()[:] self.pm.update(empty) self.assertEqual(before, self.pm.get_matrix())
for song in midi_songs_channel_other: notes = [] for message in song: if message.type == "note_on" and message.channel == 1 and message.velocity != 0: notes.append(message.note) all_notes.append(notes) #store each note transition as a tuple into a list transitions = [] for notes in all_notes: for i in range(len(notes)-1): transition = NoteTransition(notes[i],notes[i+1]) transitions.append(transition) #create markov model data structures nd = NoteTransitionDictionary() pm = ProbabilityMatrix() ntm = NoteTransitionMatrix() #feed the data from the note transitions into the model nd.add_note_transitions(transitions) pm.update(nd) #print the models #print(nd.__repr__()) #print(pm.__repr__()) #print(ntm.__repr__()) #check which rows have non-zero rows x = 0 while x < pm.get_size():
def setUp(self): self.size = 10 self.nd = NoteTransitionDictionary(self.size)
def test_init_with_filename(self): self.nd = None self.nd = NoteTransitionDictionary( self.size, filename="TestNoteTransitionDictionary.txt") self.assertEqual(self.nd.read("TestNoteTransitionDictionary.txt"), self.nd.get_dict())