Datasets:
feat: migrate parquet schema to {path, audio, label, notes}
Browse files- build_parquet.py +81 -79
- data/test-00000-of-00009.parquet +2 -2
- data/test-00001-of-00009.parquet +2 -2
- data/test-00002-of-00009.parquet +2 -2
- data/test-00003-of-00009.parquet +2 -2
- data/test-00004-of-00009.parquet +2 -2
- data/test-00005-of-00009.parquet +2 -2
- data/test-00006-of-00009.parquet +2 -2
- data/test-00007-of-00009.parquet +2 -2
- data/test-00008-of-00009.parquet +2 -2
build_parquet.py
CHANGED
|
@@ -1,112 +1,114 @@
|
|
|
|
|
| 1 |
import tempfile
|
| 2 |
-
import tarfile
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
from datasets import Audio, ClassLabel, Dataset, Features, Value
|
| 6 |
|
| 7 |
REPO_ROOT = Path(__file__).resolve().parent
|
| 8 |
-
TAR_PATH = REPO_ROOT / "data" / "ASVspoof2019LA_eval_flac.tar.gz"
|
| 9 |
-
PROTOCOL_PATH = REPO_ROOT / "protocols" / "ASVspoof2019.LA.cm.eval.trl.txt"
|
| 10 |
PARQUET_DIR = REPO_ROOT / "data"
|
| 11 |
-
|
| 12 |
NUM_SHARDS = 9
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
{
|
| 16 |
-
"
|
| 17 |
"audio": Audio(sampling_rate=16000),
|
| 18 |
-
"utterance_id": Value("string"),
|
| 19 |
-
"speaker_id": Value("string"),
|
| 20 |
-
"attack_id": Value("string"),
|
| 21 |
"label": ClassLabel(names=["bonafide", "spoof"]),
|
|
|
|
| 22 |
}
|
| 23 |
)
|
| 24 |
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
parts = line.strip().split()
|
| 31 |
-
if len(parts) != 5:
|
| 32 |
-
continue
|
| 33 |
-
speaker_id, utterance_id, _, attack_id, label = parts
|
| 34 |
-
rows.append(
|
| 35 |
-
{
|
| 36 |
-
"trial_id": utterance_id,
|
| 37 |
-
"utterance_id": utterance_id,
|
| 38 |
-
"speaker_id": speaker_id,
|
| 39 |
-
"attack_id": attack_id,
|
| 40 |
-
"label": label,
|
| 41 |
-
}
|
| 42 |
-
)
|
| 43 |
-
return rows
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def verify_protocol(rows: list[dict]) -> None:
|
| 47 |
-
assert len(rows) == EXPECTED_ROWS, f"Expected {EXPECTED_ROWS} rows, got {len(rows)}"
|
| 48 |
-
labels = {r["label"] for r in rows}
|
| 49 |
-
assert labels == {"bonafide", "spoof"}, f"Unexpected labels: {labels}"
|
| 50 |
-
trial_ids = [r["trial_id"] for r in rows]
|
| 51 |
-
assert len(set(trial_ids)) == EXPECTED_ROWS, "Duplicate trial_ids found"
|
| 52 |
-
for row in rows[:10]:
|
| 53 |
-
assert row["speaker_id"].startswith("LA_"), f"Bad speaker_id: {row['speaker_id']}"
|
| 54 |
-
assert row["utterance_id"].startswith("LA_E_"), f"Bad utterance_id: {row['utterance_id']}"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def build_parquet() -> None:
|
| 58 |
-
rows = parse_protocol(PROTOCOL_PATH)
|
| 59 |
-
verify_protocol(rows)
|
| 60 |
-
print(f"Parsed {len(rows)} protocol rows")
|
| 61 |
-
|
| 62 |
-
with tempfile.TemporaryDirectory() as tmpdir:
|
| 63 |
-
print(f"Extracting tarball to {tmpdir}...")
|
| 64 |
-
with tarfile.open(TAR_PATH, "r:gz") as tar:
|
| 65 |
-
tar.extractall(tmpdir)
|
| 66 |
-
|
| 67 |
-
flac_dir = Path(tmpdir) / "flac"
|
| 68 |
-
protocol_ids = {r["utterance_id"] for r in rows}
|
| 69 |
-
missing = [uid for uid in protocol_ids if not (flac_dir / f"{uid}.flac").exists()]
|
| 70 |
-
assert not missing, f"Missing FLAC files for {len(missing)} utterance IDs"
|
| 71 |
-
print(f"All {len(protocol_ids)} protocol FLAC files found")
|
| 72 |
|
| 73 |
-
for row in rows:
|
| 74 |
-
flac_path = flac_dir / f"{row['utterance_id']}.flac"
|
| 75 |
-
row["audio"] = {"bytes": flac_path.read_bytes(), "path": f"{row['utterance_id']}.flac"}
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
print(f"Dataset created: {len(ds)} rows, features: {ds.features}")
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
for i in range(NUM_SHARDS):
|
| 83 |
shard = ds.shard(num_shards=NUM_SHARDS, index=i)
|
| 84 |
-
shard_path =
|
| 85 |
shard.to_parquet(str(shard_path))
|
| 86 |
print(f" Wrote {shard_path.name} ({len(shard)} rows)")
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
print(f" Audio decode OK: {sample['audio']['sampling_rate']}Hz, {len(sample['audio']['array'])} samples")
|
| 94 |
-
|
| 95 |
-
print("Verifying parquet row count...")
|
| 96 |
-
import pyarrow.parquet as pq
|
| 97 |
|
|
|
|
| 98 |
parquet_files = sorted(PARQUET_DIR.glob("test-*.parquet"))
|
| 99 |
total_rows = sum(pq.read_metadata(f).num_rows for f in parquet_files)
|
| 100 |
-
assert total_rows == EXPECTED_ROWS, f"Expected {EXPECTED_ROWS}
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
|
| 105 |
print("All verifications passed!")
|
| 106 |
-
print(f" Rows: {total_rows}")
|
| 107 |
-
print(f" Bonafide: {bonafide_count}, Spoof: {spoof_count}")
|
| 108 |
-
print(f" Shards: {NUM_SHARDS}")
|
| 109 |
|
| 110 |
|
| 111 |
if __name__ == "__main__":
|
| 112 |
-
|
|
|
|
| 1 |
+
import json
|
| 2 |
import tempfile
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
|
| 5 |
from datasets import Audio, ClassLabel, Dataset, Features, Value
|
| 6 |
|
| 7 |
REPO_ROOT = Path(__file__).resolve().parent
|
|
|
|
|
|
|
| 8 |
PARQUET_DIR = REPO_ROOT / "data"
|
| 9 |
+
PROTOCOL_PATH = REPO_ROOT / "protocols" / "ASVspoof2019.LA.cm.eval.trl.txt"
|
| 10 |
NUM_SHARDS = 9
|
| 11 |
+
EXPECTED_ROWS = 71237
|
| 12 |
|
| 13 |
+
NEW_FEATURES = Features(
|
| 14 |
{
|
| 15 |
+
"path": Value("string"),
|
| 16 |
"audio": Audio(sampling_rate=16000),
|
|
|
|
|
|
|
|
|
|
| 17 |
"label": ClassLabel(names=["bonafide", "spoof"]),
|
| 18 |
+
"notes": Value("string"),
|
| 19 |
}
|
| 20 |
)
|
| 21 |
|
| 22 |
|
| 23 |
+
def build_notes(utterance_id: str, speaker_id: str, subset: str = "eval") -> str:
|
| 24 |
+
return json.dumps(
|
| 25 |
+
{"utterance_id": utterance_id, "speaker_id": speaker_id, "subset": subset}
|
| 26 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def migrate_shards() -> None:
|
| 30 |
+
import pyarrow.parquet as pq
|
|
|
|
| 31 |
|
| 32 |
+
parquet_files = sorted(PARQUET_DIR.glob("test-*.parquet"))
|
| 33 |
+
print(f"Found {len(parquet_files)} parquet shards")
|
| 34 |
+
|
| 35 |
+
all_rows = []
|
| 36 |
+
for pf in parquet_files:
|
| 37 |
+
table = pq.read_table(pf)
|
| 38 |
+
for i in range(table.num_rows):
|
| 39 |
+
row = {col: table.column(col)[i].as_py() for col in table.column_names}
|
| 40 |
+
all_rows.append(row)
|
| 41 |
+
|
| 42 |
+
print(f"Read {len(all_rows)} total rows")
|
| 43 |
+
assert len(all_rows) == EXPECTED_ROWS, f"Expected {EXPECTED_ROWS}, got {len(all_rows)}"
|
| 44 |
+
|
| 45 |
+
migrated = []
|
| 46 |
+
for row in all_rows:
|
| 47 |
+
utterance_id = row["utterance_id"]
|
| 48 |
+
speaker_id = row["speaker_id"]
|
| 49 |
+
label_raw = row["label"]
|
| 50 |
+
label_str = "bonafide" if label_raw == 0 else "spoof"
|
| 51 |
+
audio = row["audio"]
|
| 52 |
+
|
| 53 |
+
migrated.append(
|
| 54 |
+
{
|
| 55 |
+
"path": f"{utterance_id}.flac",
|
| 56 |
+
"audio": audio,
|
| 57 |
+
"label": label_str,
|
| 58 |
+
"notes": build_notes(utterance_id, speaker_id),
|
| 59 |
+
}
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
print("Building dataset with new schema...")
|
| 63 |
+
ds = Dataset.from_list(migrated, features=NEW_FEATURES)
|
| 64 |
+
print(f"Dataset: {len(ds)} rows, features: {ds.features}")
|
| 65 |
+
|
| 66 |
+
print("Verifying...")
|
| 67 |
+
sample = ds[0]
|
| 68 |
+
assert sample["audio"]["sampling_rate"] == 16000
|
| 69 |
+
assert len(sample["audio"]["array"]) > 0
|
| 70 |
+
notes_parsed = json.loads(sample["notes"])
|
| 71 |
+
assert "utterance_id" in notes_parsed
|
| 72 |
+
assert notes_parsed["utterance_id"].startswith("LA_E_")
|
| 73 |
+
|
| 74 |
+
uid_set = set()
|
| 75 |
+
path_set = set()
|
| 76 |
+
for row in migrated:
|
| 77 |
+
n = json.loads(row["notes"])
|
| 78 |
+
uid_set.add(n["utterance_id"])
|
| 79 |
+
path_set.add(row["path"])
|
| 80 |
+
assert len(uid_set) == EXPECTED_ROWS, f"Duplicate utterance_ids: {EXPECTED_ROWS - len(uid_set)}"
|
| 81 |
+
assert len(path_set) == EXPECTED_ROWS, f"Duplicate paths: {EXPECTED_ROWS - len(path_set)}"
|
| 82 |
+
|
| 83 |
+
bonafide_count = sum(1 for r in migrated if r["label"] == "bonafide")
|
| 84 |
+
spoof_count = sum(1 for r in migrated if r["label"] == "spoof")
|
| 85 |
+
print(f" bonafide: {bonafide_count}, spoof: {spoof_count}, total: {len(migrated)}")
|
| 86 |
+
|
| 87 |
+
print(f"Writing {NUM_SHARDS} shards...")
|
| 88 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 89 |
for i in range(NUM_SHARDS):
|
| 90 |
shard = ds.shard(num_shards=NUM_SHARDS, index=i)
|
| 91 |
+
shard_path = Path(tmpdir) / f"test-{i:05d}-of-{NUM_SHARDS:05d}.parquet"
|
| 92 |
shard.to_parquet(str(shard_path))
|
| 93 |
print(f" Wrote {shard_path.name} ({len(shard)} rows)")
|
| 94 |
|
| 95 |
+
for i in range(NUM_SHARDS):
|
| 96 |
+
shard_name = f"test-{i:05d}-of-{NUM_SHARDS:05d}.parquet"
|
| 97 |
+
src = Path(tmpdir) / shard_name
|
| 98 |
+
dst = PARQUET_DIR / shard_name
|
| 99 |
+
dst.write_bytes(src.read_bytes())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
print("Verifying written shards...")
|
| 102 |
parquet_files = sorted(PARQUET_DIR.glob("test-*.parquet"))
|
| 103 |
total_rows = sum(pq.read_metadata(f).num_rows for f in parquet_files)
|
| 104 |
+
assert total_rows == EXPECTED_ROWS, f"Expected {EXPECTED_ROWS}, got {total_rows}"
|
| 105 |
|
| 106 |
+
t = pq.read_table(str(parquet_files[0]))
|
| 107 |
+
assert set(t.column_names) == {"path", "audio", "label", "notes"}
|
| 108 |
+
print(f"Shard 0 columns: {t.column_names}")
|
| 109 |
|
| 110 |
print("All verifications passed!")
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
+
migrate_shards()
|
data/test-00000-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0ed869f9c1c5ef591f5abf363eac2242aab8690c8803055aad07a05abbcbb011
|
| 3 |
+
size 486188677
|
data/test-00001-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f3fb70e73f4dfcc50407f3d83c180fd3e1687635d172d1ff8d51056338c03772
|
| 3 |
+
size 486891443
|
data/test-00002-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:743f2f8642a190447ed76ae6e2e7d87b8a822d477a10e06eeb696b5ef2ce351b
|
| 3 |
+
size 486786361
|
data/test-00003-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ca8b3493227396651db0c3b2938fe5460b14bdb6ae9faf63ede7fbc322ae9d20
|
| 3 |
+
size 486672585
|
data/test-00004-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8368306ad7f4c8a86707832ca516151af7882abe0eb81904355a088442379d76
|
| 3 |
+
size 485813748
|
data/test-00005-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bebdf6bd5915a8e18f326d52d6c260550472976447a447b9d82937330aa5030f
|
| 3 |
+
size 483786272
|
data/test-00006-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:04acec349a4e59e5d21cfad56b79c5590558d94537b5df849d33c12c283244ba
|
| 3 |
+
size 486248843
|
data/test-00007-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f6431527082ef430dbe094e4faea8f2fbaff872dcdefdb972ba40f8039ca8234
|
| 3 |
+
size 489701913
|
data/test-00008-of-00009.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e5107356edacab203df3bda7683e378311d004815079aeccaddd8a7fbf5263e2
|
| 3 |
+
size 490138442
|