'Compares PrettyMIDI object against a sequence proto.
Args:
midi: A pretty_midi.PrettyMIDI object.
sequence_proto: A tensorflow.magenta.Sequence proto.'

def CheckPrettyMidiAndSequence(self, midi, sequence_proto):

    self.assertEqual(len(midi.time_signature_changes), len(sequence_proto.time_signatures))
    for (midi_time, sequence_time) in zip(midi.time_signature_changes, sequence_proto.time_signatures):
        self.assertEqual(midi_time.numerator, sequence_time.numerator)
        self.assertEqual(midi_time.denominator, sequence_time.denominator)
        self.assertAlmostEqual(midi_time.time, sequence_time.time)
    self.assertEqual(len(midi.key_signature_changes), len(sequence_proto.key_signatures))
    for (midi_key, sequence_key) in zip(midi.key_signature_changes, sequence_proto.key_signatures):
        self.assertEqual((midi_key.key_number % 12), sequence_key.key)
        self.assertEqual((midi_key.key_number / 12), sequence_key.mode)
        self.assertAlmostEqual(midi_key.time, sequence_key.time)
    (midi_times, midi_qpms) = midi.get_tempo_changes()
    self.assertEqual(len(midi_times), len(sequence_proto.tempos))
    self.assertEqual(len(midi_qpms), len(sequence_proto.tempos))
    for (midi_time, midi_qpm, sequence_tempo) in zip(midi_times, midi_qpms, sequence_proto.tempos):
        self.assertAlmostEqual(midi_qpm, sequence_tempo.qpm)
        self.assertAlmostEqual(midi_time, sequence_tempo.time)
    seq_instruments = defaultdict((lambda : defaultdict(list)))
    for seq_note in sequence_proto.notes:
        seq_instruments[(seq_note.instrument, seq_note.program, seq_note.is_drum)]['notes'].append(seq_note)
    for seq_bend in sequence_proto.pitch_bends:
        seq_instruments[(seq_bend.instrument, seq_bend.program, seq_bend.is_drum)]['bends'].append(seq_bend)
    for seq_control in sequence_proto.control_changes:
        seq_instruments[(seq_control.instrument, seq_control.program, seq_control.is_drum)]['controls'].append(seq_control)
    sorted_seq_instrument_keys = sorted(seq_instruments.keys(), key=(lambda (instr, program, is_drum): (instr, program, is_drum)))
    if seq_instruments:
        self.assertEqual(len(midi.instruments), len(seq_instruments))
    else:
        self.assertEqual(1, len(midi.instruments))
        self.assertEqual(0, len(midi.instruments[0].notes))
        self.assertEqual(0, len(midi.instruments[0].pitch_bends))
    for (midi_instrument, seq_instrument_key) in zip(midi.instruments, sorted_seq_instrument_keys):
        seq_instrument_notes = seq_instruments[seq_instrument_key]['notes']
        self.assertEqual(len(midi_instrument.notes), len(seq_instrument_notes))
        for (midi_note, sequence_note) in zip(midi_instrument.notes, seq_instrument_notes):
            self.assertEqual(midi_note.pitch, sequence_note.pitch)
            self.assertEqual(midi_note.velocity, sequence_note.velocity)
            self.assertAlmostEqual(midi_note.start, sequence_note.start_time)
            self.assertAlmostEqual(midi_note.end, sequence_note.end_time)
        seq_instrument_pitch_bends = seq_instruments[seq_instrument_key]['bends']
        self.assertEqual(len(midi_instrument.pitch_bends), len(seq_instrument_pitch_bends))
        for (midi_pitch_bend, sequence_pitch_bend) in zip(midi_instrument.pitch_bends, seq_instrument_pitch_bends):
            self.assertEqual(midi_pitch_bend.pitch, sequence_pitch_bend.bend)
            self.assertAlmostEqual(midi_pitch_bend.time, sequence_pitch_bend.time)
