File size: 1,935 Bytes
89552cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Rename <task>-NNNN.parquet shards to the Hub convention data/train-XXXXX-of-YYYYY.parquet.

The Hub infers the split from this naming; without it load_dataset will not find
`train`. Renaming is an in-place os.rename, no data is copied.

Before renaming, every task in the meta file must have at least one shard. A
missing task means an export job failed silently, which is very hard to notice
after upload (the dataset looks normal, it just lacks a whole subset).
"""
import argparse, glob, json, os, re

ap = argparse.ArgumentParser()
ap.add_argument("--dir", default="parquet_out/data")
ap.add_argument("--meta", default="meta/meta_full.json")
ap.add_argument("--skip-completeness", action="store_true",
                help="skip the every-task-has-a-shard check (only when exporting a subset on purpose)")
ap.add_argument("--apply", action="store_true", help="without this flag, only print the plan")
a = ap.parse_args()

files = sorted(f for f in glob.glob(os.path.join(a.dir, "*.parquet"))
               if not re.match(r"train-\d{5}-of-\d{5}\.parquet$", os.path.basename(f)))
n = len(files)
if n == 0:
    raise SystemExit("no shards to rename")

if not a.skip_completeness:
    meta = json.load(open(a.meta))
    have = {os.path.basename(f).rsplit("-", 1)[0] for f in files}
    missing = sorted(set(meta) - have)
    if missing:
        raise SystemExit(
            f"{len(missing)} task(s) have no shard; rerun them before finalizing:\n  "
            + "\n  ".join(missing))

total_b = sum(os.path.getsize(f) for f in files)
print(f"{n} shards, {total_b / 2**30:.1f} GiB in total")
for i, src in enumerate(files):
    dst = os.path.join(a.dir, f"train-{i:05d}-of-{n:05d}.parquet")
    if a.apply:
        os.rename(src, dst)
    elif i < 3 or i == n - 1:
        print(f"  {os.path.basename(src)}  ->  {os.path.basename(dst)}")
print("renamed" if a.apply else "(dry run; add --apply to rename)")