File size: 2,433 Bytes
1340dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
"""Verify Hugging Face Datasets loads the local card and every configured split offline."""

import json
import os
from pathlib import Path
from tempfile import TemporaryDirectory


ROOT = Path(__file__).resolve().parents[1]
FILES = {
    **{config: {split: f"data/research/v3c1/{scope}/{split}.jsonl" for split in ("train", "validation", "test")}
       for config, scope in (("corpus_3c1", "corpus"), ("official_3c1", "official"))},
    "official": {split: f"data/research/v3b2/official/{split}.jsonl" for split in ("train", "validation", "test")},
    "default": {"examples": "data/examples.jsonl"},
    **{
        configuration: {
            split: f"data/research/{configuration}/{split}.jsonl"
            for split in ("train", "validation", "test")
        }
        for configuration in ("corpus", "benchmark")
    },
}


def main():
    os.environ["HF_HUB_OFFLINE"] = "1"
    os.environ["HF_DATASETS_OFFLINE"] = "1"
    os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
    try:
        from datasets import __version__, load_dataset
        from huggingface_hub import DatasetCard
    except ImportError as error:
        raise SystemExit("Install datasets==5.2.0 in the local test environment.") from error

    card = DatasetCard.load(ROOT / "README.md")
    configs = {
        config["config_name"]: {entry["split"]: entry["path"] for entry in config["data_files"]}
        for config in card.data.configs
    }
    if configs != FILES:
        raise ValueError("Dataset Card paths differ from the audited configuration map")
    with TemporaryDirectory(prefix="codedna-hf-") as cache:
        for config, splits in FILES.items():
            loaded = load_dataset(str(ROOT), config, cache_dir=cache, token=False)
            if loaded.keys() != splits.keys():
                raise ValueError(f"{config}: split names differ from the card")
            for split, path in splits.items():
                raw = (ROOT / path).read_text(encoding="utf-8")
                expected = [json.loads(line) for line in raw.splitlines()]
                if loaded[split].to_list() != expected:
                    raise ValueError(f"{config}/{split}: loaded records differ from JSONL")
                print(f"{config}/{split}: {len(expected)} rows, {len(loaded[split].column_names)} fields")
    print(f"Datasets {__version__}: every configured split loaded offline and matched its JSONL.")


if __name__ == "__main__":
    main()