-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_data_json.py
More file actions
147 lines (128 loc) · 4.25 KB
/
Copy pathsplit_data_json.py
File metadata and controls
147 lines (128 loc) · 4.25 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
"""
Script to split a performance JSON file into training and testing sets.
JSON is expected to be an object: benchmark_path -> { solver -> result data }.
Keys (benchmarks) are split; structure of each value is preserved.
Example: create 5 QF_BV splits (seeds 0, 10, 20, 30, 40):
python scripts/split_data_json.py \\
--input data/raw_data/smtcomp24_performance/QF_BV.json \\
--output-dir data/train_test_splits/QF_BV \\
--seeds 0 10 20 30 40
"""
import json
import argparse
from pathlib import Path
from sklearn.model_selection import train_test_split
def split_json(
input_path: str | Path,
train_path: str | Path,
test_path: str | Path,
test_size: float = 0.25,
seed: int = 42,
) -> None:
"""
Split a performance JSON into training and testing sets by benchmark keys.
Args:
input_path: Path to input JSON file
train_path: Path to save training JSON file
test_path: Path to save testing JSON file
test_size: Proportion of data to use for testing (default: 0.25)
seed: Random seed for reproducibility (default: 42)
"""
input_path = Path(input_path)
train_path = Path(train_path)
test_path = Path(test_path)
with input_path.open() as f:
data = json.load(f)
if not isinstance(data, dict):
raise ValueError("JSON root must be an object (dict)")
keys = list(data.keys())
train_keys, test_keys = train_test_split(
keys, test_size=test_size, random_state=seed, shuffle=True
)
train_data = {k: data[k] for k in train_keys}
test_data = {k: data[k] for k in test_keys}
with train_path.open("w") as f:
json.dump(train_data, f, indent=2)
with test_path.open("w") as f:
json.dump(test_data, f, indent=2)
n = len(keys)
print("Split complete!")
print(f" Total benchmarks: {n}")
print(f" Training: {len(train_keys)} ({len(train_keys) / n * 100:.1f}%)")
print(f" Testing: {len(test_keys)} ({len(test_keys) / n * 100:.1f}%)")
print(f" Training file: {train_path}")
print(f" Testing file: {test_path}")
def main() -> None:
parser = argparse.ArgumentParser(
description="Split a performance JSON into training and testing sets"
)
parser.add_argument(
"--input",
type=str,
required=True,
help="Input JSON file path (e.g. data/raw_data/smtcomp24_performance/BV.json)",
)
parser.add_argument(
"--train",
type=str,
default=None,
help="Output training JSON path (required if not using --output-dir)",
)
parser.add_argument(
"--test",
type=str,
default=None,
help="Output testing JSON path (required if not using --output-dir)",
)
parser.add_argument(
"--output-dir",
type=str,
default=None,
help="If set with --seeds, write splits to <output-dir>/seed<N>/train.json and test.json",
)
parser.add_argument(
"--seeds",
type=int,
nargs="+",
default=None,
help="Seeds for multiple splits (e.g. 0 10 20 30 40). Requires --output-dir.",
)
parser.add_argument(
"--test-size",
type=float,
default=0.25,
help="Proportion of data for testing (default: 0.25)",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Random seed when using --train/--test (default: 42)",
)
args = parser.parse_args()
if args.output_dir is not None and args.seeds is not None:
out = Path(args.output_dir)
out.mkdir(parents=True, exist_ok=True)
for s in args.seeds:
seed_dir = out / f"seed{s}"
seed_dir.mkdir(parents=True, exist_ok=True)
split_json(
args.input,
seed_dir / "train.json",
seed_dir / "test.json",
test_size=args.test_size,
seed=s,
)
return
if args.train is None or args.test is None:
parser.error("Either (--train and --test) or (--output-dir and --seeds) must be set")
split_json(
args.input,
args.train,
args.test,
test_size=args.test_size,
seed=args.seed,
)
if __name__ == "__main__":
main()