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