'Compares MusicXMLDocument object against a sequence proto.
Args:
musicxml: A MusicXMLDocument object.
sequence_proto: A tensorflow.magenta.Sequence proto.'

def checkmusicxmlandsequence(self, musicxml, sequence_proto):

    self.assertEqual(len(musicxml.get_time_signatures()), len(sequence_proto.time_signatures))
    for (musicxml_time, sequence_time) in zip(musicxml.get_time_signatures(), sequence_proto.time_signatures):
        self.assertEqual(musicxml_time.numerator, sequence_time.numerator)
        self.assertEqual(musicxml_time.denominator, sequence_time.denominator)
        self.assertAlmostEqual(musicxml_time.time_position, sequence_time.time)
    self.assertEqual(len(musicxml.get_key_signatures()), len(sequence_proto.key_signatures))
    for (musicxml_key, sequence_key) in zip(musicxml.get_key_signatures(), sequence_proto.key_signatures):
        if (musicxml_key.mode == 'major'):
            mode = 0
        elif (musicxml_key.mode == 'minor'):
            mode = 1
        music_proto_keys = [11, 6, 1, 8, 3, 10, 5, 0, 7, 2, 9, 4, 11, 6, 1]
        key = music_proto_keys[(musicxml_key.key + 7)]
        self.assertEqual(key, sequence_key.key)
        self.assertEqual(mode, sequence_key.mode)
        self.assertAlmostEqual(musicxml_key.time_position, sequence_key.time)
    musicxml_tempos = musicxml.get_tempos()
    self.assertEqual(len(musicxml_tempos), len(sequence_proto.tempos))
    for (musicxml_tempo, sequence_tempo) in zip(musicxml_tempos, sequence_proto.tempos):
        self.assertAlmostEqual(musicxml_tempo.qpm, sequence_tempo.qpm)
        self.assertAlmostEqual(musicxml_tempo.time_position, sequence_tempo.time)
    seq_parts = defaultdict(list)
    for seq_note in sequence_proto.notes:
        seq_parts[seq_note.part].append(seq_note)
    self.assertEqual(len(musicxml.parts), len(seq_parts))
    for (musicxml_part, seq_part_id) in zip(musicxml.parts, sorted(seq_parts.keys())):
        seq_instrument_notes = seq_parts[seq_part_id]
        musicxml_notes = []
        for musicxml_measure in musicxml_part.measures:
            for musicxml_note in musicxml_measure.notes:
                if (not musicxml_note.is_rest):
                    musicxml_notes.append(musicxml_note)
        self.assertEqual(len(musicxml_notes), len(seq_instrument_notes))
        for (musicxml_note, sequence_note) in zip(musicxml_notes, seq_instrument_notes):
            self.assertEqual(musicxml_note.pitch[1], sequence_note.pitch)
            self.assertEqual(musicxml_note.velocity, sequence_note.velocity)
            self.assertAlmostEqual(musicxml_note.note_duration.time_position, sequence_note.start_time)
            self.assertAlmostEqual((musicxml_note.note_duration.time_position + musicxml_note.note_duration.seconds), sequence_note.end_time)
            self.assertAlmostEqual(musicxml_note.note_duration.duration, ((musicxml_note.state.divisions * 4) * musicxml_note.note_duration.duration_float()), delta=1)
