-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshard_dataset.py
More file actions
executable file
·133 lines (111 loc) · 3.35 KB
/
shard_dataset.py
File metadata and controls
executable file
·133 lines (111 loc) · 3.35 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "pandas",
# "huggingface_hub",
# "tqdm",
# "zstandard",
# ]
# ///
"""
Download and shard the Angiosperm_16_genomes dataset.
Downloads the dataset from kuleshov-group/Angiosperm_16_genomes and creates
64 shards per split in jsonl.zst format.
Usage:
uv run shard_dataset.py [--output-dir OUTPUT_DIR] [--n-shards N_SHARDS]
"""
import argparse
from pathlib import Path
import pandas as pd
from huggingface_hub import hf_hub_download
from tqdm import tqdm
import numpy as np
def download_split(repo_id: str, split: str, cache_dir: Path) -> Path:
"""Download a single split from HuggingFace."""
filename = f"data/{split}/{split}.jsonl.zst"
return Path(
hf_hub_download(
repo_id=repo_id,
filename=filename,
repo_type="dataset",
cache_dir=cache_dir,
)
)
def shard_split(
input_path: Path,
output_dir: Path,
split: str,
n_shards: int,
seed: int = 42,
) -> None:
"""Load a split and create sharded output files."""
print(f"Loading {split} split from {input_path}...")
df = pd.read_json(input_path, lines=True)
print(f" Loaded {len(df):,} rows")
# Shuffle the data
df = df.sample(frac=1, random_state=seed).reset_index(drop=True)
# Create output directory
split_dir = output_dir / split
split_dir.mkdir(parents=True, exist_ok=True)
# Split into shards and save
print(f"Writing {n_shards} shards...")
shards = np.array_split(df, n_shards)
for i, df_shard in enumerate(tqdm(shards, desc=f"Sharding {split}")):
shard_path = split_dir / f"shard_{i:04d}.jsonl.zst"
df_shard.to_json(
shard_path,
orient="records",
lines=True,
compression={"method": "zstd", "threads": -1},
)
print(f" Wrote {n_shards} shards to {split_dir}")
def main():
parser = argparse.ArgumentParser(
description="Download and shard Angiosperm_16_genomes dataset"
)
parser.add_argument(
"--output-dir",
type=Path,
default=Path("sharded_data"),
help="Output directory for sharded files (default: sharded_data)",
)
parser.add_argument(
"--n-shards",
type=int,
default=64,
help="Number of shards per split (default: 64)",
)
parser.add_argument(
"--cache-dir",
type=Path,
default=None,
help="Cache directory for HuggingFace downloads",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Random seed for shuffling (default: 42)",
)
args = parser.parse_args()
repo_id = "kuleshov-group/Angiosperm_16_genomes"
splits = ["train", "valid", "test"]
args.output_dir.mkdir(parents=True, exist_ok=True)
for split in splits:
print(f"\n{'='*60}")
print(f"Processing {split} split")
print(f"{'='*60}")
# Download
input_path = download_split(repo_id, split, args.cache_dir)
# Shard
shard_split(
input_path=input_path,
output_dir=args.output_dir,
split=split,
n_shards=args.n_shards,
seed=args.seed,
)
print(f"\nDone! Sharded data written to {args.output_dir}")
if __name__ == "__main__":
main()