text_html_output_1.png
import music21

import os

import pandas as pd

USE_MUSESCORE = True

if USE_MUSESCORE:
    env = music21.environment.Environment()
    env['musescoreDirectPNGPath'] = '../input/data-challenge-igad-2022-data-set/3 - Challenge/musescore.exe'
    env['musicxmlPath'] = '../input/data-challenge-igad-2022-data-set/3 - Challenge/musescore.exe'

train_directory = '../input/data-challenge-igad-2022-data-set/3 - Challenge/train'
test_directory = '../input/data-challenge-igad-2022-data-set/3 - Challenge/X_test'
train_files = os.listdir(train_directory)
test_files = os.listdir(test_directory)

scores_train = [music21.converter.parse(os.path.join(train_directory, train_files[i])) for i in range(len(train_files))]
scores_test = [music21.converter.parse(os.path.join(test_directory, test_files[i])) for i in range(len(test_files))]
train_dict = {'NOM_FICHIER': train_files, 'SCORE': scores_train}
test_dict = {'NOM_FICHIER': test_files, 'SCORE': scores_test}
df_train = pd.DataFrame(train_dict)
df_test = pd.DataFrame(test_dict)

first_score = music21.converter.parse(os.path.join(train_directory, train_files[0]))

n_parts_train = [len(scores_train[i].parts) for i in range(len(train_files))]
n_parts_test = [len(scores_test[i].parts) for i in range(len(test_files))]
df_train['N_PARTS'] = n_parts_train
df_test['N_PARTS'] = n_parts_test

measures_per_file_train = [len(list(scores_train[i].recurse().getElementsByClass('Measure'))) for i in range(len(scores_train))]
measures_per_file_test = [len(list(scores_test[i].recurse().getElementsByClass('Measure'))) for i in range(len(scores_test))]
df_train['N_MEASURES'] = measures_per_file_train
df_test['N_MEASURES'] = measures_per_file_test

all_measures_train = []
for i in range(len(scores_train)):
    part = []
    measures_in_part = scores_train[i].getElementsByClass(music21.stream.Part)[0].getElementsByClass(music21.stream.Measure)
    for j in range(len(measures_in_part)):
        tmp_meas = []
        chords = measures_in_part[j].getElementsByClass(music21.chord.Chord)
        for chord in chords:
            tmp_meas.append((chord, chord.duration))
        part.append(tmp_meas)
    all_measures_train.append(part)
all_measures_test = []
for i in range(len(scores_test)):
    part = []
    measures_in_part = scores_test[i].getElementsByClass(music21.stream.Part)[0].getElementsByClass(music21.stream.Measure)
    for j in range(len(measures_in_part)):
        tmp_meas = []
        chords = measures_in_part[j].getElementsByClass(music21.chord.Chord)
        for chord in chords:
            tmp_meas.append((chord, chord.duration))
        part.append(tmp_meas)
    all_measures_test.append(part)

def remove_xml(partition):
    new_partition = []
    for measure in partition:
        new_measure = []
        for tup in measure:
            ch = tup[0]
            ch_list = []
            for pitch in ch.pitches:
                name = pitch.name
                octave = pitch.octave
                name_and_otacve = name + str(octave)
                ch_list.append(name_and_otacve)
            duration = ch.quarterLength
            new_measure.append((ch_list, duration))
        new_partition.append(new_measure)
    return new_partition

text2_train = []
text2_test = []
for i in range(len(all_measures_train)):
    text2_train.append(remove_xml(all_measures_train[i]))
for i in range(len(all_measures_test)):
    text2_test.append(remove_xml(all_measures_test[i]))

df_train.iloc[0, 5][0:2]

df_test.iloc[0, 5][0:2]

all_train_pitches = set()
all_train_durations = []
for partition in text2_train:
    for measure in partition:
        for tup in measure:
            pitches = tup[0]
            duration = tup[1]
            all_train_durations.append(duration)
            for pitch in pitches:
                all_train_pitches.add(pitch)
all_test_pitches = set()
all_test_durations = []
for partition in text2_test:
    for measure in partition:
        for tup in measure:
            pitches = tup[0]
            duration = tup[1]
            all_test_durations.append(duration)
            for pitch in pitches:
                all_test_pitches.add(pitch)

all_pitches = all_train_pitches.union(all_test_pitches)
train_dict2 = dict()
test_dict2 = dict()
train_dict2['NOM_FICHIER'] = []
test_dict2['NOM_FICHIER'] = []
for elt in all_pitches:
    train_dict2[elt] = []
    test_dict2[elt] = []
for i in range(df_train.shape[0]):
    partition = df_train.iloc[i, 6]
    for measure in partition:
        for chord_tup in measure:
            for dup_list in chord_tup:
                train_dict2['NOM_FICHIER'].append(df_train.iloc[i, 0])
                for key in train_dict2.keys():
                    if key != 'NOM_FICHIER':
                        if key in dup_list:
                            train_dict2[key].append(1)
                        else:
                            train_dict2[key].append(0)
for i in range(df_test.shape[0]):
    partition = df_test.iloc[i, 6]
    for measure in partition:
        for chord_tup in measure:
            for dup_list in chord_tup:
                test_dict2['NOM_FICHIER'].append(df_test.iloc[i, 0])
                for key in test_dict2.keys():
                    if key != 'NOM_FICHIER':
                        if key in dup_list:
                            test_dict2[key].append(1)
                        else:
                            test_dict2[key].append(0)

df_train_2 = pd.DataFrame(train_dict2)
df_test_2 = pd.DataFrame(test_dict2)

df_test_2.head(16)