pianistprogrammer commited on
Commit
747f6d0
·
1 Parent(s): ec015f3

Refactor Meter2800 dataset class and improve example generation logic

Browse files
Files changed (1) hide show
  1. meter2800.py +42 -48
meter2800.py CHANGED
@@ -1,6 +1,6 @@
1
- from pathlib import Path
2
  import datasets
3
  import pandas as pd
 
4
 
5
  _CITATION = """\
6
  @misc{meter2800_dataset,
@@ -14,8 +14,7 @@ _CITATION = """\
14
 
15
  _DESCRIPTION = """\
16
  Meter2800 is a dataset of 2,800 music audio samples for automatic meter classification.
17
- Each audio file is annotated with a primary meter class label (e.g., 'two', 'three', 'four')
18
- and an alternative meter (numerical, e.g., 2, 3, 4, 6).
19
  It is split into training, validation, and test sets, each available in two class configurations:
20
  2-class and 4-class. All audio is 16-bit WAV format.
21
  """
@@ -23,50 +22,35 @@ It is split into training, validation, and test sets, each available in two clas
23
  _HOMEPAGE = "https://huggingface.co/datasets/pianistprogrammer/Meter2800"
24
  _LICENSE = "mit"
25
 
26
- # Define the labels - adjust these based on your actual data
27
- LABELS_4 = ["three", "four", "five", "seven"]
28
- LABELS_2 = ["simple", "complex"] # or whatever your 2-class grouping actually is
29
-
30
- class Meter2800Config(datasets.BuilderConfig):
31
- """BuilderConfig for Meter2800."""
32
- def __init__(self, name, **kwargs):
33
- super(Meter2800Config, self).__init__(
34
- name=name,
35
- version=datasets.Version("1.0.0"),
36
- **kwargs
37
- )
38
-
39
  class Meter2800(datasets.GeneratorBasedBuilder):
40
- """Meter2800 dataset."""
 
 
41
 
42
  BUILDER_CONFIGS = [
43
- Meter2800Config(
44
  name="4_classes",
45
- description="4-class meter classification"
 
46
  ),
47
- Meter2800Config(
48
- name="2_classes",
49
- description="2-class meter classification"
 
50
  ),
51
  ]
52
-
53
  DEFAULT_CONFIG_NAME = "4_classes"
54
 
55
  def _info(self):
56
- if self.config.name == "4_classes":
57
- label_names = LABELS_4
58
- elif self.config.name == "2_classes":
59
- label_names = LABELS_2
60
- else:
61
- # Fallback - shouldn't happen with proper configs
62
- label_names = LABELS_4
63
-
64
  return datasets.DatasetInfo(
65
  description=_DESCRIPTION,
66
  features=datasets.Features({
67
  "filename": datasets.Value("string"),
68
  "audio": datasets.Audio(sampling_rate=None),
69
- "label": datasets.ClassLabel(names=label_names),
70
  "meter": datasets.Value("string"),
71
  "alt_meter": datasets.Value("string"),
72
  }),
@@ -77,45 +61,55 @@ class Meter2800(datasets.GeneratorBasedBuilder):
77
  )
78
 
79
  def _split_generators(self, dl_manager):
80
- # Get the data directory
81
- data_dir = dl_manager.download_and_extract("")
 
 
82
 
83
  return [
84
  datasets.SplitGenerator(
85
  name=datasets.Split.TRAIN,
86
  gen_kwargs={
87
- "csv_file": f"{data_dir}/data_train_{self.config.name}.csv",
88
- "data_dir": data_dir
89
  },
90
  ),
91
  datasets.SplitGenerator(
92
  name=datasets.Split.VALIDATION,
93
  gen_kwargs={
94
- "csv_file": f"{data_dir}/data_val_{self.config.name}.csv",
95
- "data_dir": data_dir
96
  },
97
  ),
98
  datasets.SplitGenerator(
99
  name=datasets.Split.TEST,
100
  gen_kwargs={
101
- "csv_file": f"{data_dir}/data_test_{self.config.name}.csv",
102
- "data_dir": data_dir
103
  },
104
  ),
105
  ]
106
 
107
- def _generate_examples(self, csv_file, data_dir):
 
 
 
 
 
 
108
  df = pd.read_csv(csv_file)
109
- df = df.dropna(subset=["filename", "label", "meter"]).reset_index(drop=True)
110
 
111
  for idx, row in df.iterrows():
112
- # Construct the full audio path
113
- audio_path = f"{data_dir}/{row['filename']}"
 
 
114
 
115
  yield idx, {
116
  "filename": row["filename"],
117
- "audio": audio_path,
118
- "label": row["label"],
119
- "meter": str(row["meter"]),
120
- "alt_meter": str(row.get("alt_meter", row["meter"])),
121
  }
 
 
1
  import datasets
2
  import pandas as pd
3
+ from pathlib import Path
4
 
5
  _CITATION = """\
6
  @misc{meter2800_dataset,
 
14
 
15
  _DESCRIPTION = """\
16
  Meter2800 is a dataset of 2,800 music audio samples for automatic meter classification.
17
+ Each audio file is annotated with a primary meter class label and an alternative meter.
 
18
  It is split into training, validation, and test sets, each available in two class configurations:
19
  2-class and 4-class. All audio is 16-bit WAV format.
20
  """
 
22
  _HOMEPAGE = "https://huggingface.co/datasets/pianistprogrammer/Meter2800"
23
  _LICENSE = "mit"
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  class Meter2800(datasets.GeneratorBasedBuilder):
26
+ """Meter2800 dataset for music meter classification."""
27
+
28
+ VERSION = datasets.Version("1.0.0")
29
 
30
  BUILDER_CONFIGS = [
31
+ datasets.BuilderConfig(
32
  name="4_classes",
33
+ version=VERSION,
34
+ description="4-class meter classification",
35
  ),
36
+ datasets.BuilderConfig(
37
+ name="2_classes",
38
+ version=VERSION,
39
+ description="2-class meter classification",
40
  ),
41
  ]
42
+
43
  DEFAULT_CONFIG_NAME = "4_classes"
44
 
45
  def _info(self):
46
+ # We'll determine the labels dynamically from the CSV files
47
+ # For now, use a generic ClassLabel that will be updated later
 
 
 
 
 
 
48
  return datasets.DatasetInfo(
49
  description=_DESCRIPTION,
50
  features=datasets.Features({
51
  "filename": datasets.Value("string"),
52
  "audio": datasets.Audio(sampling_rate=None),
53
+ "label": datasets.Value("string"), # We'll convert to ClassLabel later
54
  "meter": datasets.Value("string"),
55
  "alt_meter": datasets.Value("string"),
56
  }),
 
61
  )
62
 
63
  def _split_generators(self, dl_manager):
64
+ """Returns SplitGenerators."""
65
+
66
+ # The files should be in the root of the repo
67
+ config_suffix = self.config.name
68
 
69
  return [
70
  datasets.SplitGenerator(
71
  name=datasets.Split.TRAIN,
72
  gen_kwargs={
73
+ "csv_file": f"data_train_{config_suffix}.csv",
74
+ "split": "train",
75
  },
76
  ),
77
  datasets.SplitGenerator(
78
  name=datasets.Split.VALIDATION,
79
  gen_kwargs={
80
+ "csv_file": f"data_val_{config_suffix}.csv",
81
+ "split": "validation",
82
  },
83
  ),
84
  datasets.SplitGenerator(
85
  name=datasets.Split.TEST,
86
  gen_kwargs={
87
+ "csv_file": f"data_test_{config_suffix}.csv",
88
+ "split": "test",
89
  },
90
  ),
91
  ]
92
 
93
+ def _generate_examples(self, csv_file, split):
94
+ """Yields examples."""
95
+
96
+ # Read CSV directly - the file should be available in the repo
97
+ df = pd.read_csv(csv_file)
98
+
99
+ # Read CSV
100
  df = pd.read_csv(csv_file)
101
+ df = df.dropna(subset=["filename", "label"]).reset_index(drop=True)
102
 
103
  for idx, row in df.iterrows():
104
+ # The audio files should be in subdirectories
105
+ audio_file = row["filename"]
106
+ if not audio_file.startswith("/"):
107
+ audio_file = "/" + audio_file
108
 
109
  yield idx, {
110
  "filename": row["filename"],
111
+ "audio": audio_file.lstrip("/"), # Remove leading slash for HF
112
+ "label": str(row["label"]),
113
+ "meter": str(row.get("meter", "")),
114
+ "alt_meter": str(row.get("alt_meter", row.get("meter", ""))),
115
  }