File size: 1,836 Bytes
087e062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Create deterministic 600-example GenomeQA subsets balanced by subtask."""

import argparse
import random
from collections import Counter
from pathlib import Path

from datasets import load_dataset


DATASET_ID = "HuggingFaceBio/genomeqa"
SOURCE_REVISION = "a71ab7e0e72b8eca7acd4475744dccd0159b20be"
SOURCE_CONFIGS = ("binary", "mcq")
SAMPLES_PER_SUBTASK = 100
SEED = 42


def create_subset(config: str, output_dir: Path, seed: int) -> None:
    dataset = load_dataset(DATASET_ID, config, split="train", revision=SOURCE_REVISION)
    subtasks = sorted(set(dataset["subtask"]))
    rng = random.Random(f"{seed}:{config}")
    selected_indices = []

    for subtask in subtasks:
        candidate_indices = [index for index, value in enumerate(dataset["subtask"]) if value == subtask]
        if len(candidate_indices) < SAMPLES_PER_SUBTASK:
            raise ValueError(f"{subtask} has only {len(candidate_indices)} examples")
        selected_indices.extend(rng.sample(candidate_indices, SAMPLES_PER_SUBTASK))

    rng.shuffle(selected_indices)
    subset = dataset.select(selected_indices)
    expected_counts = dict.fromkeys(subtasks, SAMPLES_PER_SUBTASK)
    if Counter(subset["subtask"]) != expected_counts:
        raise RuntimeError("Generated subset is not balanced by subtask")

    config_dir = output_dir / f"{config}_600"
    config_dir.mkdir(parents=True, exist_ok=True)
    output_path = config_dir / "train-00000-of-00001.parquet"
    subset.to_parquet(output_path)


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--output-dir", type=Path, default=Path.cwd())
    parser.add_argument("--seed", type=int, default=SEED)
    args = parser.parse_args()

    for config in SOURCE_CONFIGS:
        create_subset(config, args.output_dir, args.seed)


if __name__ == "__main__":
    main()