Datasets:
Tasks:
Text Classification
Modalities:
Text
Formats:
parquet
Sub-tasks:
multi-class-classification
Size:
1M - 10M
DOI:
License:
Download scripts/validate_all.py from ma2za/many_emotions: direct link, hf CLI and curl.
- Browser
- Download file 4.67 kB
-
https://huggingface.co/datasets/ma2za/many_emotions/resolve/main/scripts/validate_all.py
- Command line
-
hf download hf://datasets/ma2za/many_emotions/scripts/validate_all.py
-
curl -L -o validate_all.py https://huggingface.co/datasets/ma2za/many_emotions/resolve/main/scripts/validate_all.py
4.67 kB
| import argparse | |
| import collections | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import pyarrow.parquet as pq | |
| from datasets import ClassLabel, Features, Value | |
| LABELS = ["anger", "fear", "joy", "love", "sadness", "surprise", "neutral"] | |
| LANGUAGES = {"de", "en", "es", "fr", "it"} | |
| FEATURES = Features( | |
| { | |
| "id": Value("string"), | |
| "source_id": Value("string"), | |
| "text": Value("string"), | |
| "label": ClassLabel(names=LABELS), | |
| "label_name": Value("string"), | |
| "dataset": Value("string"), | |
| "license": Value("string"), | |
| "language": Value("string"), | |
| "is_label_conflict": Value("bool"), | |
| "label_variants": Value("int8"), | |
| } | |
| ) | |
| def file_sha256(path): | |
| digest = hashlib.sha256() | |
| with path.open("rb") as stream: | |
| for chunk in iter(lambda: stream.read(1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def parse_args(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "--reports", | |
| type=Path, | |
| nargs="+", | |
| default=[Path("reports/all_validation.json"), Path("reports/sample_validation.json")], | |
| ) | |
| return parser.parse_args() | |
| def validate(report_path): | |
| report = json.loads(report_path.read_text(encoding="utf-8")) | |
| ids = set() | |
| source_ids = set() | |
| rows = 0 | |
| label_counts = collections.Counter() | |
| language_counts = collections.Counter() | |
| stratum_counts = collections.Counter() | |
| conflict_rows = 0 | |
| for shard in report["shards"]: | |
| path = Path(shard["path"]) | |
| if file_sha256(path) != shard["sha256"]: | |
| raise ValueError(f"checksum mismatch: {path}") | |
| parquet = pq.ParquetFile(path) | |
| if parquet.schema_arrow != FEATURES.arrow_schema: | |
| raise ValueError(f"schema mismatch: {path}") | |
| if parquet.metadata.num_rows != shard["rows"]: | |
| raise ValueError(f"row count mismatch: {path}") | |
| for batch in parquet.iter_batches(batch_size=65_536): | |
| columns = batch.to_pydict() | |
| for index, row_id in enumerate(columns["id"]): | |
| language = columns["language"][index] | |
| source_id = columns["source_id"][index] | |
| label = columns["label"][index] | |
| is_conflict = columns["is_label_conflict"][index] | |
| variants = columns["label_variants"][index] | |
| if row_id in ids: | |
| raise ValueError(f"duplicate id: {row_id}") | |
| ids.add(row_id) | |
| source_ids.add(source_id) | |
| if language not in LANGUAGES: | |
| raise ValueError(f"invalid language: {language}") | |
| if row_id != f"{source_id}_{language}": | |
| raise ValueError(f"id/source mismatch: {row_id}") | |
| if not columns["text"][index].strip(): | |
| raise ValueError(f"empty text: {row_id}") | |
| if not 0 <= label < len(LABELS): | |
| raise ValueError(f"invalid label: {row_id}") | |
| if columns["label_name"][index] != LABELS[label]: | |
| raise ValueError(f"label name mismatch: {row_id}") | |
| if is_conflict != (variants > 1) or not 1 <= variants <= len(LABELS): | |
| raise ValueError(f"conflict metadata mismatch: {row_id}") | |
| rows += 1 | |
| conflict_rows += is_conflict | |
| label_counts[LABELS[label]] += 1 | |
| language_counts[language] += 1 | |
| stratum_counts[f"{language}/{LABELS[label]}"] += 1 | |
| expected = report["counts"] | |
| checks = { | |
| "output_rows": rows, | |
| "output_unique_ids": len(ids), | |
| "retained_source_ids": len(source_ids), | |
| "label_conflict_rows": conflict_rows, | |
| "labels": dict(sorted(label_counts.items())), | |
| "languages": dict(sorted(language_counts.items())), | |
| } | |
| for key, actual in checks.items(): | |
| if actual != expected[key]: | |
| raise ValueError(f"{report['config']} {key} mismatch: {actual} != {expected[key]}") | |
| if "language_label_strata" in expected: | |
| actual = dict(sorted(stratum_counts.items())) | |
| if actual != expected["language_label_strata"]: | |
| raise ValueError(f"{report['config']} language/label balance mismatch") | |
| return { | |
| "config": report["config"], | |
| "status": "valid", | |
| "rows": rows, | |
| "unique_ids": len(ids), | |
| "source_ids": len(source_ids), | |
| "shards": len(report["shards"]), | |
| } | |
| def main(): | |
| args = parse_args() | |
| print(json.dumps([validate(path) for path in args.reports], indent=2)) | |
| if __name__ == "__main__": | |
| main() | |