'Detects if a motif was just played and if so, returns it.
A motif should contain at least three distinct notes that are not note_on
or note_off, and occur within the course of one bar.
Args:
composition: The composition in which the function will look for a
recent motif. Defaults to the model\'s composition.
bar_length: The number of notes in one bar.
Returns:
None if there is no motif, otherwise the motif in the same format as the
composition.'

def detect_last_motif(self, composition=None, bar_length=8):

    if (composition is None):
        composition = self.composition
    if (len(composition) < bar_length):
        return (None, 0)
    last_bar = composition[(- bar_length):]
    actual_notes = [a for a in last_bar if ((a != NO_EVENT) and (a != NOTE_OFF))]
    num_unique_notes = len(set(actual_notes))
    if (num_unique_notes >= 3):
        return (last_bar, num_unique_notes)
    else:
        return (None, num_unique_notes)
