File size: 1,807 Bytes
14fd7b7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import os
import pandas as pd
from datasets import DatasetBuilder, GeneratorBasedBuilder, Split, Value, Features, ClassLabel
class MidiDataset(GeneratorBasedBuilder):
"""Hugging Face Dataset for MIDI files and their labels"""
def _info(self):
return DatasetBuilder.info(
features=Features({
"file_path": Value("string"), # MIDI file path
"label": ClassLabel(names=["1", "2", "3", "4"]), # 4Q labels as class labels
"annotator": Value("string") # Annotator information
})
)
def _split_generators(self, dl_manager):
"""Split the dataset. Assumes all files are local."""
# Set paths to midis/ and label.csv
midi_path = "midis"
label_path = "label.csv"
return [
Split(
name=Split.TRAIN,
gen_kwargs={
"midi_dir": midi_path,
"label_file": label_path
}
)
]
def _generate_examples(self, midi_dir, label_file):
"""Yield examples from MIDI files and the label.csv"""
# Read the label.csv into a pandas DataFrame
df = pd.read_csv(label_file)
for index, row in df.iterrows():
midi_file = os.path.join(midi_dir, f"{row['ID']}.mid")
if os.path.exists(midi_file):
yield index, {
"file_path": midi_file,
"label": str(row["4Q"]), # Convert label to string for ClassLabel compatibility
"annotator": row["annotator"]
}
# Usage Example:
# You can now load the dataset using this script as follows:
# from datasets import load_dataset
# dataset = load_dataset("path/to/your/script", split="train") |