gresashala commited on
Commit
82be25c
·
1 Parent(s): b13a2c8

Shard large SAC directories into shard-xxxx subfolders (≤5k files per dir)

Browse files
Files changed (2) hide show
  1. repartition_script.py +0 -63
  2. shard_dirs.py +0 -46
repartition_script.py DELETED
@@ -1,63 +0,0 @@
1
- #!/usr/bin/env python3
2
- import re, shutil
3
- from pathlib import Path
4
-
5
- ROOT = Path(".") # run from HPO-RL-Bench-data
6
- ALGOS = ["DQN", "PPO", "SAC"]
7
- SEED_RE = re.compile(r"seed(\d+)(?!\d)")
8
-
9
- def target_part(seed: int) -> int:
10
- if 0 <= seed <= 2: return 0
11
- if 3 <= seed <= 5: return 1
12
- if 6 <= seed <= 9: return 2
13
- return -1
14
-
15
- def move_files(algo_dir: Path):
16
- for env_dir in algo_dir.iterdir():
17
- if not env_dir.is_dir():
18
- continue
19
- # match both old shards (ENV_0, ENV_1) and plain ENV directories
20
- # we always move files into ENV_0/1/2 (new scheme)
21
- if "_" in env_dir.name and env_dir.name.split("_")[-1] in {"0","1","2"}:
22
- base_env = "_".join(env_dir.name.split("_")[:-1])
23
- else:
24
- base_env = env_dir.name
25
-
26
- # ensure destinations exist
27
- dsts = [algo_dir / f"{base_env}_{i}" for i in range(3)]
28
- for d in dsts:
29
- d.mkdir(parents=True, exist_ok=True)
30
-
31
- # walk files in this env_dir (and only this level)
32
- for f in list(env_dir.glob("*.json")):
33
- m = SEED_RE.search(f.name)
34
- if not m:
35
- continue
36
- seed = int(m.group(1))
37
- part = target_part(seed)
38
- if part < 0:
39
- continue
40
- dest = (algo_dir / f"{base_env}_{part}") / f.name
41
- if dest.resolve() == f.resolve():
42
- continue
43
- dest.parent.mkdir(parents=True, exist_ok=True)
44
- shutil.move(str(f), str(dest))
45
-
46
- # try to remove now-empty source dir (skip if not empty)
47
- try:
48
- next(env_dir.iterdir())
49
- except StopIteration:
50
- try:
51
- env_dir.rmdir()
52
- except OSError:
53
- pass
54
-
55
- def main():
56
- for algo in ALGOS:
57
- algo_dir = ROOT / "data_hpo_rl_bench" / algo
58
- if algo_dir.is_dir():
59
- move_files(algo_dir)
60
-
61
- if __name__ == "__main__":
62
- main()
63
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
shard_dirs.py DELETED
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env python3
2
- import os, math, shutil
3
- from pathlib import Path
4
-
5
- ROOT = Path("data_hpo_rl_bench")
6
- MAX_PER_DIR = 5000 # target cap
7
- ALGOS = ["SAC"] # only SAC needs it now; add others if needed
8
-
9
- def shard_env_dir(env_dir: Path, pattern="*.json"):
10
- files = sorted([p for p in env_dir.glob(pattern) if p.is_file()])
11
- n = len(files)
12
- if n <= MAX_PER_DIR:
13
- return 0
14
- num_shards = math.ceil(n / MAX_PER_DIR)
15
- moved = 0
16
- for i, f in enumerate(files):
17
- shard_idx = i // MAX_PER_DIR
18
- shard_dir = env_dir / f"shard-{shard_idx:04d}"
19
- shard_dir.mkdir(parents=True, exist_ok=True)
20
- dest = shard_dir / f.name
21
- if dest.exists():
22
- continue
23
- shutil.move(str(f), str(dest))
24
- moved += 1
25
- return moved
26
-
27
- def main():
28
- total_moved = 0
29
- for algo in ALGOS:
30
- for env_dir in (ROOT / algo).iterdir():
31
- if not env_dir.is_dir():
32
- continue
33
- # only shard the *_0, *_1, *_2 dirs
34
- name = env_dir.name
35
- if "_" not in name or name.rsplit("_", 1)[-1] not in {"0","1","2"}:
36
- continue
37
- count = sum(1 for _ in env_dir.glob("*.json"))
38
- if count > MAX_PER_DIR:
39
- moved = shard_env_dir(env_dir)
40
- print(f"{env_dir}: moved {moved} / {count} into shards")
41
- total_moved += moved
42
- print(f"Done. Total moved: {total_moved}")
43
-
44
- if __name__ == "__main__":
45
- main()
46
-