-
Notifications
You must be signed in to change notification settings - Fork 1
/
progrexp.py
executable file
·144 lines (134 loc) · 5.85 KB
/
progrexp.py
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
#! /usr/bin/env python3
#
# Run an experiment using the progressive sampling algorithm (either fixed time
# or fixed size)
#
# Copyright 2020 Matteo Riondato <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os.path
import subprocess
import sys
import util
executable = "progrbavarian"
def usage():
sys.stderr.write(f"Usage: {sys.argv[0]} {{iter|mult}} CONFIG_FILE\n")
# Check the sanity of the command line arguments
def checkArgsSanity():
if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
usage()
sys.exit(0)
if len(sys.argv) != 3:
sys.stderr.write("Error: wrong number of arguments.\n")
usage()
sys.exit(2)
if sys.argv[1] != "iter" and sys.argv[1] != "mult":
sys.exit(
f"Error: invalid experiment type {sys.argv[1]!r}. Must be "
"one of 'iter' or 'mult'."
)
def main():
global executable
# Check the sanity of the command line arguments
checkArgsSanity()
expType = sys.argv[1]
reqConf = "multipliers"
if expType == "iter":
reqConf = "iterations"
conf = util.getConf(sys.argv[-1], (reqConf, "epsilons"))
# Use the right values
values = conf[reqConf]
# Read the optional keys
flags = ""
ext = ".txt"
if "flags" in conf:
flags = conf["flags"]
if "-j" in flags:
ext = ".json"
if "bindir" in conf:
executable = os.path.join(conf["bindir"], executable)
# Let's go!
for algo in conf["algos"]:
print(f"algo: {algo}")
for delta in conf["deltas"]:
print(f" delta: {delta}")
for mct in conf["mctrials"]:
print(f" mct: {mct}")
for eps in conf["epsilons"]:
print(f" eps: {eps}")
for val in values:
args = [executable] + flags
if expType == "mult":
# the one is for the number of iterations, which
# is ignored when running with a fixed multiplier
args += ["-m", str(val), "1"]
else: # implied expType == "iter"
args += [str(val)]
args += [str(eps), str(mct), algo, str(delta), conf["filename"]]
print(f" {expType}: {val}")
for rep in range(conf["reps"]):
logBase = (
f"{expType}_{conf['name']}_{algo}_"
f"{delta}_{mct}_{eps}_{val}_{rep}"
)
print(f" rep: {rep}")
try:
if "logdir" not in conf:
print("### start of process output ###")
subprocess.run(args, check=True)
print("### end of process output ###")
else:
# buffering=1 activates line buffering
# (maybe, because it is not very clear what
# subprocess.run( ) does with these files
# (does it re-open them?)
with open(
os.path.join(
conf["logdir"], logBase + "_out" + ext
),
mode="wt",
buffering=1,
) as outl, open(
os.path.join(
conf["logdir"], logBase + "_err.txt"
),
mode="wt",
buffering=1,
) as errl:
subprocess.run(
args, stdout=outl, stderr=errl, check=True
)
except subprocess.CalledProcessError as e:
logInfo = " See the error message above."
if "logdir" in conf:
path = os.path.join(
conf["logdir"], f"{logBase}_err.txt"
)
logInfo = f" Check logfile {path}."
sys.exit("Error running the program: " f"{e}{logInfo}")
except (
FileExistsError,
FileNotFoundError,
PermissionError,
) as e:
sys.exit(f"Error writing the log files: {e}")
except OSError as e:
sys.exit(f"Error running the program: {e}")
# end of reps loop
# end of vals loop
# end of eps loop
# end of mct loop
# end of delta loop
# end of algo loop
if __name__ == "__main__":
main()